github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/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  		Importer: &schema.ResourceImporter{
    22  			State: schema.ImportStatePassthrough,
    23  		},
    24  
    25  		SchemaVersion: 1,
    26  		MigrateState:  resourceAwsKeyPairMigrateState,
    27  
    28  		Schema: map[string]*schema.Schema{
    29  			"key_name": &schema.Schema{
    30  				Type:     schema.TypeString,
    31  				Optional: true,
    32  				Computed: true,
    33  				ForceNew: true,
    34  			},
    35  			"public_key": &schema.Schema{
    36  				Type:     schema.TypeString,
    37  				Required: true,
    38  				ForceNew: true,
    39  				StateFunc: func(v interface{}) string {
    40  					switch v.(type) {
    41  					case string:
    42  						return strings.TrimSpace(v.(string))
    43  					default:
    44  						return ""
    45  					}
    46  				},
    47  			},
    48  			"fingerprint": &schema.Schema{
    49  				Type:     schema.TypeString,
    50  				Computed: true,
    51  			},
    52  		},
    53  	}
    54  }
    55  
    56  func resourceAwsKeyPairCreate(d *schema.ResourceData, meta interface{}) error {
    57  	conn := meta.(*AWSClient).ec2conn
    58  
    59  	keyName := d.Get("key_name").(string)
    60  	if keyName == "" {
    61  		keyName = resource.UniqueId()
    62  	}
    63  
    64  	publicKey := d.Get("public_key").(string)
    65  	req := &ec2.ImportKeyPairInput{
    66  		KeyName:           aws.String(keyName),
    67  		PublicKeyMaterial: []byte(publicKey),
    68  	}
    69  	resp, err := conn.ImportKeyPair(req)
    70  	if err != nil {
    71  		return fmt.Errorf("Error import KeyPair: %s", err)
    72  	}
    73  
    74  	d.SetId(*resp.KeyName)
    75  	return nil
    76  }
    77  
    78  func resourceAwsKeyPairRead(d *schema.ResourceData, meta interface{}) error {
    79  	conn := meta.(*AWSClient).ec2conn
    80  	req := &ec2.DescribeKeyPairsInput{
    81  		KeyNames: []*string{aws.String(d.Id())},
    82  	}
    83  	resp, err := conn.DescribeKeyPairs(req)
    84  	if err != nil {
    85  		awsErr, ok := err.(awserr.Error)
    86  		if ok && awsErr.Code() == "InvalidKeyPair.NotFound" {
    87  			d.SetId("")
    88  			return nil
    89  		}
    90  		return fmt.Errorf("Error retrieving KeyPair: %s", err)
    91  	}
    92  
    93  	for _, keyPair := range resp.KeyPairs {
    94  		if *keyPair.KeyName == d.Id() {
    95  			d.Set("key_name", keyPair.KeyName)
    96  			d.Set("fingerprint", keyPair.KeyFingerprint)
    97  			return nil
    98  		}
    99  	}
   100  
   101  	return fmt.Errorf("Unable to find key pair within: %#v", resp.KeyPairs)
   102  }
   103  
   104  func resourceAwsKeyPairDelete(d *schema.ResourceData, meta interface{}) error {
   105  	conn := meta.(*AWSClient).ec2conn
   106  
   107  	_, err := conn.DeleteKeyPair(&ec2.DeleteKeyPairInput{
   108  		KeyName: aws.String(d.Id()),
   109  	})
   110  	return err
   111  }