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 18, 2023
1 parent a7a98f5 commit 2753a49
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 70 deletions.
91 changes: 32 additions & 59 deletions nsxt/resource_nsxt_uplink_host_switch_profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func resourceNsxtUplinkHostSwitchProfile() *schema.Resource {
"revision": getRevisionSchema(),
"tag": getTagsSchema(),
"lag": {
Type: schema.TypeSet,
Type: schema.TypeList,
Description: "List of LACP group",
Optional: true,
MaxItems: 64,
Expand Down Expand Up @@ -107,7 +107,7 @@ func resourceNsxtUplinkHostSwitchProfile() *schema.Resource {
ValidateFunc: validation.StringInSlice(lacpTimeoutTypes, false),
},
"uplink": {
Type: schema.TypeSet,
Type: schema.TypeList,
Description: "uplink names",
Required: false,
Optional: false,
Expand All @@ -118,14 +118,14 @@ func resourceNsxtUplinkHostSwitchProfile() *schema.Resource {
},
},
"teaming": {
Type: schema.TypeSet,
Type: schema.TypeList,
Description: "Default TeamingPolicy associated with this UplinkProfile",
Required: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"active": {
Type: schema.TypeSet,
Type: schema.TypeList,
Description: "List of Uplinks used in active list",
Required: true,
Elem: getUplinkSchemaResource(),
Expand All @@ -137,7 +137,7 @@ func resourceNsxtUplinkHostSwitchProfile() *schema.Resource {
ValidateFunc: validation.StringInSlice(teamingPolicies, false),
},
"standby": {
Type: schema.TypeSet,
Type: schema.TypeList,
Description: "List of Uplinks used in standby list",
Optional: true,
Elem: getUplinkSchemaResource(),
Expand All @@ -146,15 +146,15 @@ func resourceNsxtUplinkHostSwitchProfile() *schema.Resource {
},
},
"named_teaming": {
Type: schema.TypeSet,
Type: schema.TypeList,
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": getRequiredStringSchema("The name of the uplink teaming policy"),
"active": {
Type: schema.TypeSet,
Type: schema.TypeList,
Description: "List of Uplinks used in active list",
Required: true,
Elem: getUplinkSchemaResource(),
Expand All @@ -166,7 +166,7 @@ func resourceNsxtUplinkHostSwitchProfile() *schema.Resource {
ValidateFunc: validation.StringInSlice(teamingPolicies, false),
},
"standby": {
Type: schema.TypeSet,
Type: schema.TypeList,
Description: "List of Uplinks used in standby list",
Optional: true,
Elem: getUplinkSchemaResource(),
Expand All @@ -192,6 +192,7 @@ func resourceNsxtUplinkHostSwitchProfile() *schema.Resource {
Type: schema.TypeInt,
Description: "Maximum Transmission Unit used for uplinks",
Optional: true,
Default: 1700,
ValidateFunc: validation.IntAtLeast(1280),
},
},
Expand Down Expand Up @@ -326,13 +327,29 @@ func resourceNsxtUplinkHostSwitchProfileRead(d *schema.ResourceData, m interface
return nil
}

func uplinkSchemaToModelList(schema interface{}) []model.Uplink {
uplinkSchemaList := schema.([]interface{})
var uplinkList []model.Uplink
for _, item := range uplinkSchemaList {
data := item.(map[string]interface{})
uplinkName := data["uplink_name"].(string)
uplinkType := data["uplink_type"].(string)
obj := model.Uplink{
UplinkName: &uplinkName,
UplinkType: &uplinkType,
}
uplinkList = append(uplinkList, obj)
}
return uplinkList
}

func uplinkHostSwitchProfileSchemaToModel(d *schema.ResourceData) model.PolicyUplinkHostSwitchProfile {
displayName := d.Get("display_name").(string)
description := d.Get("description").(string)
tags := getPolicyTagsFromSchema(d)

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

var namedTeamings []model.NamedTeamingPolicy
namedTeamingsList := d.Get("named_teaming").(*schema.Set).List()
namedTeamingsList := d.Get("named_teaming").([]interface{})
for _, namedTeamingsSchema := range namedTeamingsList {
namedTeamingsSchemaData := namedTeamingsSchema.(map[string]interface{})
activeListList := namedTeamingsSchemaData["active"].(*schema.Set).List()
var activeList []model.Uplink
for _, item := range activeListList {
data := item.(map[string]interface{})
uplinkName := data["uplink_name"].(string)
uplinkType := data["uplink_type"].(string)
obj := model.Uplink{
UplinkName: &uplinkName,
UplinkType: &uplinkType,
}
activeList = append(activeList, obj)
}
activeList := uplinkSchemaToModelList(namedTeamingsSchemaData["active"])
standbyList := uplinkSchemaToModelList(namedTeamingsSchemaData["standby"])
policy := namedTeamingsSchemaData["policy"].(string)
standbyListList := namedTeamingsSchemaData["standby"].(*schema.Set).List()
var standbyList []model.Uplink
for _, item := range standbyListList {
data := item.(map[string]interface{})
uplinkName := data["uplink_name"].(string)
uplinkType := data["uplink_type"].(string)
obj := model.Uplink{
UplinkName: &uplinkName,
UplinkType: &uplinkType,
}
standbyList = append(standbyList, obj)
}
name := namedTeamingsSchemaData["name"].(string)
namedTeaming := model.NamedTeamingPolicy{
ActiveList: activeList,
Expand All @@ -389,33 +384,11 @@ func uplinkHostSwitchProfileSchemaToModel(d *schema.ResourceData) model.PolicyUp
namedTeamings = append(namedTeamings, namedTeaming)
}

teamingSchema := d.Get("teaming").(*schema.Set).List()[0]
teamingSchema := d.Get("teaming").([]interface{})[0]
teamingSchemaData := teamingSchema.(map[string]interface{})
activeListList := teamingSchemaData["active"].(*schema.Set).List()
var activeList []model.Uplink
for _, item := range activeListList {
data := item.(map[string]interface{})
uplinkName := data["uplink_name"].(string)
uplinkType := data["uplink_type"].(string)
obj := model.Uplink{
UplinkName: &uplinkName,
UplinkType: &uplinkType,
}
activeList = append(activeList, obj)
}
activeList := uplinkSchemaToModelList(teamingSchemaData["active"])
standbyList := uplinkSchemaToModelList(teamingSchemaData["standby"])
policy := teamingSchemaData["policy"].(string)
standbyListList := teamingSchemaData["standby"].(*schema.Set).List()
var standbyList []model.Uplink
for _, item := range standbyListList {
data := item.(map[string]interface{})
uplinkName := data["uplink_name"].(string)
uplinkType := data["uplink_type"].(string)
obj := model.Uplink{
UplinkName: &uplinkName,
UplinkType: &uplinkType,
}
standbyList = append(standbyList, obj)
}
teaming := model.TeamingPolicy{
ActiveList: activeList,
Policy: &policy,
Expand Down
42 changes: 36 additions & 6 deletions nsxt/resource_nsxt_uplink_host_switch_profile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ func TestAccResourceNsxtUplinkHostSwitchProfile_basic(t *testing.T) {
testResourceName := "nsxt_uplink_host_switch_profile.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
PreCheck: func() {
testAccOnlyLocalManager(t)
testAccPreCheck(t)
},
Providers: testAccProviders,
CheckDestroy: func(state *terraform.State) error {
return testAccNsxtUplinkHostSwitchProfileCheckDestroy(state, accTestUplinkHostSwitchProfileUpdateAttributes["display_name"])
Expand Down Expand Up @@ -155,19 +158,39 @@ func TestAccResourceNsxtUplinkHostSwitchProfile_basic(t *testing.T) {
resource.TestCheckResourceAttr(testResourceName, "tag.#", "1"),
),
},
{
Config: testAccNsxtUplinkHostSwitchProfileMinimalistic(),
Check: resource.ComposeTestCheckFunc(
testAccNsxtUplinkHostSwitchProfileExists(accTestUplinkHostSwitchProfileCreateAttributes["display_name"], testResourceName),
resource.TestCheckResourceAttr(testResourceName, "description", ""),
resource.TestCheckResourceAttrSet(testResourceName, "nsx_id"),
resource.TestCheckResourceAttrSet(testResourceName, "path"),
resource.TestCheckResourceAttrSet(testResourceName, "revision"),
resource.TestCheckResourceAttr(testResourceName, "tag.#", "0"),

resource.TestCheckNoResourceAttr(testResourceName, "lag.#"),
resource.TestCheckNoResourceAttr(testResourceName, "named_teaming.#"),
resource.TestCheckNoResourceAttr(testResourceName, "teaming.0.standby.#"),
resource.TestCheckResourceAttr(testResourceName, "mtu", "1700"),
resource.TestCheckResourceAttr(testResourceName, "overlay_encap", "GENEVE"),
resource.TestCheckResourceAttr(testResourceName, "transport_vlan", "0"),
),
},
},
})
}

func TestAccResourceNsxtUplinkHostSwitchProfile_importBasic(t *testing.T) {
name := getAccTestResourceName()
testResourceName := "nsxt_policy_uplink_host_switch_profile.test"
testResourceName := "nsxt_uplink_host_switch_profile.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
PreCheck: func() {
testAccOnlyLocalManager(t)
testAccPreCheck(t)
},
Providers: testAccProviders,
CheckDestroy: func(state *terraform.State) error {
return testAccNsxtUplinkHostSwitchProfileCheckDestroy(state, name)
return testAccNsxtUplinkHostSwitchProfileCheckDestroy(state, accTestUplinkHostSwitchProfileUpdateAttributes["display_name"])
},
Steps: []resource.TestStep{
{
Expand Down Expand Up @@ -292,5 +315,12 @@ func testAccNsxtUplinkHostSwitchProfileMinimalistic() string {
return fmt.Sprintf(`
resource "nsxt_uplink_host_switch_profile" "test" {
display_name = "%s"
}`, accTestUplinkHostSwitchProfileUpdateAttributes["display_name"])
teaming {
active {
uplink_name = "%s"
uplink_type = "%s"
}
policy = "%s"
}
}`, accTestUplinkHostSwitchProfileUpdateAttributes["display_name"], accTestUplinkHostSwitchProfileUpdateAttributes["teaming_al_uplink_name"], accTestUplinkHostSwitchProfileUpdateAttributes["teaming_al_uplink_type"], accTestUplinkHostSwitchProfileUpdateAttributes["teaming_policy"])
}
10 changes: 5 additions & 5 deletions website/docs/r/uplink_host_switch_profile.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -85,22 +85,22 @@ The following arguments are supported:
* `number_of_uplinks` - (Required) Number of uplinks format: int32, minimum: 2, maximum: 32.
* `timeout_type` - (Optional) LACP timeout type. Possible values are: SLOW, FAST. Default: SLOW
* `teaming` - (Required) Default TeamingPolicy associated with this UplinkProfile.
* `active_list` - (Required) List of Uplinks used in active list
* `active` - (Required) List of Uplinks used in active list
* `uplink_name` - (Required) Name of this uplink
* `uplink_type` - (Required) Type of the uplink. Possible values are: PNIC, LAG.
* `policy` - (Required) Teaming policy. Possible values are: FAILOVER_ORDER, LOADBALANCE_SRCID, LOADBALANCE_SRC_MAC.
* `standby_list` - (Optional) List of Uplinks used in standby list
* `standby` - (Optional) List of Uplinks used in standby list
* `uplink_name` - (Required) Name of this uplink
* `uplink_type` - (Required) Type of the uplink. Possible values are: PNIC, LAG.
* `named_teaming` - (Optional) List of named uplink teaming policies that can be used by logical switches
* `active_list` - (Required) List of Uplinks used in active list
* `active` - (Required) List of Uplinks used in active list
* `uplink_name` - (Required) Name of this uplink
* `uplink_type` - (Required) Type of the uplink. Possible values are: PNIC, LAG.
* `policy` - (Required) Teaming policy. Possible values are: FAILOVER_ORDER, LOADBALANCE_SRCID, LOADBALANCE_SRC_MAC.
* `standby_list` - (Optional) List of Uplinks used in standby list
* `standby` - (Optional) List of Uplinks used in standby list
* `uplink_name` - (Required) Name of this uplink
* `uplink_type` - (Required) Type of the uplink. Possible values are: PNIC, LAG.
* `name` - (Optional) An uplink teaming policy of a given name defined in UplinkHostSwitchProfile. The names of all NamedTeamingPolicies in an UplinkHostSwitchProfile must be different, but a name can be shared by different UplinkHostSwitchProfiles. Different TransportNodes can use different NamedTeamingPolicies having the same name in different UplinkHostSwitchProfiles to realize an uplink teaming policy on a logical switch. An uplink teaming policy on a logical switch can be any policy defined by a user; it does not have to be a single type of FAILOVER or LOADBALANCE. It can be a combination of types, for instance, a user can define a policy with name \"MyHybridTeamingPolicy\" as \"FAILOVER on all ESX TransportNodes and LOADBALANCE on all KVM TransportNodes\". The name is the key of the teaming policy and can not be changed once assigned.
* `name` - (Optional) An uplink teaming policy of a given name defined in UplinkHostSwitchProfile. The names of all NamedTeamingPolicies in an UplinkHostSwitchProfile must be different, but a name can be shared by different UplinkHostSwitchProfiles. Different TransportNodes can use different NamedTeamingPolicies having the same name in different UplinkHostSwitchProfiles to realize an uplink teaming policy on a logical switch. An uplink teaming policy on a logical switch can be any policy defined by a user; it does not have to be a single type of FAILOVER or LOADBALANCE. It can be a combination of types, for instance, a user can define a policy with name \"MyHybridTeamingPolicy\" as \"FAILOVER on all ESX TransportNodes and LOADBALANCE on all KVM TransportNodes\".

## Attributes Reference

Expand Down

0 comments on commit 2753a49

Please sign in to comment.