Skip to content

Commit

Permalink
Merge pull request #6856 from saravanan30erd/issue-6384
Browse files Browse the repository at this point in the history
Data source for aws_service_discovery_dns_namespace
  • Loading branch information
anGie44 committed May 19, 2021
2 parents cfd0d2b + e996eb4 commit 8ee12f5
Show file tree
Hide file tree
Showing 5 changed files with 246 additions and 0 deletions.
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 @@ -387,6 +387,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.

0 comments on commit 8ee12f5

Please sign in to comment.