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 undeployment on destroy for resource mso_schema_site. (DCNE-55) #293

Merged
merged 3 commits into from
Oct 3, 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
19 changes: 11 additions & 8 deletions examples/schema_site/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,12 @@ resource "mso_tenant" "tenant_1" {
}

resource "mso_schema" "schema_1" {
name = var.schema_name
template_name = var.template_name
tenant_id = mso_tenant.tenant_1.id
name = var.schema_name
template {
tenant_id = mso_tenant.tenant_1.id
name = var.template_name
display_name = var.template_name
}
}

resource "mso_schema_site" "schema_site_1" {
Expand All @@ -61,10 +64,10 @@ resource "mso_schema_site" "schema_site_1" {
template_name = var.template_name
}

// when a template should be undeployed from site before disassociation the 'undeploy_on_delete' argument should be set to true prior to terraform destroy
// when a template should be undeployed from site before disassociation the 'undeploy_on_destroy' argument should be set to true prior to terraform destroy
resource "mso_schema_site" "schema_site_2" {
schema_id = mso_schema.schema_1.id
site_id = mso_site.site_test_1.id
template_name = var.template_name
undeploy_on_delete = true
schema_id = mso_schema.schema_1.id
site_id = mso_site.site_test_1.id
template_name = var.template_name
undeploy_on_destroy = true
}
54 changes: 37 additions & 17 deletions mso/resource_mso_schema_site.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,31 +205,51 @@ func resourceMSOSchemaSiteDelete(d *schema.ResourceData, m interface{}) error {
siteId := d.Get("site_id").(string)
templateName := d.Get("template_name").(string)

versionInt, err := msoClient.CompareVersion("3.7.0.0")

if d.Get("undeploy_on_destroy").(bool) && versionInt == -1 {
payload, err := container.ParseJSON([]byte(fmt.Sprintf(`{"schemaId": "%s", "templateName": "%s", "undeploy": ["%s"]}`, schemaId, templateName, siteId)))
if err != nil {
log.Printf("[DEBUG] Parse of JSON failed with err: %s.", err)
return err
}
req, err := msoClient.MakeRestRequest("POST", "api/v1/task", payload, true)
if d.Get("undeploy_on_destroy").(bool) {
req, err := msoClient.MakeRestRequest("GET", fmt.Sprintf("api/v1/deploy/status/schema/%s/template/%s", schemaId, templateName), nil, true)
if err != nil {
log.Printf("[DEBUG] MakeRestRequest failed with err: %s.", err)
return err
}
_, resp, err := msoClient.Do(req)
if err != nil || resp.StatusCode != 202 {
obj, resp, err := msoClient.Do(req)
if err != nil || resp.StatusCode != 200 {
log.Printf("[DEBUG] Request failed with resp: %v. Err: %s.", resp, err)
return err
}
} else if d.Get("undeploy_on_destroy").(bool) && err == nil {
_, err := msoClient.GetViaURL(fmt.Sprintf("/api/v1/execute/schema/%s/template/%s?undeploy=%s", schemaId, templateName, siteId))
if err != nil {
return err
if deployMap, ok := obj.Data().(map[string]interface{}); ok {
if statusList, ok := deployMap["status"].([]interface{}); ok && len(statusList) > 0 {
for _, statusMap := range statusList {
if statusMap.(map[string]interface{})["siteId"] == siteId && statusMap.(map[string]interface{})["status"].(map[string]interface{})["siteStatus"] == "Succeeded" {
versionInt, err := msoClient.CompareVersion("3.7.0.0")
if versionInt == -1 {
payload, err := container.ParseJSON([]byte(fmt.Sprintf(`{"schemaId": "%s", "templateName": "%s", "undeploy": ["%s"]}`, schemaId, templateName, siteId)))
if err != nil {
log.Printf("[DEBUG] Parse of JSON failed with err: %s.", err)
return err
}
req, err := msoClient.MakeRestRequest("POST", "api/v1/task", payload, true)
if err != nil {
log.Printf("[DEBUG] MakeRestRequest failed with err: %s.", err)
return err
}
_, resp, err := msoClient.Do(req)
if err != nil || resp.StatusCode != 202 {
log.Printf("[DEBUG] Request failed with resp: %v. Err: %s.", resp, err)
return err
}
} else if err == nil {
_, err := msoClient.GetViaURL(fmt.Sprintf("/api/v1/execute/schema/%s/template/%s?undeploy=%s", schemaId, templateName, siteId))
if err != nil {
return err
}
} else {
log.Printf("[WARNING] Failed to compare version. Template could not be undeployed prior to schema site deletion. Err: %s.", err)
}
break
}
}
}
}
} else if d.Get("undeploy_on_destroy").(bool) {
log.Printf("[WARNING] Failed to compare version. Template could not be undeployed prior to schema site deletion. Err: %s.", err)
}

schemasite := models.NewSchemaSite("remove", fmt.Sprintf("/sites/%s-%s", siteId, templateName), siteId, templateName)
Expand Down
Loading