Skip to content

Commit 495f16a

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 788b190 commit 495f16a

4 files changed

+125
-2
lines changed

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

+28-1
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
}
@@ -104,6 +117,11 @@ func resourceNsxtPolicyIPBlockRead(d *schema.ResourceData, m interface{}) error
104117
if util.NsxVersionHigherOrEqual("4.2.0") {
105118
d.Set("visibility", block.Visibility)
106119
}
120+
if util.NsxVersionHigherOrEqual("9.1.0") {
121+
d.Set("cidr_list", block.CidrList)
122+
d.Set("range_list", setAllocationRangeListInSchema(block.RangeList))
123+
d.Set("reserved_ips", setAllocationRangeListInSchema(block.ReservedIps))
124+
}
107125

108126
return nil
109127
}
@@ -125,6 +143,9 @@ func resourceNsxtPolicyIPBlockCreate(d *schema.ResourceData, m interface{}) erro
125143
cidr := d.Get("cidr").(string)
126144
visibility := d.Get("visibility").(string)
127145
tags := getPolicyTagsFromSchema(d)
146+
cidrList := getStringListFromSchemaList(d, "cidr_list")
147+
rangeList := getAllocationRangeListFromSchema(d.Get("range_list").([]interface{}))
148+
reservedIPs := getAllocationRangeListFromSchema(d.Get("reserved_ips").([]interface{}))
128149

129150
obj := model.IpAddressBlock{
130151
DisplayName: &displayName,
@@ -135,6 +156,12 @@ func resourceNsxtPolicyIPBlockCreate(d *schema.ResourceData, m interface{}) erro
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+
}
164+
138165
// Create the resource using PATCH
139166
log.Printf("[INFO] Creating IP Block with ID %s", id)
140167
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 {

website/docs/r/policy_ip_block.html.markdown

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

6565
* `display_name` - (Required) The display name for the IP Block.
6666
* `description` - (Optional) Description of the resource.
67-
* `cidr` - (Required) Network address and the prefix length which will be associated with a layer-2 broadcast domain.
67+
* `cidr` - (Optional) Network address and the prefix length which will be associated with a layer-2 broadcast domain.
68+
* `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.
69+
* `range_list` - (Optional) Represents list of IP address ranges in the form of start and end IPs.
70+
* `start` - (Required) The start IP address for the allocation range.
71+
* `end` - (Required) The end IP address for the allocation range.
72+
* `reserved_ips` - (Optional) Represents list of reserved IP address in the form of start and end IPs.
73+
* `start` - (Required) The start IP address for the allocation range.
74+
* `end` - (Required) The end IP address for the allocation range.
6875
* `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.
6976
* `nsx_id` - (Optional) The NSX ID of this resource. If set, this ID will be used to create the resource.
7077
* `tag` - (Optional) A list of scope + tag pairs to associate with this IP Block.

0 commit comments

Comments
 (0)