From ec28e19f2acddf6375f5c8dccf51f38020a136ad Mon Sep 17 00:00:00 2001 From: Neil Ye Date: Mon, 4 Mar 2024 14:52:43 +0800 Subject: [PATCH] `azurerm_private_dns_resolver_inbound_endpoint` - mark `private_ip_address` as `Optional` (#25035) * azurerm_private_dns_resolver_inbound_endpoint - mark private_ip_address as Optional * update code * update code * update code --- ..._dns_resolver_inbound_endpoint_resource.go | 23 +++++++++--- ...resolver_inbound_endpoint_resource_test.go | 35 ++++++++++++++++++- ...ns_resolver_inbound_endpoint.html.markdown | 11 ++---- 3 files changed, 56 insertions(+), 13 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..f0aa5f36df79 100644 --- a/internal/services/privatednsresolver/private_dns_resolver_inbound_endpoint_resource.go +++ b/internal/services/privatednsresolver/private_dns_resolver_inbound_endpoint_resource.go @@ -79,6 +79,7 @@ func (r PrivateDNSResolverInboundEndpointResource) Arguments() map[string]*plugi "private_ip_address": { Type: pluginsdk.TypeString, + Optional: true, Computed: true, }, @@ -133,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 @@ -176,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 @@ -289,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 } @@ -310,7 +325,7 @@ func expandIPConfigurationModel(inputList []IPConfigurationModel) *[]inboundendp outputList = append(outputList, output) } - return &outputList + return &outputList, nil } func flattenIPConfigurationModel(inputList *[]inboundendpoints.IPConfiguration) []IPConfigurationModel { 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..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 @@ -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 { @@ -112,7 +126,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"] @@ -226,3 +240,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) +} 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: