Skip to content

Commit

Permalink
Fix for issue IBM-Cloud#3295 - Update template_inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
Venkatesh Murugan committed Nov 12, 2021
1 parent cb8fe6a commit 2e6d28a
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 14 deletions.
92 changes: 79 additions & 13 deletions ibm/resource_ibm_schematics_workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -1434,38 +1434,54 @@ func resourceIBMSchematicsWorkspaceUpdate(context context.Context, d *schema.Res
}

updateWorkspaceOptions := &schematicsv1.UpdateWorkspaceOptions{}
replaceWorkspaceOptions := &schematicsv1.ReplaceWorkspaceOptions{}

updateWorkspaceOptions.SetWID(d.Id())
replaceWorkspaceOptions.SetWID(d.Id())

hasChange := false

metadataChange := false
repoChange := false
templateInputsChange := false

if d.HasChange("catalog_ref") {
catalogRefAttr := d.Get("catalog_ref").([]interface{})
if len(catalogRefAttr) > 0 {
catalogRef := resourceIBMSchematicsWorkspaceMapToCatalogRef(d.Get("catalog_ref.0").(map[string]interface{}))
updateWorkspaceOptions.SetCatalogRef(&catalogRef)
replaceWorkspaceOptions.SetCatalogRef(&catalogRef)
hasChange = true
repoChange = true
}
}

if d.HasChange("description") {
updateWorkspaceOptions.SetDescription(d.Get("description").(string))
replaceWorkspaceOptions.SetDescription(d.Get("description").(string))
hasChange = true
metadataChange = true
}
if d.HasChange("name") {
updateWorkspaceOptions.SetName(d.Get("name").(string))
replaceWorkspaceOptions.SetName(d.Get("name").(string))
hasChange = true
metadataChange = true
}
if d.HasChange("shared_data") {
sharedDataAttr := d.Get("shared_data").([]interface{})
if len(sharedDataAttr) > 0 {
sharedData := resourceIBMSchematicsWorkspaceMapToSharedTargetData(d.Get("shared_data.0").(map[string]interface{}))
updateWorkspaceOptions.SetSharedData(&sharedData)
replaceWorkspaceOptions.SetSharedData(&sharedData)
hasChange = true
}
}
if d.HasChange("tags") {
updateWorkspaceOptions.SetTags(expandStringList(d.Get("tags").([]interface{})))
replaceWorkspaceOptions.SetTags(expandStringList(d.Get("tags").([]interface{})))
hasChange = true
metadataChange = true
}

