forked from aliyun/terraform-provider-alicloud
/
resource_alicloud_key_pair_attachment.go
159 lines (142 loc) · 4.5 KB
/
resource_alicloud_key_pair_attachment.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
package alicloud
import (
"fmt"
"strings"
"time"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
"github.com/aliyun/alibaba-cloud-sdk-go/services/ecs"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/terraform-providers/terraform-provider-alicloud/alicloud/connectivity"
)
func resourceAlicloudKeyPairAttachment() *schema.Resource {
return &schema.Resource{
Create: resourceAlicloudKeyPairAttachmentCreate,
Read: resourceAlicloudKeyPairAttachmentRead,
Delete: resourceAlicloudKeyPairAttachmentDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"key_name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validateKeyPairName,
ForceNew: true,
},
"instance_ids": {
Type: schema.TypeSet,
Elem: &schema.Schema{Type: schema.TypeString},
Required: true,
ForceNew: true,
},
"force": {
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
},
},
}
}
func resourceAlicloudKeyPairAttachmentCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
ecsService := EcsService{client}
keyName := d.Get("key_name").(string)
instanceIds := d.Get("instance_ids").(*schema.Set).List()
force := d.Get("force").(bool)
idsMap := make(map[string]string)
var newIds []string
if force {
ids, _, err := ecsService.QueryInstancesWithKeyPair("", keyName)
if err != nil {
return WrapError(err)
}
for _, id := range ids {
idsMap[id] = id
}
for _, id := range instanceIds {
if _, ok := idsMap[id.(string)]; !ok {
newIds = append(newIds, id.(string))
}
}
}
if err := ecsService.AttachKeyPair(keyName, instanceIds); err != nil {
return WrapError(err)
}
if force {
request := ecs.CreateRebootInstanceRequest()
request.ForceStop = requests.NewBoolean(true)
for _, id := range newIds {
request.InstanceId = id
raw, err := client.WithEcsClient(func(ecsClient *ecs.Client) (interface{}, error) {
return ecsClient.RebootInstance(request)
})
if err != nil {
return WrapErrorf(err, DefaultErrorMsg, d.Id(), request.GetActionName(), AlibabaCloudSdkGoERROR)
}
addDebug(request.GetActionName(), raw)
}
for _, id := range newIds {
if err := ecsService.WaitForEcsInstance(id, Running, DefaultLongTimeout); err != nil {
return WrapError(err)
}
}
}
d.SetId(keyName + ":" + convertListToJsonString(instanceIds))
return resourceAlicloudKeyPairAttachmentRead(d, meta)
}
func resourceAlicloudKeyPairAttachmentRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
ecsService := EcsService{client}
keyName := strings.Split(d.Id(), ":")[0]
object, err := ecsService.DescribeKeyPairAttachment(d.Id())
if err != nil {
if NotFoundError(err) || IsExceptedError(err, KeyPairNotFound) {
d.SetId("")
return nil
}
return WrapError(err)
}
d.Set("key_name", object.KeyPairName)
if ids, ok := d.GetOk("instance_ids"); ok {
d.Set("instance_ids", ids)
} else {
ids, _, err := ecsService.QueryInstancesWithKeyPair("", keyName)
if err != nil {
return WrapError(err)
}
d.Set("instance_ids", ids)
}
return nil
}
func resourceAlicloudKeyPairAttachmentDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
ecsService := EcsService{client}
keyName := strings.Split(d.Id(), ":")[0]
instanceIds := strings.Split(d.Id(), ":")[1]
request := ecs.CreateDetachKeyPairRequest()
request.KeyPairName = keyName
return resource.Retry(5*time.Minute, func() *resource.RetryError {
request.InstanceIds = instanceIds
raw, err := client.WithEcsClient(func(ecsClient *ecs.Client) (interface{}, error) {
return ecsClient.DetachKeyPair(request)
})
if err != nil {
return resource.NonRetryableError(WrapErrorf(err, DefaultErrorMsg, d.Id(), request.GetActionName(), AlibabaCloudSdkGoERROR))
}
addDebug(request.GetActionName(), raw)
instance_ids, _, err := ecsService.QueryInstancesWithKeyPair(instanceIds, keyName)
if err != nil {
return resource.NonRetryableError(WrapError(err))
}
if len(instance_ids) > 0 {
var ids []interface{}
for _, id := range instance_ids {
ids = append(ids, id)
}
instanceIds = convertListToJsonString(ids)
return resource.RetryableError(WrapError(fmt.Errorf("detach Key Pair timeout and the instances including %s has not yet been detached. ", instanceIds)))
}
return nil
})
}