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