Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Catalog Management): fix cm validation resource panic #5172

Merged
merged 4 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .secrets.baseline
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"files": "go.mod|go.sum|.*.map|^.secrets.baseline$",
"lines": null
},
"generated_at": "2024-03-05T13:58:58Z",
"generated_at": "2024-03-05T17:14:40Z",
"plugins_used": [
{
"name": "ArtifactoryDetector"
Expand Down
26 changes: 14 additions & 12 deletions ibm/service/catalogmanagement/resource_ibm_cm_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ func resourceIBMCmValidationCreate(context context.Context, d *schema.ResourceDa
validateInstallOptions.SetOverrideValues(&overridesModel)
}
if _, ok := d.GetOk("environment_variables"); ok {
envsModel, err := envVariablesToDeployRequestBodyEnvVariables(d.Get("environment_variables").([]map[string]interface{}))
envsModel, err := envVariablesToDeployRequestBodyEnvVariables(d.Get("environment_variables").([]interface{}))
if err != nil {
return diag.FromErr(err)
}
Expand Down Expand Up @@ -409,20 +409,22 @@ func markVersionAsConsumable(version catalogmanagementv1.Version, context contex
return nil
}

func envVariablesToDeployRequestBodyEnvVariables(envVariables []map[string]interface{}) ([]catalogmanagementv1.DeployRequestBodyEnvironmentVariablesItem, error) {
func envVariablesToDeployRequestBodyEnvVariables(envVariables []interface{}) ([]catalogmanagementv1.DeployRequestBodyEnvironmentVariablesItem, error) {
var modelArr []catalogmanagementv1.DeployRequestBodyEnvironmentVariablesItem
for _, envVar := range envVariables {
model := catalogmanagementv1.DeployRequestBodyEnvironmentVariablesItem{}
if envVar["name"] != nil && envVar["name"].(string) != "" {
model.Name = core.StringPtr(envVar["name"].(string))
}
if envVar["value"] != nil {
model.Value = envVar["value"]
}
if envVar["secure"] != nil {
model.Secure = core.BoolPtr(envVar["secure"].(bool))
if envVar != nil {
model := catalogmanagementv1.DeployRequestBodyEnvironmentVariablesItem{}
if envVar.(map[string]interface{})["name"] != nil && envVar.(map[string]interface{})["name"].(string) != "" {
model.Name = core.StringPtr(envVar.(map[string]interface{})["name"].(string))
}
if envVar.(map[string]interface{})["value"] != nil {
model.Value = envVar.(map[string]interface{})["value"]
}
if envVar.(map[string]interface{})["secure"] != nil {
model.Secure = core.BoolPtr(envVar.(map[string]interface{})["secure"].(bool))
}
modelArr = append(modelArr, model)
}
modelArr = append(modelArr, model)
}
return modelArr, nil
}
Expand Down
70 changes: 70 additions & 0 deletions ibm/service/catalogmanagement/resource_ibm_cm_validation_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright IBM Corp. 2022 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0

package catalogmanagement_test

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"

acc "github.com/IBM-Cloud/terraform-provider-ibm/ibm/acctest"
"github.com/IBM/platform-services-go-sdk/catalogmanagementv1"
)

func TestAccIBMCmValidationSimpleArgs(t *testing.T) {
var conf catalogmanagementv1.Version
versionLocator := "dba7e7dd-2bd7-4fcd-a846-4c370eab2672.98ba725b-86fa-4c6a-8430-70f38ec988da"

resource.Test(t, resource.TestCase{
PreCheck: func() { acc.TestAccPreCheck(t) },
Providers: acc.TestAccProviders,
CheckDestroy: testAccCheckIBMCmValidationDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccCheckIBMCmValidationSimpleConfig(versionLocator),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckIBMCmValidationExists("ibm_cm_validation.cm_validation", conf),
resource.TestCheckResourceAttr("ibm_cm_validation.cm_validation", "version_locator", versionLocator),
),
},
},
})
}

func testAccCheckIBMCmValidationSimpleConfig(versionLocator string) string {
return fmt.Sprintf(`
resource "ibm_cm_validation" "cm_validation" {
version_locator = "%s"
revalidate_if_validated = true
mark_version_consumable = false

override_values = {
name = "My TF"
}

environment_variables {
name = "test"
value = "test"
secure = true
}

environment_variables {

}
}
`, versionLocator)
}

func testAccCheckIBMCmValidationExists(n string, obj catalogmanagementv1.Version) resource.TestCheckFunc {

return func(s *terraform.State) error {
return nil
}
}

func testAccCheckIBMCmValidationDestroy(s *terraform.State) error {
return nil
}
Loading