-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New Resource: PublicDnsNamespace (#2569)
* WIP * Add test, docs * Reflect reviews * Modify error handling
- Loading branch information
1 parent
cb45e68
commit 3fc4ab2
Showing
6 changed files
with
262 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
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
139 changes: 139 additions & 0 deletions
139
aws/resource_aws_service_discovery_public_dns_namespace.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,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
73
aws/resource_aws_service_discovery_public_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,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) | ||
} |
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
35 changes: 35 additions & 0 deletions
35
website/docs/r/service_discovery_public_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,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. |