github.com/chalford/terraform@v0.3.7-0.20150113080010-a78c69a8c81f/builtin/providers/aws/resource_aws_key_pair.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 6 "github.com/hashicorp/terraform/helper/schema" 7 ) 8 9 func resourceAwsKeyPair() *schema.Resource { 10 return &schema.Resource{ 11 Create: resourceAwsKeyPairCreate, 12 Read: resourceAwsKeyPairRead, 13 Update: nil, 14 Delete: resourceAwsKeyPairDelete, 15 16 Schema: map[string]*schema.Schema{ 17 "key_name": &schema.Schema{ 18 Type: schema.TypeString, 19 Required: true, 20 ForceNew: true, 21 }, 22 "public_key": &schema.Schema{ 23 Type: schema.TypeString, 24 Required: true, 25 ForceNew: true, 26 }, 27 "fingerprint": &schema.Schema{ 28 Type: schema.TypeString, 29 Computed: true, 30 }, 31 }, 32 } 33 } 34 35 func resourceAwsKeyPairCreate(d *schema.ResourceData, meta interface{}) error { 36 ec2conn := meta.(*AWSClient).ec2conn 37 38 keyName := d.Get("key_name").(string) 39 publicKey := d.Get("public_key").(string) 40 resp, err := ec2conn.ImportKeyPair(keyName, publicKey) 41 if err != nil { 42 return fmt.Errorf("Error import KeyPair: %s", err) 43 } 44 45 d.SetId(resp.KeyName) 46 47 return nil 48 } 49 50 func resourceAwsKeyPairRead(d *schema.ResourceData, meta interface{}) error { 51 ec2conn := meta.(*AWSClient).ec2conn 52 53 resp, err := ec2conn.KeyPairs([]string{d.Id()}, nil) 54 if err != nil { 55 return fmt.Errorf("Error retrieving KeyPair: %s", err) 56 } 57 58 for _, keyPair := range resp.Keys { 59 if keyPair.Name == d.Id() { 60 d.Set("key_name", keyPair.Name) 61 d.Set("fingerprint", keyPair.Fingerprint) 62 return nil 63 } 64 } 65 66 return fmt.Errorf("Unable to find key pair within: %#v", resp.Keys) 67 } 68 69 func resourceAwsKeyPairDelete(d *schema.ResourceData, meta interface{}) error { 70 ec2conn := meta.(*AWSClient).ec2conn 71 72 _, err := ec2conn.DeleteKeyPair(d.Id()) 73 return err 74 }