-
Notifications
You must be signed in to change notification settings - Fork 9.6k
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
Implement a subset of IAM resources #939
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/awslabs/aws-sdk-go/aws" | ||
"github.com/awslabs/aws-sdk-go/service/iam" | ||
|
||
"github.com/hashicorp/terraform/helper/schema" | ||
) | ||
|
||
func resourceAwsIamAccessKey() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceAwsIamAccessKeyCreate, | ||
Read: resourceAwsIamAccessKeyRead, | ||
Delete: resourceAwsIamAccessKeyDelete, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"user": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
"status": &schema.Schema{ | ||
Type: schema.TypeString, | ||
// this could be settable, but goamz does not support the | ||
// UpdateAccessKey API yet. | ||
Computed: true, | ||
}, | ||
"secret": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceAwsIamAccessKeyCreate(d *schema.ResourceData, meta interface{}) error { | ||
iamconn := meta.(*AWSClient).iamconn | ||
|
||
request := &iam.CreateAccessKeyInput{ | ||
UserName: aws.String(d.Get("user").(string)), | ||
} | ||
|
||
createResp, err := iamconn.CreateAccessKey(request) | ||
if err != nil { | ||
return fmt.Errorf( | ||
"Error creating access key for user %s: %s", | ||
*request.UserName, | ||
err, | ||
) | ||
} | ||
|
||
if err := d.Set("secret", createResp.AccessKey.SecretAccessKey); err != nil { | ||
return err | ||
} | ||
return resourceAwsIamAccessKeyReadResult(d, &iam.AccessKeyMetadata{ | ||
AccessKeyID: createResp.AccessKey.AccessKeyID, | ||
CreateDate: createResp.AccessKey.CreateDate, | ||
Status: createResp.AccessKey.Status, | ||
UserName: createResp.AccessKey.UserName, | ||
}) | ||
} | ||
|
||
func resourceAwsIamAccessKeyRead(d *schema.ResourceData, meta interface{}) error { | ||
iamconn := meta.(*AWSClient).iamconn | ||
|
||
request := &iam.ListAccessKeysInput{ | ||
UserName: aws.String(d.Get("user").(string)), | ||
} | ||
|
||
getResp, err := iamconn.ListAccessKeys(request) | ||
if err != nil { | ||
if iamerr, ok := err.(aws.APIError); ok && iamerr.Code == "NoSuchEntity" { // XXX TEST ME | ||
// the user does not exist, so the key can't exist. | ||
d.SetId("") | ||
return nil | ||
} | ||
return fmt.Errorf("Error reading IAM acces key: %s", err) | ||
} | ||
|
||
for _, key := range getResp.AccessKeyMetadata { | ||
if key.AccessKeyID != nil && *key.AccessKeyID == d.Id() { | ||
return resourceAwsIamAccessKeyReadResult(d, key) | ||
} | ||
} | ||
|
||
// Guess the key isn't around anymore. | ||
d.SetId("") | ||
return nil | ||
} | ||
|
||
func resourceAwsIamAccessKeyReadResult(d *schema.ResourceData, key *iam.AccessKeyMetadata) error { | ||
d.SetId(*key.AccessKeyID) | ||
if err := d.Set("user", key.UserName); err != nil { | ||
return err | ||
} | ||
if err := d.Set("status", key.Status); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func resourceAwsIamAccessKeyDelete(d *schema.ResourceData, meta interface{}) error { | ||
iamconn := meta.(*AWSClient).iamconn | ||
|
||
request := &iam.DeleteAccessKeyInput{ | ||
AccessKeyID: aws.String(d.Id()), | ||
UserName: aws.String(d.Get("user").(string)), | ||
} | ||
|
||
if _, err := iamconn.DeleteAccessKey(request); err != nil { | ||
return fmt.Errorf("Error deleting access key %s: %s", d.Id(), err) | ||
} | ||
return nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
@ctiwald here's the list of resources added by this PR