Skip to content
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

Fix aws_iam_user_ssh_key update method, set key status after creation #3390

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions aws/resource_aws_iam_user_ssh_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,9 @@ func resourceAwsIamUserSshKeyCreate(d *schema.ResourceData, meta interface{}) er
return fmt.Errorf("Error creating IAM User SSH Key %s: %s", username, err)
}

d.Set("ssh_public_key_id", createResp.SSHPublicKey.SSHPublicKeyId)
d.SetId(*createResp.SSHPublicKey.SSHPublicKeyId)

return resourceAwsIamUserSshKeyRead(d, meta)
return resourceAwsIamUserSshKeyUpdate(d, meta)
}

func resourceAwsIamUserSshKeyRead(d *schema.ResourceData, meta interface{}) error {
Expand All @@ -95,7 +94,7 @@ func resourceAwsIamUserSshKeyRead(d *schema.ResourceData, meta interface{}) erro

d.Set("fingerprint", getResp.SSHPublicKey.Fingerprint)
d.Set("status", getResp.SSHPublicKey.Status)

d.Set("ssh_public_key_id", getResp.SSHPublicKey.SSHPublicKeyId)
return nil
}

Expand All @@ -119,9 +118,8 @@ func resourceAwsIamUserSshKeyUpdate(d *schema.ResourceData, meta interface{}) er
}
return fmt.Errorf("Error updating IAM User SSH Key %s: %s", d.Id(), err)
}
return resourceAwsIamUserRead(d, meta)
}
return nil
return resourceAwsIamUserSshKeyRead(d, meta)
}

func resourceAwsIamUserSshKeyDelete(d *schema.ResourceData, meta interface{}) error {
Expand Down
13 changes: 10 additions & 3 deletions aws/resource_aws_iam_user_ssh_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestAccAWSUserSSHKey_basic(t *testing.T) {
resource.TestStep{
Config: config,
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSUserSSHKeyExists("aws_iam_user_ssh_key.user", &conf),
testAccCheckAWSUserSSHKeyExists("aws_iam_user_ssh_key.user", "Inactive", &conf),
),
},
},
Expand All @@ -47,7 +47,7 @@ func TestAccAWSUserSSHKey_pemEncoding(t *testing.T) {
resource.TestStep{
Config: config,
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSUserSSHKeyExists("aws_iam_user_ssh_key.user", &conf),
testAccCheckAWSUserSSHKeyExists("aws_iam_user_ssh_key.user", "Active", &conf),
),
},
},
Expand Down Expand Up @@ -86,7 +86,7 @@ func testAccCheckAWSUserSSHKeyDestroy(s *terraform.State) error {
return nil
}

func testAccCheckAWSUserSSHKeyExists(n string, res *iam.GetSSHPublicKeyOutput) resource.TestCheckFunc {
func testAccCheckAWSUserSSHKeyExists(n, status string, res *iam.GetSSHPublicKeyOutput) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
Expand All @@ -112,6 +112,12 @@ func testAccCheckAWSUserSSHKeyExists(n string, res *iam.GetSSHPublicKeyOutput) r

*res = *resp

keyStruct := resp.SSHPublicKey

if *keyStruct.Status != status {
return fmt.Errorf("Key status has wrong status should be %s is %s", status, *keyStruct.Status)
}

return nil
}
}
Expand All @@ -126,6 +132,7 @@ resource "aws_iam_user_ssh_key" "user" {
username = "${aws_iam_user.user.name}"
encoding = "SSH"
public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD3F6tyPEFEzV0LX3X8BsXdMsQz1x2cEikKDEY0aIj41qgxMCP/iteneqXSIFZBp5vizPvaoIR3Um9xK7PGoW8giupGn+EPuxIA4cDM4vzOqOkiMPhz5XK0whEjkVzTo4+S0puvDZuwIsdiW9mxhJc7tgBNL0cYlWSYVkz4G/fslNfRPW5mYAM49f4fhtxPb5ok4Q2Lg9dPKVHO/Bgeu5woMc7RY0p1ej6D4CKFE6lymSDJpW0YHX/wqE9+cfEauh7xZcG0q9t2ta6F6fmX0agvpFyZo8aFbXeUBr7osSCJNgvavWbM/06niWrOvYX2xwWdhXmXSrbX8ZbabVohBK41 [email protected]"
status = "Inactive"
}
`

Expand Down