-
Notifications
You must be signed in to change notification settings - Fork 9.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6856 from saravanan30erd/issue-6384
Data source for aws_service_discovery_dns_namespace
- Loading branch information
Showing
5 changed files
with
246 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```release-note:new-data-source | ||
aws_service_discovery_dns_namespace | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
87
aws/data_source_aws_service_discovery_dns_namespace_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
website/docs/d/service_discovery_dns_namespace.html.markdown
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |