Skip to content

Commit

Permalink
New Resource: PublicDnsNamespace (#2569)
Browse files Browse the repository at this point in the history
* WIP

* Add test, docs

* Reflect reviews

* Modify error handling
  • Loading branch information
atsushi-ishibashi authored and radeksimko committed Dec 12, 2017
1 parent cb45e68 commit 3fc4ab2
Show file tree
Hide file tree
Showing 6 changed files with 262 additions and 0 deletions.
3 changes: 3 additions & 0 deletions aws/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ import (
"github.com/aws/aws-sdk-go/service/route53"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/servicecatalog"
"github.com/aws/aws-sdk-go/service/servicediscovery"
"github.com/aws/aws-sdk-go/service/ses"
"github.com/aws/aws-sdk-go/service/sfn"
"github.com/aws/aws-sdk-go/service/simpledb"
Expand Down Expand Up @@ -181,6 +182,7 @@ type AWSClient struct {
codedeployconn *codedeploy.CodeDeploy
codecommitconn *codecommit.CodeCommit
codepipelineconn *codepipeline.CodePipeline
sdconn *servicediscovery.ServiceDiscovery
sfnconn *sfn.SFN
ssmconn *ssm.SSM
wafconn *waf.WAF
Expand Down Expand Up @@ -418,6 +420,7 @@ func (c *Config) Client() (interface{}, error) {
client.simpledbconn = simpledb.New(sess)
client.s3conn = s3.New(awsS3Sess)
client.scconn = servicecatalog.New(sess)
client.sdconn = servicediscovery.New(sess)
client.sesConn = ses.New(sess)
client.sfnconn = sfn.New(sess)
client.snsconn = sns.New(awsSnsSess)
Expand Down
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_public_dns_namespace": resourceAwsServiceDiscoveryPublicDnsNamespace(),
"aws_simpledb_domain": resourceAwsSimpleDBDomain(),
"aws_ssm_activation": resourceAwsSsmActivation(),
"aws_ssm_association": resourceAwsSsmAssociation(),
Expand Down
139 changes: 139 additions & 0 deletions aws/resource_aws_service_discovery_public_dns_namespace.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
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 resourceAwsServiceDiscoveryPublicDnsNamespace() *schema.Resource {
return &schema.Resource{
Create: resourceAwsServiceDiscoveryPublicDnsNamespaceCreate,
Read: resourceAwsServiceDiscoveryPublicDnsNamespaceRead,
Delete: resourceAwsServiceDiscoveryPublicDnsNamespaceDelete,

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

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

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

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

resp, err := conn.CreatePublicDnsNamespace(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 resourceAwsServiceDiscoveryPublicDnsNamespaceRead(d, meta)
}

func resourceAwsServiceDiscoveryPublicDnsNamespaceRead(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
}
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 resourceAwsServiceDiscoveryPublicDnsNamespaceDelete(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
}

func servicediscoveryOperationRefreshStatusFunc(conn *servicediscovery.ServiceDiscovery, oid string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
input := &servicediscovery.GetOperationInput{
OperationId: aws.String(oid),
}
resp, err := conn.GetOperation(input)
if err != nil {
return nil, "failed", err
}
return resp, *resp.Operation.Status, nil
}
}
73 changes: 73 additions & 0 deletions aws/resource_aws_service_discovery_public_dns_namespace_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
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 TestAccAwsServiceDiscoveryPublicDnsNamespace_basic(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAwsServiceDiscoveryPublicDnsNamespaceDestroy,
Steps: []resource.TestStep{
{
Config: testAccServiceDiscoveryPublicDnsNamespaceConfig(acctest.RandString(5)),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsServiceDiscoveryPublicDnsNamespaceExists("aws_service_discovery_public_dns_namespace.test"),
resource.TestCheckResourceAttrSet("aws_service_discovery_public_dns_namespace.test", "arn"),
resource.TestCheckResourceAttrSet("aws_service_discovery_public_dns_namespace.test", "hosted_zone"),
),
},
},
})
}

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

for _, rs := range s.RootModule().Resources {
if rs.Type != "aws_service_discovery_public_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 testAccCheckAwsServiceDiscoveryPublicDnsNamespaceExists(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 testAccServiceDiscoveryPublicDnsNamespaceConfig(rName string) string {
return fmt.Sprintf(`
resource "aws_service_discovery_public_dns_namespace" "test" {
name = "tf-sd-%s.terraform.com"
description = "test"
}
`, rName)
}
11 changes: 11 additions & 0 deletions website/aws.erb
Original file line number Diff line number Diff line change
Expand Up @@ -1450,6 +1450,17 @@
</ul>
</li>

<li<%= sidebar_current("docs-aws-resource-service-discovery") %>>
<a href="#">Service Discovery Resources</a>
<ul class="nav nav-visible">

<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>

</ul>
</li>

<li<%= sidebar_current("docs-aws-resource-sfn") %>>
<a href="#">Step Function Resources</a>
<ul class="nav nav-visible">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
layout: "aws"
page_title: "AWS: aws_service_discovery_public_dns_namespace"
sidebar_current: "docs-aws-resource-service-discovery-public-dns-namespace"
description: |-
Provides a Service Discovery Public DNS Namespace resource.
---

# aws_service_discovery_public_dns_namespace

Provides a Service Discovery Public DNS Namespace resource.

## Example Usage

```hcl
resource "aws_service_discovery_public_dns_namespace" "example" {
name = "hoge.example.com"
description = "example"
}
```

## Argument Reference

The following arguments are supported:

* `name` - (Required) The name of the namespace.
* `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.

0 comments on commit 3fc4ab2

Please sign in to comment.