2014-12-18 04:27:57 +01:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2015-04-30 14:26:17 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
2014-12-18 04:27:57 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
2015-03-04 22:17:23 +01:00
|
|
|
|
2015-04-06 22:13:29 +02:00
|
|
|
"github.com/awslabs/aws-sdk-go/aws"
|
2015-05-21 16:21:57 +02:00
|
|
|
"github.com/awslabs/aws-sdk-go/aws/awserr"
|
2015-04-06 22:13:29 +02:00
|
|
|
"github.com/awslabs/aws-sdk-go/service/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,
|
2015-04-30 14:26:17 +02:00
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
2014-12-18 04:27:57 +01:00
|
|
|
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-04-16 22:05:55 +02:00
|
|
|
conn := meta.(*AWSClient).ec2conn
|
2014-12-18 04:27:57 +01:00
|
|
|
|
|
|
|
keyName := d.Get("key_name").(string)
|
2015-04-30 14:26:17 +02:00
|
|
|
if keyName == "" {
|
|
|
|
keyName = resource.UniqueId()
|
|
|
|
}
|
2014-12-18 04:27:57 +01:00
|
|
|
publicKey := d.Get("public_key").(string)
|
2015-04-06 22:13:29 +02:00
|
|
|
req := &ec2.ImportKeyPairInput{
|
|
|
|
KeyName: aws.String(keyName),
|
2015-04-06 19:14:12 +02:00
|
|
|
PublicKeyMaterial: []byte(publicKey),
|
2015-03-04 22:17:23 +01:00
|
|
|
}
|
2015-04-06 22:13:29 +02:00
|
|
|
resp, err := conn.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-04-16 22:05:55 +02:00
|
|
|
conn := meta.(*AWSClient).ec2conn
|
2015-04-06 22:13:29 +02:00
|
|
|
req := &ec2.DescribeKeyPairsInput{
|
|
|
|
KeyNames: []*string{aws.String(d.Id())},
|
2015-03-04 22:17:23 +01:00
|
|
|
}
|
2015-04-06 22:13:29 +02:00
|
|
|
resp, err := conn.DescribeKeyPairs(req)
|
2014-12-18 04:27:57 +01:00
|
|
|
if err != nil {
|
2015-05-21 16:21:57 +02:00
|
|
|
awsErr, ok := err.(awserr.Error)
|
|
|
|
if ok && awsErr.Code() == "InvalidKeyPair.NotFound" {
|
2015-05-18 22:19:51 +02:00
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|
2014-12-18 04:27:57 +01:00
|
|
|
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-04-16 22:05:55 +02:00
|
|
|
conn := meta.(*AWSClient).ec2conn
|
2014-12-18 04:27:57 +01:00
|
|
|
|
2015-04-06 22:13:29 +02:00
|
|
|
_, err := conn.DeleteKeyPair(&ec2.DeleteKeyPairInput{
|
|
|
|
KeyName: aws.String(d.Id()),
|
2015-03-04 22:17:23 +01:00
|
|
|
})
|
2014-12-18 04:27:57 +01:00
|
|
|
return err
|
|
|
|
}
|