2014-12-18 04:27:57 +01:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
2015-03-04 22:17:23 +01:00
|
|
|
"encoding/base64"
|
2014-12-18 04:27:57 +01:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
2015-03-04 22:17:23 +01:00
|
|
|
|
|
|
|
"github.com/hashicorp/aws-sdk-go/aws"
|
|
|
|
"github.com/hashicorp/aws-sdk-go/gen/ec2"
|
2014-12-18 04:27:57 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func resourceAwsKeyPair() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceAwsKeyPairCreate,
|
|
|
|
Read: resourceAwsKeyPairRead,
|
|
|
|
Update: nil,
|
|
|
|
Delete: resourceAwsKeyPairDelete,
|
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"key_name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
"public_key": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
"fingerprint": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsKeyPairCreate(d *schema.ResourceData, meta interface{}) error {
|
2015-03-13 15:42:50 +01:00
|
|
|
ec2conn := meta.(*AWSClient).ec2conn
|
2014-12-18 04:27:57 +01:00
|
|
|
|
|
|
|
keyName := d.Get("key_name").(string)
|
|
|
|
publicKey := d.Get("public_key").(string)
|
2015-03-04 22:17:23 +01:00
|
|
|
req := &ec2.ImportKeyPairRequest{
|
|
|
|
KeyName: aws.String(keyName),
|
|
|
|
PublicKeyMaterial: []byte(base64.StdEncoding.EncodeToString([]byte(publicKey))),
|
|
|
|
}
|
|
|
|
resp, err := ec2conn.ImportKeyPair(req)
|
2014-12-18 04:27:57 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error import KeyPair: %s", err)
|
|
|
|
}
|
|
|
|
|
2015-03-04 22:17:23 +01:00
|
|
|
d.SetId(*resp.KeyName)
|
2014-12-18 04:27:57 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsKeyPairRead(d *schema.ResourceData, meta interface{}) error {
|
2015-03-13 15:42:50 +01:00
|
|
|
ec2conn := meta.(*AWSClient).ec2conn
|
2014-12-18 04:27:57 +01:00
|
|
|
|
2015-03-04 22:17:23 +01:00
|
|
|
req := &ec2.DescribeKeyPairsRequest{
|
|
|
|
KeyNames: []string{d.Id()},
|
|
|
|
}
|
|
|
|
resp, err := ec2conn.DescribeKeyPairs(req)
|
2014-12-18 04:27:57 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error retrieving KeyPair: %s", err)
|
|
|
|
}
|
|
|
|
|
2015-03-04 22:17:23 +01:00
|
|
|
for _, keyPair := range resp.KeyPairs {
|
|
|
|
if *keyPair.KeyName == d.Id() {
|
|
|
|
d.Set("key_name", keyPair.KeyName)
|
|
|
|
d.Set("fingerprint", keyPair.KeyFingerprint)
|
2014-12-18 04:27:57 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-04 22:17:23 +01:00
|
|
|
return fmt.Errorf("Unable to find key pair within: %#v", resp.KeyPairs)
|
2014-12-18 04:27:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsKeyPairDelete(d *schema.ResourceData, meta interface{}) error {
|
2015-03-13 15:42:50 +01:00
|
|
|
ec2conn := meta.(*AWSClient).ec2conn
|
2014-12-18 04:27:57 +01:00
|
|
|
|
2015-03-04 22:17:23 +01:00
|
|
|
err := ec2conn.DeleteKeyPair(&ec2.DeleteKeyPairRequest{
|
|
|
|
KeyName: aws.String(d.Id()),
|
|
|
|
})
|
2014-12-18 04:27:57 +01:00
|
|
|
return err
|
|
|
|
}
|