-
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
[WIP] new resource for ses domain identity Policy #5128
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,140 @@ | ||||||||||||||
package aws | ||||||||||||||
|
||||||||||||||
import ( | ||||||||||||||
"log" | ||||||||||||||
|
||||||||||||||
"github.com/hashicorp/terraform/helper/schema" | ||||||||||||||
|
||||||||||||||
"fmt" | ||||||||||||||
"github.com/aws/aws-sdk-go/aws" | ||||||||||||||
"github.com/aws/aws-sdk-go/aws/awserr" | ||||||||||||||
"github.com/aws/aws-sdk-go/service/ses" | ||||||||||||||
"github.com/hashicorp/terraform/helper/resource" | ||||||||||||||
) | ||||||||||||||
|
||||||||||||||
func resourceAwsSesDomainIdentityPolicy() *schema.Resource { | ||||||||||||||
return &schema.Resource{ | ||||||||||||||
Create: resourceAwsSesDomainIdentityPolicyCreate, | ||||||||||||||
Read: resourceAwsSesDomainIdentityPolicyRead, | ||||||||||||||
Update: resourceAwsSesDomainIdentityPolicyUpdate, | ||||||||||||||
Delete: resourceAwsSesDomainIdentityPolicyDelete, | ||||||||||||||
|
||||||||||||||
Schema: map[string]*schema.Schema{ | ||||||||||||||
"arn": { | ||||||||||||||
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. This attribute should be changed to ValidateFunc: validateArn, |
||||||||||||||
Type: schema.TypeString, | ||||||||||||||
Required: true, | ||||||||||||||
ForceNew: true, | ||||||||||||||
}, | ||||||||||||||
"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. We can validate this argument via: ValidateFunc: validation.All(
validation.StringLenBetween(1, 64),
validation.StringMatch(regexp.MustCompile(`^[a-zA-Z0-9\-\_]$`), "must contain only alphanumeric characters, dashes, and underscores"),
), Reference: https://docs.aws.amazon.com/ses/latest/APIReference/API_PutIdentityPolicy.html |
||||||||||||||
Type: schema.TypeString, | ||||||||||||||
Required: true, | ||||||||||||||
ForceNew: true, | ||||||||||||||
}, | ||||||||||||||
"policy": { | ||||||||||||||
Type: schema.TypeString, | ||||||||||||||
Required: true, | ||||||||||||||
ValidateFunc: validateJsonString, | ||||||||||||||
DiffSuppressFunc: suppressEquivalentAwsPolicyDiffs, | ||||||||||||||
}, | ||||||||||||||
}, | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
func resourceAwsSesDomainIdentityPolicyCreate(d *schema.ResourceData, meta interface{}) error { | ||||||||||||||
conn := meta.(*AWSClient).sesConn | ||||||||||||||
|
||||||||||||||
arn := d.Get("arn").(string) | ||||||||||||||
policyName := d.Get("name").(string) | ||||||||||||||
policy := d.Get("policy").(string) | ||||||||||||||
|
||||||||||||||
req := ses.PutIdentityPolicyInput{ | ||||||||||||||
Identity: aws.String(arn), | ||||||||||||||
PolicyName: aws.String(policyName), | ||||||||||||||
Policy: aws.String(policy), | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
_, err := conn.PutIdentityPolicy(&req) | ||||||||||||||
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 return error context for operators and code maintainers, e.g.
Suggested change
|
||||||||||||||
} | ||||||||||||||
|
||||||||||||||
d.SetId(resource.PrefixedUniqueId(fmt.Sprintf("%s-", policyName))) | ||||||||||||||
return resourceAwsSesDomainIdentityPolicyRead(d, meta) | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
func resourceAwsSesDomainIdentityPolicyUpdate(d *schema.ResourceData, meta interface{}) error { | ||||||||||||||
conn := meta.(*AWSClient).sesConn | ||||||||||||||
|
||||||||||||||
arn := d.Get("arn").(string) | ||||||||||||||
policyName := d.Get("name").(string) | ||||||||||||||
policy := d.Get("policy").(string) | ||||||||||||||
|
||||||||||||||
req := ses.PutIdentityPolicyInput{ | ||||||||||||||
Identity: aws.String(arn), | ||||||||||||||
PolicyName: aws.String(policyName), | ||||||||||||||
Policy: aws.String(policy), | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
_, err := conn.PutIdentityPolicy(&req) | ||||||||||||||
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 return error context here for operators and code maintainers:
Suggested change
|
||||||||||||||
} | ||||||||||||||
|
||||||||||||||
return resourceAwsSesDomainIdentityPolicyRead(d, meta) | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
func resourceAwsSesDomainIdentityPolicyRead(d *schema.ResourceData, meta interface{}) error { | ||||||||||||||
conn := meta.(*AWSClient).sesConn | ||||||||||||||
|
||||||||||||||
arn := d.Get("arn").(string) | ||||||||||||||
policyName := d.Get("name").(string) | ||||||||||||||
policyNames := make([]*string, 1) | ||||||||||||||
policyNames[0] = aws.String(policyName) | ||||||||||||||
|
||||||||||||||
policiesOutput, err := conn.GetIdentityPolicies(&ses.GetIdentityPoliciesInput{ | ||||||||||||||
Identity: aws.String(arn), | ||||||||||||||
PolicyNames: policyNames, | ||||||||||||||
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. Nit: The above logic can be simplified with:
Suggested change
|
||||||||||||||
}) | ||||||||||||||
if err != nil { | ||||||||||||||
if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "NotFound" { | ||||||||||||||
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. This logic can be simplified with a helper function we have available:
Suggested change
|
||||||||||||||
log.Printf("[WARN] SES Domain Identity Policy (%s) not found, error code (404)", policyName) | ||||||||||||||
d.SetId("") | ||||||||||||||
return 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 return error context here for operators and code maintainers:
Suggested change
|
||||||||||||||
} | ||||||||||||||
|
||||||||||||||
if policiesOutput.Policies == nil { | ||||||||||||||
log.Printf("[WARN] SES Domain Identity Policy (%s) not found (nil)", policyName) | ||||||||||||||
d.SetId("") | ||||||||||||||
return nil | ||||||||||||||
} | ||||||||||||||
policies := policiesOutput.Policies | ||||||||||||||
|
||||||||||||||
policy, ok := policies[*aws.String(policyName)] | ||||||||||||||
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 casting to a pointer and dereferencing looks extraneous here:
Suggested change
|
||||||||||||||
if !ok { | ||||||||||||||
log.Printf("[WARN] SES Domain Identity Policy (%s) not found in attributes", policyName) | ||||||||||||||
d.SetId("") | ||||||||||||||
return nil | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
d.Set("policy", policy) | ||||||||||||||
return nil | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
func resourceAwsSesDomainIdentityPolicyDelete(d *schema.ResourceData, meta interface{}) error { | ||||||||||||||
conn := meta.(*AWSClient).sesConn | ||||||||||||||
|
||||||||||||||
arn := d.Get("arn").(string) | ||||||||||||||
policyName := d.Get("name").(string) | ||||||||||||||
|
||||||||||||||
req := ses.DeleteIdentityPolicyInput{ | ||||||||||||||
Identity: aws.String(arn), | ||||||||||||||
PolicyName: aws.String(policyName), | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
log.Printf("[DEBUG] Deleting SES Domain Identity Policy: %s", req) | ||||||||||||||
_, err := conn.DeleteIdentityPolicy(&req) | ||||||||||||||
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 return error context here for operators and code maintainers:
Suggested change
|
||||||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package aws | ||
|
||
import ( | ||
"regexp" | ||
"testing" | ||
|
||
"fmt" | ||
"github.com/hashicorp/terraform/helper/acctest" | ||
"github.com/hashicorp/terraform/helper/resource" | ||
) | ||
|
||
func TestAccAWSSESDomainIdentityPolicy_basic(t *testing.T) { | ||
domain := fmt.Sprintf( | ||
"%s.terraformtesting.com.", | ||
acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckAwsSESDomainIdentityDestroy, | ||
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. A new |
||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccAWSSESDomainIdentityConfig_withPolicy(domain), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckAwsSESDomainIdentityExists("aws_ses_domain_identity.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. A new |
||
resource.TestMatchResourceAttr("aws_ses_domain_identity_policy.custom", "policy", | ||
regexp.MustCompile("^{\"Version\":\"2012-10-17\".+")), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccAWSSESDomainIdentityConfig_withPolicy(domain string) string { | ||
return fmt.Sprintf(` | ||
resource "aws_ses_domain_identity" "test" { | ||
name = "%s" | ||
} | ||
|
||
resource "aws_ses_domain_identity_policy" "custom" { | ||
arn = "${aws_ses_domain_identity.test.arn}" | ||
name = "test" | ||
policy = <<POLICY | ||
{ | ||
"Version":"2012-10-17", | ||
"Id": "default", | ||
"Statement":[{ | ||
"Sid":"default", | ||
"Effect":"Allow", | ||
"Principal":{"AWS":"*"}, | ||
"Action":["SES:SendEmail","SES:SendRawEmail"], | ||
"Resource":"${aws_ses_domain_identity.test.arn}" | ||
}] | ||
} | ||
POLICY | ||
} | ||
`, 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.
Two larger items:
aws_ses_identity_policy
as there would be no benefit to having separate resources between the two. The file, resource, and resource testing function names will need to be updated to reflect this change.website/docs/r/ses_identity_policy.html.markdown
file and a website sidebar link inwebsite/aws.erb