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

New Resource: ServiceDiscovery PrivateDNS Namespace #2589

Merged
merged 2 commits into from
Dec 12, 2017
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
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,7 @@ func Provider() terraform.ResourceProvider {
"aws_default_security_group": resourceAwsDefaultSecurityGroup(),
"aws_security_group_rule": resourceAwsSecurityGroupRule(),
"aws_servicecatalog_portfolio": resourceAwsServiceCatalogPortfolio(),
"aws_service_discovery_private_dns_namespace": resourceAwsServiceDiscoveryPrivateDnsNamespace(),
"aws_service_discovery_public_dns_namespace": resourceAwsServiceDiscoveryPublicDnsNamespace(),
"aws_simpledb_domain": resourceAwsSimpleDBDomain(),
"aws_ssm_activation": resourceAwsSsmActivation(),
Expand Down
132 changes: 132 additions & 0 deletions aws/resource_aws_service_discovery_private_dns_namespace.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package aws

import (
"fmt"
"time"

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

func resourceAwsServiceDiscoveryPrivateDnsNamespace() *schema.Resource {
return &schema.Resource{
Create: resourceAwsServiceDiscoveryPrivateDnsNamespaceCreate,
Read: resourceAwsServiceDiscoveryPrivateDnsNamespaceRead,
Delete: resourceAwsServiceDiscoveryPrivateDnsNamespaceDelete,

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"vpc": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"arn": {
Type: schema.TypeString,
Computed: true,
},
"hosted_zone": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

func resourceAwsServiceDiscoveryPrivateDnsNamespaceCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).sdconn

requestId := resource.PrefixedUniqueId(fmt.Sprintf("tf-%s", d.Get("name").(string)))
input := &servicediscovery.CreatePrivateDnsNamespaceInput{
Name: aws.String(d.Get("name").(string)),
Vpc: aws.String(d.Get("vpc").(string)),
CreatorRequestId: aws.String(requestId),
}

if v, ok := d.GetOk("description"); ok {
input.Description = aws.String(v.(string))
}

resp, err := conn.CreatePrivateDnsNamespace(input)
if err != nil {
return err
}

stateConf := &resource.StateChangeConf{
Pending: []string{servicediscovery.OperationStatusSubmitted, servicediscovery.OperationStatusPending},
Target: []string{servicediscovery.OperationStatusSuccess},
Refresh: servicediscoveryOperationRefreshStatusFunc(conn, *resp.OperationId),
Timeout: 5 * time.Minute,
}

opresp, err := stateConf.WaitForState()
if err != nil {
return err
}

d.SetId(*opresp.(*servicediscovery.GetOperationOutput).Operation.Targets["NAMESPACE"])
return resourceAwsServiceDiscoveryPrivateDnsNamespaceRead(d, meta)
}

func resourceAwsServiceDiscoveryPrivateDnsNamespaceRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).sdconn

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

resp, err := conn.GetNamespace(input)
if err != nil {
if isAWSErr(err, servicediscovery.ErrCodeNamespaceNotFound, "") {
d.SetId("")
return nil
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check shouldn't be necessary as we perform refresh prior to destroy and Read() would wipe the resource from state if it didn't exist.

return err
}

d.Set("description", resp.Namespace.Description)
d.Set("arn", resp.Namespace.Arn)
if resp.Namespace.Properties != nil {
d.Set("hosted_zone", resp.Namespace.Properties.DnsProperties.HostedZoneId)
}
return nil
}

func resourceAwsServiceDiscoveryPrivateDnsNamespaceDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).sdconn

input := &servicediscovery.DeleteNamespaceInput{
Id: aws.String(d.Id()),
}

resp, err := conn.DeleteNamespace(input)
if err != nil {
return err
}

stateConf := &resource.StateChangeConf{
Pending: []string{servicediscovery.OperationStatusSubmitted, servicediscovery.OperationStatusPending},
Target: []string{servicediscovery.OperationStatusSuccess},
Refresh: servicediscoveryOperationRefreshStatusFunc(conn, *resp.OperationId),
Timeout: 5 * time.Minute,
}

