diff --git a/mso/datasource_mso_schema_site_vrf_region.go b/mso/datasource_mso_schema_site_vrf_region.go index b829e8da..75b49d87 100644 --- a/mso/datasource_mso_schema_site_vrf_region.go +++ b/mso/datasource_mso_schema_site_vrf_region.go @@ -147,27 +147,42 @@ func dataSourceMSOSchemaSiteVrfRegionRead(d *schema.ResourceData, m interface{}) d.Set("region_name", region) } - if regionCont.Exists("isVpnGatewayRouter") { - d.Set("vpn_gateway", regionCont.S("isVpnGatewayRouter").Data().(bool)) - } - if regionCont.Exists("isTGWAttachment") { - d.Set("hub_network_enable", regionCont.S("isTGWAttachment").Data().(bool)) + var cidrs []interface{} + var regionOrVpcsContainer map[string]interface{} + if regionCont.Exists("vpcs") { + vpcsInterface := regionCont.S("vpcs").Data() + vpcs := vpcsInterface.([]interface{}) + if len(vpcs) > 0 { + regionOrVpcsContainer = vpcs[0].(map[string]interface{}) + if cidrsInterface, exists := regionOrVpcsContainer["cidrs"]; exists { + cidrs = cidrsInterface.([]interface{}) + } + } + } else { + regionOrVpcsContainer = regionCont.Data().(map[string]interface{}) + if regionCont.Exists("cidrs") { + cidrsData := regionCont.S("cidrs").Data() + if cidrsData != nil { + cidrs = cidrsData.([]interface{}) + } + } } + if isVpnGatewayRouter, exists := regionOrVpcsContainer["isVpnGatewayRouter"]; exists { + d.Set("vpn_gateway", isVpnGatewayRouter) + } + if isTGWAttachment, exists := regionOrVpcsContainer["isTGWAttachment"]; exists { + d.Set("hub_network_enable", isTGWAttachment) + } hubMap := make(map[string]interface{}) - if regionCont.Exists("cloudRsCtxProfileToGatewayRouterP") { - temp := regionCont.S("cloudRsCtxProfileToGatewayRouterP").Data().(map[string]interface{}) - + if cloudRsCtxProfileToGatewayRouterP, exists := regionOrVpcsContainer["cloudRsCtxProfileToGatewayRouterP"]; exists { + temp := cloudRsCtxProfileToGatewayRouterP.(map[string]interface{}) hubMap["name"] = temp["name"] hubMap["tenant_name"] = temp["tenantName"] - - d.Set("hub_network", hubMap) - } else { d.Set("hub_network", hubMap) } cidrList := make([]interface{}, 0, 1) - cidrs := regionCont.S("cidrs").Data().([]interface{}) for _, tempCidr := range cidrs { cidr := tempCidr.(map[string]interface{}) diff --git a/mso/datasource_mso_tenant.go b/mso/datasource_mso_tenant.go index 6e75b12f..4448e00f 100644 --- a/mso/datasource_mso_tenant.go +++ b/mso/datasource_mso_tenant.go @@ -192,7 +192,10 @@ func datasourceMSOTenantRead(d *schema.ResourceData, m interface{}) error { readAzureAccountDataFromSchema(sitesCont, mapSite) if sitesCont.Exists("cloudAccount") && sitesCont.S("cloudAccount").String() != "{}" { - setCloudAccountInfo(strings.Split(sitesCont.S("cloudAccount").String(), "/")[2], mapSite) + splitStr := strings.Split(sitesCont.S("cloudAccount").String(), "/") + if len(splitStr) > 2 { + setCloudAccountInfo(splitStr[2], mapSite) + } } site_associations = append(site_associations, mapSite) diff --git a/mso/resource_mso_schema_site_vrf_region.go b/mso/resource_mso_schema_site_vrf_region.go index d8d199e7..77d9aecd 100644 --- a/mso/resource_mso_schema_site_vrf_region.go +++ b/mso/resource_mso_schema_site_vrf_region.go @@ -86,7 +86,8 @@ func resourceMSOSchemaSiteVrfRegion() *schema.Resource { }, "cidr": &schema.Schema{ Type: schema.TypeList, - Required: true, + Optional: true, + Computed: true, Elem: &schema.Resource{ Schema: map[string]*schema.Schema{ "cidr_ip": &schema.Schema{ @@ -139,6 +140,15 @@ func resourceMSOSchemaSiteVrfRegion() *schema.Resource { }, }, }), + CustomizeDiff: func(diff *schema.ResourceDiff, v interface{}) error { + configOld, configNew := diff.GetChange("hub_network") + stateHub := configOld.(map[string]interface{}) + configHub := configNew.(map[string]interface{}) + if len(stateHub) == 0 && len(configHub) == 0 { + diff.Clear("hub_network") + } + return nil + }, } } @@ -200,29 +210,44 @@ func resourceMSOSchemaSiteVrfRegionImport(d *schema.ResourceData, m interface{}) } apiRegion := models.StripQuotes(regionCont.S("name").String()) if apiRegion == stateRegion { - d.SetId(apiRegion) + d.SetId(d.Id()) d.Set("region_name", apiRegion) - if regionCont.Exists("isVpnGatewayRouter") { - d.Set("vpn_gateway", regionCont.S("isVpnGatewayRouter").Data().(bool)) - } - if regionCont.Exists("isTGWAttachment") { - d.Set("hub_network_enable", regionCont.S("isTGWAttachment").Data().(bool)) + var cidrs []interface{} + var regionOrVpcsContainer map[string]interface{} + if regionCont.Exists("vpcs") { + vpcsInterface := regionCont.S("vpcs").Data() + vpcs := vpcsInterface.([]interface{}) + if len(vpcs) > 0 { + regionOrVpcsContainer = vpcs[0].(map[string]interface{}) + if cidrsInterface, exists := regionOrVpcsContainer["cidrs"]; exists { + cidrs = cidrsInterface.([]interface{}) + } + } + } else { + regionOrVpcsContainer = regionCont.Data().(map[string]interface{}) + if regionCont.Exists("cidrs") { + cidrsData := regionCont.S("cidrs").Data() + if cidrsData != nil { + cidrs = cidrsData.([]interface{}) + } + } } + if isVpnGatewayRouter, exists := regionOrVpcsContainer["isVpnGatewayRouter"]; exists { + d.Set("vpn_gateway", isVpnGatewayRouter) + } + if isTGWAttachment, exists := regionOrVpcsContainer["isTGWAttachment"]; exists { + d.Set("hub_network_enable", isTGWAttachment) + } hubMap := make(map[string]interface{}) - if regionCont.Exists("cloudRsCtxProfileToGatewayRouterP") { - temp := regionCont.S("cloudRsCtxProfileToGatewayRouterP").Data().(map[string]interface{}) - + if cloudRsCtxProfileToGatewayRouterP, exists := regionOrVpcsContainer["cloudRsCtxProfileToGatewayRouterP"]; exists { + temp := cloudRsCtxProfileToGatewayRouterP.(map[string]interface{}) hubMap["name"] = temp["name"] hubMap["tenant_name"] = temp["tenantName"] - - d.Set("hub_network", hubMap) - } else { d.Set("hub_network", hubMap) } cidrList := make([]interface{}, 0, 1) - cidrs := regionCont.S("cidrs").Data().([]interface{}) for _, tempCidr := range cidrs { cidr := tempCidr.(map[string]interface{}) @@ -463,6 +488,7 @@ func resourceMSOSchemaSiteVrfRegionRead(d *schema.ResourceData, m interface{}) e } stateSite := d.Get("site_id").(string) + stateTemplate := d.Get("template_name").(string) found := false stateVrf := d.Get("vrf_name").(string) stateRegion := d.Get("region_name").(string) @@ -473,8 +499,9 @@ func resourceMSOSchemaSiteVrfRegionRead(d *schema.ResourceData, m interface{}) e return err } apiSite := models.StripQuotes(tempCont.S("siteId").String()) + apiTemplate := models.StripQuotes(tempCont.S("templateName").String()) - if apiSite == stateSite { + if apiSite == stateSite && apiTemplate == stateTemplate { vrfCount, err := tempCont.ArrayCount("vrfs") if err != nil { return fmt.Errorf("Unable to get Vrf list") @@ -503,29 +530,45 @@ func resourceMSOSchemaSiteVrfRegionRead(d *schema.ResourceData, m interface{}) e } apiRegion := models.StripQuotes(regionCont.S("name").String()) if apiRegion == stateRegion { - d.SetId(apiRegion) + d.SetId(fmt.Sprintf("%s/sites/%s/template/%s/vrf/%s/region/%s", schemaId, stateSite, stateTemplate, stateVrf, stateRegion)) d.Set("region_name", apiRegion) - if regionCont.Exists("isVpnGatewayRouter") { - d.Set("vpn_gateway", regionCont.S("isVpnGatewayRouter").Data().(bool)) + var cidrs []interface{} + var regionOrVpcsContainer map[string]interface{} + if regionCont.Exists("vpcs") { + vpcsInterface := regionCont.S("vpcs").Data() + vpcs := vpcsInterface.([]interface{}) + if len(vpcs) > 0 { + regionOrVpcsContainer = vpcs[0].(map[string]interface{}) + if cidrsInterface, exists := regionOrVpcsContainer["cidrs"]; exists { + cidrs = cidrsInterface.([]interface{}) + } + } + } else { + regionOrVpcsContainer = regionCont.Data().(map[string]interface{}) + if regionCont.Exists("cidrs") { + cidrsData := regionCont.S("cidrs").Data() + if cidrsData != nil { + cidrs = cidrsData.([]interface{}) + } + } + } + + if isVpnGatewayRouter, exists := regionOrVpcsContainer["isVpnGatewayRouter"]; exists { + d.Set("vpn_gateway", isVpnGatewayRouter) } - if regionCont.Exists("isTGWAttachment") { - d.Set("hub_network_enable", regionCont.S("isTGWAttachment").Data().(bool)) + if isTGWAttachment, exists := regionOrVpcsContainer["isTGWAttachment"]; exists { + d.Set("hub_network_enable", isTGWAttachment) } hubMap := make(map[string]interface{}) - if regionCont.Exists("cloudRsCtxProfileToGatewayRouterP") { - temp := regionCont.S("cloudRsCtxProfileToGatewayRouterP").Data().(map[string]interface{}) - + if cloudRsCtxProfileToGatewayRouterP, exists := regionOrVpcsContainer["cloudRsCtxProfileToGatewayRouterP"]; exists { + temp := cloudRsCtxProfileToGatewayRouterP.(map[string]interface{}) hubMap["name"] = temp["name"] hubMap["tenant_name"] = temp["tenantName"] - - d.Set("hub_network", hubMap) - } else { d.Set("hub_network", hubMap) } cidrList := make([]interface{}, 0, 1) - cidrs := regionCont.S("cidrs").Data().([]interface{}) for _, tempCidr := range cidrs { cidr := tempCidr.(map[string]interface{}) diff --git a/mso/resource_mso_tenant.go b/mso/resource_mso_tenant.go index cb10579a..dc927813 100644 --- a/mso/resource_mso_tenant.go +++ b/mso/resource_mso_tenant.go @@ -336,7 +336,10 @@ func resourceMSOTenantImport(d *schema.ResourceData, m interface{}) ([]*schema.R readAzureAccountDataFromSchema(sitesCont, mapSite) if sitesCont.Exists("cloudAccount") && sitesCont.S("cloudAccount").String() != "{}" { - setCloudAccountInfo(strings.Split(sitesCont.S("cloudAccount").String(), "/")[2], mapSite) + splitStr := strings.Split(sitesCont.S("cloudAccount").String(), "/") + if len(splitStr) > 2 { + setCloudAccountInfo(splitStr[2], mapSite) + } } site_associations = append(site_associations, mapSite) @@ -794,7 +797,10 @@ func resourceMSOTenantRead(d *schema.ResourceData, m interface{}) error { readAzureAccountDataFromSchema(sitesCont, mapSite) if sitesCont.Exists("cloudAccount") && sitesCont.S("cloudAccount").String() != "{}" { - setCloudAccountInfo(strings.Split(sitesCont.S("cloudAccount").String(), "/")[2], mapSite) + splitStr := strings.Split(sitesCont.S("cloudAccount").String(), "/") + if len(splitStr) > 2 { + setCloudAccountInfo(splitStr[2], mapSite) + } } site_associations = append(site_associations, mapSite)