Skip to content

Commit 5afbe8b

Browse files
committed
Add cidr_list attribute to nsxt_policy_ip_block
This attribute is added for NSX v9.1.0. Signed-off-by: Kobi Samoray <[email protected]>
1 parent 9d85019 commit 5afbe8b

File tree

4 files changed

+129
-4
lines changed

4 files changed

+129
-4
lines changed

docs/resources/policy_ip_block.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,14 @@ The following arguments are supported:
6363

6464
* `display_name` - (Required) The display name for the IP Block.
6565
* `description` - (Optional) Description of the resource.
66-
* `cidr` - (Required) Network address and the prefix length which will be associated with a layer-2 broadcast domain.
66+
* `cidr` - (Optional) Network address and the prefix length which will be associated with a layer-2 broadcast domain. This attribute is deprecated for NSX 9.1.0 onwards.
67+
* `cidr_list` - (Optional) Array of contiguous IP address spaces represented by network address and prefix length. This attribute is supported with NSX 9.1.0 onwards.
68+
* `range_list` - (Optional) Represents list of IP address ranges in the form of start and end IPs. This attribute is supported with NSX 9.1.0 onwards.
69+
* `start` - (Required) The start IP address for the allocation range.
70+
* `end` - (Required) The end IP address for the allocation range.
71+
* `reserved_ips` - (Optional) Represents list of reserved IP address in the form of start and end IPs. This attribute is supported with NSX 9.1.0 onwards.
72+
* `start` - (Required) The start IP address for the allocation range.
73+
* `end` - (Required) The end IP address for the allocation range.
6774
* `visibility` - (Optional) Visibility of the IP Block. Valid options are `PRIVATE`, `EXTERNAL` or unset. Visibility cannot be changed once the block is associated with other resources.
6875
* `nsx_id` - (Optional) The NSX ID of this resource. If set, this ID will be used to create the resource.
6976
* `tag` - (Optional) A list of scope + tag pairs to associate with this IP Block.

nsxt/policy_common.go

+27
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,33 @@ func getAllocationRangeListSchema(required bool, description string) *schema.Sch
755755
}
756756
}
757757

758+
func getAllocationRangeListFromSchema(allocRanges []interface{}) []model.IpPoolRange {
759+
var poolRanges []model.IpPoolRange
760+
for _, allocRange := range allocRanges {
761+
allocMap := allocRange.(map[string]interface{})
762+
start := allocMap["start"].(string)
763+
end := allocMap["end"].(string)
764+
ipRange := model.IpPoolRange{
765+
Start: &start,
766+
End: &end,
767+
}
768+
poolRanges = append(poolRanges, ipRange)
769+
}
770+
return poolRanges
771+
}
772+
773+
func setAllocationRangeListInSchema(allocRanges []model.IpPoolRange) []map[string]interface{} {
774+
var allocations []map[string]interface{}
775+
for _, allocRange := range allocRanges {
776+
allocMap := make(map[string]interface{})
777+
allocMap["start"] = allocRange.Start
778+
allocMap["end"] = allocRange.End
779+
allocations = append(allocations, allocMap)
780+
}
781+
782+
return allocations
783+
}
784+
758785
func localManagerOnlyError() error {
759786
return fmt.Errorf("This configuration is not supported with NSX Global Manager")
760787
}

nsxt/resource_nsxt_policy_ip_block.go

+32-3
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,28 @@ func resourceNsxtPolicyIPBlock() *schema.Resource {
4646
"cidr": {
4747
Type: schema.TypeString,
4848
Description: "Network address and the prefix length which will be associated with a layer-2 broadcast domain",
49-
Required: true,
49+
Optional: true,
5050
ValidateFunc: validateCidr(),
51+
Deprecated: "Use cidr_list attribute instead, for v9.1 and above",
5152
},
5253
"visibility": {
5354
Type: schema.TypeString,
5455
Description: "Visibility of the Ip Block. Cannot be updated once associated with other resources.",
5556
Optional: true,
5657
ValidateFunc: validation.StringInSlice(visibilityTypes, false),
5758
},
59+
"cidr_list": {
60+
Type: schema.TypeList,
61+
Optional: true,
62+
Description: "Array of contiguous IP address spaces represented by network address and prefix length",
63+
Elem: &schema.Schema{
64+
Type: schema.TypeString,
65+
ValidateFunc: validateCidr(),
66+
},
67+
ConflictsWith: []string{"cidr"},
68+
},
69+
"range_list": getAllocationRangeListSchema(false, "Represents list of IP address ranges in the form of start and end IPs"),
70+
"reserved_ips": getAllocationRangeListSchema(false, "Represents list of reserved IP address in the form of start and end IPs"),
5871
},
5972
}
6073
}
@@ -100,10 +113,16 @@ func resourceNsxtPolicyIPBlockRead(d *schema.ResourceData, m interface{}) error
100113
d.Set("nsx_id", block.Id)
101114
d.Set("path", block.Path)
102115
d.Set("revision", block.Revision)
103-
d.Set("cidr", block.Cidr)
104116
if util.NsxVersionHigherOrEqual("4.2.0") {
105117
d.Set("visibility", block.Visibility)
106118
}
119+
if util.NsxVersionHigherOrEqual("9.1.0") {
120+
d.Set("cidr_list", block.CidrList)
121+
d.Set("range_list", setAllocationRangeListInSchema(block.RangeList))
122+
d.Set("reserved_ips", setAllocationRangeListInSchema(block.ReservedIps))
123+
} else {
124+
d.Set("cidr", block.Cidr)
125+
}
107126

