github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/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  				ConflictsWith: []string{"key_name_prefix"},
    35  			},
    36  			"key_name_prefix": &schema.Schema{
    37  				Type:     schema.TypeString,
    38  				Optional: true,
    39  				ForceNew: true,
    40  				ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
    41  					value := v.(string)
    42  					if len(value) > 100 {
    43  						errors = append(errors, fmt.Errorf(
    44  							"%q cannot be longer than 100 characters, name is limited to 255", k))
    45  					}
    46  					return
    47  				},
    48  			},
    49  			"public_key": &schema.Schema{
    50  				Type:     schema.TypeString,
    51  				Required: true,
    52  				ForceNew: true,
    53  				StateFunc: func(v interface{}) string {
    54  					switch v.(type) {
    55  					case string:
    56  						return strings.TrimSpace(v.(string))
    57  					default:
    58  						return ""
    59  					}
    60  				},
    61  			},
    62  			"fingerprint": &schema.Schema{
    63  				Type:     schema.TypeString,
    64  				Computed: true,
    65  			},
    66  		},
    67  	}
    68  }
    69  
    70  func resourceAwsKeyPairCreate(d *schema.ResourceData, meta interface{}) error {
    71  	conn := meta.(*AWSClient).ec2conn
    72  
    73  	var keyName string
    74  	if v, ok := d.GetOk("key_name"); ok {
    75  		keyName = v.(string)
    76  	} else if v, ok := d.GetOk("key_name_prefix"); ok {
    77  		keyName = resource.PrefixedUniqueId(v.(string))
    78  		d.Set("key_name", keyName)
    79  	} else {
    80  		keyName = resource.UniqueId()
    81  		d.Set("key_name", keyName)
    82  	}
    83  
    84  	publicKey := d.Get("public_key").(string)
    85  	req := &ec2.ImportKeyPairInput{
    86  		KeyName:           aws.String(keyName),
    87  		PublicKeyMaterial: []byte(publicKey),
    88  	}
    89  	resp, err := conn.ImportKeyPair(req)
    90  	if err != nil {
    91  		return fmt.Errorf("Error import KeyPair: %s", err)
    92  	}
    93  
    94  	d.SetId(*resp.KeyName)
    95  	return nil
    96  }
    97  
    98  func resourceAwsKeyPairRead(d *schema.ResourceData, meta interface{}) error {
    99  	conn := meta.(*AWSClient).ec2conn
   100  	req := &ec2.DescribeKeyPairsInput{
   101  		KeyNames: []*string{aws.String(d.Id())},
   102  	}
   103  	resp, err := conn.DescribeKeyPairs(req)
   104  	if err != nil {
   105  		awsErr, ok := err.(awserr.Error)
   106  		if ok && awsErr.Code() == "InvalidKeyPair.NotFound" {
   107  			d.SetId("")
   108  			return nil
   109  		}
   110  		return fmt.Errorf("Error retrieving KeyPair: %s", err)
   111  	}
   112  
   113  	for _, keyPair := range resp.KeyPairs {
   114  		if *keyPair.KeyName == d.Id() {
   115  			d.Set("key_name", keyPair.KeyName)
   116  			d.Set("fingerprint", keyPair.KeyFingerprint)
   117  			return nil
   118  		}
   119  	}
   120  
   121  	return fmt.Errorf("Unable to find key pair within: %#v", resp.KeyPairs)
   122  }
   123  
   124  func resourceAwsKeyPairDelete(d *schema.ResourceData, meta interface{}) error {
   125  	conn := meta.(*AWSClient).ec2conn
   126  
   127  	_, err := conn.DeleteKeyPair(&ec2.DeleteKeyPairInput{
   128  		KeyName: aws.String(d.Id()),
   129  	})
   130  	return err
   131  }