Skip to content

Commit

Permalink
fix: Updating model method to fix terraform plan update issue
Browse files Browse the repository at this point in the history
  • Loading branch information
srushti-patl committed Oct 31, 2024
1 parent 8881b77 commit a07c062
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 103 deletions.
108 changes: 25 additions & 83 deletions internal/resources/fabric/service_token/datasources_schema.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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{
Expand Down
18 changes: 9 additions & 9 deletions internal/resources/fabric/service_token/datasources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down Expand Up @@ -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
}
Expand Down
30 changes: 19 additions & 11 deletions internal/resources/fabric/service_token/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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{}
}

Expand Down Expand Up @@ -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)
}

Expand All @@ -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{}
}

Expand All @@ -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{}
}

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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{}
}

Expand All @@ -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{}
}

Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit a07c062

Please sign in to comment.