New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support address_prefixes for multiple prefixes in subnet and virtualnetwork (ipv6) #6148
Support address_prefixes for multiple prefixes in subnet and virtualnetwork (ipv6) #6148
Conversation
maybe commits from #6142 belong with this PR since they are addressing the same subnets with multiple address prefixes |
@abhinavdahiya makes sense, I added commit #6142 to this PR and closed the other one. |
@katbyte PTAL |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @fabianofranz, looks like we're missing doc updates, could we update them? thanks!
@@ -94,7 +100,18 @@ func dataSourceArmSubnetRead(d *schema.ResourceData, meta interface{}) error { | |||
d.Set("virtual_network_name", virtualNetworkName) | |||
|
|||
if props := resp.SubnetPropertiesFormat; props != nil { | |||
d.Set("address_prefix", props.AddressPrefix) | |||
if props.AddressPrefix != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we don't need a nil check here as d.Set will do it for us
"address_prefixes": { | ||
Type: schema.TypeList, | ||
Optional: true, | ||
Elem: &schema.Schema{Type: schema.TypeString}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could we validate this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should do the same validation I mentioned above here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @fabianofranz, this is off to a good start. I've left some comments on what we can do differently but the bulk of my issues are around testing. I've left some notes on one of the tests but it'd also be great to get a more comprehensive suite of tests since we're deprecating and we should ensure that the old functionality works with the new way of doing things. We'll also want to update the attributes by adding and removing address prefixes. Lastly, we'll need to update the documentation by deprecating the old field there and adding the new ones.
properties.AddressPrefixes = nil | ||
} | ||
if !prefixSet { | ||
return fmt.Errorf("[ERROR] either address_prefix or address_prefixes is required") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can remove this check and prefixSet
if we swap the ConflictsWith
tag in the schema for address_prefix
and address_prefixes
with ExactlyOneOf: []string{"address_prefix", "address_prefixes"},
@@ -306,7 +330,18 @@ func resourceArmSubnetRead(d *schema.ResourceData, meta interface{}) error { | |||
d.Set("virtual_network_name", networkName) | |||
|
|||
if props := resp.SubnetPropertiesFormat; props != nil { | |||
d.Set("address_prefix", props.AddressPrefix) | |||
if props.AddressPrefix != nil { | |||
d.Set("address_prefix", props.AddressPrefix) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar to Katie's comment above, we don't need to nil check this before setting it
@@ -30,6 +30,28 @@ func TestAccDataSourceSubnet_basic(t *testing.T) { | |||
}) | |||
} | |||
|
|||
func TestAccDataSourceAzureRMSubnet_basic_addressPrefixes(t *testing.T) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like we're missing a similar test for the actual resource
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test also fails with:
Subnet is not valid because its IP address range is outside the IP address range of virtual network.
It looks like it's due to the ipv6 address that was specified.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Test now passes!
d.Set("address_prefix", props.AddressPrefix) | ||
} | ||
if props.AddressPrefixes == nil { | ||
if props.AddressPrefix != nil && len(*props.AddressPrefix) > 0 { |
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
func testAccAzureRMVirtualNetwork_basic_addressPrefixes(data acceptance.TestData) string { | ||
return fmt.Sprintf(` | ||
resource "azurerm_resource_group" "test" { | ||
name = "acctestRG-%d" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should append the package initial to the resource group name like so:
name = "acctestRG-%d" | |
name = "acctestRG-n-%d" |
so we can better track the resource groups that are getting hung up on what tests
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since most already existing tests already use that, I'd suggest to handle this for all tests in a separate PR. ;-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree we should do it for the rest but we should do the best practice now for any new features and we can fix up the rest when we can
properties.AddressPrefix = &(*properties.AddressPrefixes)[0] | ||
properties.AddressPrefixes = nil | ||
} | ||
if prefixSet == 0 { |
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
@katbyte @mbfrahry I'm having a test failure with
|
Hey @fabianofranz, unfortunately You can use it with List/Set that has a Also, I want to apologize for sending you down this rabbit hole. The files changed tab made it tricky to see that those values were in a Set. But the validation should be able to stay with the subnet resource |
Ok @mbfrahry, thanks for the explanation. I updated this PR back to validations in the subnet resource and addressed most comments from you and @katbyte. I think it must be good for another round of reviews, thanks! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @fabianofranz, thanks for making my requested changes. The tests for the subnet resource/datasource side of things looks good. I just have some minor comments/fixes to address
I am having issues getting the AllowMultipleAddressPrefixesOnSubnet
feature for virtual networks registered and so I can't move forward on reviewing that because I can't test it. I've opened a support ticket to see why that's the case but I'm not sure how long that'll take.
We can either wait some unknown amount of time to see what they come back with or we split this PR into two PRs. One for subnets and one for virtual networks and we get the subnets one in quickly. Apologies for the added work while we wait on the Azure side
@@ -241,6 +263,11 @@ func resourceArmSubnetUpdate(d *schema.ResourceData, meta interface{}) error { | |||
props.AddressPrefix = utils.String(d.Get("address_prefix").(string)) | |||
} | |||
|
|||
if d.HasChange("address_prefixes") { | |||
addressPrefixesRaw := d.Get("address_prefixes").([]interface{}) | |||
props.AddressPrefixes = utils.ExpandStringSlice(addressPrefixesRaw) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You do logic in the Create method for when address prefixes length is 1. Do we want to mirror that here?
Type: schema.TypeList, | ||
Optional: true, | ||
Computed: true, | ||
Elem: &schema.Schema{Type: schema.TypeString}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you have an empty address prefix? If not we should definitely enforce that we pass something to the API through
Elem: &schema.Schema{Type: schema.TypeString}, | |
Elem: &schema.Schema{ | |
Type: schema.TypeString, | |
ValidateFunc: validation.StringIsNotEmpty, | |
}, |
Type: schema.TypeString, | ||
Optional: true, | ||
Computed: true, | ||
Deprecated: "Use the `address_prefixes` property instead.", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should add a TODO comment here to remove these deprecations in the next major version. This will help us quickly find and remove all the deprecations when the time comes for the next major version
Deprecated: "Use the `address_prefixes` property instead.", | |
// TODO Remove this in the next major version release | |
Deprecated: "Use the `address_prefixes` property instead.", |
"address_prefixes": { | ||
Type: schema.TypeList, | ||
Optional: true, | ||
Elem: &schema.Schema{Type: schema.TypeString}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should do the same validation I mentioned above here
properties.AddressPrefixes = nil | ||
} | ||
if prefixSet == 0 { | ||
return nil, fmt.Errorf("[ERROR] either address_prefix or address_prefixes is required") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return nil, fmt.Errorf("[ERROR] either address_prefix or address_prefixes is required") | |
return nil, fmt.Errorf("one of `address_prefix` or `address_prefixes` must be set") |
just cleaning up the language here to be more consistent with similar error messages.
website/docs/r/subnet.html.markdown
Outdated
@@ -101,7 +105,8 @@ The following attributes are exported: | |||
* `name` - The name of the subnet. | |||
* `resource_group_name` - The name of the resource group in which the subnet is created in. | |||
* `virtual_network_name` - The name of the virtual network in which the subnet is created in | |||
* `address_prefix` - The address prefix for the subnet | |||
* `address_prefix` - (Deprecated) The address prefix for the subnet | |||
* `address_prefixes` - The address prefixes for the subnet |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We'll want to add something similar over on the datasource docs as well
func testAccAzureRMVirtualNetwork_basic_addressPrefixes(data acceptance.TestData) string { | ||
return fmt.Sprintf(` | ||
resource "azurerm_resource_group" "test" { | ||
name = "acctestRG-%d" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree we should do it for the rest but we should do the best practice now for any new features and we can fix up the rest when we can
func testAccAzureRMSubnet_basic_addressPrefixes(data acceptance.TestData) string { | ||
return fmt.Sprintf(` | ||
resource "azurerm_resource_group" "test" { | ||
name = "acctestRG-%d" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
name = "acctestRG-%d" | |
name = "acctestRG-n-%d" |
Updating for best practices
func testAccDataSourceArmVirtualNetwork_basic_addressPrefixes(data acceptance.TestData) string { | ||
return fmt.Sprintf(` | ||
resource "azurerm_resource_group" "test" { | ||
name = "acctest%d-rg" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
name = "acctest%d-rg" | |
name = "acctest%d-n-rg" |
Updating for best practices
@@ -30,6 +30,28 @@ func TestAccDataSourceSubnet_basic(t *testing.T) { | |||
}) | |||
} | |||
|
|||
func TestAccDataSourceAzureRMSubnet_basic_addressPrefixes(t *testing.T) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Test now passes!
@mbfrahry thanks for the review, all latest comments were addressed.
I'm facing exactly the same issue when trying to test it since yesterday, and I'm pretty sure it wasn't happening until a few days ago. Let's wait and see how your ticket goes. ;-) So here goes another PR that splits this one. If we're good to go with the subnets part of this, let's have the other PR going and once it lands, I'll rebase this one to focus on the VNet and wait for a solution about the test issue. Tks! |
Thanks for splitting this out @fabianofranz, I just heard back from support and they said that this feature isn't available for the public use yet and they aren't approving people to use it. I'll move this PR to our Blocked milestone until they release this feature to the public. |
The azure subnet resource allows for multiple address prefixes using the address_prefixes argument. The virtual network resource allows inline subnets, but does not currently support this argument. When the address_prefixes argument is used, address_prefix will be null when terraform refreshes its state causing terraform to panic. This code updates the virtual network resource to allow address_prefixes to be used and for address_prefix to be null. Tests have been added to test the multiple prefixes. The magic number in the new tests is the same magic number used in the other VNet tests. I'm not 100% clear on this number, but it appears to be a known ID. It's origin is documented (not very well) here: #1913 This fix addresses several IPv6 related bugs.
Any updates on this @fabianofranz @mbfrahry |
Closing this for now as it's not yet available for public use |
I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. If you feel I made an error 🤖 🙉 , please reach out to my human friends 👉 hashibot-feedback@hashicorp.com. Thanks! |
The azure subnet resource supports addressPrefix when one prefix is available and addressPrefixes when two or or more are set (for instance, when configuring an ipv4 and ipv6 dual stack configuration for a network). Update azurerm_subnet to allow either address_prefix or address_prefixes as input, where the latter is a list of string. Check that at least one is set, and mark the older address_prefix as deprecated.
The virtual network resource allows inline subnets, but does not currently support this argument. When the address_prefixes argument is used, address_prefix will be null when terraform refreshes its state causing terraform to panic. This code updates also the virtual network resource to allow
address_prefixes to be used and for address_prefix to be null.
Fixes #5140
Takes over #5139