diff --git a/examples/template/main.tf b/examples/template/main.tf index 8d8f1397..18737ed5 100644 --- a/examples/template/main.tf +++ b/examples/template/main.tf @@ -30,7 +30,7 @@ data "mso_tenant" "example_tenant" { resource "mso_template" "tenant_template" { template_name = "tenant_template" template_type = "tenant" - tenant_name = data.mso_tenant.example_tenant.display_name + tenant_id = data.mso_tenant.example_tenant.id sites = [data.mso_site.site_1.id, data.mso_site.site_2.id] } @@ -39,7 +39,7 @@ resource "mso_template" "tenant_template" { resource "mso_template" "l3out_template" { template_name = "l3out_template" template_type = "l3out" - tenant_name = data.mso_tenant.example_tenant.display_name + tenant_id = data.mso_tenant.example_tenant.id sites = [data.mso_site.site_1.id] } @@ -64,7 +64,7 @@ resource "mso_template" "fabric_resource_template" { resource "mso_template" "monitoring_tenant_template" { template_name = "monitoring_tenant_template" template_type = "monitoring_tenant" - tenant_name = data.mso_tenant.example_tenant.display_name + tenant_id = data.mso_tenant.example_tenant.id sites = [data.mso_site.site_1.id] } @@ -81,6 +81,6 @@ resource "mso_template" "monitoring_access_template" { resource "mso_template" "service_device_template" { template_name = "service_device_template" template_type = "service_device" - tenant_name = data.mso_tenant.example_tenant.display_name + tenant_id = data.mso_tenant.example_tenant.id sites = [data.mso_site.site_1.id, data.mso_site.site_2.id] } diff --git a/mso/datasource_mso_template.go b/mso/datasource_mso_template.go index 1a2fe309..db4572ac 100644 --- a/mso/datasource_mso_template.go +++ b/mso/datasource_mso_template.go @@ -43,7 +43,7 @@ func datasourceMSOTemplate() *schema.Resource { "service_device", }, false), }, - "tenant_name": &schema.Schema{ + "tenant_id": &schema.Schema{ Type: schema.TypeString, Computed: true, }, diff --git a/mso/resource_mso_template.go b/mso/resource_mso_template.go index 3e4c379e..7cc95995 100644 --- a/mso/resource_mso_template.go +++ b/mso/resource_mso_template.go @@ -43,7 +43,7 @@ func resourceMSOTemplate() *schema.Resource { "service_device", }, false), }, - "tenant_name": { + "tenant_id": { Type: schema.TypeString, Optional: true, Computed: true, @@ -150,28 +150,26 @@ type ndoTemplate struct { id string templateName string templateType string - tenantName string + tenantId string sites []string msoClient *client.Client - ndoTenants map[string]string - ndoSites map[string]string } func (ndoTemplate *ndoTemplate) SetToSchema(d *schema.ResourceData) { d.SetId(ndoTemplate.id) d.Set("template_name", ndoTemplate.templateName) d.Set("template_type", ndoTemplate.templateType) - d.Set("tenant_name", ndoTemplate.tenantName) + d.Set("tenant_id", ndoTemplate.tenantId) d.Set("sites", ndoTemplate.sites) } func (ndoTemplate *ndoTemplate) validateConfig() []error { errors := []error{} - if ndoTemplate.tenantName != "" && !ndoTemplateTypes[ndoTemplate.templateType].tenant { + if ndoTemplate.tenantId != "" && !ndoTemplateTypes[ndoTemplate.templateType].tenant { errors = append(errors, fmt.Errorf(fmt.Sprintf("Tenant cannot be attached to template of type %s.", ndoTemplate.templateType))) } - if ndoTemplate.tenantName == "" && ndoTemplateTypes[ndoTemplate.templateType].tenant { + if ndoTemplate.tenantId == "" && ndoTemplateTypes[ndoTemplate.templateType].tenant { errors = append(errors, fmt.Errorf(fmt.Sprintf("Tenant is required for template of type %s.", ndoTemplate.templateType))) } if len(ndoTemplate.sites) == 0 && ndoTemplateTypes[ndoTemplate.templateType].siteAmount == 1 { @@ -202,71 +200,33 @@ func (ndoTemplate *ndoTemplate) ToMap() (map[string]interface{}, error) { func (ndoTemplate *ndoTemplate) createTypeSpecificPayload() map[string]interface{} { if ndoTemplate.templateType == "tenant" { - return map[string]interface{}{"template": map[string]interface{}{"tenantId": ndoTemplate.ndoTenants[ndoTemplate.tenantName]}, "sites": ndoTemplate.createSitesPayload()} + return map[string]interface{}{"template": map[string]interface{}{"tenantId": ndoTemplate.tenantId}, "sites": ndoTemplate.createSitesPayload()} } else if ndoTemplate.templateType == "l3out" { - return map[string]interface{}{"tenantId": ndoTemplate.ndoTenants[ndoTemplate.tenantName], "siteId": ndoTemplate.createSitesPayload()[0]["siteId"]} + return map[string]interface{}{"tenantId": ndoTemplate.tenantId, "siteId": ndoTemplate.createSitesPayload()[0]["siteId"]} } else if ndoTemplate.templateType == "fabric_policy" { return map[string]interface{}{"sites": ndoTemplate.createSitesPayload()} } else if ndoTemplate.templateType == "fabric_resource" { return map[string]interface{}{"sites": ndoTemplate.createSitesPayload()} } else if ndoTemplate.templateType == "monitoring_tenant" { - return map[string]interface{}{"template": map[string]interface{}{"mtType": "tenant", "tenant": ndoTemplate.ndoTenants[ndoTemplate.tenantName]}, "sites": ndoTemplate.createSitesPayload()} + return map[string]interface{}{"template": map[string]interface{}{"mtType": "tenant", "tenant": ndoTemplate.tenantId}, "sites": ndoTemplate.createSitesPayload()} } else if ndoTemplate.templateType == "monitoring_access" { return map[string]interface{}{"template": map[string]interface{}{"mtType": "access"}, "sites": ndoTemplate.createSitesPayload()} } else if ndoTemplate.templateType == "service_device" { - return map[string]interface{}{"template": map[string]interface{}{"tenantId": ndoTemplate.ndoTenants[ndoTemplate.tenantName]}, "sites": ndoTemplate.createSitesPayload()} + return map[string]interface{}{"template": map[string]interface{}{"tenantId": ndoTemplate.tenantId}, "sites": ndoTemplate.createSitesPayload()} } return nil } func (ndoTemplate *ndoTemplate) createSitesPayload() []map[string]interface{} { siteIds := []map[string]interface{}{} - for _, site := range ndoTemplate.sites { - siteIds = append(siteIds, ndoTemplate.createSitePayload(site)) + for _, siteId := range ndoTemplate.sites { + siteIds = append(siteIds, ndoTemplate.createSitePayload(siteId)) } return siteIds } -func (ndoTemplate *ndoTemplate) createSitePayload(site string) map[string]interface{} { - return map[string]interface{}{"siteId": ndoTemplate.ndoSites[site]} -} - -func (ndoTemplate *ndoTemplate) setNdoSites() error { - cont, err := ndoTemplate.msoClient.GetViaURL(fmt.Sprintf("api/v1/sites")) - if err != nil { - return err - } - if cont.Exists("sites") { - ndoTemplate.ndoSites = map[string]string{} - for siteIndex, _ := range cont.S("sites").Data().([]interface{}) { - siteCont, err := cont.ArrayElement(siteIndex, "sites") - if err != nil { - return err - } - ndoTemplate.ndoSites[models.StripQuotes(siteCont.S("id").String())] = models.StripQuotes(siteCont.S("name").String()) - ndoTemplate.ndoSites[models.StripQuotes(siteCont.S("name").String())] = models.StripQuotes(siteCont.S("id").String()) - } - } - return nil -} - -func (ndoTemplate *ndoTemplate) setNdoTenants() error { - cont, err := ndoTemplate.msoClient.GetViaURL(fmt.Sprintf("api/v1/tenants")) - if err != nil { - return err - } - if cont.Exists("tenants") { - ndoTemplate.ndoTenants = map[string]string{} - for tenantIndex, _ := range cont.S("tenants").Data().([]interface{}) { - tenantCont, err := cont.ArrayElement(tenantIndex, "tenants") - if err != nil { - return err - } - ndoTemplate.ndoTenants[models.StripQuotes(tenantCont.S("id").String())] = models.StripQuotes(tenantCont.S("displayName").String()) - ndoTemplate.ndoTenants[models.StripQuotes(tenantCont.S("displayName").String())] = models.StripQuotes(tenantCont.S("id").String()) - } - } - return nil +func (ndoTemplate *ndoTemplate) createSitePayload(siteId string) map[string]interface{} { + return map[string]interface{}{"siteId": siteId} } func (ndoTemplate *ndoTemplate) getTemplate(errorNotFound bool) error { @@ -290,27 +250,17 @@ func (ndoTemplate *ndoTemplate) getTemplate(errorNotFound bool) error { ndoTemplate.sites = []string{} - err = ndoTemplate.setNdoTenants() - if err != nil { - return err - } - - err = ndoTemplate.setNdoSites() - if err != nil { - return err - } - ndoTemplate.templateName = models.StripQuotes(cont.S("displayName").String()) templateType := models.StripQuotes(cont.S("templateType").String()) if templateType == "tenantPolicy" { ndoTemplate.templateType = "tenant" - ndoTemplate.tenantName = ndoTemplate.ndoTenants[models.StripQuotes(cont.S(ndoTemplateTypes[ndoTemplate.templateType].templateTypeContainer).S("template").S("tenantId").String())] + ndoTemplate.tenantId = models.StripQuotes(cont.S(ndoTemplateTypes[ndoTemplate.templateType].templateTypeContainer).S("template").S("tenantId").String()) } else if templateType == "l3out" { ndoTemplate.templateType = "l3out" - ndoTemplate.tenantName = ndoTemplate.ndoTenants[models.StripQuotes(cont.S(ndoTemplateTypes[ndoTemplate.templateType].templateTypeContainer).S("tenantId").String())] - ndoTemplate.sites = append(ndoTemplate.sites, ndoTemplate.ndoSites[models.StripQuotes(cont.S(ndoTemplateTypes[ndoTemplate.templateType].templateTypeContainer).S("siteId").String())]) + ndoTemplate.tenantId = models.StripQuotes(cont.S(ndoTemplateTypes[ndoTemplate.templateType].templateTypeContainer).S("tenantId").String()) + ndoTemplate.sites = append(ndoTemplate.sites, models.StripQuotes(cont.S(ndoTemplateTypes[ndoTemplate.templateType].templateTypeContainer).S("siteId").String())) } else if templateType == "fabricPolicy" { ndoTemplate.templateType = "fabric_policy" @@ -322,19 +272,19 @@ func (ndoTemplate *ndoTemplate) getTemplate(errorNotFound bool) error { ndoTemplate.templateType = "monitoring_access" if models.StripQuotes(cont.S(ndoTemplateTypes[ndoTemplate.templateType].templateTypeContainer).S("template").S("mtType").String()) == "tenant" { ndoTemplate.templateType = "monitoring_tenant" - ndoTemplate.tenantName = ndoTemplate.ndoTenants[models.StripQuotes(cont.S(ndoTemplateTypes[ndoTemplate.templateType].templateTypeContainer).S("template").S("tenant").String())] + ndoTemplate.tenantId = models.StripQuotes(cont.S(ndoTemplateTypes[ndoTemplate.templateType].templateTypeContainer).S("template").S("tenant").String()) } } else if templateType == "serviceDevice" { ndoTemplate.templateType = "service_device" - ndoTemplate.tenantName = ndoTemplate.ndoTenants[models.StripQuotes(cont.S(ndoTemplateTypes[ndoTemplate.templateType].templateTypeContainer).S("template").S("tenantId").String())] + ndoTemplate.tenantId = models.StripQuotes(cont.S(ndoTemplateTypes[ndoTemplate.templateType].templateTypeContainer).S("template").S("tenantId").String()) } if ndoTemplate.templateType != "l3out" { if cont.S(ndoTemplateTypes[ndoTemplate.templateType].templateTypeContainer).Exists("sites") { for _, site := range cont.S(ndoTemplateTypes[ndoTemplate.templateType].templateTypeContainer).S("sites").Data().([]interface{}) { siteId := models.StripQuotes(site.(map[string]interface{})["siteId"].(string)) - ndoTemplate.sites = append(ndoTemplate.sites, ndoTemplate.ndoSites[siteId]) + ndoTemplate.sites = append(ndoTemplate.sites, siteId) } } } @@ -368,33 +318,18 @@ func resourceMSOTemplateCreate(d *schema.ResourceData, m interface{}) error { log.Println("[DEBUG] MSO Template Resource: Beginning Create", d.Id()) msoClient := m.(*client.Client) - sites := getListOfStringsFromSchemaList(d, "sites") - tenant := d.Get("tenant_name").(string) - ndoTemplate := ndoTemplate{ msoClient: msoClient, templateName: d.Get("template_name").(string), templateType: d.Get("template_type").(string), - tenantName: tenant, - sites: sites, + sites: getListOfStringsFromSchemaList(d, "sites"), } - validationErrors := ndoTemplate.validateConfig() - - if len(sites) > 0 { - err := ndoTemplate.setNdoSites() - if err != nil { - return err - } - } - - if tenant != "" { - err := ndoTemplate.setNdoTenants() - if err != nil { - return err - } + if tenantId, ok := d.GetOk("tenant_id"); ok { + ndoTemplate.tenantId = tenantId.(string) } + validationErrors := ndoTemplate.validateConfig() if len(validationErrors) > 0 { d.SetId("") return errors.Join(validationErrors...) @@ -429,30 +364,24 @@ func resourceMSOTemplateUpdate(d *schema.ResourceData, m interface{}) error { templateType := d.Get("template_type").(string) templateName := d.Get("template_name").(string) + sites := getListOfStringsFromSchemaList(d, "sites") if ndoTemplateTypes[templateType].siteAmount == 1 && d.HasChange("sites") { return fmt.Errorf("Cannot change site for template of type %s.", templateType) } - sites := getListOfStringsFromSchemaList(d, "sites") - ndoTemplate := ndoTemplate{ msoClient: msoClient, templateName: templateName, templateType: templateType, - tenantName: d.Get("tenant_name").(string), sites: sites, } - if len(sites) > 0 { - err := ndoTemplate.setNdoSites() - if err != nil { - return err - } + if tenantId, ok := d.GetOk("tenant_id"); ok { + ndoTemplate.tenantId = tenantId.(string) } validationErrors := ndoTemplate.validateConfig() - if len(validationErrors) > 0 { return errors.Join(validationErrors...) } diff --git a/website/docs/d/template.html.markdown b/website/docs/d/template.html.markdown index b746b54c..43d603b0 100644 --- a/website/docs/d/template.html.markdown +++ b/website/docs/d/template.html.markdown @@ -33,5 +33,5 @@ data "mso_template" "example_with_id" { ## Attribute Reference ## -* `tenant_name` - (Read-Only) The name of the tenant to associate with the template. -* `sites` - (Read-Only) A list of site names to associate with the template. +* `tenant_id` - (Read-Only) The ID of the tenant associated with the template. +* `sites` - (Read-Only) A list of site names associated with the template. diff --git a/website/docs/r/template.html.markdown b/website/docs/r/template.html.markdown index d44b5880..7ae6879c 100644 --- a/website/docs/r/template.html.markdown +++ b/website/docs/r/template.html.markdown @@ -17,7 +17,7 @@ Manages MSO Template resource "mso_template" "tenant_template" { template_name = "tenant_template" template_type = "tenant" - tenant_name = data.mso_tenant.example_tenant.display_name + tenant_id = data.mso_tenant.example_tenant.id sites = [data.mso_site.site_1.id, data.mso_site.site_2.id] } @@ -27,8 +27,8 @@ resource "mso_template" "tenant_template" { * `template_name` - (Required) The name of the template. * `template_type` - (Required) The type of the template. Allowed values are `tenant`, `l3out`, `fabric_policy`, `fabric_resource`, `monitoring_tenant`, `monitoring_access`, or `service_device`. -* `tenant_name` - (Optional) The name of the tenant to associate with the template. -* `sites` - (Optional) A list of site names to associate with the template. +* `tenant_id` - (Optional) The ID of the tenant to associate with the template. +* `sites` - (Optional) A list of site IDs to associate with the template. ## Attribute Reference ##