Skip to content

Commit

Permalink
[ignore] Adjust undeployment conditions to reduce API calls and updat…
Browse files Browse the repository at this point in the history
…e example for resource schema_site.
  • Loading branch information
gmicol committed Sep 18, 2024
1 parent 27eda4e commit 9c8df05
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 45 deletions.
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
}
77 changes: 40 additions & 37 deletions mso/resource_mso_schema_site.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,44 +205,47 @@ func resourceMSOSchemaSiteDelete(d *schema.ResourceData, m interface{}) error {
siteId := d.Get("site_id").(string)
templateName := d.Get("template_name").(string)

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
}
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
}
if deployMap, ok := obj.Data().(map[string]interface{}); ok {
if statusList, ok := deployMap["status"].([]map[string]interface{}); ok && len(statusList) > 0 {
for _, statusMap := range statusList {
if statusMap["siteId"] == siteId && statusMap["status"].(map[string]interface{})["siteStatus"] == "Succeeded" {
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 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 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 d.Get("undeploy_on_destroy").(bool) {
req, err := msoClient.MakeRestRequest("GET", fmt.Sprintf("mso/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
}
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
}
if deployMap, ok := obj.Data().(map[string]interface{}); ok {
if statusList, ok := deployMap["status"].([]map[string]interface{}); ok && len(statusList) > 0 {
for _, statusMap := range statusList {
if statusMap["siteId"] == siteId && statusMap["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)
}
} 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)
break
}
}
}
Expand Down

0 comments on commit 9c8df05

Please sign in to comment.