From a07c062efbca07dace588b4221acc9c3ed4bb067 Mon Sep 17 00:00:00 2001 From: srushti-patl Date: Thu, 31 Oct 2024 13:42:14 -0700 Subject: [PATCH] fix: Updating model method to fix terraform plan update issue --- .../service_token/datasources_schema.go | 108 ++++-------------- .../fabric/service_token/datasources_test.go | 18 +-- .../resources/fabric/service_token/models.go | 30 +++-- 3 files changed, 53 insertions(+), 103 deletions(-) diff --git a/internal/resources/fabric/service_token/datasources_schema.go b/internal/resources/fabric/service_token/datasources_schema.go index 046112cee..3ea4dffaa 100644 --- a/internal/resources/fabric/service_token/datasources_schema.go +++ b/internal/resources/fabric/service_token/datasources_schema.go @@ -1,92 +1,34 @@ package service_token import ( - equinix_fabric_schema "github.com/equinix/terraform-provider-equinix/internal/fabric/schema" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" ) func dataSourceBaseSchema() map[string]*schema.Schema { - return map[string]*schema.Schema{ - "type": { - Type: schema.TypeString, - Computed: true, - Description: "Service Token Type; VC_TOKEN,EPL_TOKEN", - }, - "uuid": { - Type: schema.TypeString, - Required: true, - Description: "Equinix-assigned service token identifier", - }, - "href": { - Type: schema.TypeString, - Computed: true, - Description: "An absolute URL that is the subject of the link's context.", - }, - "issuer_side": { - Type: schema.TypeString, - Computed: true, - Description: "Information about token side; ASIDE, ZSIDE", - }, - "name": { - Type: schema.TypeString, - Computed: true, - Description: "Name of the Service Token", - }, - "description": { - Type: schema.TypeString, - Computed: true, - Description: "Optional Description to the Service Token you will be creating", - }, - "expiration_date_time": { - Type: schema.TypeString, - Computed: true, - Description: "Expiration date and time of the service token; 2020-11-06T07:00:00Z", - }, - "service_token_connection": { - Type: schema.TypeSet, - Computed: true, - Description: "Service Token Connection Type Information", - Elem: serviceTokenConnectionSch(), - }, - "state": { - Type: schema.TypeString, - Computed: true, - Description: "Service token state; ACTIVE, INACTIVE, EXPIRED, DELETED", - }, - "notifications": { - Type: schema.TypeSet, - Computed: true, - Description: "Preferences for notifications on Service Token configuration or status changes", - Elem: &schema.Resource{ - Schema: equinix_fabric_schema.NotificationSch(), - }, - }, - "account": { - Type: schema.TypeSet, - Computed: true, - Description: "Customer account information that is associated with this service token", - Elem: &schema.Resource{ - Schema: equinix_fabric_schema.AccountSch(), - }, - }, - "change_log": { - Type: schema.TypeSet, - Computed: true, - Description: "Captures connection lifecycle change information", - Elem: &schema.Resource{ - Schema: equinix_fabric_schema.ChangeLogSch(), - }, - }, - "project": { - Type: schema.TypeSet, - Computed: true, - Description: "Project information", - Elem: &schema.Resource{ - Schema: equinix_fabric_schema.ProjectSch(), - }, - }, + sch := resourceSchema() + for key := range sch { + if key == "uuid" { + sch[key].Required = true + sch[key].Optional = false + sch[key].Computed = false + } else { + sch[key].Required = false + sch[key].Optional = false + sch[key].Computed = true + sch[key].MaxItems = 0 + sch[key].ValidateFunc = nil + } } + return sch +} + +func dataSourceBaseSchemaUpdated() map[string]*schema.Schema { + sch := dataSourceBaseSchema() + sch["uuid"].Computed = true + sch["uuid"].Optional = false + sch["uuid"].Required = false + return sch } func paginationSchema() *schema.Resource { @@ -129,15 +71,15 @@ func dataSourceSearchSchema() map[string]*schema.Schema { "data": { Type: schema.TypeList, Computed: true, - Description: "List of Route Filters", + Description: "List of Service Tokens", Elem: &schema.Resource{ - Schema: dataSourceBaseSchema(), + Schema: dataSourceBaseSchemaUpdated(), }, }, "filter": { Type: schema.TypeList, Required: true, - Description: "Filters for the Data Source Search Request. Maximum of 8 total filters.", + Description: "Filters for the Data Source Search Request", MaxItems: 10, Elem: &schema.Resource{ Schema: map[string]*schema.Schema{ diff --git a/internal/resources/fabric/service_token/datasources_test.go b/internal/resources/fabric/service_token/datasources_test.go index df9986b0a..f075af1e6 100644 --- a/internal/resources/fabric/service_token/datasources_test.go +++ b/internal/resources/fabric/service_token/datasources_test.go @@ -23,14 +23,14 @@ func TestAccFabricServiceTokenDataSource_PNFV(t *testing.T) { { Config: testAccFabricServiceTokenConfigDataSourceConfig(virtualDevice), Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttrSet("data.equinix_fabric_service_token.service-token-for-zside-virtual-device", "uuid"), - resource.TestCheckResourceAttr("data.equinix_fabric_service_token.service-token-for-zside-virtual-device", "type", "VC_TOKEN"), - resource.TestCheckResourceAttr("data.equinix_fabric_service_token.service-token-for-zside-virtual-device", "expiration_date_time", "2025-01-18T06:43:49.981Z"), - resource.TestCheckResourceAttr("data.equinix_fabric_service_token.service-token-for-zside-virtual-device", "service_token_connection.0.supported_bandwidths.#", "3"), - resource.TestCheckResourceAttr("data.equinix_fabric_service_token.service-token-for-zside-virtual-device", "service_token_connection.0.z_side.0.access_point_selectors.0.virtual_device.0.type", "EDGE"), - resource.TestCheckResourceAttr("data.equinix_fabric_service_token.service-token-for-zside-virtual-device", "service_token_connection.0.z_side.0.access_point_selectors.0.virtual_device.0.uuid", virtualDevice), - resource.TestCheckResourceAttr("data.equinix_fabric_service_token.service-token-for-zside-virtual-device", "service_token_connection.0.z_side.0.access_point_selectors.0.interface.0.type", "NETWORK"), - resource.TestCheckResourceAttrSet("data.equinix_fabric_service_token.service-token-for-zside-virtual-device", "service_token_connection.0.z_side.0.access_point_selectors.0.interface.0.id"), + resource.TestCheckResourceAttrSet("data.equinix_fabric_service_token.service-token-for-zside-virtual-device-for-zside-virtual-device", "uuid"), + resource.TestCheckResourceAttr("data.equinix_fabric_service_token.service-token-for-zside-virtual-device-for-zside-virtual-device", "type", "VC_TOKEN"), + resource.TestCheckResourceAttr("data.equinix_fabric_service_token.service-token-for-zside-virtual-device-for-zside-virtual-device", "expiration_date_time", "2025-01-18T06:43:49.981Z"), + resource.TestCheckResourceAttr("data.equinix_fabric_service_token.service-token-for-zside-virtual-device-for-zside-virtual-device", "service_token_connection.0.supported_bandwidths.#", "3"), + resource.TestCheckResourceAttr("data.equinix_fabric_service_token.service-token-for-zside-virtual-device-for-zside-virtual-device", "service_token_connection.0.z_side.0.access_point_selectors.0.virtual_device.0.type", "EDGE"), + resource.TestCheckResourceAttr("data.equinix_fabric_service_token.service-token-for-zside-virtual-device-for-zside-virtual-device", "service_token_connection.0.z_side.0.access_point_selectors.0.virtual_device.0.uuid", virtualDevice), + resource.TestCheckResourceAttr("data.equinix_fabric_service_token.service-token-for-zside-virtual-device-for-zside-virtual-device", "service_token_connection.0.z_side.0.access_point_selectors.0.interface.0.type", "NETWORK"), + resource.TestCheckResourceAttrSet("data.equinix_fabric_service_token.service-token-for-zside-virtual-device-for-zside-virtual-device", "service_token_connection.0.z_side.0.access_point_selectors.0.interface.0.id"), resource.TestCheckResourceAttrSet("data.equinix_fabric_service_tokens.service-tokens", "data.0.uuid"), resource.TestCheckResourceAttr("data.equinix_fabric_service_tokens.service-tokens", "data.0.type", "VC_TOKEN"), resource.TestCheckResourceAttr("data.equinix_fabric_service_tokens.service-tokens", "data.0.expiration_date_time", "2025-01-18T06:43:49.981Z"), @@ -73,7 +73,7 @@ func testAccFabricServiceTokenConfigDataSourceConfig(virtualDeviceUuid string) s } } - data "equinix_fabric_service_token" "service-token-for-zside-virtual-device" { + data "equinix_fabric_service_token" "service-token-for-zside-virtual-device-for-zside-virtual-device" { uuid = equinix_fabric_service_token.test.id } diff --git a/internal/resources/fabric/service_token/models.go b/internal/resources/fabric/service_token/models.go index dbf71fa1c..0a3a58c24 100644 --- a/internal/resources/fabric/service_token/models.go +++ b/internal/resources/fabric/service_token/models.go @@ -2,15 +2,16 @@ package service_token import ( "fmt" + "reflect" + "sort" + "time" + "github.com/equinix/equinix-sdk-go/services/fabricv4" "github.com/equinix/terraform-provider-equinix/internal/converters" equinix_fabric_schema "github.com/equinix/terraform-provider-equinix/internal/fabric/schema" equinix_schema "github.com/equinix/terraform-provider-equinix/internal/schema" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" - "reflect" - "sort" - "time" ) func buildCreateRequest(d *schema.ResourceData) fabricv4.ServiceToken { @@ -277,7 +278,7 @@ func serviceTokenResponseMap(token *fabricv4.ServiceToken) map[string]interface{ } func connectionTerraformToGo(connectionTerraform []interface{}) fabricv4.ServiceTokenConnection { - if connectionTerraform == nil || len(connectionTerraform) == 0 { + if len(connectionTerraform) == 0 { return fabricv4.ServiceTokenConnection{} } @@ -306,6 +307,9 @@ func connectionTerraformToGo(connectionTerraform []interface{}) fabricv4.Service for i, v := range supportedBandwidths { int32Bandwidths[i] = int32(v.(int)) } + sort.Slice(int32Bandwidths, func(i, j int) bool { + return int32Bandwidths[i] < int32Bandwidths[j] + }) connection.SetSupportedBandwidths(int32Bandwidths) } @@ -323,7 +327,7 @@ func connectionTerraformToGo(connectionTerraform []interface{}) fabricv4.Service } func accessPointTerraformToGo(accessPoint []interface{}) fabricv4.ServiceTokenSide { - if accessPoint == nil || len(accessPoint) == 0 { + if len(accessPoint) == 0 { return fabricv4.ServiceTokenSide{} } @@ -339,7 +343,7 @@ func accessPointTerraformToGo(accessPoint []interface{}) fabricv4.ServiceTokenSi } func accessPointSelectorsTerraformToGo(accessPointSelectors []interface{}) []fabricv4.AccessPointSelector { - if accessPointSelectors == nil || len(accessPointSelectors) == 0 { + if len(accessPointSelectors) == 0 { return []fabricv4.AccessPointSelector{} } @@ -383,7 +387,7 @@ func accessPointSelectorsTerraformToGo(accessPointSelectors []interface{}) []fab } func portTerraformToGo(portList []interface{}) fabricv4.SimplifiedMetadataEntity { - if portList == nil || len(portList) == 0 { + if len(portList) == 0 { return fabricv4.SimplifiedMetadataEntity{} } var port fabricv4.SimplifiedMetadataEntity @@ -395,7 +399,7 @@ func portTerraformToGo(portList []interface{}) fabricv4.SimplifiedMetadataEntity } func linkProtocolTerraformToGo(linkProtocolList []interface{}) fabricv4.SimplifiedLinkProtocol { - if linkProtocolList == nil || len(linkProtocolList) == 0 { + if len(linkProtocolList) == 0 { return fabricv4.SimplifiedLinkProtocol{} } var linkProtocol fabricv4.SimplifiedLinkProtocol @@ -420,7 +424,7 @@ func linkProtocolTerraformToGo(linkProtocolList []interface{}) fabricv4.Simplifi } func virtualDeviceTerraformToGo(virtualDeviceList []interface{}) fabricv4.SimplifiedVirtualDevice { - if virtualDeviceList == nil || len(virtualDeviceList) == 0 { + if len(virtualDeviceList) == 0 { return fabricv4.SimplifiedVirtualDevice{} } @@ -441,7 +445,7 @@ func virtualDeviceTerraformToGo(virtualDeviceList []interface{}) fabricv4.Simpli } func interfaceTerraformToGo(interfaceList []interface{}) fabricv4.VirtualDeviceInterface { - if interfaceList == nil || len(interfaceList) == 0 { + if len(interfaceList) == 0 { return fabricv4.VirtualDeviceInterface{} } @@ -458,7 +462,7 @@ func interfaceTerraformToGo(interfaceList []interface{}) fabricv4.VirtualDeviceI } func networkTerraformToGo(networkList []interface{}) fabricv4.SimplifiedTokenNetwork { - if networkList == nil || len(networkList) == 0 { + if len(networkList) == 0 { return fabricv4.SimplifiedTokenNetwork{} } var network fabricv4.SimplifiedTokenNetwork @@ -530,6 +534,10 @@ func connectionGoToTerraform(connection *fabricv4.ServiceTokenConnection) *schem interfaceBandwidths[i] = int(v) // Convert each int32 to interface{} } + sort.Slice(interfaceBandwidths, func(i, j int) bool { + return interfaceBandwidths[i].(int) < interfaceBandwidths[j].(int) + }) + mappedConnection["supported_bandwidths"] = interfaceBandwidths } if connection.BandwidthLimit != nil {