Skip to content
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

Data source for aws_service_discovery_dns_namespace #6856

Merged
merged 8 commits into from
May 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/6856.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-data-source
aws_service_discovery_dns_namespace
```
123 changes: 123 additions & 0 deletions aws/data_source_aws_service_discovery_dns_namespace.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package aws

import (
"context"
"fmt"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/servicediscovery"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)

func dataSourceServiceDiscoveryDnsNamespace() *schema.Resource {
return &schema.Resource{
ReadWithoutTimeout: dataSourceServiceDiscoveryDnsNamespaceRead,

Schema: map[string]*schema.Schema{
"arn": {
Type: schema.TypeString,
Computed: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
"hosted_zone": {
Type: schema.TypeString,
Computed: true,
},
"name": {
Type: schema.TypeString,
Required: true,
},
"type": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{
servicediscovery.NamespaceTypeDnsPublic,
servicediscovery.NamespaceTypeDnsPrivate,
}, false),
},
},
}
}

func dataSourceServiceDiscoveryDnsNamespaceRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*AWSClient).sdconn

name := d.Get("name").(string)

input := &servicediscovery.ListNamespacesInput{}

var filters []*servicediscovery.NamespaceFilter

filter := &servicediscovery.NamespaceFilter{
Condition: aws.String(servicediscovery.FilterConditionEq),
Name: aws.String(servicediscovery.NamespaceFilterNameType),
Values: []*string{aws.String(d.Get("type").(string))},
}

filters = append(filters, filter)

input.Filters = filters

namespaceIds := make([]string, 0)

err := conn.ListNamespacesPagesWithContext(ctx, input, func(page *servicediscovery.ListNamespacesOutput, lastPage bool) bool {
if page == nil {
return !lastPage
}

for _, namespace := range page.Namespaces {
if namespace == nil {
continue
}

if name == aws.StringValue(namespace.Name) {
namespaceIds = append(namespaceIds, aws.StringValue(namespace.Id))
}
}
return !lastPage
})

if err != nil {
return diag.FromErr(fmt.Errorf("error listing Service Discovery DNS Namespaces: %w", err))
}

if len(namespaceIds) == 0 {
return diag.Errorf("no matching Service Discovery DNS Namespace found")
}

if len(namespaceIds) != 1 {
return diag.FromErr(fmt.Errorf("search returned %d Service Discovery DNS Namespaces, please revise so only one is returned", len(namespaceIds)))
}

d.SetId(namespaceIds[0])

req := &servicediscovery.GetNamespaceInput{
Id: aws.String(d.Id()),
}

output, err := conn.GetNamespaceWithContext(ctx, req)

if err != nil {
return diag.FromErr(fmt.Errorf("error reading Service Discovery DNS Namespace (%s): %w", d.Id(), err))
}

if output == nil || output.Namespace == nil {
return diag.FromErr(fmt.Errorf("error reading Service Discovery DNS Namespace (%s): empty output", d.Id()))
}

namespace := output.Namespace

d.Set("name", namespace.Name)
d.Set("description", namespace.Description)
d.Set("arn", namespace.Arn)
if namespace.Properties != nil && namespace.Properties.DnsProperties != nil {
d.Set("hosted_zone", namespace.Properties.DnsProperties.HostedZoneId)
}

return nil
}
87 changes: 87 additions & 0 deletions aws/data_source_aws_service_discovery_dns_namespace_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package aws

import (
"fmt"
"testing"

"github.com/aws/aws-sdk-go/service/servicediscovery"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccAWSServiceDiscoveryDnsNamespaceDataSource_private(t *testing.T) {
rName := acctest.RandomWithPrefix("tf-acc-test")
dataSourceName := "data.aws_service_discovery_dns_namespace.test"
resourceName := "aws_service_discovery_private_dns_namespace.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccPreCheckAWSServiceDiscovery(t) },
ErrorCheck: testAccErrorCheck(t, servicediscovery.EndpointsID),
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCheckAwsServiceDiscoveryPrivateDnsNamespaceConfig(rName),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrPair(dataSourceName, "arn", resourceName, "arn"),
resource.TestCheckResourceAttrPair(dataSourceName, "name", resourceName, "name"),
resource.TestCheckResourceAttrPair(dataSourceName, "description", resourceName, "description"),
resource.TestCheckResourceAttrPair(dataSourceName, "hosted_zone", resourceName, "hosted_zone"),
),
},
},
})
}

func TestAccAWSServiceDiscoveryDnsNamespaceDataSource_public(t *testing.T) {
rName := acctest.RandomWithPrefix("tf-acc-test")
dataSourceName := "data.aws_service_discovery_dns_namespace.test"
resourceName := "aws_service_discovery_public_dns_namespace.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccPreCheckAWSServiceDiscovery(t) },
ErrorCheck: testAccErrorCheck(t, servicediscovery.EndpointsID),
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCheckAwsServiceDiscoveryPublicDnsNamespaceConfig(rName),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrPair(dataSourceName, "arn", resourceName, "arn"),
resource.TestCheckResourceAttrPair(dataSourceName, "name", resourceName, "name"),
resource.TestCheckResourceAttrPair(dataSourceName, "description", resourceName, "description"),
resource.TestCheckResourceAttrPair(dataSourceName, "hosted_zone", resourceName, "hosted_zone"),
),
},
},
})
}

func testAccCheckAwsServiceDiscoveryPrivateDnsNamespaceConfig(rName string) string {
return fmt.Sprintf(`
resource "aws_vpc" "test" {
cidr_block = "10.0.0.0/16"
}

