-
Notifications
You must be signed in to change notification settings - Fork 533
/
resource_alicloud_bastionhost_host_account.go
245 lines (238 loc) · 7.28 KB
/
resource_alicloud_bastionhost_host_account.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package alicloud
import (
"fmt"
"log"
"time"
util "github.com/alibabacloud-go/tea-utils/service"
"github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
)
func resourceAlicloudBastionhostHostAccount() *schema.Resource {
return &schema.Resource{
Create: resourceAlicloudBastionhostHostAccountCreate,
Read: resourceAlicloudBastionhostHostAccountRead,
Update: resourceAlicloudBastionhostHostAccountUpdate,
Delete: resourceAlicloudBastionhostHostAccountDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"host_account_id": {
Type: schema.TypeString,
Computed: true,
},
"host_account_name": {
Type: schema.TypeString,
Required: true,
},
"host_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"instance_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"pass_phrase": {
Type: schema.TypeString,
Optional: true,
Sensitive: true,
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
if v, ok := d.GetOk("protocol_name"); ok && v.(string) == "SSH" {
return false
}
return true
},
},
"password": {
Type: schema.TypeString,
Optional: true,
Sensitive: true,
},
"private_key": {
Type: schema.TypeString,
Optional: true,
Sensitive: true,
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
if v, ok := d.GetOk("protocol_name"); ok && v.(string) == "SSH" {
return false
}
return true
},
},
"protocol_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{"RDP", "SSH"}, false),
},
},
}
}
func resourceAlicloudBastionhostHostAccountCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
var response map[string]interface{}
action := "CreateHostAccount"
request := make(map[string]interface{})
conn, err := client.NewBastionhostClient()
if err != nil {
return WrapError(err)
}
request["HostAccountName"] = d.Get("host_account_name")
request["HostId"] = d.Get("host_id")
request["InstanceId"] = d.Get("instance_id")
if v, ok := d.GetOk("pass_phrase"); ok {
request["PassPhrase"] = v
}
if v, ok := d.GetOk("password"); ok {
request["Password"] = v
}
if v, ok := d.GetOk("private_key"); ok {
request["PrivateKey"] = v
}
request["ProtocolName"] = d.Get("protocol_name")
request["RegionId"] = client.RegionId
wait := incrementalWait(3*time.Second, 3*time.Second)
err = resource.Retry(d.Timeout(schema.TimeoutCreate), func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2019-12-09"), StringPointer("AK"), nil, request, &util.RuntimeOptions{})
if err != nil {
if NeedRetry(err) {
wait()
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
}
return nil
})
addDebug(action, response, request)
if err != nil {
return WrapErrorf(err, DefaultErrorMsg, "alicloud_bastionhost_host_account", action, AlibabaCloudSdkGoERROR)
}
d.SetId(fmt.Sprint(request["InstanceId"], ":", response["HostAccountId"]))
return resourceAlicloudBastionhostHostAccountRead(d, meta)
}
func resourceAlicloudBastionhostHostAccountRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
yundunBastionhostService := YundunBastionhostService{client}
object, err := yundunBastionhostService.DescribeBastionhostHostAccount(d.Id())
if err != nil {
if NotFoundError(err) {
log.Printf("[DEBUG] Resource alicloud_bastionhost_host_account yundunBastionhostService.DescribeBastionhostHostAccount Failed!!! %s", err)
d.SetId("")
return nil
}
return WrapError(err)
}
parts, err := ParseResourceId(d.Id(), 2)
if err != nil {
return WrapError(err)
}
d.Set("host_account_id", parts[1])
d.Set("instance_id", parts[0])
d.Set("host_account_name", object["HostAccountName"])
d.Set("host_id", object["HostId"])
d.Set("protocol_name", object["ProtocolName"])
return nil
}
func resourceAlicloudBastionhostHostAccountUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
var response map[string]interface{}
conn, err := client.NewBastionhostClient()
if err != nil {
return WrapError(err)
}
parts, err := ParseResourceId(d.Id(), 2)
if err != nil {
return WrapError(err)
}
update := false
request := map[string]interface{}{
"HostAccountId": parts[1],
"InstanceId": parts[0],
}
if d.HasChange("host_account_name") {
update = true
request["HostAccountName"] = d.Get("host_account_name")
}
if d.HasChange("pass_phrase") {
update = true
if v, ok := d.GetOk("pass_phrase"); ok {
request["PassPhrase"] = v
}
}
if d.HasChange("password") {
update = true
if v, ok := d.GetOk("password"); ok {
request["Password"] = v
}
}
if d.HasChange("private_key") {
update = true
if v, ok := d.GetOk("private_key"); ok {
request["PrivateKey"] = v
}
}
request["RegionId"] = client.RegionId
if update {
action := "ModifyHostAccount"
wait := incrementalWait(3*time.Second, 3*time.Second)
err = resource.Retry(d.Timeout(schema.TimeoutUpdate), func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2019-12-09"), StringPointer("AK"), nil, request, &util.RuntimeOptions{})
if err != nil {
if NeedRetry(err) {
wait()
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
}
return nil
})
addDebug(action, response, request)
if err != nil {
return WrapErrorf(err, DefaultErrorMsg, d.Id(), action, AlibabaCloudSdkGoERROR)
}
}
return resourceAlicloudBastionhostHostAccountRead(d, meta)
}
func resourceAlicloudBastionhostHostAccountDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
parts, err := ParseResourceId(d.Id(), 2)
if err != nil {
return WrapError(err)
}
action := "DeleteHostAccount"
var response map[string]interface{}
conn, err := client.NewBastionhostClient()
if err != nil {
return WrapError(err)
}
request := map[string]interface{}{
"HostAccountId": parts[1],
"InstanceId": parts[0],
}
request["RegionId"] = client.RegionId
wait := incrementalWait(3*time.Second, 3*time.Second)
err = resource.Retry(d.Timeout(schema.TimeoutDelete), func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2019-12-09"), StringPointer("AK"), nil, request, &util.RuntimeOptions{})
if err != nil {
if NeedRetry(err) {
wait()
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
}
return nil
})
addDebug(action, response, request)
if err != nil {
if IsExpectedErrors(err, []string{"Commodity.BizError.InvalidStatus", "OBJECT_NOT_FOUND"}) {
return nil
}
return WrapErrorf(err, DefaultErrorMsg, d.Id(), action, AlibabaCloudSdkGoERROR)
}
return nil
}