From 15037b35b02b8088139a897376982062e481b535 Mon Sep 17 00:00:00 2001 From: akinross Date: Wed, 9 Oct 2024 13:18:10 +0200 Subject: [PATCH] [bug_fix] Fix importing for mso_schema_site to support multiple templates in the same schema with the same site --- mso/resource_mso_schema_site.go | 25 +++++++++++++++++------- website/docs/r/schema_site.html.markdown | 10 ++++++++-- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/mso/resource_mso_schema_site.go b/mso/resource_mso_schema_site.go index a065dccc..77b195c0 100644 --- a/mso/resource_mso_schema_site.go +++ b/mso/resource_mso_schema_site.go @@ -3,6 +3,7 @@ package mso import ( "fmt" "log" + "strconv" "strings" "github.com/ciscoecosystem/mso-go-client/client" @@ -63,6 +64,7 @@ func resourceMSOSchemaSiteImport(d *schema.ResourceData, m interface{}) ([]*sche get_attribute := strings.Split(d.Id(), "/") schemaId := get_attribute[0] name := get_attribute[2] + template := get_attribute[4] con, err := msoClient.GetViaURL(fmt.Sprintf("api/v1/sites")) if err != nil { return nil, err @@ -106,21 +108,30 @@ func resourceMSOSchemaSiteImport(d *schema.ResourceData, m interface{}) ([]*sche apiSiteId := models.StripQuotes(tempCont.S("siteId").String()) apiTemplate := models.StripQuotes(tempCont.S("templateName").String()) - if apiSiteId == stateSiteId { - d.SetId(apiSiteId) + if apiSiteId == stateSiteId && apiTemplate == template { + d.SetId(fmt.Sprintf("%s/sites/%s-%s", schemaId, stateSiteId, template)) d.Set("schema_id", schemaId) d.Set("site_id", apiSiteId) d.Set("template_name", apiTemplate) + + // when undeploy var is provided set it else set default value + if len(get_attribute) == 7 { + boolValue, err := strconv.ParseBool(get_attribute[6]) + if err != nil { + return nil, fmt.Errorf("Boolean value for 'undeploy_on_destroy' cannot be parsed: %s", boolValue) + } + d.Set("undeploy_on_destroy", boolValue) + } else { + d.Set("undeploy_on_destroy", false) + } + found = true } } if !found { - d.SetId("") - d.Set("schema_id", "") - d.Set("site_id", "") - d.Set("template_name", "") + return nil, fmt.Errorf("Resource not found for %s", d.Id()) } log.Printf("[DEBUG] %s: Import finished successfully", d.Id()) @@ -173,7 +184,7 @@ func resourceMSOSchemaSiteRead(d *schema.ResourceData, m interface{}) error { apiTemplate := models.StripQuotes(tempCont.S("templateName").String()) if apiSiteId == stateSiteId && apiTemplate == stateTemplate { - d.SetId(apiSiteId) + d.SetId(fmt.Sprintf("%s/sites/%s-%s", schemaId, stateSiteId, stateTemplate)) d.Set("schema_id", schemaId) d.Set("site_id", apiSiteId) d.Set("template_name", apiTemplate) diff --git a/website/docs/r/schema_site.html.markdown b/website/docs/r/schema_site.html.markdown index 6b623e77..e366fca4 100644 --- a/website/docs/r/schema_site.html.markdown +++ b/website/docs/r/schema_site.html.markdown @@ -55,5 +55,11 @@ The only attribute exported with this resource is `id`. Which is set to the id o An existing MSO Schema Site can be [imported][docs-import] into this resource via its Id/path, via the following command: [docs-import]: ```bash -terraform import mso_schema_site.site1 {schema_id}/site/{site_name} -``` \ No newline at end of file +terraform import mso_schema_site.site1 {schema_id}/site/{site_name}/template/{template_name} +``` + +Optionally the `undeploy_on_destroy` argument can be set to `true` during import with the following command: + +```bash +terraform import mso_schema_site.site1 {schema_id}/site/{site_name}/template/{template_name}/undeploy_on_destroy/true +```