108127
return nil
109128
}
@@ -125,16 +144,26 @@ func resourceNsxtPolicyIPBlockCreate(d *schema.ResourceData, m interface{}) erro
125144
cidr := d.Get("cidr").(string)
126145
visibility := d.Get("visibility").(string)
127146
tags := getPolicyTagsFromSchema(d)
147+
cidrList := getStringListFromSchemaList(d, "cidr_list")
148+
rangeList := getAllocationRangeListFromSchema(d.Get("range_list").([]interface{}))
149+
reservedIPs := getAllocationRangeListFromSchema(d.Get("reserved_ips").([]interface{}))
128150

129151
obj := model.IpAddressBlock{
130152
DisplayName: &displayName,
131153
Description: &description,
132-
Cidr: &cidr,
133154
Tags: tags,
134155
}
135156
if util.NsxVersionHigherOrEqual("4.2.0") && len(visibility) > 0 {
136157
obj.Visibility = &visibility
137158
}
159+
if util.NsxVersionHigherOrEqual("9.1.0") && len(cidrList) > 0 {
160+
obj.CidrList = cidrList
161+
obj.RangeList = rangeList
162+
obj.ReservedIps = reservedIPs
163+
} else {
164+
obj.Cidr = &cidr
165+
}
166+
138167
// Create the resource using PATCH
139168
log.Printf("[INFO] Creating IP Block with ID %s", id)
140169
err = client.Patch(id, obj)

nsxt/resource_nsxt_policy_ip_block_test.go

+62
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,40 @@ func TestAccResourceNsxtPolicyIPBlock_minimal(t *testing.T) {
4242
})
4343
}
4444

45+
func TestAccResourceNsxtPolicyIPBlock_v910(t *testing.T) {
46+
name := getAccTestResourceName()
47+
testResourceName := "nsxt_policy_ip_block.test"
48+
cidr := "192.168.1.0/24"
49+
resource.ParallelTest(t, resource.TestCase{
50+
PreCheck: func() {
51+
testAccOnlyLocalManager(t)
52+
testAccPreCheck(t)
53+
testAccNSXVersion(t, "9.1.0")
54+
},
55+
Providers: testAccProviders,
56+
CheckDestroy: func(state *terraform.State) error {
57+
return testAccNSXPolicyIPBlockCheckDestroy(state)
58+
},
59+
Steps: []resource.TestStep{
60+
{
61+
Config: testAccNSXPolicyIPBlockCreateV910Template(name, cidr, false, false),
62+
Check: resource.ComposeTestCheckFunc(
63+
testAccNSXPolicyIPBlockCheckExists(testResourceName),
64+
resource.TestCheckResourceAttr(testResourceName, "display_name", name),
65+
resource.TestCheckResourceAttr(testResourceName, "cidr_list.#", "1"),
66+
resource.TestCheckResourceAttr(testResourceName, "cidr_list.0", cidr),
67+
resource.TestCheckResourceAttr(testResourceName, "reserved_ips.#", "2"),
68+
resource.TestCheckResourceAttr(testResourceName, "range_list.#", "1"),
69+
resource.TestCheckResourceAttr(testResourceName, "tag.#", "0"),
70+
resource.TestCheckResourceAttrSet(testResourceName, "revision"),
71+
resource.TestCheckResourceAttrSet(testResourceName, "nsx_id"),
72+
resource.TestCheckResourceAttrSet(testResourceName, "path"),
73+
),
74+
},
75+
},
76+
})
77+
}
78+
4579
func TestAccResourceNsxtPolicyIPBlock_basic(t *testing.T) {
4680
testAccResourceNsxtPolicyIPBlockBasic(t, false, false, func() {
4781
testAccPreCheck(t)
@@ -254,6 +288,34 @@ resource "nsxt_policy_ip_block" "test" {
254288
}`, context, displayName, cidr, visibility)
255289
}
256290

291+
func testAccNSXPolicyIPBlockCreateV910Template(displayName string, cidr string, withContext, withVisibility bool) string {
292+
context := ""
293+
if withContext {
294+
context = testAccNsxtPolicyMultitenancyContext()
295+
}
296+
297+
visibility := ""
298+
if withVisibility {
299+
visibility = " visibility = \"EXTERNAL\""
300+
}
301+
302+
return fmt.Sprintf(`
303+
resource "nsxt_policy_ip_block" "test" {
304+
%s
305+
display_name = "%s"
306+
cidr_list = ["%s"]
307+
reserved_ips {
308+
start = "192.168.1.10"
309+
end = "192.168.1.11"
310+
}
311+
range_list {
312+
start = "192.168.1.20"
313+
end = "192.168.1.39"
314+
}
315+
%s
316+
}`, context, displayName, cidr, visibility)
317+
}
318+
257319
func testAccNSXPolicyIPBlockUpdateTemplate(displayName string, cidr string, withContext, withVisibility bool) string {
258320
context := ""
259321
if withContext {

0 commit comments

Comments
 (0)