-
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: aws_swf_domain #2803
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,126 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/swf" | ||
"github.com/hashicorp/terraform/helper/resource" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
) | ||
|
||
func resourceAwsSwfDomain() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceAwsSwfDomainCreate, | ||
Read: resourceAwsSwfDomainRead, | ||
Delete: resourceAwsSwfDomainDelete, | ||
Importer: &schema.ResourceImporter{ | ||
State: schema.ImportStatePassthrough, | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Computed: true, | ||
ForceNew: true, | ||
ConflictsWith: []string{"name_prefix"}, | ||
}, | ||
"name_prefix": &schema.Schema{ | ||
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. Minor nitpick: the The same applies to both |
||
Type: schema.TypeString, | ||
Optional: true, | ||
ForceNew: true, | ||
}, | ||
"description": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Optional: true, | ||
ForceNew: true, | ||
}, | ||
"workflow_execution_retention_period_in_days": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: func(v interface{}, k string) (ws []string, es []error) { | ||
value, err := strconv.Atoi(v.(string)) | ||
if err != nil || value > 90 || value < 0 { | ||
es = append(es, fmt.Errorf( | ||
"%q must be between 0 and 90 days inclusive", k)) | ||
} | ||
return | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceAwsSwfDomainCreate(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).swfconn | ||
|
||
var name string | ||
|
||
if v, ok := d.GetOk("name"); ok { | ||
name = v.(string) | ||
} else if v, ok := d.GetOk("name_prefix"); ok { | ||
name = resource.PrefixedUniqueId(v.(string)) | ||
} else { | ||
name = resource.UniqueId() | ||
} | ||
d.Set("name", name) | ||
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. The |
||
|
||
input := &swf.RegisterDomainInput{ | ||
Name: aws.String(name), | ||
WorkflowExecutionRetentionPeriodInDays: aws.String(d.Get("workflow_execution_retention_period_in_days").(string)), | ||
} | ||
|
||
if v, ok := d.GetOk("description"); ok { | ||
input.Description = aws.String(v.(string)) | ||
} | ||
|
||
_, err := conn.RegisterDomain(input) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
d.SetId(name) | ||
|
||
return resourceAwsSwfDomainRead(d, meta) | ||
} | ||
|
||
func resourceAwsSwfDomainRead(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).swfconn | ||
|
||
input := &swf.DescribeDomainInput{ | ||
Name: aws.String(d.Id()), | ||
} | ||
|
||
resp, err := conn.DescribeDomain(input) | ||
if err != nil { | ||
return err | ||
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. We should perform a check here to allow Terraform to properly suggest recreating the resource if it is deleted outside Terraform as well as return a more helpful message about when the error occurred: if err != nil {
if isAWSErr(err, swf.ErrCodeUnknownResourceFault, "") {
log.Printf("[WARN] SWF Domain %q not found, removing from state", d.Id())
d.SetId("")
return nil
}
return fmt.Errorf("error reading SWF Domain: %s", err)
} |
||
} | ||
|
||
info := resp.DomainInfo | ||
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. To prevent potential panics, we should perform if resp.DomainInfo == nil || resp.DomainInfo.Configuration == nil {
log.Printf("[WARN] SWF Domain %q not found, removing from state", d.Id())
d.SetId("")
return nil
} |
||
config := resp.Configuration | ||
d.Set("name", info.Name) | ||
d.Set("description", info.Description) | ||
d.Set("workflow_execution_retention_period_in_days", config.WorkflowExecutionRetentionPeriodInDays) | ||
|
||
return nil | ||
} | ||
|
||
func resourceAwsSwfDomainDelete(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).swfconn | ||
|
||
input := &swf.DeprecateDomainInput{ | ||
Name: aws.String(d.Get("name").(string)), | ||
} | ||
|
||
_, err := conn.DeprecateDomain(input) | ||
if err != nil { | ||
return err | ||
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. We should allow the resource to skip returning an error if its already been deprecated/deleted: if err != nil {
if isAWSErr(err, swf.ErrCodeDomainDeprecatedFault, "") {
return nil
}
if isAWSErr(err, swf.ErrCodeUnknownResourceFault, "") {
return nil
}
return fmt.Errorf("error deleting SWF Domain: %s", err)
} |
||
} | ||
|
||
d.SetId("") | ||
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. Minor nitpick: calling |
||
|
||
return nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/swf" | ||
"github.com/hashicorp/terraform/helper/resource" | ||
"github.com/hashicorp/terraform/terraform" | ||
) | ||
|
||
func TestAccAwsSwfDomain_basic(t *testing.T) { | ||
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. Since this resource is accepting three ways to configure the name (via |
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { | ||
testAccPreCheck(t) | ||
}, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckAwsSwfDomainDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: fmt.Sprintf(testAccAwsSwfDomainConfig, "test_swf_domain_"), | ||
Check: testAccCheckAwsSwfDomainExists("aws_swf_domain.test"), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCheckAwsSwfDomainDestroy(s *terraform.State) error { | ||
conn := testAccProvider.Meta().(*AWSClient).swfconn | ||
|
||
for _, rs := range s.RootModule().Resources { | ||
if rs.Type != "aws_swf_domain" { | ||
continue | ||
} | ||
|
||
name := rs.Primary.ID | ||
input := &swf.DescribeDomainInput{ | ||
Name: aws.String(name), | ||
} | ||
|
||
resp, err := conn.DescribeDomain(input) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if *resp.DomainInfo.Status != "DEPRECATED" { | ||
return fmt.Errorf(`SWF Domain %s status is %s instead of "DEPRECATED". Failing!`, name, *resp.DomainInfo.Status) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func testAccCheckAwsSwfDomainExists(n string) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
rs, ok := s.RootModule().Resources[n] | ||
if !ok { | ||
return fmt.Errorf("SWF Domain not found: %s", n) | ||
} | ||
|
||
if rs.Primary.ID == "" { | ||
return fmt.Errorf("SWF Domain name not set") | ||
} | ||
|
||
name := rs.Primary.ID | ||
conn := testAccProvider.Meta().(*AWSClient).swfconn | ||
|
||
input := &swf.DescribeDomainInput{ | ||
Name: aws.String(name), | ||
} | ||
|
||
resp, err := conn.DescribeDomain(input) | ||
if err != nil { | ||
return fmt.Errorf("SWF Domain %s not found in AWS", name) | ||
} | ||
|
||
if *resp.DomainInfo.Status != "REGISTERED" { | ||
return fmt.Errorf(`SWF Domain %s status is %s instead of "REGISTERED". Failing!`, name, *resp.DomainInfo.Status) | ||
} | ||
return nil | ||
} | ||
} | ||
|
||
const testAccAwsSwfDomainConfig = ` | ||
resource "aws_swf_domain" "test" { | ||
name_prefix = "%s" | ||
workflow_execution_retention_period_in_days = 1 | ||
} | ||
` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
--- | ||
layout: "aws" | ||
page_title: "AWS: aws_swf_domain" | ||
sidebar_current: "docs-aws-resource-swf-domain" | ||
description: |- | ||
Provides an SWF Domain resource | ||
--- | ||
|
||
# aws_swf_domain | ||
|
||
Provides an SWF Domain resource. | ||
|
||
## Example Usage | ||
|
||
To register a basic SWF domain: | ||
|
||
```hcl | ||
resource "aws_swf_domain" "foo" { | ||
name = "foo" | ||
description = "Terraform SWF Domain" | ||
workflow_execution_retention_period_in_days = 30 | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
The following arguments are supported: | ||
|
||
* `name` - (Optional, Forces new resource) The name of the domain. If omitted, Terraform will assign a random, unique name. | ||
* `name_prefix` - (Optional, Forces new resource) Creates a unique name beginning with the specified prefix. Conflicts with `name`. | ||
* `description` - (Optional, Forces new resource) The domain description. | ||
* `workflow_execution_retention_period_in_days` - (Required, Forces new resource) Length of time that SWF will continue to retain information about the workflow execution after the workflow execution is complete, must be between 0 and 90 days. | ||
|
||
## Attributes Reference | ||
|
||
The following attributes are exported: | ||
|
||
* `name` - The name of the parameter. | ||
* `description` - The domain description. | ||
* `workflow_execution_retention_period_in_days` - Length of time that SWF will continue to retain information about the workflow execution after the workflow execution is complete. | ||
|
||
## Import | ||
|
||
SWF Domains can be imported using the `name`, e.g. | ||
|
||
``` | ||
$ terraform import aws_swf_domain.foo test-domain | ||
``` |
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.
The acceptance testing is currently not exercising resource importing. To add it we can add the following
TestStep
toTestAccAwsSwfDomain_basic
: