From 6507b06fffbe4018b0dc0a0528b22793d6275494 Mon Sep 17 00:00:00 2001 From: neil-yechenwei Date: Tue, 27 Feb 2024 14:41:25 +0800 Subject: [PATCH 1/4] azurerm_private_dns_resolver_inbound_endpoint - mark private_ip_address as Optional --- .../private_dns_resolver_inbound_endpoint_resource.go | 3 +++ ...ate_dns_resolver_inbound_endpoint_resource_test.go | 6 ++++-- ...rivate_dns_resolver_inbound_endpoint.html.markdown | 11 +++-------- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/internal/services/privatednsresolver/private_dns_resolver_inbound_endpoint_resource.go b/internal/services/privatednsresolver/private_dns_resolver_inbound_endpoint_resource.go index 41904c45a62e..0f1c4cf18b21 100644 --- a/internal/services/privatednsresolver/private_dns_resolver_inbound_endpoint_resource.go +++ b/internal/services/privatednsresolver/private_dns_resolver_inbound_endpoint_resource.go @@ -77,8 +77,11 @@ func (r PrivateDNSResolverInboundEndpointResource) Arguments() map[string]*plugi ValidateFunc: commonids.ValidateSubnetID, }, + // When `private_ip_allocation_method` is `Dynamic`, API would automatically set the value for `private_ip_address` + // When `private_ip_allocation_method` is `Static`, `private_ip_address` needs to be set by user "private_ip_address": { Type: pluginsdk.TypeString, + Optional: true, Computed: true, }, diff --git a/internal/services/privatednsresolver/private_dns_resolver_inbound_endpoint_resource_test.go b/internal/services/privatednsresolver/private_dns_resolver_inbound_endpoint_resource_test.go index 27f2858a956c..87f531a3fabd 100644 --- a/internal/services/privatednsresolver/private_dns_resolver_inbound_endpoint_resource_test.go +++ b/internal/services/privatednsresolver/private_dns_resolver_inbound_endpoint_resource_test.go @@ -112,7 +112,7 @@ resource "azurerm_resource_group" "test" { } resource "azurerm_virtual_network" "test" { - name = "acctest-rg-%[2]d" + name = "acctest-vnet-%[2]d" resource_group_name = azurerm_resource_group.test.name location = azurerm_resource_group.test.location address_space = ["10.0.0.0/16"] @@ -218,7 +218,9 @@ resource "azurerm_private_dns_resolver_inbound_endpoint" "test" { private_dns_resolver_id = azurerm_private_dns_resolver.test.id location = azurerm_private_dns_resolver.test.location ip_configurations { - subnet_id = azurerm_subnet.test.id + subnet_id = azurerm_subnet.test.id + private_ip_allocation_method = "Static" + private_ip_address = "10.0.0.4" } tags = { key = "updated value" diff --git a/website/docs/r/private_dns_resolver_inbound_endpoint.html.markdown b/website/docs/r/private_dns_resolver_inbound_endpoint.html.markdown index 12a9c5834df6..fae9d13f87db 100644 --- a/website/docs/r/private_dns_resolver_inbound_endpoint.html.markdown +++ b/website/docs/r/private_dns_resolver_inbound_endpoint.html.markdown @@ -79,10 +79,11 @@ The following arguments are supported: An `ip_configurations` block supports the following: -* `private_ip_allocation_method` - (Optional) Private IP address allocation method. Allowed value is `Dynamic` and `Static`. Defaults to `Dynamic`. - * `subnet_id` - (Required) The subnet ID of the IP configuration. +* `private_ip_address` - (Optional) Private IP address of the IP configuration. + +* `private_ip_allocation_method` - (Optional) Private IP address allocation method. Allowed value is `Dynamic` and `Static`. Defaults to `Dynamic`. ## Attributes Reference @@ -90,12 +91,6 @@ In addition to the Arguments listed above - the following Attributes are exporte * `id` - The ID of the Private DNS Resolver Inbound Endpoint. ---- - -An `ip_configurations` block exports the following: - -* `private_ip_address` - Private IP address of the IP configuration. - ## Timeouts The `timeouts` block allows you to specify [timeouts](https://www.terraform.io/docs/configuration/resources.html#timeouts) for certain actions: From 18443caf9de3e649b6c2d47be67d27e68151a73c Mon Sep 17 00:00:00 2001 From: neil-yechenwei Date: Tue, 27 Feb 2024 15:26:40 +0800 Subject: [PATCH 2/4] update code --- ...resolver_inbound_endpoint_resource_test.go | 36 +++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/internal/services/privatednsresolver/private_dns_resolver_inbound_endpoint_resource_test.go b/internal/services/privatednsresolver/private_dns_resolver_inbound_endpoint_resource_test.go index 87f531a3fabd..7f9877e8fcd3 100644 --- a/internal/services/privatednsresolver/private_dns_resolver_inbound_endpoint_resource_test.go +++ b/internal/services/privatednsresolver/private_dns_resolver_inbound_endpoint_resource_test.go @@ -82,6 +82,20 @@ func TestAccDNSResolverInboundEndpoint_update(t *testing.T) { }) } +func TestAccDNSResolverInboundEndpoint_static(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_private_dns_resolver_inbound_endpoint", "test") + r := DNSResolverInboundEndpointResource{} + data.ResourceTest(t, r, []acceptance.TestStep{ + { + Config: r.static(data), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + ), + }, + data.ImportStep(), + }) +} + func (r DNSResolverInboundEndpointResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) { id, err := inboundendpoints.ParseInboundEndpointID(state.ID) if err != nil { @@ -219,8 +233,7 @@ resource "azurerm_private_dns_resolver_inbound_endpoint" "test" { location = azurerm_private_dns_resolver.test.location ip_configurations { subnet_id = azurerm_subnet.test.id - private_ip_allocation_method = "Static" - private_ip_address = "10.0.0.4" + private_ip_allocation_method = "Dynamic" } tags = { key = "updated value" @@ -228,3 +241,22 @@ resource "azurerm_private_dns_resolver_inbound_endpoint" "test" { } `, template, data.RandomInteger) } + +func (r DNSResolverInboundEndpointResource) static(data acceptance.TestData) string { + template := r.template(data) + return fmt.Sprintf(` + %s + +resource "azurerm_private_dns_resolver_inbound_endpoint" "test" { + name = "acctest-drie-%d" + private_dns_resolver_id = azurerm_private_dns_resolver.test.id + location = azurerm_private_dns_resolver.test.location + + ip_configurations { + subnet_id = azurerm_subnet.test.id + private_ip_allocation_method = "Static" + private_ip_address = "10.0.0.4" + } +} +`, template, data.RandomInteger) +} From 0f16876eab50adfde268b99a1308ed802a59a6b3 Mon Sep 17 00:00:00 2001 From: neil-yechenwei Date: Tue, 27 Feb 2024 15:43:06 +0800 Subject: [PATCH 3/4] update code --- .../private_dns_resolver_inbound_endpoint_resource_test.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/internal/services/privatednsresolver/private_dns_resolver_inbound_endpoint_resource_test.go b/internal/services/privatednsresolver/private_dns_resolver_inbound_endpoint_resource_test.go index 7f9877e8fcd3..bff92d3fc122 100644 --- a/internal/services/privatednsresolver/private_dns_resolver_inbound_endpoint_resource_test.go +++ b/internal/services/privatednsresolver/private_dns_resolver_inbound_endpoint_resource_test.go @@ -232,8 +232,7 @@ resource "azurerm_private_dns_resolver_inbound_endpoint" "test" { private_dns_resolver_id = azurerm_private_dns_resolver.test.id location = azurerm_private_dns_resolver.test.location ip_configurations { - subnet_id = azurerm_subnet.test.id - private_ip_allocation_method = "Dynamic" + subnet_id = azurerm_subnet.test.id } tags = { key = "updated value" From c8f07295cbced75b66315cb0c9c5671684a18439 Mon Sep 17 00:00:00 2001 From: neil-yechenwei Date: Fri, 1 Mar 2024 17:58:31 +0800 Subject: [PATCH 4/4] update code --- ..._dns_resolver_inbound_endpoint_resource.go | 24 ++++++++++++++----- 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/internal/services/privatednsresolver/private_dns_resolver_inbound_endpoint_resource.go b/internal/services/privatednsresolver/private_dns_resolver_inbound_endpoint_resource.go index 0f1c4cf18b21..f0aa5f36df79 100644 --- a/internal/services/privatednsresolver/private_dns_resolver_inbound_endpoint_resource.go +++ b/internal/services/privatednsresolver/private_dns_resolver_inbound_endpoint_resource.go @@ -77,8 +77,6 @@ func (r PrivateDNSResolverInboundEndpointResource) Arguments() map[string]*plugi ValidateFunc: commonids.ValidateSubnetID, }, - // When `private_ip_allocation_method` is `Dynamic`, API would automatically set the value for `private_ip_address` - // When `private_ip_allocation_method` is `Static`, `private_ip_address` needs to be set by user "private_ip_address": { Type: pluginsdk.TypeString, Optional: true, @@ -136,7 +134,10 @@ func (r PrivateDNSResolverInboundEndpointResource) Create() sdk.ResourceFunc { Tags: &model.Tags, } - iPConfigurationsValue := expandIPConfigurationModel(model.IPConfigurations) + iPConfigurationsValue, err := expandIPConfigurationModel(model.IPConfigurations) + if err != nil { + return err + } if iPConfigurationsValue != nil { properties.Properties.IPConfigurations = *iPConfigurationsValue @@ -179,7 +180,10 @@ func (r PrivateDNSResolverInboundEndpointResource) Update() sdk.ResourceFunc { } if metadata.ResourceData.HasChange("ip_configurations") { - iPConfigurationsValue := expandIPConfigurationModel(model.IPConfigurations) + iPConfigurationsValue, err := expandIPConfigurationModel(model.IPConfigurations) + if err != nil { + return err + } if iPConfigurationsValue != nil { properties.Properties.IPConfigurations = *iPConfigurationsValue @@ -292,12 +296,20 @@ func dnsResolverInboundEndpointDeleteRefreshFunc(ctx context.Context, client *in } } -func expandIPConfigurationModel(inputList []IPConfigurationModel) *[]inboundendpoints.IPConfiguration { +func expandIPConfigurationModel(inputList []IPConfigurationModel) (*[]inboundendpoints.IPConfiguration, error) { var outputList []inboundendpoints.IPConfiguration for _, v := range inputList { input := v output := inboundendpoints.IPConfiguration{} + if input.PrivateIPAllocationMethod == inboundendpoints.IPAllocationMethodDynamic && input.PrivateIPAddress != "" { + return nil, fmt.Errorf("`private_ip_address` cannot be set when `private_ip_allocation_method` is `Dynamic`") + } + + if input.PrivateIPAllocationMethod == inboundendpoints.IPAllocationMethodStatic && input.PrivateIPAddress == "" { + return nil, fmt.Errorf("`private_ip_address` must be set when `private_ip_allocation_method` is `Static`") + } + if input.PrivateIPAllocationMethod != "" { output.PrivateIPAllocationMethod = &input.PrivateIPAllocationMethod } @@ -313,7 +325,7 @@ func expandIPConfigurationModel(inputList []IPConfigurationModel) *[]inboundendp outputList = append(outputList, output) } - return &outputList + return &outputList, nil } func flattenIPConfigurationModel(inputList *[]inboundendpoints.IPConfiguration) []IPConfigurationModel {