Skip to content

Commit

Permalink
Address comments
Browse files Browse the repository at this point in the history
Signed-off-by: graysonwu <[email protected]>
  • Loading branch information
GraysonWu committed Aug 10, 2023
1 parent 3c1b95e commit 2101909
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 78 deletions.
1 change: 1 addition & 0 deletions nsxt/data_source_nsxt_policy_host_switch_profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package nsxt

import (
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/infra"
)
Expand Down
2 changes: 1 addition & 1 deletion nsxt/policy_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func getDataSourceStringSchema(description string) *schema.Schema {
}
}

func getDataSourceRequiredStringSchema(description string) *schema.Schema {
func getRequiredStringSchema(description string) *schema.Schema {
return &schema.Schema{
Type: schema.TypeString,
Description: description,
Expand Down
119 changes: 42 additions & 77 deletions nsxt/resource_nsxt_policy_host_switch_profile.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
/* Copyright © 2022 VMware, Inc. All Rights Reserved.
/* Copyright © 2023 VMware, Inc. All Rights Reserved.
SPDX-License-Identifier: MPL-2.0 */

package nsxt

import (
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/vmware/vsphere-automation-sdk-go/runtime/bindings"
Expand Down Expand Up @@ -84,6 +85,20 @@ func resourceNsxtPolicyHostSwitchProfile() *schema.Resource {
}
}

func getUplinkSchemaResource() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"uplink_name": getRequiredStringSchema("Name of this uplink"),
"uplink_type": {
Type: schema.TypeString,
Description: "Type of the uplink",
Required: true,
ValidateFunc: validation.StringInSlice(uplinkTypes, false),
},
},
}
}

func getUplinkHostSwitchProfileSchema() *schema.Schema {
return &schema.Schema{
Type: schema.TypeSet,
Expand All @@ -92,7 +107,7 @@ func getUplinkHostSwitchProfileSchema() *schema.Schema {
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"lags": {
"lag": {
Type: schema.TypeSet,
Description: "List of LACP group",
Optional: true,
Expand All @@ -118,7 +133,7 @@ func getUplinkHostSwitchProfileSchema() *schema.Schema {
Required: true,
ValidateFunc: validation.StringInSlice(lacpGroupModes, false),
},
"name": getDataSourceRequiredStringSchema("Lag name"),
"name": getRequiredStringSchema("Lag name"),
"number_of_uplinks": {
Type: schema.TypeInt,
Description: "Number of uplinks",
Expand All @@ -132,23 +147,13 @@ func getUplinkHostSwitchProfileSchema() *schema.Schema {
Default: model.Lag_TIMEOUT_TYPE_SLOW,
ValidateFunc: validation.StringInSlice(lacpTimeoutTypes, false),
},
"uplinks": {
"uplink": {
Type: schema.TypeSet,
Description: "uplink names",
Required: false,
Optional: false,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"uplink_name": getDataSourceRequiredStringSchema("Name of this uplink"),
"uplink_type": {
Type: schema.TypeString,
Description: "Type of the uplink",
Required: true,
ValidateFunc: validation.StringInSlice(uplinkTypes, false),
},
},
},
Elem: getUplinkSchemaResource(),
},
},
},
Expand All @@ -160,21 +165,11 @@ func getUplinkHostSwitchProfileSchema() *schema.Schema {
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"active_list": {
"active": {
Type: schema.TypeSet,
Description: "List of Uplinks used in active list",
Required: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"uplink_name": getDataSourceRequiredStringSchema("Name of this uplink"),
"uplink_type": {
Type: schema.TypeString,
Description: "Type of the uplink",
Required: true,
ValidateFunc: validation.StringInSlice(uplinkTypes, false),
},
},
},
Elem: getUplinkSchemaResource(),
},
"policy": {
Type: schema.TypeString,
Expand All @@ -188,48 +183,28 @@ func getUplinkHostSwitchProfileSchema() *schema.Schema {
Default: false,
Optional: true,
},
"standby_list": {
"standby": {
Type: schema.TypeSet,
Description: "List of Uplinks used in standby list",
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"uplink_name": getDataSourceRequiredStringSchema("Name of this uplink"),
"uplink_type": {
Type: schema.TypeString,
Description: "Type of the uplink",
Required: true,
ValidateFunc: validation.StringInSlice(uplinkTypes, false),
},
},
},
Elem: getUplinkSchemaResource(),
},
},
},
},
"named_teamings": {
"named_teaming": {
Type: schema.TypeSet,
Description: "List of named uplink teaming policies that can be used by logical switches",
Optional: true,
MaxItems: 32,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": getDataSourceRequiredStringSchema("The name of the uplink teaming policy"),
"active_list": {
"name": getRequiredStringSchema("The name of the uplink teaming policy"),
"active": {
Type: schema.TypeSet,
Description: "List of Uplinks used in active list",
Required: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"uplink_name": getDataSourceRequiredStringSchema("Name of this uplink"),
"uplink_type": {
Type: schema.TypeString,
Description: "Type of the uplink",
Required: true,
ValidateFunc: validation.StringInSlice(uplinkTypes, false),
},
},
},
Elem: getUplinkSchemaResource(),
},
"policy": {
Type: schema.TypeString,
Expand All @@ -243,21 +218,11 @@ func getUplinkHostSwitchProfileSchema() *schema.Schema {
Default: false,
Optional: true,
},
"standby_list": {
"standby": {
Type: schema.TypeSet,
Description: "List of Uplinks used in standby list",
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"uplink_name": getDataSourceRequiredStringSchema("Name of this uplink"),
"uplink_type": {
Type: schema.TypeString,
Description: "Type of the uplink",
Required: true,
ValidateFunc: validation.StringInSlice(uplinkTypes, false),
},
},
},
Elem: getUplinkSchemaResource(),
},
},
},
Expand Down Expand Up @@ -312,16 +277,16 @@ func uplinkHostSwitchProfileModelToSchema(profile model.PolicyUplinkHostSwitchPr
lagMap["name"] = lag.Name
lagMap["number_of_uplinks"] = lag.NumberOfUplinks
lagMap["timeout_type"] = lag.TimeoutType
lagMap["uplinks"] = uplinksToMap(lag.Uplinks)
lagMap["uplink"] = uplinksToMap(lag.Uplinks)
lagsSchema = append(lagsSchema, lagMap)
}
uplinkHostSwitchProfileMap["lags"] = lagsSchema
uplinkHostSwitchProfileMap["lag"] = lagsSchema

