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 1 commit
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
5 changes: 3 additions & 2 deletions aws/resource_aws_iam_user_ssh_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,10 @@ func resourceAwsIamUserSshKeyCreate(d *schema.ResourceData, meta interface{}) er
}

d.Set("ssh_public_key_id", createResp.SSHPublicKey.SSHPublicKeyId)
d.Set("fingerprint", createResp.SSHPublicKey.Fingerprint)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should really only be calling d.Set() during Read functions. Can you remove this here and move the ssh_public_key_id one above to resourceAwsIamUserSshKeyRead? Thanks!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bflad so, ssh_public_key_id set should be also removed?

d.SetId(*createResp.SSHPublicKey.SSHPublicKeyId)

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

func resourceAwsIamUserSshKeyRead(d *schema.ResourceData, meta interface{}) error {
Expand Down Expand Up @@ -119,7 +120,7 @@ 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 resourceAwsIamUserSshKeyRead(d, meta)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😬 yikes, good catch. Instead of having this only called during d.HasChange("status"), can you remove this line here and replace the return nil right below this with return resourceAwsIamUserSshKeyRead(d, meta)? Thanks!

}
return nil
}
Expand Down
13 changes: 13 additions & 0 deletions aws/resource_aws_iam_user_ssh_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package aws

import (
"fmt"
"strings"
"testing"

"github.com/aws/aws-sdk-go/aws"
Expand Down Expand Up @@ -112,6 +113,17 @@ func testAccCheckAWSUserSSHKeyExists(n string, res *iam.GetSSHPublicKeyOutput) r

*res = *resp

keyStruct := resp.SSHPublicKey
keyFields := strings.Fields(rs.Primary.Attributes["public_key"])
sshkey := fmt.Sprintf("%s %s", keyFields[0], keyFields[1])

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

if *keyStruct.SSHPublicKeyBody != sshkey {
return fmt.Errorf("Public key mismatch. \nIAM: %s\nResource: %s\n", *keyStruct.SSHPublicKeyBody, sshkey)
}
return nil
}
}
Expand All @@ -126,6 +138,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