forked from hashicorp/terraform-provider-aws
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_aws_lightsail_key_pair.go
225 lines (194 loc) · 5.51 KB
/
resource_aws_lightsail_key_pair.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package aws
import (
"fmt"
"log"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/lightsail"
"github.com/hashicorp/terraform/helper/encryption"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceAwsLightsailKeyPair() *schema.Resource {
return &schema.Resource{
Create: resourceAwsLightsailKeyPairCreate,
Read: resourceAwsLightsailKeyPairRead,
Delete: resourceAwsLightsailKeyPairDelete,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
ConflictsWith: []string{"name_prefix"},
},
"name_prefix": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
// optional fields
"pgp_key": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
// additional info returned from the API
"arn": {
Type: schema.TypeString,
Computed: true,
},
// fields returned from CreateKey
"fingerprint": {
Type: schema.TypeString,
Computed: true,
},
"public_key": {
Type: schema.TypeString,
Computed: true,
Optional: true,
ForceNew: true,
},
"private_key": {
Type: schema.TypeString,
Computed: true,
},
// encrypted fields if pgp_key is given
"encrypted_fingerprint": {
Type: schema.TypeString,
Computed: true,
},
"encrypted_private_key": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func resourceAwsLightsailKeyPairCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).lightsailconn
var kName string
if v, ok := d.GetOk("name"); ok {
kName = v.(string)
} else if v, ok := d.GetOk("name_prefix"); ok {
kName = resource.PrefixedUniqueId(v.(string))
} else {
kName = resource.UniqueId()
}
var pubKey string
var op *lightsail.Operation
if pubKeyInterface, ok := d.GetOk("public_key"); ok {
pubKey = pubKeyInterface.(string)
}
if pubKey == "" {
// creating new key
resp, err := conn.CreateKeyPair(&lightsail.CreateKeyPairInput{
KeyPairName: aws.String(kName),
})
if err != nil {
return err
}
if resp.Operation == nil {
return fmt.Errorf("[ERR] No operation found for CreateKeyPair response")
}
if resp.KeyPair == nil {
return fmt.Errorf("[ERR] No KeyPair information found for CreateKeyPair response")
}
d.SetId(kName)
// private_key and public_key are only available in the response from
// CreateKey pair. Here we set the public_key, and encrypt the private_key
// if a pgp_key is given, else we store the private_key in state
d.Set("public_key", resp.PublicKeyBase64)
// encrypt private key if pgp_key is given
pgpKey, err := encryption.RetrieveGPGKey(d.Get("pgp_key").(string))
if err != nil {
return err
}
if pgpKey != "" {
fingerprint, encrypted, err := encryption.EncryptValue(pgpKey, *resp.PrivateKeyBase64, "Lightsail Private Key")
if err != nil {
return err
}
d.Set("encrypted_fingerprint", fingerprint)
d.Set("encrypted_private_key", encrypted)
} else {
d.Set("private_key", resp.PrivateKeyBase64)
}
op = resp.Operation
} else {
// importing key
resp, err := conn.ImportKeyPair(&lightsail.ImportKeyPairInput{
KeyPairName: aws.String(kName),
PublicKeyBase64: aws.String(pubKey),
})
if err != nil {
log.Printf("[ERR] Error importing key: %s", err)
return err
}
d.SetId(kName)
op = resp.Operation
}
stateConf := &resource.StateChangeConf{
Pending: []string{"Started"},
Target: []string{"Completed", "Succeeded"},
Refresh: resourceAwsLightsailOperationRefreshFunc(op.Id, meta),
Timeout: 10 * time.Minute,
Delay: 5 * time.Second,
MinTimeout: 3 * time.Second,
}
_, err := stateConf.WaitForState()
if err != nil {
// We don't return an error here because the Create call succeded
log.Printf("[ERR] Error waiting for KeyPair (%s) to become ready: %s", d.Id(), err)
}
return resourceAwsLightsailKeyPairRead(d, meta)
}
func resourceAwsLightsailKeyPairRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).lightsailconn
resp, err := conn.GetKeyPair(&lightsail.GetKeyPairInput{
KeyPairName: aws.String(d.Id()),
})
if err != nil {
log.Printf("[WARN] Error getting KeyPair (%s): %s", d.Id(), err)
// check for known not found error
if awsErr, ok := err.(awserr.Error); ok {
if awsErr.Code() == "NotFoundException" {
log.Printf("[WARN] Lightsail KeyPair (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}
}
return err
}
d.Set("arn", resp.KeyPair.Arn)
d.Set("name", resp.KeyPair.Name)
d.Set("fingerprint", resp.KeyPair.Fingerprint)
return nil
}
func resourceAwsLightsailKeyPairDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).lightsailconn
resp, err := conn.DeleteKeyPair(&lightsail.DeleteKeyPairInput{
KeyPairName: aws.String(d.Id()),
})
if err != nil {
return err
}
op := resp.Operation
stateConf := &resource.StateChangeConf{
Pending: []string{"Started"},
Target: []string{"Completed", "Succeeded"},
Refresh: resourceAwsLightsailOperationRefreshFunc(op.Id, meta),
Timeout: 10 * time.Minute,
Delay: 5 * time.Second,
MinTimeout: 3 * time.Second,
}
_, err = stateConf.WaitForState()
if err != nil {
return fmt.Errorf(
"Error waiting for KeyPair (%s) to become destroyed: %s",
d.Id(), err)
}
d.SetId("")
return nil
}