github.com/i0n/terraform@v0.4.3-0.20150506151324-010a39a58ec1/builtin/providers/aws/resource_aws_key_pair_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/awslabs/aws-sdk-go/aws"
     9  	"github.com/awslabs/aws-sdk-go/service/ec2"
    10  	"github.com/hashicorp/terraform/helper/resource"
    11  	"github.com/hashicorp/terraform/terraform"
    12  )
    13  
    14  func TestAccAWSKeyPair_normal(t *testing.T) {
    15  	var conf ec2.KeyPairInfo
    16  
    17  	resource.Test(t, resource.TestCase{
    18  		PreCheck:     func() { testAccPreCheck(t) },
    19  		Providers:    testAccProviders,
    20  		CheckDestroy: testAccCheckAWSKeyPairDestroy,
    21  		Steps: []resource.TestStep{
    22  			resource.TestStep{
    23  				Config: testAccAWSKeyPairConfig,
    24  				Check: resource.ComposeTestCheckFunc(
    25  					testAccCheckAWSKeyPairExists("aws_key_pair.a_key_pair", &conf),
    26  					testAccCheckAWSKeyPairFingerprint("d7:ff:a6:63:18:64:9c:57:a1:ee:ca:a4:ad:c2:81:62", &conf),
    27  				),
    28  			},
    29  		},
    30  	})
    31  }
    32  
    33  func TestAccAWSKeyPair_generatedName(t *testing.T) {
    34  	var conf ec2.KeyPairInfo
    35  
    36  	resource.Test(t, resource.TestCase{
    37  		PreCheck:     func() { testAccPreCheck(t) },
    38  		Providers:    testAccProviders,
    39  		CheckDestroy: testAccCheckAWSKeyPairDestroy,
    40  		Steps: []resource.TestStep{
    41  			resource.TestStep{
    42  				Config: testAccAWSKeyPairConfig_generatedName,
    43  				Check: resource.ComposeTestCheckFunc(
    44  					testAccCheckAWSKeyPairExists("aws_key_pair.a_key_pair", &conf),
    45  					testAccCheckAWSKeyPairFingerprint("d7:ff:a6:63:18:64:9c:57:a1:ee:ca:a4:ad:c2:81:62", &conf),
    46  					func(s *terraform.State) error {
    47  						if conf.KeyName == nil {
    48  							return fmt.Errorf("bad: No SG name")
    49  						}
    50  						if !strings.HasPrefix(*conf.KeyName, "terraform-") {
    51  							return fmt.Errorf("No terraform- prefix: %s", *conf.KeyName)
    52  						}
    53  						return nil
    54  					},
    55  				),
    56  			},
    57  		},
    58  	})
    59  }
    60  
    61  func testAccCheckAWSKeyPairDestroy(s *terraform.State) error {
    62  	ec2conn := testAccProvider.Meta().(*AWSClient).ec2conn
    63  
    64  	for _, rs := range s.RootModule().Resources {
    65  		if rs.Type != "aws_key_pair" {
    66  			continue
    67  		}
    68  
    69  		// Try to find key pair
    70  		resp, err := ec2conn.DescribeKeyPairs(&ec2.DescribeKeyPairsInput{
    71  			KeyNames: []*string{aws.String(rs.Primary.ID)},
    72  		})
    73  		if err == nil {
    74  			if len(resp.KeyPairs) > 0 {
    75  				return fmt.Errorf("still exist.")
    76  			}
    77  			return nil
    78  		}
    79  
    80  		// Verify the error is what we want
    81  		ec2err, ok := err.(aws.APIError)
    82  		if !ok {
    83  			return err
    84  		}
    85  		if ec2err.Code != "InvalidKeyPair.NotFound" {
    86  			return err
    87  		}
    88  	}
    89  
    90  	return nil
    91  }
    92  
    93  func testAccCheckAWSKeyPairFingerprint(expectedFingerprint string, conf *ec2.KeyPairInfo) resource.TestCheckFunc {
    94  	return func(s *terraform.State) error {
    95  		if *conf.KeyFingerprint != expectedFingerprint {
    96  			return fmt.Errorf("incorrect fingerprint. expected %s, got %s", expectedFingerprint, *conf.KeyFingerprint)
    97  		}
    98  		return nil
    99  	}
   100  }
   101  
   102  func testAccCheckAWSKeyPairExists(n string, res *ec2.KeyPairInfo) resource.TestCheckFunc {
   103  	return func(s *terraform.State) error {
   104  		rs, ok := s.RootModule().Resources[n]
   105  		if !ok {
   106  			return fmt.Errorf("Not found: %s", n)
   107  		}
   108  
   109  		if rs.Primary.ID == "" {
   110  			return fmt.Errorf("No KeyPair name is set")
   111  		}
   112  
   113  		ec2conn := testAccProvider.Meta().(*AWSClient).ec2conn
   114  
   115  		resp, err := ec2conn.DescribeKeyPairs(&ec2.DescribeKeyPairsInput{
   116  			KeyNames: []*string{aws.String(rs.Primary.ID)},
   117  		})
   118  		if err != nil {
   119  			return err
   120  		}
   121  		if len(resp.KeyPairs) != 1 ||
   122  			*resp.KeyPairs[0].KeyName != rs.Primary.ID {
   123  			return fmt.Errorf("KeyPair not found")
   124  		}
   125  
   126  		*res = *resp.KeyPairs[0]
   127  
   128  		return nil
   129  	}
   130  }
   131  
   132  const testAccAWSKeyPairConfig = `
   133  resource "aws_key_pair" "a_key_pair" {
   134  	key_name   = "tf-acc-key-pair"
   135  	public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD3F6tyPEFEzV0LX3X8BsXdMsQz1x2cEikKDEY0aIj41qgxMCP/iteneqXSIFZBp5vizPvaoIR3Um9xK7PGoW8giupGn+EPuxIA4cDM4vzOqOkiMPhz5XK0whEjkVzTo4+S0puvDZuwIsdiW9mxhJc7tgBNL0cYlWSYVkz4G/fslNfRPW5mYAM49f4fhtxPb5ok4Q2Lg9dPKVHO/Bgeu5woMc7RY0p1ej6D4CKFE6lymSDJpW0YHX/wqE9+cfEauh7xZcG0q9t2ta6F6fmX0agvpFyZo8aFbXeUBr7osSCJNgvavWbM/06niWrOvYX2xwWdhXmXSrbX8ZbabVohBK41 phodgson@thoughtworks.com"
   136  }
   137  `
   138  
   139  const testAccAWSKeyPairConfig_generatedName = `
   140  resource "aws_key_pair" "a_key_pair" {
   141  	public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD3F6tyPEFEzV0LX3X8BsXdMsQz1x2cEikKDEY0aIj41qgxMCP/iteneqXSIFZBp5vizPvaoIR3Um9xK7PGoW8giupGn+EPuxIA4cDM4vzOqOkiMPhz5XK0whEjkVzTo4+S0puvDZuwIsdiW9mxhJc7tgBNL0cYlWSYVkz4G/fslNfRPW5mYAM49f4fhtxPb5ok4Q2Lg9dPKVHO/Bgeu5woMc7RY0p1ej6D4CKFE6lymSDJpW0YHX/wqE9+cfEauh7xZcG0q9t2ta6F6fmX0agvpFyZo8aFbXeUBr7osSCJNgvavWbM/06niWrOvYX2xwWdhXmXSrbX8ZbabVohBK41 phodgson@thoughtworks.com"
   142  }
   143  `