Skip to content

Commit 58784e2

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 203438c commit 58784e2

File tree

4 files changed

+211
-5
lines changed

4 files changed

+211
-5
lines changed

docs/resources/policy_ip_block.md

+28-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,26 @@ resource "nsxt_policy_ip_block" "block1" {
3030
}
3131
```
3232

33+
## Example Usage - CIDR List
34+
35+
```hcl
36+
resource "nsxt_policy_ip_block" "block1" {
37+
display_name = "ip-block1"
38+
cidr_list = ["192.168.1.0/24"]
39+
visibility = "PRIVATE"
40+
41+
tag {
42+
scope = "color"
43+
tag = "blue"
44+
}
45+
46+
tag {
47+
scope = "env"
48+
tag = "test"
49+
}
50+
}
51+
```
52+
3353
## Example Usage - Multi-Tenancy
3454

3555
```hcl
@@ -63,7 +83,14 @@ The following arguments are supported:
6383

6484
* `display_name` - (Required) The display name for the IP Block.
6585
* `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.
86+
* `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.
87+
* `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.
88+
* `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.
89+
* `start` - (Required) The start IP address for the allocation range.
90+
* `end` - (Required) The end IP address for the allocation range.
91+
* `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.
92+
* `start` - (Required) The start IP address for the allocation range.
93+
* `end` - (Required) The end IP address for the allocation range.
6794
* `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.
6895
* `nsx_id` - (Optional) The NSX ID of this resource. If set, this ID will be used to create the resource.
6996
* `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

+46-4
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,29 @@ 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+
Computed: true, // For v9.1.0 and above, when setting cidr attribute, it's being reflected here as well
63+
Description: "Array of contiguous IP address spaces represented by network address and prefix length",
64+
Elem: &schema.Schema{
65+
Type: schema.TypeString,
66+
ValidateFunc: validateCidr(),
67+
},
68+
ConflictsWith: []string{"cidr"},
69+
},
70+
"range_list": getAllocationRangeListSchema(false, "Represents list of IP address ranges in the form of start and end IPs"),
71+
"reserved_ips": getAllocationRangeListSchema(false, "Represents list of reserved IP address in the form of start and end IPs"),
5872
},
5973
}
6074
}
@@ -100,10 +114,19 @@ func resourceNsxtPolicyIPBlockRead(d *schema.ResourceData, m interface{}) error
100114
d.Set("nsx_id", block.Id)
101115
d.Set("path", block.Path)
102116
d.Set("revision", block.Revision)
103-
d.Set("cidr", block.Cidr)
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+
if block.Cidr != nil {
125+
d.Set("cidr", block.Cidr)
126+
}
127+
} else {
128+
d.Set("cidr", block.Cidr)
129+
}
107130

108131
return nil
109132
}
@@ -125,16 +148,26 @@ func resourceNsxtPolicyIPBlockCreate(d *schema.ResourceData, m interface{}) erro
125148
cidr := d.Get("cidr").(string)
126149
visibility := d.Get("visibility").(string)
127150
tags := getPolicyTagsFromSchema(d)
151+
cidrList := getStringListFromSchemaList(d, "cidr_list")
152+
rangeList := getAllocationRangeListFromSchema(d.Get("range_list").([]interface{}))
153+
reservedIPs := getAllocationRangeListFromSchema(d.Get("reserved_ips").([]interface{}))
128154

129155
obj := model.IpAddressBlock{
130156
DisplayName: &displayName,
131157
Description: &description,
132-
Cidr: &cidr,
133158
Tags: tags,
134159
}
135160
if util.NsxVersionHigherOrEqual("4.2.0") && len(visibility) > 0 {
136161
obj.Visibility = &visibility
137162
}
163+
if util.NsxVersionHigherOrEqual("9.1.0") && len(cidrList) > 0 {
164+
obj.CidrList = cidrList
165+
obj.RangeList = rangeList
166+
obj.ReservedIps = reservedIPs
167+
} else {
168+
obj.Cidr = &cidr
169+
}
170+
138171
// Create the resource using PATCH
139172
log.Printf("[INFO] Creating IP Block with ID %s", id)
140173
err = client.Patch(id, obj)
@@ -166,18 +199,27 @@ func resourceNsxtPolicyIPBlockUpdate(d *schema.ResourceData, m interface{}) erro
166199
visibility := d.Get("visibility").(string)
167200
revision := int64(d.Get("revision").(int))
168201
tags := getPolicyTagsFromSchema(d)
202+
cidrList := getStringListFromSchemaList(d, "cidr_list")
203+
rangeList := getAllocationRangeListFromSchema(d.Get("range_list").([]interface{}))
204+
reservedIPs := getAllocationRangeListFromSchema(d.Get("reserved_ips").([]interface{}))
169205

170206
obj := model.IpAddressBlock{
171207
Id: &id,
172208
DisplayName: &displayName,
173209
Description: &description,
174-
Cidr: &cidr,
175210
Tags: tags,
176211
Revision: &revision,
177212
}
178213
if util.NsxVersionHigherOrEqual("4.2.0") && len(visibility) > 0 {
179214
obj.Visibility = &visibility
180215
}
216+
if util.NsxVersionHigherOrEqual("9.1.0") && len(cidrList) > 0 {
217+
obj.CidrList = cidrList
218+
obj.RangeList = rangeList
219+
obj.ReservedIps = reservedIPs
220+
} else {
221+
obj.Cidr = &cidr
222+
}
181223

182224
_, err := client.Update(id, obj)
183225
if err != nil {

nsxt/resource_nsxt_policy_ip_block_test.go

+110
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,86 @@ 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.#", "1"),
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+
79+
func TestAccResourceNsxtPolicyIPBlock_v910_migrate(t *testing.T) {
80+
name := getAccTestResourceName()
81+
testResourceName := "nsxt_policy_ip_block.test"
82+
cidr := "192.168.1.0/24"
83+
resource.ParallelTest(t, resource.TestCase{
84+
PreCheck: func() {
85+
testAccOnlyLocalManager(t)
86+
testAccPreCheck(t)
87+
testAccNSXVersion(t, "9.1.0")
88+
},
89+
Providers: testAccProviders,
90+
CheckDestroy: func(state *terraform.State) error {
91+
return testAccNSXPolicyIPBlockCheckDestroy(state)
92+
},
93+
Steps: []resource.TestStep{
94+
{
95+
Config: testAccNSXPolicyIPBlockCreateMinimalTemplate(name, cidr, false, false),
96+
Check: resource.ComposeTestCheckFunc(
97+
testAccNSXPolicyIPBlockCheckExists(testResourceName),
98+
resource.TestCheckResourceAttr(testResourceName, "display_name", name),
99+
resource.TestCheckResourceAttr(testResourceName, "cidr", cidr),
100+
resource.TestCheckResourceAttr(testResourceName, "tag.#", "0"),
101+
resource.TestCheckResourceAttrSet(testResourceName, "revision"),
102+
resource.TestCheckResourceAttrSet(testResourceName, "nsx_id"),
103+
resource.TestCheckResourceAttrSet(testResourceName, "path"),
104+
),
105+
},
106+
{
107+
Config: testAccNSXPolicyIPBlockCreateV910Template(name, cidr, false, false),
108+
Check: resource.ComposeTestCheckFunc(
109+
testAccNSXPolicyIPBlockCheckExists(testResourceName),
110+
resource.TestCheckResourceAttr(testResourceName, "display_name", name),
111+
resource.TestCheckResourceAttr(testResourceName, "cidr_list.#", "1"),
112+
resource.TestCheckResourceAttr(testResourceName, "cidr_list.0", cidr),
113+
resource.TestCheckResourceAttr(testResourceName, "reserved_ips.#", "1"),
114+
resource.TestCheckResourceAttr(testResourceName, "range_list.#", "1"),
115+
resource.TestCheckResourceAttr(testResourceName, "tag.#", "0"),
116+
resource.TestCheckResourceAttrSet(testResourceName, "revision"),
117+
resource.TestCheckResourceAttrSet(testResourceName, "nsx_id"),
118+
resource.TestCheckResourceAttrSet(testResourceName, "path"),
119+
),
120+
},
121+
},
122+
})
123+
}
124+
45125
func TestAccResourceNsxtPolicyIPBlock_basic(t *testing.T) {
46126
testAccResourceNsxtPolicyIPBlockBasic(t, false, false, func() {
47127
testAccPreCheck(t)
@@ -254,6 +334,36 @@ resource "nsxt_policy_ip_block" "test" {
254334
}`, context, displayName, cidr, visibility)
255335
}
256336

337+
func testAccNSXPolicyIPBlockCreateV910Template(displayName string, cidr string, withContext, withVisibility bool) string {
338+
context := ""
339+
if withContext {
340+
context = testAccNsxtPolicyMultitenancyContext()
341+
}
342+
343+
visibility := ""
344+
if withVisibility {
345+
visibility = " visibility = \"EXTERNAL\""
346+
}
347+
348+
return fmt.Sprintf(`
349+
resource "nsxt_policy_ip_block" "test" {
350+
%s
351+
display_name = "%s"
352+
cidr_list = ["%s"]
353+
reserved_ips {
354+
start = "192.168.1.10"
355+
end = "192.168.1.11"
356+
}
357+
358+
range_list {
359+
start = "192.168.2.20"
360+
end = "192.168.2.39"
361+
}
362+
363+
%s
364+
}`, context, displayName, cidr, visibility)
365+
}
366+
257367
func testAccNSXPolicyIPBlockUpdateTemplate(displayName string, cidr string, withContext, withVisibility bool) string {
258368
context := ""
259369
if withContext {

0 commit comments

Comments
 (0)