Skip to content

Commit 1559eb5

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 302efad commit 1559eb5

File tree

3 files changed

+73
-2
lines changed

3 files changed

+73
-2
lines changed

nsxt/resource_nsxt_policy_ip_block.go

+18-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ 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(),
5151
},
5252
"visibility": {
@@ -55,6 +55,15 @@ func resourceNsxtPolicyIPBlock() *schema.Resource {
5555
Optional: true,
5656
ValidateFunc: validation.StringInSlice(visibilityTypes, false),
5757
},
58+
"cidr_list": {
59+
Type: schema.TypeList,
60+
Optional: true,
61+
Description: "Array of contiguous IP address spaces represented by network address and prefix length",
62+
Elem: &schema.Schema{
63+
Type: schema.TypeString,
64+
ValidateFunc: validateCidr(),
65+
},
66+
},
5867
},
5968
}
6069
}
@@ -104,6 +113,9 @@ func resourceNsxtPolicyIPBlockRead(d *schema.ResourceData, m interface{}) error
104113
if util.NsxVersionHigherOrEqual("4.2.0") {
105114
d.Set("visibility", block.Visibility)
106115
}
116+
if util.NsxVersionHigherOrEqual("9.1.0") {
117+
d.Set("cidr_list", block.CidrList)
118+
}
107119

108120
return nil
109121
}
@@ -125,6 +137,7 @@ func resourceNsxtPolicyIPBlockCreate(d *schema.ResourceData, m interface{}) erro
125137
cidr := d.Get("cidr").(string)
126138
visibility := d.Get("visibility").(string)
127139
tags := getPolicyTagsFromSchema(d)
140+
cidrList := getStringListFromSchemaList(d, "cidr_list")
128141

129142
obj := model.IpAddressBlock{
130143
DisplayName: &displayName,
@@ -135,6 +148,10 @@ func resourceNsxtPolicyIPBlockCreate(d *schema.ResourceData, m interface{}) erro
135148
if util.NsxVersionHigherOrEqual("4.2.0") && len(visibility) > 0 {
136149
obj.Visibility = &visibility
137150
}
151+
if util.NsxVersionHigherOrEqual("9.1.0") && len(cidrList) > 0 {
152+
obj.CidrList = cidrList
153+
}
154+
138155
// Create the resource using PATCH
139156
log.Printf("[INFO] Creating IP Block with ID %s", id)
140157
err = client.Patch(id, obj)

nsxt/resource_nsxt_policy_ip_block_test.go

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

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

290+
func testAccNSXPolicyIPBlockCreateCidrListTemplate(displayName string, cidr string, withContext, withVisibility bool) string {
291+
context := ""
292+
if withContext {
293+
context = testAccNsxtPolicyMultitenancyContext()
294+
}
295+
296+
visibility := ""
297+
if withVisibility {
298+
visibility = " visibility = \"EXTERNAL\""
299+
}
300+
301+
return fmt.Sprintf(`
302+
resource "nsxt_policy_ip_block" "test" {
303+
%s
304+
display_name = "%s"
305+
cidr_list = ["%s"]
306+
%s
307+
}`, context, displayName, cidr, visibility)
308+
}
309+
257310
func testAccNSXPolicyIPBlockUpdateTemplate(displayName string, cidr string, withContext, withVisibility bool) string {
258311
context := ""
259312
if withContext {

website/docs/r/policy_ip_block.html.markdown

+2-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ 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.
6869
* `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.
6970
* `nsx_id` - (Optional) The NSX ID of this resource. If set, this ID will be used to create the resource.
7071
* `tag` - (Optional) A list of scope + tag pairs to associate with this IP Block.

0 commit comments

Comments
 (0)