Skip to content

Commit

Permalink
feat: Updating service token resource to support network token creation
Browse files Browse the repository at this point in the history
  • Loading branch information
srushti-patl committed Dec 17, 2024
1 parent 54796c3 commit 3d610e1
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 38 deletions.
64 changes: 55 additions & 9 deletions internal/resources/fabric/service_token/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ func buildCreateRequest(d *schema.ResourceData) fabricv4.ServiceToken {
}
serviceTokenRequest.SetExpirationDateTime(expirationTime)

descriptionConfig := d.Get("description").(string)
serviceTokenRequest.SetDescription(descriptionConfig)

connectionConfig := d.Get("service_token_connection").(*schema.Set).List()
connection := connectionTerraformToGo(connectionConfig)
serviceTokenRequest.SetConnection(connection)
Expand Down Expand Up @@ -257,6 +260,8 @@ func serviceTokenResponseMap(token *fabricv4.ServiceToken) map[string]interface{
const TimeFormat = "2006-01-02T15:04:05.000Z"
serviceToken["expiration_date_time"] = expirationDateTime.Format(TimeFormat)
serviceToken["state"] = token.GetState()
serviceToken["description"] = token.GetDescription()

if token.Connection != nil {
connection := token.GetConnection()
serviceToken["service_token_connection"] = connectionGoToTerraform(&connection)
Expand Down Expand Up @@ -290,7 +295,9 @@ func connectionTerraformToGo(connectionTerraform []interface{}) fabricv4.Service
connection.SetType(fabricv4.ServiceTokenConnectionType(typeVal))

uuid := connectionMap["uuid"].(string)
connection.SetUuid(uuid)
if uuid != "" {
connection.SetUuid(uuid)
}

allowRemoteConnection := connectionMap["allow_remote_connection"].(bool)
connection.SetAllowRemoteConnection(allowRemoteConnection)
Expand All @@ -299,10 +306,12 @@ func connectionTerraformToGo(connectionTerraform []interface{}) fabricv4.Service
connection.SetAllowCustomBandwidth(allowCustomBandwidth)

bandwidthLimit := connectionMap["bandwidth_limit"].(int)
connection.SetBandwidthLimit(int32(bandwidthLimit))
if bandwidthLimit > 0 {
connection.SetBandwidthLimit(int32(bandwidthLimit))
}

supportedBandwidths := connectionMap["supported_bandwidths"].([]interface{})
if supportedBandwidths != nil {
if len(supportedBandwidths) > 0 {
int32Bandwidths := make([]int32, len(supportedBandwidths))
for i, v := range supportedBandwidths {
int32Bandwidths[i] = int32(v.(int))
Expand Down Expand Up @@ -508,9 +517,31 @@ func networkTerraformToGo(networkList []interface{}) fabricv4.SimplifiedTokenNet
var network fabricv4.SimplifiedTokenNetwork
networkListMap := networkList[0].(map[string]interface{})
uuid := networkListMap["uuid"].(string)
href := networkListMap["href"].(string)
type_ := networkListMap["type"].(string)
network.SetUuid(uuid)
network.SetType(fabricv4.SimplifiedTokenNetworkType(type_))
name := networkListMap["name"].(string)
scope := networkListMap["scope"].(string)
locationList := networkListMap["location"].(*schema.Set).List()

if uuid != "" {
network.SetUuid(uuid)
}
if href != "" {
network.SetHref(href)
}
if type_ != "" {
network.SetType(fabricv4.SimplifiedTokenNetworkType(type_))
}
if name != "" {
network.SetName(name)
}
if scope != "" {
network.SetScope(fabricv4.SimplifiedTokenNetworkScope(scope))
}
if len(locationList) != 0 {
location := equinix_fabric_schema.LocationTerraformToGo(locationList)
network.SetLocation(location)
}
return network
}

Expand Down Expand Up @@ -748,10 +779,25 @@ func networkGoToTerraform(network *fabricv4.SimplifiedTokenNetwork) *schema.Set
}

mappedNetwork := make(map[string]interface{})
mappedNetwork["uuid"] = network.GetUuid()
mappedNetwork["href"] = network.GetHref()
mappedNetwork["type"] = string(network.GetType())

if uuid := network.GetUuid(); uuid != "" {
mappedNetwork["uuid"] = uuid
}
if href := network.GetHref(); href != "" {
mappedNetwork["href"] = href
}
if type_ := network.GetType(); type_ != "" {
mappedNetwork["type"] = string(type_)
}
if name := network.GetName(); name != "" {
mappedNetwork["name"] = name
}
if scope := network.GetName(); scope != "" {
mappedNetwork["scope"] = string(network.GetScope())
}
if network.Location != nil {
location := network.GetLocation()
mappedNetwork["location"] = equinix_fabric_schema.LocationGoToTerraform(&location)
}
return schema.NewSet(
schema.HashResource(networkSch()),
[]interface{}{mappedNetwork},
Expand Down
10 changes: 1 addition & 9 deletions internal/resources/fabric/service_token/resource_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,15 +181,13 @@ func accessPointSelectorsSch() *schema.Resource {
"port": {
Type: schema.TypeSet,
Optional: true,
Computed: true,
Description: "Port Configuration",
MaxItems: 1,
Elem: portSch(),
},
"link_protocol": {
Type: schema.TypeSet,
Optional: true,
Computed: true,
Description: "Link protocol Configuration",
MaxItems: 1,
Elem: linkProtocolSch(),
Expand All @@ -204,7 +202,6 @@ func accessPointSelectorsSch() *schema.Resource {
"interface": {
Type: schema.TypeSet,
Optional: true,
Computed: true,
Description: "Virtual Device Interface Configuration",
MaxItems: 1,
Elem: interfaceSch(),
Expand Down Expand Up @@ -374,8 +371,7 @@ func networkSch() *schema.Resource {
Schema: map[string]*schema.Schema{
"uuid": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Required: true,
Description: "Equinix-assigned Network identifier",
},
"href": {
Expand All @@ -385,25 +381,21 @@ func networkSch() *schema.Resource {
},
"type": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "Type of Network",
},
"name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "Network Name",
},
"scope": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "Scope of Network",
},
"location": {
Type: schema.TypeSet,
Optional: true,
Computed: true,
Description: "Location",
Elem: &schema.Resource{
Expand Down
103 changes: 83 additions & 20 deletions internal/resources/fabric/service_token/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,43 @@ func TestAccFabricZsidePortServiceToken_PNFV(t *testing.T) {
})
}

func testAccFabricZsideVirtualDeviceServiceTokenConfig(serviceTokenName string, serviceTokenDescription string, virtualDeviceUuid string) string {
func TestAccFabricZsideNetworkServiceToken_PNFV(t *testing.T) {
serviceTokenName, serviceTokenUpdatedName := "token_zwan_PNFV", "UP_Token_zwan_PNFV"
serviceTokenDescription, serviceTokenUpdatedDescription := "zside port token", "Updated zside port token"
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acceptance.TestAccPreCheck(t) },
Providers: acceptance.TestAccProviders,
CheckDestroy: CheckServiceTokenDelete,
Steps: []resource.TestStep{
{
Config: testAccFabricZsideNetworkServiceTokenConfig(serviceTokenName, serviceTokenDescription),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet("equinix_fabric_service_token.test", "uuid"),
resource.TestCheckResourceAttr("equinix_fabric_service_token.test", "name", serviceTokenName),
resource.TestCheckResourceAttr("equinix_fabric_service_token.test", "type", "VC_TOKEN"),
resource.TestCheckResourceAttr("equinix_fabric_service_token.test", "description", serviceTokenDescription),
resource.TestCheckResourceAttr("equinix_fabric_service_token.test", "expiration_date_time", "2025-02-18T06:43:49.981Z"),
resource.TestCheckResourceAttr("equinix_fabric_service_token.test", "service_token_connection.0.z_side.0.access_point_selectors.0.network.0.uuid", "ff241e62-42f5-48bc-96c4-e0b5297fbed1"),
),
ExpectNonEmptyPlan: false,
},
{
Config: testAccFabricZsideNetworkServiceTokenConfig(serviceTokenUpdatedName, serviceTokenUpdatedDescription),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet("equinix_fabric_service_token.test", "uuid"),
resource.TestCheckResourceAttr("equinix_fabric_service_token.test", "name", serviceTokenUpdatedName),
resource.TestCheckResourceAttr("equinix_fabric_service_token.test", "type", "VC_TOKEN"),
resource.TestCheckResourceAttr("equinix_fabric_service_token.test", "description", serviceTokenUpdatedDescription),
resource.TestCheckResourceAttr("equinix_fabric_service_token.test", "expiration_date_time", "2025-02-18T06:43:49.981Z"),
resource.TestCheckResourceAttr("equinix_fabric_service_token.test", "service_token_connection.0.z_side.0.access_point_selectors.0.network.0.uuid", "ff241e62-42f5-48bc-96c4-e0b5297fbed1"),
),
ExpectNonEmptyPlan: false,
},
},
})
}

func testAccFabricAsidePortServiceTokenConfig(serviceTokenName string, serviceTokenDescription string, portUuid string) string {
return fmt.Sprintf(
`resource "equinix_fabric_service_token" "test"{
type = "VC_TOKEN"
Expand All @@ -166,16 +202,16 @@ func testAccFabricZsideVirtualDeviceServiceTokenConfig(serviceTokenName string,
expiration_date_time = "2025-01-18T06:43:49.981Z"
service_token_connection {
type = "EVPL_VC"
supported_bandwidths = [50, 200, 10000]
z_side {
bandwidth_limit = 1000
a_side {
access_point_selectors{
type = "VD"
virtual_device{
type = "EDGE"
type = "COLO"
port {
uuid = "%s"
}
interface{
type = "NETWORK"
link_protocol {
type = "DOT1Q"
vlan_tag = "2987"
}
}
}
Expand All @@ -186,10 +222,10 @@ func testAccFabricZsideVirtualDeviceServiceTokenConfig(serviceTokenName string,
}
}
`, serviceTokenName, serviceTokenDescription, virtualDeviceUuid)
`, serviceTokenName, serviceTokenDescription, portUuid)
}

func testAccFabricAsidePortServiceTokenConfig(serviceTokenName string, serviceTokenDescription string, portUuid string) string {
func testAccFabricZsidePortServiceTokenConfig(serviceTokenName string, serviceTokenDescription string, portUuid string) string {
return fmt.Sprintf(
`resource "equinix_fabric_service_token" "test"{
type = "VC_TOKEN"
Expand All @@ -198,16 +234,16 @@ func testAccFabricAsidePortServiceTokenConfig(serviceTokenName string, serviceTo
expiration_date_time = "2025-01-18T06:43:49.981Z"
service_token_connection {
type = "EVPL_VC"
bandwidth_limit = 1000
a_side {
supported_bandwidths = [50, 200, 10000]
z_side {
access_point_selectors{
type = "COLO"
port {
uuid = "%s"
}
link_protocol {
type = "DOT1Q"
vlan_tag = "2987"
vlan_tag = "2087"
}
}
}
Expand All @@ -221,7 +257,7 @@ func testAccFabricAsidePortServiceTokenConfig(serviceTokenName string, serviceTo
`, serviceTokenName, serviceTokenDescription, portUuid)
}

func testAccFabricZsidePortServiceTokenConfig(serviceTokenName string, serviceTokenDescription string, portUuid string) string {
func testAccFabricZsideVirtualDeviceServiceTokenConfig(serviceTokenName string, serviceTokenDescription string, virtualDeviceUuid string) string {
return fmt.Sprintf(
`resource "equinix_fabric_service_token" "test"{
type = "VC_TOKEN"
Expand All @@ -233,13 +269,13 @@ func testAccFabricZsidePortServiceTokenConfig(serviceTokenName string, serviceTo
supported_bandwidths = [50, 200, 10000]
z_side {
access_point_selectors{
type = "COLO"
port {
type = "VD"
virtual_device{
type = "EDGE"
uuid = "%s"
}
link_protocol {
type = "DOT1Q"
vlan_tag = "2087"
interface{
type = "NETWORK"
}
}
}
Expand All @@ -250,7 +286,34 @@ func testAccFabricZsidePortServiceTokenConfig(serviceTokenName string, serviceTo
}
}
`, serviceTokenName, serviceTokenDescription, portUuid)
`, serviceTokenName, serviceTokenDescription, virtualDeviceUuid)
}

func testAccFabricZsideNetworkServiceTokenConfig(serviceTokenName string, serviceTokenDescription string) string {
return fmt.Sprintf(
`resource "equinix_fabric_service_token" "test" {
type = "VC_TOKEN"
name = "%s"
description = "%s"
expiration_date_time = "2025-02-18T06:43:49.981Z"
service_token_connection {
type = "EVPLAN_VC"
supported_bandwidths = [50, 200, 10000]
z_side {
access_point_selectors{
type = "NETWORK"
network {
uuid = "ff241e62-42f5-48bc-96c4-e0b5297fbed1"
}
}
}
}
notifications {
type = "ALL"
emails = ["[email protected]", "[email protected]"]
}
}
`, serviceTokenName, serviceTokenDescription)
}

func CheckServiceTokenDelete(s *terraform.State) error {
Expand Down

0 comments on commit 3d610e1

Please sign in to comment.