_, err = stateConf.WaitForState()
if err != nil {
return err
}

d.SetId("")
return nil
}
78 changes: 78 additions & 0 deletions aws/resource_aws_service_discovery_private_dns_namespace_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package aws

import (
"fmt"
"testing"

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

func TestAccAwsServiceDiscoveryPrivateDnsNamespace_basic(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAwsServiceDiscoveryPrivateDnsNamespaceDestroy,
Steps: []resource.TestStep{
{
Config: testAccServiceDiscoveryPrivateDnsNamespaceConfig(acctest.RandString(5)),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsServiceDiscoveryPrivateDnsNamespaceExists("aws_service_discovery_private_dns_namespace.test"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As mentioned in your other PR - can we add more checks here?

resource.TestCheckResourceAttrSet("aws_service_discovery_private_dns_namespace.test", "arn"),
resource.TestCheckResourceAttrSet("aws_service_discovery_private_dns_namespace.test", "hosted_zone"),
),
},
},
})
}

func testAccCheckAwsServiceDiscoveryPrivateDnsNamespaceDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).sdconn

for _, rs := range s.RootModule().Resources {
if rs.Type != "aws_service_discovery_private_dns_namespace" {
continue
}

input := &servicediscovery.GetNamespaceInput{
Id: aws.String(rs.Primary.ID),
}

_, err := conn.GetNamespace(input)
if err != nil {
if isAWSErr(err, servicediscovery.ErrCodeNamespaceNotFound, "") {
return nil
}
return err
}
}
return nil
}

func testAccCheckAwsServiceDiscoveryPrivateDnsNamespaceExists(name string) resource.TestCheckFunc {
return func(s *terraform.State) error {
_, ok := s.RootModule().Resources[name]
if !ok {
return fmt.Errorf("Not found: %s", name)
}

return nil
}
}

func testAccServiceDiscoveryPrivateDnsNamespaceConfig(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 = "tf-sd-%s.terraform.local"
description = "test"
vpc = "${aws_vpc.test.id}"
}
`, rName)
}
4 changes: 4 additions & 0 deletions website/aws.erb
Original file line number Diff line number Diff line change
Expand Up @@ -1454,6 +1454,10 @@
<a href="#">Service Discovery Resources</a>
<ul class="nav nav-visible">

<li<%= sidebar_current("docs-aws-resource-service-discovery-private-dns-namespace") %>>
<a href="/docs/providers/aws/r/service_discovery_private_dns_namespace.html">aws_service_discovery_private_dns_namespace</a>
</li>

<li<%= sidebar_current("docs-aws-resource-service-discovery-public-dns-namespace") %>>
<a href="/docs/providers/aws/r/service_discovery_public_dns_namespace.html">aws_service_discovery_public_dns_namespace</a>
</li>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---
layout: "aws"
page_title: "AWS: aws_service_discovery_private_dns_namespace"
sidebar_current: "docs-aws-resource-service-discovery-private-dns-namespace"
description: |-
Provides a Service Discovery Private DNS Namespace resource.
---

# aws_service_discovery_private_dns_namespace

Provides a Service Discovery Private DNS Namespace resource.

## Example Usage

```hcl
resource "aws_vpc" "example" {
cidr_block = "10.0.0.0/16"
}

resource "aws_service_discovery_private_dns_namespace" "example" {
name = "hoge.example.local"
description = "example"
vpc = "${aws_vpc.example.id}"
}
```

## Argument Reference

The following arguments are supported:

* `name` - (Required) The name of the namespace.
* `vpc` - (Required) The ID of VPC that you want to associate the namespace with.
* `description` - (Optional) The description that you specify for the namespace when you create it.

## Attributes Reference

The following attributes are exported:

* `id` - The ID of a namespace.
* `arn` - The ARN that Amazon Route 53 assigns to the namespace when you create it.
* `hosted_zone` - The ID for the hosted zone that Amazon Route 53 creates when you create a namespace.