forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 2
/
resource_aws_iam_user_ssh_key.go
153 lines (129 loc) · 4.18 KB
/
resource_aws_iam_user_ssh_key.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
package aws
import (
"fmt"
"log"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/iam"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceAwsIamUserSshKey() *schema.Resource {
return &schema.Resource{
Create: resourceAwsIamUserSshKeyCreate,
Read: resourceAwsIamUserSshKeyRead,
Update: resourceAwsIamUserSshKeyUpdate,
Delete: resourceAwsIamUserSshKeyDelete,
Schema: map[string]*schema.Schema{
"ssh_public_key_id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"fingerprint": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"username": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"public_key": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"encoding": &schema.Schema{
Type: schema.TypeString,
Required: true,
ValidateFunc: validateIamUserSSHKeyEncoding,
},
"status": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
},
},
}
}
func resourceAwsIamUserSshKeyCreate(d *schema.ResourceData, meta interface{}) error {
iamconn := meta.(*AWSClient).iamconn
username := d.Get("username").(string)
publicKey := d.Get("public_key").(string)
request := &iam.UploadSSHPublicKeyInput{
UserName: aws.String(username),
SSHPublicKeyBody: aws.String(publicKey),
}
log.Println("[DEBUG] Create IAM User SSH Key Request:", request)
createResp, err := iamconn.UploadSSHPublicKey(request)
if err != nil {
return fmt.Errorf("Error creating IAM User SSH Key %s: %s", username, err)
}
d.Set("ssh_public_key_id", createResp.SSHPublicKey.SSHPublicKeyId)
d.SetId(*createResp.SSHPublicKey.SSHPublicKeyId)
return resourceAwsIamUserSshKeyRead(d, meta)
}
func resourceAwsIamUserSshKeyRead(d *schema.ResourceData, meta interface{}) error {
iamconn := meta.(*AWSClient).iamconn
username := d.Get("username").(string)
request := &iam.GetSSHPublicKeyInput{
UserName: aws.String(username),
SSHPublicKeyId: aws.String(d.Id()),
Encoding: aws.String(d.Get("encoding").(string)),
}
getResp, err := iamconn.GetSSHPublicKey(request)
if err != nil {
if iamerr, ok := err.(awserr.Error); ok && iamerr.Code() == "NoSuchEntity" { // XXX test me
log.Printf("[WARN] No IAM user ssh key (%s) found", d.Id())
d.SetId("")
return nil
}
return fmt.Errorf("Error reading IAM User SSH Key %s: %s", d.Id(), err)
}
d.Set("fingerprint", getResp.SSHPublicKey.Fingerprint)
d.Set("status", getResp.SSHPublicKey.Status)
return nil
}
func resourceAwsIamUserSshKeyUpdate(d *schema.ResourceData, meta interface{}) error {
if d.HasChange("status") {
iamconn := meta.(*AWSClient).iamconn
request := &iam.UpdateSSHPublicKeyInput{
UserName: aws.String(d.Get("username").(string)),
SSHPublicKeyId: aws.String(d.Id()),
Status: aws.String(d.Get("status").(string)),
}
log.Println("[DEBUG] Update IAM User SSH Key request:", request)
_, err := iamconn.UpdateSSHPublicKey(request)
if err != nil {
if iamerr, ok := err.(awserr.Error); ok && iamerr.Code() == "NoSuchEntity" {
log.Printf("[WARN] No IAM user ssh key by ID (%s) found", d.Id())
d.SetId("")
return nil
}
return fmt.Errorf("Error updating IAM User SSH Key %s: %s", d.Id(), err)
}
return resourceAwsIamUserRead(d, meta)
}
return nil
}
func resourceAwsIamUserSshKeyDelete(d *schema.ResourceData, meta interface{}) error {
iamconn := meta.(*AWSClient).iamconn
request := &iam.DeleteSSHPublicKeyInput{
UserName: aws.String(d.Get("username").(string)),
SSHPublicKeyId: aws.String(d.Id()),
}
log.Println("[DEBUG] Delete IAM User SSH Key request:", request)
if _, err := iamconn.DeleteSSHPublicKey(request); err != nil {
return fmt.Errorf("Error deleting IAM User SSH Key %s: %s", d.Id(), err)
}
return nil
}
func validateIamUserSSHKeyEncoding(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
encodingTypes := map[string]bool{
"PEM": true,
"SSH": true,
}
if !encodingTypes[value] {
errors = append(errors, fmt.Errorf("IAM User SSH Key Encoding can only be PEM or SSH"))
}
return
}