Skip to content

Commit

Permalink
resource/iam_instance_profile: Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
YakDriver committed Feb 11, 2021
1 parent 5bf1a3a commit a5544f2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 32 deletions.
34 changes: 14 additions & 20 deletions aws/resource_aws_iam_instance_profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,10 @@ func resourceAwsIamInstanceProfile() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},

"create_date": {
Type: schema.TypeString,
Computed: true,
},

"unique_id": {
Type: schema.TypeString,
Computed: true,
},

"name": {
Type: schema.TypeString,
Optional: true,
Expand All @@ -50,7 +43,6 @@ func resourceAwsIamInstanceProfile() *schema.Resource {
validation.StringMatch(regexp.MustCompile(`^[\w+=,.@-]*$`), "must match [\\w+=,.@-]"),
),
},

"name_prefix": {
Type: schema.TypeString,
Optional: true,
Expand All @@ -61,18 +53,20 @@ func resourceAwsIamInstanceProfile() *schema.Resource {
validation.StringMatch(regexp.MustCompile(`^[\w+=,.@-]*$`), "must match [\\w+=,.@-]"),
),
},

"path": {
Type: schema.TypeString,
Optional: true,
Default: "/",
ForceNew: true,
},

"role": {
Type: schema.TypeString,
Optional: true,
},
"unique_id": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
Expand Down Expand Up @@ -100,7 +94,7 @@ func resourceAwsIamInstanceProfileCreate(d *schema.ResourceData, meta interface{
err = instanceProfileReadResult(d, response.InstanceProfile)
}
if err != nil {
return fmt.Errorf("Error creating IAM instance profile %s: %w", name, err)
return fmt.Errorf("creating IAM instance profile %s: %w", name, err)
}

waiterRequest := &iam.GetInstanceProfileInput{
Expand All @@ -111,7 +105,7 @@ func resourceAwsIamInstanceProfileCreate(d *schema.ResourceData, meta interface{
// that the instance profile exists.
err = conn.WaitUntilInstanceProfileExists(waiterRequest)
if err != nil {
return fmt.Errorf("Timed out while waiting for instance profile %s: %w", name, err)
return fmt.Errorf("timed out while waiting for instance profile %s: %w", name, err)
}

return resourceAwsIamInstanceProfileUpdate(d, meta)
Expand Down Expand Up @@ -141,7 +135,7 @@ func instanceProfileAddRole(conn *iam.IAM, profileName, roleName string) error {
_, err = conn.AddRoleToInstanceProfile(request)
}
if err != nil {
return fmt.Errorf("Error adding IAM Role %s to Instance Profile %s: %w", roleName, profileName, err)
return fmt.Errorf("adding IAM Role %s to Instance Profile %s: %w", roleName, profileName, err)
}

return err
Expand All @@ -164,7 +158,7 @@ func instanceProfileRemoveAllRoles(d *schema.ResourceData, conn *iam.IAM) error
if role, ok := d.GetOk("role"); ok {
err := instanceProfileRemoveRole(conn, d.Id(), role.(string))
if err != nil {
return fmt.Errorf("Error removing role %s from IAM instance profile %s: %w", role, d.Id(), err)
return fmt.Errorf("removing role %s from IAM instance profile %s: %w", role, d.Id(), err)
}
}

Expand All @@ -180,14 +174,14 @@ func resourceAwsIamInstanceProfileUpdate(d *schema.ResourceData, meta interface{
if oldRole.(string) != "" {
err := instanceProfileRemoveRole(conn, d.Id(), oldRole.(string))
if err != nil {
return fmt.Errorf("Error removing role %s to IAM instance profile %s: %w", oldRole.(string), d.Id(), err)
return fmt.Errorf("removing role %s to IAM instance profile %s: %w", oldRole.(string), d.Id(), err)
}
}

if newRole.(string) != "" {
err := instanceProfileAddRole(conn, d.Id(), newRole.(string))
if err != nil {
return fmt.Errorf("Error adding role %s to IAM instance profile %s: %w", newRole.(string), d.Id(), err)
return fmt.Errorf("adding role %s to IAM instance profile %s: %w", newRole.(string), d.Id(), err)
}
}
}
Expand All @@ -209,7 +203,7 @@ func resourceAwsIamInstanceProfileRead(d *schema.ResourceData, meta interface{})
return nil
}
if err != nil {
return fmt.Errorf("Error reading IAM instance profile %s: %w", d.Id(), err)
return fmt.Errorf("reading IAM instance profile %s: %w", d.Id(), err)
}

instanceProfile := result.InstanceProfile
Expand All @@ -224,10 +218,10 @@ func resourceAwsIamInstanceProfileRead(d *schema.ResourceData, meta interface{})
if isAWSErr(err, iam.ErrCodeNoSuchEntityException, "") {
err := instanceProfileRemoveRole(conn, d.Id(), roleName)
if err != nil {
return fmt.Errorf("Error removing role %s to IAM instance profile %s: %w", roleName, d.Id(), err)
return fmt.Errorf("removing role %s to IAM instance profile %s: %w", roleName, d.Id(), err)
}
}
return fmt.Errorf("Error reading IAM Role %s attcahed to IAM Instance Profile %s: %w", roleName, d.Id(), err)
return fmt.Errorf("reading IAM Role %s attcahed to IAM Instance Profile %s: %w", roleName, d.Id(), err)
}
}

Expand All @@ -249,7 +243,7 @@ func resourceAwsIamInstanceProfileDelete(d *schema.ResourceData, meta interface{
if isAWSErr(err, iam.ErrCodeNoSuchEntityException, "") {
return nil
}
return fmt.Errorf("Error deleting IAM instance profile %s: %w", d.Id(), err)
return fmt.Errorf("deleting IAM instance profile %s: %w", d.Id(), err)
}

return nil
Expand Down
21 changes: 9 additions & 12 deletions website/docs/r/iam_instance_profile.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -42,24 +42,21 @@ EOF

## Argument Reference

The following arguments are supported:
The following arguments are optional:

* `name` - (Optional, Forces new resource) The profile's name. If omitted, Terraform will assign a random, unique name.
* `name` - (Optional, Forces new resource) Name of the instance profile. If omitted, Terraform will assign a random, unique name. Conflicts with `name_prefix`. Can be a string of characters consisting of upper and lowercase alphanumeric characters and these special characters: `_`, `+`, `=`, `,`, `.`, `@`, `-`. Spaces are not allowed.
* `name_prefix` - (Optional, Forces new resource) Creates a unique name beginning with the specified prefix. Conflicts with `name`.
* `path` - (Optional, default "/") Path in which to create the profile.
* `role` - (Optional) The role name to include in the profile.
* `path` - (Optional, default "/") Path to the instance profile. For more information about paths, see [IAM Identifiers](https://docs.aws.amazon.com/IAM/latest/UserGuide/Using_Identifiers.html) in the IAM User Guide. Can be a string of characters consisting of either a forward slash (`/`) by itself or a string that must begin and end with forward slashes. Can include any ASCII character from the ! (\u0021) through the DEL character (\u007F), including most punctuation characters, digits, and upper and lowercase letters.
* `role` - (Optional) Name of the role to add to the profile.

## Attributes Reference

In addition to all arguments above, the following attributes are exported:
In addition to the arguments above, the following attributes are exported:

* `id` - The instance profile's ID.
* `arn` - The ARN assigned by AWS to the instance profile.
* `create_date` - The creation timestamp of the instance profile.
* `name` - The instance profile's name.
* `path` - The path of the instance profile in IAM.
* `role` - The role assigned to the instance profile.
* `unique_id` - The [unique ID][1] assigned by AWS.
* `arn` - ARN assigned by AWS to the instance profile.
* `create_date` - Creation timestamp of the instance profile.
* `id` - Instance profile's ID.
* `unique_id` - [Unique ID][1] assigned by AWS.

[1]: https://docs.aws.amazon.com/IAM/latest/UserGuide/Using_Identifiers.html#GUIDs

Expand Down

0 comments on commit a5544f2

Please sign in to comment.