var templateData []schematicsv1.TemplateSourceDataRequest
Expand All @@ -1485,11 +1501,12 @@ func resourceIBMSchematicsWorkspaceUpdate(context context.Context, d *schema.Res
templateSourceDataRequestMap["init_state_file"] = d.Get("template_init_state_file").(string)
hasTemplateData = true
}
if d.HasChange("template_type") {
templateSourceDataRequestMap["type"] = d.Get("template_type").(string)
updateWorkspaceOptions.SetType([]string{d.Get("template_type").(string)})
hasTemplateData = true
}
//if d.HasChange("template_type") {
templateSourceDataRequestMap["type"] = d.Get("template_type").(string)
updateWorkspaceOptions.SetType([]string{d.Get("template_type").(string)})
replaceWorkspaceOptions.SetType([]string{d.Get("template_type").(string)})
//hasTemplateData = true
//}
if d.HasChange("template_uninstall_script_name") {
templateSourceDataRequestMap["uninstall_script_name"] = d.Get("template_uninstall_script_name").(string)
hasTemplateData = true
Expand All @@ -1510,7 +1527,9 @@ func resourceIBMSchematicsWorkspaceUpdate(context context.Context, d *schema.Res
templateDataItem := resourceIBMSchematicsWorkspaceMapToTemplateSourceDataRequest(templateSourceDataRequestMap)
templateData = append(templateData, templateDataItem)
updateWorkspaceOptions.SetTemplateData(templateData)
replaceWorkspaceOptions.SetTemplateData(templateData)
hasChange = true
templateInputsChange = true
}

templateRepoRequestMap := map[string]interface{}{}
Expand Down Expand Up @@ -1542,13 +1561,16 @@ func resourceIBMSchematicsWorkspaceUpdate(context context.Context, d *schema.Res
if hasTemplateRepo {
templateRepo := resourceIBMSchematicsWorkspaceMapToTemplateRepoUpdateRequest(templateRepoRequestMap)
updateWorkspaceOptions.SetTemplateRepo(&templateRepo)
replaceWorkspaceOptions.SetTemplateRepo(&templateRepo)
hasChange = true
repoChange = true
}

if d.HasChange("template_type") {
updateWorkspaceOptions.SetType([]string{d.Get("template_type").(string)})
hasChange = true
}
//if d.HasChange("template_type") {
updateWorkspaceOptions.SetType([]string{d.Get("template_type").(string)})
replaceWorkspaceOptions.SetType([]string{d.Get("template_type").(string)})
//hasChange = true
//}

workspaceStatusRequestMap := map[string]interface{}{}
workspaceStatus := false
Expand Down Expand Up @@ -1579,15 +1601,59 @@ func resourceIBMSchematicsWorkspaceUpdate(context context.Context, d *schema.Res
if workspaceStatus {
workspaceStatus := resourceIBMSchematicsWorkspaceMapToWorkspaceStatusUpdateRequest(workspaceStatusRequestMap)
updateWorkspaceOptions.SetWorkspaceStatus(&workspaceStatus)
replaceWorkspaceOptions.SetWorkspaceStatus(&workspaceStatus)
hasChange = true
metadataChange = true
}

if hasChange {
_, response, err := schematicsClient.UpdateWorkspaceWithContext(context, updateWorkspaceOptions)
if err != nil {
log.Printf("[DEBUG] UpdateWorkspaceWithContext failed %s\n%s", err, response)
return diag.FromErr(fmt.Errorf("UpdateWorkspaceWithContext failed %s\n%s", err, response))
changed := false

if !changed && repoChange {
changed = true
_, response, err := schematicsClient.ReplaceWorkspaceWithContext(context, replaceWorkspaceOptions)
if err != nil {
log.Printf("[DEBUG] ReplaceWorkspaceWithContext failed %s\n%s", err, response)
return diag.FromErr(fmt.Errorf("ReplaceWorkspaceWithContext failed %s\n%s", err, response))
}
}

if !changed && metadataChange {
_, response, err := schematicsClient.UpdateWorkspaceWithContext(context, updateWorkspaceOptions)
if err != nil {
log.Printf("[DEBUG] UpdateWorkspaceWithContext failed %s\n%s", err, response)
return diag.FromErr(fmt.Errorf("UpdateWorkspaceWithContext failed %s\n%s", err, response))
}
}

if !changed && templateInputsChange {

for i := range replaceWorkspaceOptions.TemplateData {

workspaceId := d.Id()
runtimeData := d.Get("runtime_data").([]interface{})
templateId := runtimeData[i].(map[string]interface{})["id"].(string)

workspaceVariableRequestModel := replaceWorkspaceOptions.TemplateData[i].Variablestore
envVariables := replaceWorkspaceOptions.TemplateData[i].EnvValues
values := replaceWorkspaceOptions.TemplateData[i].Values

replaceWorkspaceInputsOptions := &schematicsv1.ReplaceWorkspaceInputsOptions{
WID: &workspaceId,
TID: &templateId,
EnvValues: envVariables,
Values: values,
Variablestore: workspaceVariableRequestModel,
}

_, response, err := schematicsClient.ReplaceWorkspaceInputs(replaceWorkspaceInputsOptions)
if err != nil {
log.Printf("[DEBUG] ReplaceWorkspaceInputs failed %s\n%s", err, response)
return diag.FromErr(fmt.Errorf("ReplaceWorkspaceInputs failed %s\n%s", err, response))
}
}
}

}

return resourceIBMSchematicsWorkspaceRead(context, d, meta)
Expand Down
20 changes: 19 additions & 1 deletion ibm/resource_ibm_schematics_workspace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func TestAccIBMSchematicsWorkspaceAllArgs(t *testing.T) {
),
},
resource.TestStep{
Config: testAccCheckIBMSchematicsWorkspaceConfig(descriptionUpdate, nameUpdate),
Config: testAccCheckIBMSchematicsWorkspaceConfigUpdate(descriptionUpdate, nameUpdate),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("ibm_schematics_workspace.schematics_workspace", "description", descriptionUpdate),
resource.TestCheckResourceAttr("ibm_schematics_workspace.schematics_workspace", "name", nameUpdate),
Expand Down Expand Up @@ -106,6 +106,24 @@ func testAccCheckIBMSchematicsWorkspaceConfig(description string, name string) s
`, description, name)
}

func testAccCheckIBMSchematicsWorkspaceConfigUpdate(description string, name string) string {
return fmt.Sprintf(`
resource "ibm_schematics_workspace" "schematics_workspace" {
description = "%s"
location = "us-east"
name = "%s"
resource_group = "default"
template_type = "terraform_v0.13.5"
template_inputs {
name = "testinput"
value = "test"
type = "string"
}
}
`, description, name)
}

func testAccCheckIBMSchematicsWorkspaceExists(n string, obj schematicsv1.WorkspaceResponse) resource.TestCheckFunc {

return func(s *terraform.State) error {
Expand Down

0 comments on commit 2e6d28a

Please sign in to comment.