diff --git a/internal/services/privatednsresolver/private_dns_resolver_inbound_endpoint_data_source.go b/internal/services/privatednsresolver/private_dns_resolver_inbound_endpoint_data_source.go new file mode 100644 index 000000000000..4b46e5203f4e --- /dev/null +++ b/internal/services/privatednsresolver/private_dns_resolver_inbound_endpoint_data_source.go @@ -0,0 +1,133 @@ +package privatednsresolver + +import ( + "context" + "fmt" + "time" + + "github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema" + "github.com/hashicorp/go-azure-helpers/resourcemanager/location" + "github.com/hashicorp/go-azure-sdk/resource-manager/dnsresolver/2022-07-01/dnsresolvers" + "github.com/hashicorp/go-azure-sdk/resource-manager/dnsresolver/2022-07-01/inboundendpoints" + "github.com/hashicorp/terraform-provider-azurerm/internal/sdk" + "github.com/hashicorp/terraform-provider-azurerm/internal/tags" + "github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" + "github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation" +) + +type PrivateDNSResolverInboundEndpointDataSourceModel struct { + Name string `tfschema:"name"` + PrivateDNSResolverId string `tfschema:"private_dns_resolver_id"` + IPConfigurations []IPConfigurationModel `tfschema:"ip_configurations"` + Location string `tfschema:"location"` + Tags map[string]string `tfschema:"tags"` +} + +type PrivateDNSResolverInboundEndpointDataSource struct{} + +var _ sdk.DataSource = PrivateDNSResolverInboundEndpointDataSource{} + +func (r PrivateDNSResolverInboundEndpointDataSource) ResourceType() string { + return "azurerm_private_dns_resolver_inbound_endpoint" +} + +func (r PrivateDNSResolverInboundEndpointDataSource) ModelObject() interface{} { + return &PrivateDNSResolverInboundEndpointDataSourceModel{} +} + +func (r PrivateDNSResolverInboundEndpointDataSource) IDValidationFunc() pluginsdk.SchemaValidateFunc { + return inboundendpoints.ValidateInboundEndpointID +} + +func (r PrivateDNSResolverInboundEndpointDataSource) Arguments() map[string]*pluginsdk.Schema { + return map[string]*pluginsdk.Schema{ + "name": { + Type: pluginsdk.TypeString, + Required: true, + ValidateFunc: validation.StringIsNotEmpty, + }, + + "private_dns_resolver_id": { + Type: pluginsdk.TypeString, + Required: true, + ValidateFunc: dnsresolvers.ValidateDnsResolverID, + }, + } +} + +func (r PrivateDNSResolverInboundEndpointDataSource) Attributes() map[string]*pluginsdk.Schema { + return map[string]*pluginsdk.Schema{ + "ip_configurations": { + Type: pluginsdk.TypeList, + Computed: true, + Elem: &pluginsdk.Resource{ + Schema: map[string]*pluginsdk.Schema{ + "subnet_id": { + Type: pluginsdk.TypeString, + Computed: true, + }, + + "private_ip_address": { + Type: pluginsdk.TypeString, + Computed: true, + }, + + "private_ip_allocation_method": { + Type: pluginsdk.TypeString, + Computed: true, + }, + }, + }, + }, + + "location": commonschema.LocationComputed(), + + "tags": tags.SchemaDataSource(), + } +} + +func (r PrivateDNSResolverInboundEndpointDataSource) Read() sdk.ResourceFunc { + return sdk.ResourceFunc{ + Timeout: 5 * time.Minute, + Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { + client := metadata.Client.PrivateDnsResolver.InboundEndpointsClient + + var state PrivateDNSResolverInboundEndpointDataSourceModel + if err := metadata.Decode(&state); err != nil { + return fmt.Errorf("decoding: %+v", err) + } + + dnsForwardingRulesetId, err := inboundendpoints.ParseDnsResolverID(state.PrivateDNSResolverId) + if err != nil { + return err + } + + id := inboundendpoints.NewInboundEndpointID( + dnsForwardingRulesetId.SubscriptionId, + dnsForwardingRulesetId.ResourceGroupName, + dnsForwardingRulesetId.DnsResolverName, + state.Name) + resp, err := client.Get(ctx, id) + if err != nil { + return fmt.Errorf("retrieving %s: %+v", id, err) + } + + model := resp.Model + if model == nil { + return fmt.Errorf("retrieving %s: model was nil", id) + } + + state.Location = location.Normalize(model.Location) + state.PrivateDNSResolverId = dnsresolvers.NewDnsResolverID(id.SubscriptionId, id.ResourceGroupName, id.DnsResolverName).ID() + + state.IPConfigurations = flattenIPConfigurationModel(&model.Properties.IPConfigurations) + if model.Tags != nil { + state.Tags = *model.Tags + } + + metadata.SetID(id) + + return metadata.Encode(&state) + }, + } +} diff --git a/internal/services/privatednsresolver/private_dns_resolver_inbound_endpoint_data_source_test.go b/internal/services/privatednsresolver/private_dns_resolver_inbound_endpoint_data_source_test.go new file mode 100644 index 000000000000..1a500ec5ce7f --- /dev/null +++ b/internal/services/privatednsresolver/private_dns_resolver_inbound_endpoint_data_source_test.go @@ -0,0 +1,41 @@ +package privatednsresolver_test + +import ( + "fmt" + "testing" + + "github.com/hashicorp/go-azure-helpers/resourcemanager/location" + "github.com/hashicorp/terraform-provider-azurerm/internal/acceptance" + "github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check" +) + +type DNSResolverInboundEndpointDataSource struct{} + +func TestAccDNSResolverInboundEndpointDataSource_basic(t *testing.T) { + data := acceptance.BuildTestData(t, "data.azurerm_private_dns_resolver_inbound_endpoint", "test") + d := DNSResolverInboundEndpointDataSource{} + + data.DataSourceTest(t, []acceptance.TestStep{ + { + Config: d.basic(data), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).Key("name").Exists(), + check.That(data.ResourceName).Key("private_dns_resolver_id").Exists(), + check.That(data.ResourceName).Key("location").HasValue(location.Normalize(data.Locations.Primary)), + check.That(data.ResourceName).Key("ip_configurations.0.subnet_id").Exists(), + check.That(data.ResourceName).Key("ip_configurations.0.private_ip_address").Exists(), + ), + }, + }) +} + +func (d DNSResolverInboundEndpointDataSource) basic(data acceptance.TestData) string { + return fmt.Sprintf(` +%s + +data "azurerm_private_dns_resolver_inbound_endpoint" "test" { + name = azurerm_private_dns_resolver_inbound_endpoint.test.name + private_dns_resolver_id = azurerm_private_dns_resolver.test.id +} +`, DNSResolverInboundEndpointResource{}.basic(data)) +} diff --git a/internal/services/privatednsresolver/registration.go b/internal/services/privatednsresolver/registration.go index a7f80e28cf3a..1bf5109e334b 100644 --- a/internal/services/privatednsresolver/registration.go +++ b/internal/services/privatednsresolver/registration.go @@ -43,6 +43,7 @@ func (r Registration) DataSources() []sdk.DataSource { return []sdk.DataSource{ PrivateDNSResolverDnsForwardingRulesetDataSource{}, PrivateDNSResolverDnsResolverDataSource{}, + PrivateDNSResolverInboundEndpointDataSource{}, PrivateDNSResolverOutboundEndpointDataSource{}, } } diff --git a/website/docs/d/private_dns_resolver_inbound_endpoint.html.markdown b/website/docs/d/private_dns_resolver_inbound_endpoint.html.markdown new file mode 100644 index 000000000000..d940c2f401f4 --- /dev/null +++ b/website/docs/d/private_dns_resolver_inbound_endpoint.html.markdown @@ -0,0 +1,56 @@ +--- +subcategory: "Private DNS Resolver" +layout: "azurerm" +page_title: "Azure Resource Manager: azurerm_private_dns_resolver_inbound_endpoint" +description: |- + Gets information about an existing Private DNS Resolver Inbound Endpoint. +--- + +# Data Source: azurerm_private_dns_resolver_inbound_endpoint + +Gets information about an existing Private DNS Resolver Inbound Endpoint. + +## Example Usage + +```hcl +resource "azurerm_private_dns_resolver_inbound_endpoint" "example" { + name = "example-drie" + private_dns_resolver_id = "example-private-dns-resolver-id" +} +``` + +## Arguments Reference + +The following arguments are supported: + +* `name` - (Required) Name of the Private DNS Resolver Inbound Endpoint. + +* `private_dns_resolver_id` - (Required) ID of the Private DNS Resolver Inbound Endpoint. + +## Attributes Reference + +In addition to the Arguments listed above - the following Attributes are exported: + +* `id` - The ID of the Private DNS Resolver Inbound Endpoint. + +* `ip_configurations` - A list of `ip_configurations` block as defined below. + +* `location` - The Azure Region where the Private DNS Resolver Inbound Endpoint exists. + +* `tags` - The tags assigned to the Private DNS Resolver Inbound Endpoint. + +--- + +An `ip_configurations` block exports the following: + +* `private_ip_allocation_method` - The private IP address allocation method. + +* `subnet_id` - The subnet ID of the IP configuration. + +* `private_ip_address` - The private IP address of the IP configuration. + +## Timeouts + +The `timeouts` block allows you to specify [timeouts](https://www.terraform.io/language/resources/syntax#operation-timeouts) for certain actions: + +* `read` - (Defaults to 5 minutes) Used when retrieving the Private DNS Resolver Inbound Endpoint.