github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/aws/resource_aws_key_pair.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/hashicorp/terraform/helper/resource"
     8  	"github.com/hashicorp/terraform/helper/schema"
     9  
    10  	"github.com/aws/aws-sdk-go/aws"
    11  	"github.com/aws/aws-sdk-go/aws/awserr"
    12  	"github.com/aws/aws-sdk-go/service/ec2"
    13  )
    14  
    15  func resourceAwsKeyPair() *schema.Resource {
    16  	return &schema.Resource{
    17  		Create: resourceAwsKeyPairCreate,
    18  		Read:   resourceAwsKeyPairRead,
    19  		Update: nil,
    20  		Delete: resourceAwsKeyPairDelete,
    21  
    22  		SchemaVersion: 1,
    23  		MigrateState:  resourceAwsKeyPairMigrateState,
    24  
    25  		Schema: map[string]*schema.Schema{
    26  			"key_name": &schema.Schema{
    27  				Type:     schema.TypeString,
    28  				Optional: true,
    29  				Computed: true,
    30  				ForceNew: true,
    31  			},
    32  			"public_key": &schema.Schema{
    33  				Type:     schema.TypeString,
    34  				Required: true,
    35  				ForceNew: true,
    36  				StateFunc: func(v interface{}) string {
    37  					switch v.(type) {
    38  					case string:
    39  						return strings.TrimSpace(v.(string))
    40  					default:
    41  						return ""
    42  					}
    43  				},
    44  			},
    45  			"fingerprint": &schema.Schema{
    46  				Type:     schema.TypeString,
    47  				Computed: true,
    48  			},
    49  		},
    50  	}
    51  }
    52  
    53  func resourceAwsKeyPairCreate(d *schema.ResourceData, meta interface{}) error {
    54  	conn := meta.(*AWSClient).ec2conn
    55  
    56  	keyName := d.Get("key_name").(string)
    57  	if keyName == "" {
    58  		keyName = resource.UniqueId()
    59  	}
    60  
    61  	publicKey := d.Get("public_key").(string)
    62  	req := &ec2.ImportKeyPairInput{
    63  		KeyName:           aws.String(keyName),
    64  		PublicKeyMaterial: []byte(publicKey),
    65  	}
    66  	resp, err := conn.ImportKeyPair(req)
    67  	if err != nil {
    68  		return fmt.Errorf("Error import KeyPair: %s", err)
    69  	}
    70  
    71  	d.SetId(*resp.KeyName)
    72  	return nil
    73  }
    74  
    75  func resourceAwsKeyPairRead(d *schema.ResourceData, meta interface{}) error {
    76  	conn := meta.(*AWSClient).ec2conn
    77  	req := &ec2.DescribeKeyPairsInput{
    78  		KeyNames: []*string{aws.String(d.Id())},
    79  	}
    80  	resp, err := conn.DescribeKeyPairs(req)
    81  	if err != nil {
    82  		awsErr, ok := err.(awserr.Error)
    83  		if ok && awsErr.Code() == "InvalidKeyPair.NotFound" {
    84  			d.SetId("")
    85  			return nil
    86  		}
    87  		return fmt.Errorf("Error retrieving KeyPair: %s", err)
    88  	}
    89  
    90  	for _, keyPair := range resp.KeyPairs {
    91  		if *keyPair.KeyName == d.Id() {
    92  			d.Set("key_name", keyPair.KeyName)
    93  			d.Set("fingerprint", keyPair.KeyFingerprint)
    94  			return nil
    95  		}
    96  	}
    97  
    98  	return fmt.Errorf("Unable to find key pair within: %#v", resp.KeyPairs)
    99  }
   100  
   101  func resourceAwsKeyPairDelete(d *schema.ResourceData, meta interface{}) error {
   102  	conn := meta.(*AWSClient).ec2conn
   103  
   104  	_, err := conn.DeleteKeyPair(&ec2.DeleteKeyPairInput{
   105  		KeyName: aws.String(d.Id()),
   106  	})
   107  	return err
   108  }