add composeErrors function to only expose errors that happened

This commit is contained in:
Patrick Gray 2015-06-28 22:30:54 -04:00
parent 0f5c9c012d
commit 28506c3750
1 changed files with 15 additions and 10 deletions

View File

@ -59,7 +59,7 @@ func resourceAwsIamPolicyAttachmentCreate(d *schema.ResourceData, meta interface
groups := expandStringList(d.Get("groups").(*schema.Set).List()) groups := expandStringList(d.Get("groups").(*schema.Set).List())
if len(users) > 0 && len(roles) > 0 && len(groups) > 0 { if len(users) > 0 && len(roles) > 0 && len(groups) > 0 {
return fmt.Errorf("[WARN] No Users, Roles, or Groups specified for %s", name) return fmt.Errorf("[WARN] No Users, Roles, or Groups specified for IAM Policy Attachment %s", name)
} else { } else {
var userErr, roleErr, groupErr error var userErr, roleErr, groupErr error
if users != nil { if users != nil {
@ -72,7 +72,7 @@ func resourceAwsIamPolicyAttachmentCreate(d *schema.ResourceData, meta interface
groupErr = attachPolicyToGroups(conn, groups, arn) groupErr = attachPolicyToGroups(conn, groups, arn)
} }
if userErr != nil || roleErr != nil || groupErr != nil { if userErr != nil || roleErr != nil || groupErr != nil {
return fmt.Errorf("[WARN] Error attaching policy with IAM Policy Attach (%s), error:\n users - %v\n roles - %v\n groups - %v", name, userErr, roleErr, groupErr) return composeErrors(fmt.Sprint("[WARN] Error attaching policy with IAM Policy Attachment ", name, ":"), userErr, roleErr, groupErr)
} }
} }
d.SetId(d.Get("name").(string)) d.SetId(d.Get("name").(string))
@ -127,7 +127,7 @@ func resourceAwsIamPolicyAttachmentRead(d *schema.ResourceData, meta interface{}
groupErr := d.Set("groups", gl) groupErr := d.Set("groups", gl)
if userErr != nil || roleErr != nil || groupErr != nil { if userErr != nil || roleErr != nil || groupErr != nil {
return fmt.Errorf("[WARN} Error setting user, role, or group list from IAM Policy Attach (%s):\n user error - %s\n role error - %s\n group error - %s", name, userErr, roleErr, groupErr) return composeErrors(fmt.Sprint("[WARN} Error setting user, role, or group list from IAM Policy Attachment ", name, ":"), userErr, roleErr, groupErr)
} }
return nil return nil
@ -147,7 +147,7 @@ func resourceAwsIamPolicyAttachmentUpdate(d *schema.ResourceData, meta interface
groupErr = updateGroups(conn, d, meta) groupErr = updateGroups(conn, d, meta)
} }
if userErr != nil || roleErr != nil || groupErr != nil { if userErr != nil || roleErr != nil || groupErr != nil {
return fmt.Errorf("[WARN] Error updating user, role, or group list from IAM Policy Attach (%s):\n user error - %s\n role error - %s\n group error - %s", name, userErr, roleErr, groupErr) return composeErrors(fmt.Sprint("[WARN] Error updating user, role, or group list from IAM Policy Attachment ", name, ":"), userErr, roleErr, groupErr)
} }
return resourceAwsIamPolicyAttachmentRead(d, meta) return resourceAwsIamPolicyAttachmentRead(d, meta)
} }
@ -171,16 +171,21 @@ func resourceAwsIamPolicyAttachmentDelete(d *schema.ResourceData, meta interface
groupErr = detachPolicyFromGroups(conn, groups, arn) groupErr = detachPolicyFromGroups(conn, groups, arn)
} }
if userErr != nil || roleErr != nil || groupErr != nil { if userErr != nil || roleErr != nil || groupErr != nil {
return fmt.Errorf("[WARN] Error removing user, role, or group list from IAM Policy Detach (%s), error:\n users - %v\n roles - %v\n groups - %v", name, userErr, roleErr, groupErr) return composeErrors(fmt.Sprint("[WARN] Error removing user, role, or group list from IAM Policy Detach ", name, ":"), userErr, roleErr, groupErr)
} }
return nil return nil
} }
//func composeErrors(desc string, uErr error, rErr error, gErr error) error { func composeErrors(desc string, uErr error, rErr error, gErr error) error {
// errMsg := fmt.Sprintf(desc) errMsg := fmt.Sprintf(desc)
// errs := []error{uErr, rErr, gErr} errs := []error{uErr, rErr, gErr}
// return nil for _, e := range errs {
//} if e != nil {
errMsg = errMsg + "\n " + e.Error()
}
}
return fmt.Errorf(errMsg)
}
func attachPolicyToUsers(conn *iam.IAM, users []*string, arn string) error { func attachPolicyToUsers(conn *iam.IAM, users []*string, arn string) error {
for _, u := range users { for _, u := range users {