github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/resource_aws_iam_user_ssh_key_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/aws/aws-sdk-go/aws" 8 "github.com/aws/aws-sdk-go/aws/awserr" 9 "github.com/aws/aws-sdk-go/service/iam" 10 "github.com/hashicorp/terraform/helper/acctest" 11 "github.com/hashicorp/terraform/helper/resource" 12 "github.com/hashicorp/terraform/terraform" 13 ) 14 15 func TestAccAWSUserSSHKey_basic(t *testing.T) { 16 var conf iam.GetSSHPublicKeyOutput 17 18 ri := acctest.RandInt() 19 config := fmt.Sprintf(testAccAWSSSHKeyConfig_sshEncoding, ri) 20 21 resource.Test(t, resource.TestCase{ 22 PreCheck: func() { testAccPreCheck(t) }, 23 Providers: testAccProviders, 24 CheckDestroy: testAccCheckAWSUserSSHKeyDestroy, 25 Steps: []resource.TestStep{ 26 resource.TestStep{ 27 Config: config, 28 Check: resource.ComposeTestCheckFunc( 29 testAccCheckAWSUserSSHKeyExists("aws_iam_user_ssh_key.user", &conf), 30 ), 31 }, 32 }, 33 }) 34 } 35 36 func TestAccAWSUserSSHKey_pemEncoding(t *testing.T) { 37 var conf iam.GetSSHPublicKeyOutput 38 39 ri := acctest.RandInt() 40 config := fmt.Sprintf(testAccAWSSSHKeyConfig_pemEncoding, ri) 41 42 resource.Test(t, resource.TestCase{ 43 PreCheck: func() { testAccPreCheck(t) }, 44 Providers: testAccProviders, 45 CheckDestroy: testAccCheckAWSUserSSHKeyDestroy, 46 Steps: []resource.TestStep{ 47 resource.TestStep{ 48 Config: config, 49 Check: resource.ComposeTestCheckFunc( 50 testAccCheckAWSUserSSHKeyExists("aws_iam_user_ssh_key.user", &conf), 51 ), 52 }, 53 }, 54 }) 55 } 56 57 func testAccCheckAWSUserSSHKeyDestroy(s *terraform.State) error { 58 iamconn := testAccProvider.Meta().(*AWSClient).iamconn 59 60 for _, rs := range s.RootModule().Resources { 61 if rs.Type != "aws_iam_user_ssh_key" { 62 continue 63 } 64 65 username := rs.Primary.Attributes["username"] 66 encoding := rs.Primary.Attributes["encoding"] 67 _, err := iamconn.GetSSHPublicKey(&iam.GetSSHPublicKeyInput{ 68 SSHPublicKeyId: aws.String(rs.Primary.ID), 69 UserName: aws.String(username), 70 Encoding: aws.String(encoding), 71 }) 72 if err == nil { 73 return fmt.Errorf("still exist.") 74 } 75 76 // Verify the error is what we want 77 ec2err, ok := err.(awserr.Error) 78 if !ok { 79 return err 80 } 81 if ec2err.Code() != "NoSuchEntity" { 82 return err 83 } 84 } 85 86 return nil 87 } 88 89 func testAccCheckAWSUserSSHKeyExists(n string, res *iam.GetSSHPublicKeyOutput) resource.TestCheckFunc { 90 return func(s *terraform.State) error { 91 rs, ok := s.RootModule().Resources[n] 92 if !ok { 93 return fmt.Errorf("Not found: %s", n) 94 } 95 96 if rs.Primary.ID == "" { 97 return fmt.Errorf("No SSHPublicKeyID is set") 98 } 99 100 iamconn := testAccProvider.Meta().(*AWSClient).iamconn 101 102 username := rs.Primary.Attributes["username"] 103 encoding := rs.Primary.Attributes["encoding"] 104 resp, err := iamconn.GetSSHPublicKey(&iam.GetSSHPublicKeyInput{ 105 SSHPublicKeyId: aws.String(rs.Primary.ID), 106 UserName: aws.String(username), 107 Encoding: aws.String(encoding), 108 }) 109 if err != nil { 110 return err 111 } 112 113 *res = *resp 114 115 return nil 116 } 117 } 118 119 const testAccAWSSSHKeyConfig_sshEncoding = ` 120 resource "aws_iam_user" "user" { 121 name = "test-user-%d" 122 path = "/" 123 } 124 125 resource "aws_iam_user_ssh_key" "user" { 126 username = "${aws_iam_user.user.name}" 127 encoding = "SSH" 128 public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD3F6tyPEFEzV0LX3X8BsXdMsQz1x2cEikKDEY0aIj41qgxMCP/iteneqXSIFZBp5vizPvaoIR3Um9xK7PGoW8giupGn+EPuxIA4cDM4vzOqOkiMPhz5XK0whEjkVzTo4+S0puvDZuwIsdiW9mxhJc7tgBNL0cYlWSYVkz4G/fslNfRPW5mYAM49f4fhtxPb5ok4Q2Lg9dPKVHO/Bgeu5woMc7RY0p1ej6D4CKFE6lymSDJpW0YHX/wqE9+cfEauh7xZcG0q9t2ta6F6fmX0agvpFyZo8aFbXeUBr7osSCJNgvavWbM/06niWrOvYX2xwWdhXmXSrbX8ZbabVohBK41 phodgson@thoughtworks.com" 129 } 130 ` 131 132 const testAccAWSSSHKeyConfig_pemEncoding = ` 133 resource "aws_iam_user" "user" { 134 name = "test-user-%d" 135 path = "/" 136 } 137 138 resource "aws_iam_user_ssh_key" "user" { 139 username = "${aws_iam_user.user.name}" 140 encoding = "PEM" 141 public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD3F6tyPEFEzV0LX3X8BsXdMsQz1x2cEikKDEY0aIj41qgxMCP/iteneqXSIFZBp5vizPvaoIR3Um9xK7PGoW8giupGn+EPuxIA4cDM4vzOqOkiMPhz5XK0whEjkVzTo4+S0puvDZuwIsdiW9mxhJc7tgBNL0cYlWSYVkz4G/fslNfRPW5mYAM49f4fhtxPb5ok4Q2Lg9dPKVHO/Bgeu5woMc7RY0p1ej6D4CKFE6lymSDJpW0YHX/wqE9+cfEauh7xZcG0q9t2ta6F6fmX0agvpFyZo8aFbXeUBr7osSCJNgvavWbM/06niWrOvYX2xwWdhXmXSrbX8ZbabVohBK41 phodgson@thoughtworks.com" 142 } 143 `