teamingSchema := make(map[string]interface{})
teamingSchema["policy"] = profile.Teaming.Policy
teamingSchema["rolling_order"] = profile.Teaming.RollingOrder
teamingSchema["active_list"] = uplinksToMap(profile.Teaming.ActiveList)
teamingSchema["standby_list"] = uplinksToMap(profile.Teaming.StandbyList)
teamingSchema["active"] = uplinksToMap(profile.Teaming.ActiveList)
teamingSchema["standby"] = uplinksToMap(profile.Teaming.StandbyList)
uplinkHostSwitchProfileMap["teaming"] = []map[string]interface{}{teamingSchema}

var namedTeamingsSchema []map[string]interface{}
Expand All @@ -330,8 +295,8 @@ func uplinkHostSwitchProfileModelToSchema(profile model.PolicyUplinkHostSwitchPr
namedTeamingMap["name"] = namedTeaming.Name
namedTeamingMap["policy"] = namedTeaming.Policy
namedTeamingMap["rolling_order"] = namedTeaming.RollingOrder
namedTeamingMap["active_list"] = uplinksToMap(namedTeaming.ActiveList)
namedTeamingMap["standby_list"] = uplinksToMap(namedTeaming.StandbyList)
namedTeamingMap["active"] = uplinksToMap(namedTeaming.ActiveList)
namedTeamingMap["standby"] = uplinksToMap(namedTeaming.StandbyList)
namedTeamingsSchema = append(namedTeamingsSchema, namedTeamingMap)
}
uplinkHostSwitchProfileMap["names_teamings"] = namedTeamingsSchema
Expand Down Expand Up @@ -405,7 +370,7 @@ func uplinkHostSwitchProfileSchemaToModel(d *schema.ResourceData) model.PolicyUp
transportVlan := int64(uplinkHostSwitchProfileData["transport_vlan"].(int))

var lags []model.Lag
lagsList := uplinkHostSwitchProfileData["lags"].(*schema.Set).List()
lagsList := uplinkHostSwitchProfileData["lag"].(*schema.Set).List()
for _, lagSchema := range lagsList {
lagData := lagSchema.(map[string]interface{})
loadBalanceAlgorithm := lagData["load_balance_algorithm"].(string)
Expand All @@ -424,10 +389,10 @@ func uplinkHostSwitchProfileSchemaToModel(d *schema.ResourceData) model.PolicyUp
}

var namedTeamings []model.NamedTeamingPolicy
namedTeamingsList := uplinkHostSwitchProfileData["named_teamings"].(*schema.Set).List()
namedTeamingsList := uplinkHostSwitchProfileData["named_teaming"].(*schema.Set).List()
for _, namedTeamingsSchema := range namedTeamingsList {
namedTeamingsSchemaData := namedTeamingsSchema.(map[string]interface{})
activeListList := namedTeamingsSchemaData["active_list"].(*schema.Set).List()
activeListList := namedTeamingsSchemaData["active"].(*schema.Set).List()
var activeList []model.Uplink
for _, item := range activeListList {
data := item.(map[string]interface{})
Expand All @@ -441,7 +406,7 @@ func uplinkHostSwitchProfileSchemaToModel(d *schema.ResourceData) model.PolicyUp
}
policy := namedTeamingsSchemaData["policy"].(string)
rollingOrder := namedTeamingsSchemaData["rolling_order"].(bool)
standbyListList := namedTeamingsSchemaData["standby_list"].(*schema.Set).List()
standbyListList := namedTeamingsSchemaData["standby"].(*schema.Set).List()
var standbyList []model.Uplink
for _, item := range standbyListList {
data := item.(map[string]interface{})
Expand All @@ -466,7 +431,7 @@ func uplinkHostSwitchProfileSchemaToModel(d *schema.ResourceData) model.PolicyUp

teamingSchema := uplinkHostSwitchProfileData["teaming"].(*schema.Set).List()[0]
teamingSchemaData := teamingSchema.(map[string]interface{})
activeListList := teamingSchemaData["active_list"].(*schema.Set).List()
activeListList := teamingSchemaData["active"].(*schema.Set).List()
var activeList []model.Uplink
for _, item := range activeListList {
data := item.(map[string]interface{})
Expand All @@ -480,7 +445,7 @@ func uplinkHostSwitchProfileSchemaToModel(d *schema.ResourceData) model.PolicyUp
}
policy := teamingSchemaData["policy"].(string)
rollingOrder := teamingSchemaData["rolling_order"].(bool)
standbyListList := teamingSchemaData["standby_list"].(*schema.Set).List()
standbyListList := teamingSchemaData["standby"].(*schema.Set).List()
var standbyList []model.Uplink
for _, item := range standbyListList {
data := item.(map[string]interface{})
Expand Down

0 comments on commit 2101909

Please sign in to comment.