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: PublicDnsNamespace #2569

Merged
merged 4 commits into from
Dec 12, 2017
Merged
Show file tree
Hide file tree
Changes from 2 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 aws/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,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 @@ -180,6 +181,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 @@ -416,6 +418,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 @@ -462,6 +462,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
144 changes: 144 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,144 @@
package aws

import (
"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

input := &servicediscovery.CreatePublicDnsNamespaceInput{
Name: aws.String(d.Get("name").(string)),
}

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

resp, err := conn.CreatePublicDnsNamespace(input)
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't we also use CreatorRequestId to make it safe to create multiple resources in parallel?

We usually use randomly generated string, like here:
https://github.com/terraform-providers/terraform-provider-aws/blob/0c0887cff37a40c1d35ee2bbe1788831d1f871e2/aws/resource_aws_mq_broker.go#L184

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,
Delay: 10 * time.Second,
MinTimeout: 3 * time.Second,
Copy link
Member

Choose a reason for hiding this comment

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

Is there any particular reason for delaying the refresh here? i.e. do the two fields Delay & MinTimeout have any reason? Based on my brief testing the creation can finish in 2 seconds, so we're potentially letting the user wait for 8 more seconds unnecessarily here.

}

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

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

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)
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if Amazon has plans for expanding Namespace.Properties or Namespace.Properties.DnsProperties.

It's no big deal though as we can always deprecate this 1st-level field in favour of its nested version if it becomes necessary.

}
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 {
if isAWSErr(err, servicediscovery.ErrCodeNamespaceNotFound, "") {
d.SetId("")
return 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,
Delay: 10 * time.Second,
MinTimeout: 3 * time.Second,
Copy link
Member

Choose a reason for hiding this comment

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

Is there any particular reason for delaying the refresh here? i.e. do the two fields Delay & MinTimeout have any reason? Based on my brief testing the deletion can finish in 2 seconds, so we're potentially letting the user wait for 8 more seconds unnecessarily here.

}

_, 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
}
}
71 changes: 71 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,71 @@
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"),
Copy link
Member

Choose a reason for hiding this comment

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

Can you also add 2 more checks here for the computed fields, please? i.e.

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 @@ -1435,6 +1435,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.