Skip to content

Commit

Permalink
[bug_fix] Fix importing for mso_schema_site to support multiple templ…
Browse files Browse the repository at this point in the history
…ates in the same schema with the same site
  • Loading branch information
akinross committed Oct 9, 2024
1 parent 0e3bc1c commit 15037b3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
25 changes: 18 additions & 7 deletions mso/resource_mso_schema_site.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package mso
import (
"fmt"
"log"
"strconv"
"strings"

"github.com/ciscoecosystem/mso-go-client/client"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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)
Expand Down
10 changes: 8 additions & 2 deletions website/docs/r/schema_site.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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]: <https://www.terraform.io/docs/import/index.html>

```bash
terraform import mso_schema_site.site1 {schema_id}/site/{site_name}
```
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
```

0 comments on commit 15037b3

Please sign in to comment.