Skip to content

Commit

Permalink
Merge pull request #779 from vmware/replication-mode
Browse files Browse the repository at this point in the history
Support replication mode in Segment resources
  • Loading branch information
annakhm authored Aug 5, 2022
2 parents 0618c2e + 350f08f commit 866e722
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 20 deletions.
24 changes: 14 additions & 10 deletions nsxt/resource_nsxt_policy_fixed_segment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ func TestAccResourceNsxtPolicyFixedSegment_updateAdvConfig(t *testing.T) {
testAccNsxtPolicyFixedSegmentExists(testResourceName),
resource.TestCheckResourceAttr(testResourceName, "display_name", name),
resource.TestCheckResourceAttr(testResourceName, "description", "Acceptance Test"),
resource.TestCheckResourceAttr(testResourceName, "replication_mode", "SOURCE"),
resource.TestCheckResourceAttr(testResourceName, "subnet.#", "1"),
resource.TestCheckResourceAttr(testResourceName, "subnet.0.cidr", "12.12.2.1/24"),
resource.TestCheckResourceAttr(testResourceName, "domain_name", "tftest.org"),
Expand All @@ -158,6 +159,7 @@ func TestAccResourceNsxtPolicyFixedSegment_updateAdvConfig(t *testing.T) {
testAccNsxtPolicyFixedSegmentExists(testResourceName),
resource.TestCheckResourceAttr(testResourceName, "display_name", name),
resource.TestCheckResourceAttr(testResourceName, "description", "Acceptance Test"),
resource.TestCheckResourceAttr(testResourceName, "replication_mode", "MTEP"),
resource.TestCheckResourceAttr(testResourceName, "subnet.#", "1"),
resource.TestCheckResourceAttr(testResourceName, "subnet.0.cidr", "12.12.2.1/24"),
resource.TestCheckResourceAttr(testResourceName, "domain_name", "tftest.org"),
Expand Down Expand Up @@ -406,11 +408,12 @@ func testAccNsxtPolicyFixedSegmentBasicAdvConfigTemplate(tzName string, name str
return testAccNsxtPolicySegmentDeps(tzName) + fmt.Sprintf(`
resource "nsxt_policy_fixed_segment" "test" {
display_name = "%s"
description = "Acceptance Test"
domain_name = "tftest.org"
overlay_id = 1011
vlan_ids = ["101", "102"]
display_name = "%s"
description = "Acceptance Test"
replication_mode = "SOURCE"
domain_name = "tftest.org"
overlay_id = 1011
vlan_ids = ["101", "102"]
connectivity_path = nsxt_policy_tier1_gateway.anotherTier1ForSegments.path
Expand All @@ -435,11 +438,12 @@ func testAccNsxtPolicyFixedSegmentBasicAdvConfigUpdateTemplate(tzName string, na
return testAccNsxtPolicySegmentDeps(tzName) + fmt.Sprintf(`
resource "nsxt_policy_fixed_segment" "test" {
display_name = "%s"
description = "Acceptance Test"
domain_name = "tftest.org"
overlay_id = 1011
vlan_ids = ["101-104"]
display_name = "%s"
description = "Acceptance Test"
replication_mode = "MTEP"
domain_name = "tftest.org"
overlay_id = 1011
vlan_ids = ["101-104"]
connectivity_path = nsxt_policy_tier1_gateway.anotherTier1ForSegments.path
Expand Down
12 changes: 8 additions & 4 deletions nsxt/resource_nsxt_policy_segment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ func TestAccResourceNsxtPolicySegment_withDhcp(t *testing.T) {
preferredTimes := []string{"3200", "32000"}
dnsServersV4 := []string{"2.2.2.2", "3.3.3.3"}
dnsServersV6 := []string{"2000::2", "3000::3"}
replicationModes := []string{"SOURCE", "MTEP"}
tzName := getOverlayTransportZoneName()

resource.ParallelTest(t, resource.TestCase{
Expand All @@ -307,10 +308,11 @@ func TestAccResourceNsxtPolicySegment_withDhcp(t *testing.T) {
},
Steps: []resource.TestStep{
{
Config: testAccNsxtPolicySegmentWithDhcpTemplate(tzName, name, dnsServersV4[0], dnsServersV6[0], leaseTimes[0], preferredTimes[0]),
Config: testAccNsxtPolicySegmentWithDhcpTemplate(tzName, name, dnsServersV4[0], dnsServersV6[0], leaseTimes[0], preferredTimes[0], replicationModes[0]),
Check: resource.ComposeTestCheckFunc(
testAccNsxtPolicySegmentExists(testResourceName),
resource.TestCheckResourceAttr(testResourceName, "display_name", name),
resource.TestCheckResourceAttr(testResourceName, "replication_mode", replicationModes[0]),
resource.TestCheckResourceAttr(testResourceName, "subnet.#", "2"),
resource.TestCheckResourceAttr(testResourceName, "subnet.0.cidr", "12.12.2.1/24"),
resource.TestCheckResourceAttr(testResourceName, "subnet.0.dhcp_v6_config.#", "0"),
Expand All @@ -334,10 +336,11 @@ func TestAccResourceNsxtPolicySegment_withDhcp(t *testing.T) {
),
},
{
Config: testAccNsxtPolicySegmentWithDhcpTemplate(tzName, name, dnsServersV4[1], dnsServersV6[1], leaseTimes[1], preferredTimes[1]),
Config: testAccNsxtPolicySegmentWithDhcpTemplate(tzName, name, dnsServersV4[1], dnsServersV6[1], leaseTimes[1], preferredTimes[1], replicationModes[1]),
Check: resource.ComposeTestCheckFunc(
testAccNsxtPolicySegmentExists(testResourceName),
resource.TestCheckResourceAttr(testResourceName, "display_name", name),
resource.TestCheckResourceAttr(testResourceName, "replication_mode", replicationModes[1]),
resource.TestCheckResourceAttr(testResourceName, "subnet.#", "2"),
resource.TestCheckResourceAttr(testResourceName, "subnet.0.cidr", "12.12.2.1/24"),
resource.TestCheckResourceAttr(testResourceName, "subnet.0.dhcp_v6_config.#", "0"),
Expand Down Expand Up @@ -640,7 +643,7 @@ data "nsxt_policy_edge_cluster" "EC" {
}`, name)
}

func testAccNsxtPolicySegmentWithDhcpTemplate(tzName string, name string, dnsServerV4 string, dnsServerV6 string, lease string, preferred string) string {
func testAccNsxtPolicySegmentWithDhcpTemplate(tzName string, name string, dnsServerV4 string, dnsServerV6 string, lease string, preferred string, replicationMode string) string {
return testAccNsxtPolicySegmentDeps(tzName) +
testAccNsxtPolicyEdgeCluster(getEdgeClusterName()) + fmt.Sprintf(`
Expand All @@ -652,6 +655,7 @@ resource "nsxt_policy_dhcp_server" "test" {
resource "nsxt_policy_segment" "test" {
display_name = "%s"
transport_zone_path = data.nsxt_policy_transport_zone.test.path
replication_mode = "%s"
subnet {
cidr = "12.12.2.1/24"
Expand Down Expand Up @@ -696,7 +700,7 @@ resource "nsxt_policy_segment" "test" {
dhcp_config_path = nsxt_policy_dhcp_server.test.path
}
`, name, lease, dnsServerV4, lease, preferred, dnsServerV6)
`, name, replicationMode, lease, dnsServerV4, lease, preferred, dnsServerV6)
}

func testAccNsxtPolicySegmentNoTransportZoneTemplate(name string, cidr string) string {
Expand Down
3 changes: 3 additions & 0 deletions nsxt/resource_nsxt_policy_vlan_segment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ func TestAccResourceNsxtPolicyVlanSegment_updateAdvConfig(t *testing.T) {
testAccNsxtPolicyVlanSegmentExists(testResourceName),
resource.TestCheckResourceAttr(testResourceName, "display_name", name),
resource.TestCheckResourceAttr(testResourceName, "description", "Acceptance Test"),
resource.TestCheckResourceAttr(testResourceName, "replication_mode", "SOURCE"),
resource.TestCheckResourceAttr(testResourceName, "domain_name", "tftest.org"),
resource.TestCheckResourceAttr(testResourceName, "tag.#", "1"),
resource.TestCheckResourceAttr(testResourceName, "advanced_config.0.connectivity", "OFF"),
Expand All @@ -99,6 +100,7 @@ func TestAccResourceNsxtPolicyVlanSegment_updateAdvConfig(t *testing.T) {
testAccNsxtPolicyVlanSegmentExists(testResourceName),
resource.TestCheckResourceAttr(testResourceName, "display_name", name),
resource.TestCheckResourceAttr(testResourceName, "description", "Acceptance Test"),
resource.TestCheckResourceAttr(testResourceName, "replication_mode", "MTEP"),
resource.TestCheckResourceAttr(testResourceName, "domain_name", "tftest.org"),
resource.TestCheckResourceAttr(testResourceName, "tag.#", "1"),
resource.TestCheckResourceAttr(testResourceName, "advanced_config.0.connectivity", "ON"),
Expand Down Expand Up @@ -343,6 +345,7 @@ resource "nsxt_policy_vlan_segment" "test" {
description = "Acceptance Test"
domain_name = "tftest.org"
transport_zone_path = data.nsxt_policy_transport_zone.test.path
replication_mode = "SOURCE"
vlan_ids = ["101", "102"]
tag {
Expand Down
35 changes: 29 additions & 6 deletions nsxt/segment_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ var urpfModeValues = []string{
model.SegmentAdvancedConfig_URPF_MODE_STRICT,
}

var replicationModeValues = []string{
model.Segment_REPLICATION_MODE_MTEP,
model.Segment_REPLICATION_MODE_SOURCE,
}

func getPolicySegmentDhcpV4ConfigSchema() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
Expand Down Expand Up @@ -121,7 +126,7 @@ func getPolicySegmentSubnetSchema() *schema.Resource {
"cidr": {
Type: schema.TypeString,
Description: "Gateway IP address in CIDR format",
Required: true,
Optional: true,
ValidateFunc: validateIPCidr(),
},
"network": {
Expand Down Expand Up @@ -316,6 +321,13 @@ func getPolicyCommonSegmentSchema(vlanRequired bool, isFixed bool) map[string]*s
Optional: true,
MaxItems: 1,
},
"replication_mode": {
Type: schema.TypeString,
Description: "Replication mode - MTEP or SOURCE",
Optional: true,
Default: model.Segment_REPLICATION_MODE_MTEP,
ValidateFunc: validation.StringInSlice(replicationModeValues, false),
},
}

if isFixed {
Expand Down Expand Up @@ -652,6 +664,7 @@ func policySegmentResourceToInfraStruct(id string, d *schema.ResourceData, isVla
tags := getPolicyTagsFromSchema(d)
domainName := d.Get("domain_name").(string)
tzPath := d.Get("transport_zone_path").(string)
replicationMode := d.Get("replication_mode").(string)
dhcpConfigPath := d.Get("dhcp_config_path").(string)
revision := int64(d.Get("revision").(int))
resourceType := "Segment"
Expand All @@ -677,8 +690,11 @@ func policySegmentResourceToInfraStruct(id string, d *schema.ResourceData, isVla
if tzPath != "" {
obj.TransportZonePath = &tzPath
}
if dhcpConfigPath != "" && nsxVersionHigherOrEqual("3.0.0") {
obj.DhcpConfigPath = &dhcpConfigPath
if nsxVersionHigherOrEqual("3.0.0") {
obj.ReplicationMode = &replicationMode
if dhcpConfigPath != "" {
obj.DhcpConfigPath = &dhcpConfigPath
}
}

var vlanIds []string
Expand Down Expand Up @@ -715,10 +731,13 @@ func policySegmentResourceToInfraStruct(id string, d *schema.ResourceData, isVla
gwAddr := subnetMap["cidr"].(string)
network := subnetMap["network"].(string)
subnetStruct := model.SegmentSubnet{
DhcpRanges: dhcpRangeList,
GatewayAddress: &gwAddr,
Network: &network,
DhcpRanges: dhcpRangeList,
Network: &network,
}
if len(gwAddr) > 0 {
subnetStruct.GatewayAddress = &gwAddr
}

config, err := getSegmentSubnetDhcpConfigFromSchema(subnetMap)
if err != nil {
return model.Infra{}, err
Expand Down Expand Up @@ -1302,6 +1321,10 @@ func nsxtPolicySegmentRead(d *schema.ResourceData, m interface{}, isVlan bool, i
}
}

if nsxVersionHigherOrEqual("3.0.0") {
d.Set("replication_mode", obj.ReplicationMode)
}

if obj.AdvancedConfig != nil {
advConfig := make(map[string]interface{})
poolPaths := obj.AdvancedConfig.AddressPoolPaths
Expand Down

0 comments on commit 866e722

Please sign in to comment.