Merge pull request #8988 from kwilczynski/feature/refactor-function-aws_cloudwatch_event_rule

provider/aws: Allow buildPutRuleInputStruct helper function to return an error.
This commit is contained in:
Paul Stack 2016-09-22 16:29:09 +01:00 committed by GitHub
commit 2b966cf2cc
1 changed files with 18 additions and 9 deletions

View File

@ -71,12 +71,15 @@ func resourceAwsCloudWatchEventRule() *schema.Resource {
func resourceAwsCloudWatchEventRuleCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).cloudwatcheventsconn
input := buildPutRuleInputStruct(d)
input, err := buildPutRuleInputStruct(d)
if err != nil {
return errwrap.Wrapf("Creating CloudWatch Event Rule failed: {{err}}", err)
}
log.Printf("[DEBUG] Creating CloudWatch Event Rule: %s", input)
// IAM Roles take some time to propagate
var out *events.PutRuleOutput
err := resource.Retry(30*time.Second, func() *resource.RetryError {
err = resource.Retry(30*time.Second, func() *resource.RetryError {
var err error
out, err = conn.PutRule(input)
pattern := regexp.MustCompile("cannot be assumed by principal '[a-z]+\\.amazonaws\\.com'\\.$")
@ -92,7 +95,7 @@ func resourceAwsCloudWatchEventRuleCreate(d *schema.ResourceData, meta interface
return nil
})
if err != nil {
return fmt.Errorf("Creating CloudWatch Event Rule failed: %s", err)
return errwrap.Wrapf("Creating CloudWatch Event Rule failed: {{err}}", err)
}
d.Set("arn", out.RuleArn)
@ -160,11 +163,14 @@ func resourceAwsCloudWatchEventRuleUpdate(d *schema.ResourceData, meta interface
log.Printf("[DEBUG] CloudWatch Event Rule (%q) enabled", d.Id())
}
input := buildPutRuleInputStruct(d)
input, err := buildPutRuleInputStruct(d)
if err != nil {
return errwrap.Wrapf("Updating CloudWatch Event Rule failed: {{err}}", err)
}
log.Printf("[DEBUG] Updating CloudWatch Event Rule: %s", input)
// IAM Roles take some time to propagate
err := resource.Retry(30*time.Second, func() *resource.RetryError {
err = resource.Retry(30*time.Second, func() *resource.RetryError {
_, err := conn.PutRule(input)
pattern := regexp.MustCompile("cannot be assumed by principal '[a-z]+\\.amazonaws\\.com'\\.$")
if err != nil {
@ -179,7 +185,7 @@ func resourceAwsCloudWatchEventRuleUpdate(d *schema.ResourceData, meta interface
return nil
})
if err != nil {
return fmt.Errorf("Updating CloudWatch Event Rule failed: %s", err)
return errwrap.Wrapf("Updating CloudWatch Event Rule failed: {{err}}", err)
}
if d.HasChange("is_enabled") && !d.Get("is_enabled").(bool) {
@ -213,7 +219,7 @@ func resourceAwsCloudWatchEventRuleDelete(d *schema.ResourceData, meta interface
return nil
}
func buildPutRuleInputStruct(d *schema.ResourceData) *events.PutRuleInput {
func buildPutRuleInputStruct(d *schema.ResourceData) (*events.PutRuleInput, error) {
input := events.PutRuleInput{
Name: aws.String(d.Get("name").(string)),
}
@ -221,7 +227,10 @@ func buildPutRuleInputStruct(d *schema.ResourceData) *events.PutRuleInput {
input.Description = aws.String(v.(string))
}
if v, ok := d.GetOk("event_pattern"); ok {
pattern, _ := normalizeJsonString(v)
pattern, err := normalizeJsonString(v)
if err != nil {
return nil, errwrap.Wrapf("event pattern contains an invalid JSON: {{err}}", err)
}
input.EventPattern = aws.String(pattern)
}
if v, ok := d.GetOk("role_arn"); ok {
@ -233,7 +242,7 @@ func buildPutRuleInputStruct(d *schema.ResourceData) *events.PutRuleInput {
input.State = aws.String(getStringStateFromBoolean(d.Get("is_enabled").(bool)))
return &input
return &input, nil
}
// State is represented as (ENABLED|DISABLED) in the API