resource "aws_service_discovery_private_dns_namespace" "test" {
name = "%[1]s.tf"
vpc = aws_vpc.test.id
}

data "aws_service_discovery_dns_namespace" "test" {
name = aws_service_discovery_private_dns_namespace.test.name
type = "DNS_PRIVATE"
}
`, rName)
}

func testAccCheckAwsServiceDiscoveryPublicDnsNamespaceConfig(rName string) string {
return fmt.Sprintf(`
resource "aws_service_discovery_public_dns_namespace" "test" {
name = "%[1]s.tf"
}

data "aws_service_discovery_dns_namespace" "test" {
name = aws_service_discovery_public_dns_namespace.test.name
type = "DNS_PUBLIC"
}
`, rName)
}
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@ func Provider() *schema.Provider {
"aws_secretsmanager_secret_version": dataSourceAwsSecretsManagerSecretVersion(),
"aws_servicequotas_service": dataSourceAwsServiceQuotasService(),
"aws_servicequotas_service_quota": dataSourceAwsServiceQuotasServiceQuota(),
"aws_service_discovery_dns_namespace": dataSourceServiceDiscoveryDnsNamespace(),
"aws_sfn_activity": dataSourceAwsSfnActivity(),
"aws_sfn_state_machine": dataSourceAwsSfnStateMachine(),
"aws_signer_signing_job": dataSourceAwsSignerSigningJob(),
Expand Down
32 changes: 32 additions & 0 deletions website/docs/d/service_discovery_dns_namespace.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
subcategory: "Service Discovery"
layout: "aws"
page_title: "AWS: aws_service_discovery_dns_namespace"
description: |-
Retrieves information about a Service Discovery private or public DNS namespace.
---

# Data Source: aws_service_discovery_dns_namespace

Retrieves information about a Service Discovery private or public DNS namespace.

## Example Usage

```hcl
data "aws_service_discovery_dns_namespace" "test" {
name = "example.terraform.local"
type = "DNS_PRIVATE"
}
```

## Argument Reference

* `name` - (Required) The name of the namespace.
* `type` - (Required) The type of the namespace. Allowed values are `DNS_PUBLIC` or `DNS_PRIVATE`.

## Attributes Reference

* `arn` - The Amazon Resource Name (ARN) of the namespace.
* `description` - A description of the namespace.
* `id` - The namespace ID.
* `hosted_zone` - The ID for the hosted zone that Amazon Route 53 creates when you create a namespace.