-
Notifications
You must be signed in to change notification settings - Fork 9.3k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if Amazon has plans for expanding 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 { | ||
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 | ||
} | ||
} |
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"), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"), |
||
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) | ||
} |
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. |
There was a problem hiding this comment.
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