-
Notifications
You must be signed in to change notification settings - Fork 7
/
data_source_huaweicloud_as_groups.go
355 lines (346 loc) · 12.1 KB
/
data_source_huaweicloud_as_groups.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
package as
import (
"context"
"log"
"github.com/huaweicloud/terraform-provider-hcs/huaweicloudstack/common"
"github.com/huaweicloud/terraform-provider-hcs/huaweicloudstack/config"
"github.com/huaweicloud/terraform-provider-hcs/huaweicloudstack/helper/hashcode"
"github.com/huaweicloud/terraform-provider-hcs/huaweicloudstack/sdk/huaweicloud/openstack/autoscaling/v1/groups"
"github.com/huaweicloud/terraform-provider-hcs/huaweicloudstack/sdk/huaweicloud/openstack/autoscaling/v1/tags"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func DataSourceASGroups() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceASGroupRead,
Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "The region where the AS groups are located.",
},
"name": {
Type: schema.TypeString,
Optional: true,
Description: "The AS group name used to query group list.",
},
"scaling_configuration_id": {
Type: schema.TypeString,
Optional: true,
Description: "The AS group configuration id used to query group list.",
},
"status": {
Type: schema.TypeString,
Optional: true,
Description: "The AS group status used to query group list.",
},
"enterprise_project_id": {
Type: schema.TypeString,
Optional: true,
Description: "The Enterprise Project id used to query group list.",
},
"groups": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"scaling_group_name": {
Type: schema.TypeString,
Computed: true,
Description: "The group name of the AS group.",
},
"scaling_group_id": {
Type: schema.TypeString,
Computed: true,
Description: "The group id of the AS group.",
},
"status": {
Type: schema.TypeString,
Computed: true,
Description: "The group status of the AS group.",
},
"scaling_configuration_id": {
Type: schema.TypeString,
Computed: true,
Description: "The configuration id of the AS group.",
},
"scaling_configuration_name": {
Type: schema.TypeString,
Computed: true,
Description: "The configuration name of the AS group.",
},
"current_instance_number": {
Type: schema.TypeInt,
Computed: true,
Description: "The number of current instances in the AS group.",
},
"desire_instance_number": {
Type: schema.TypeInt,
Computed: true,
Description: "The expected number of instances in the AS group.",
},
"min_instance_number": {
Type: schema.TypeInt,
Computed: true,
Description: "The minimum number of instances in the AS group.",
},
"max_instance_number": {
Type: schema.TypeInt,
Computed: true,
Description: "The maximum number of instances in the AS group.",
},
"cool_down_time": {
Type: schema.TypeInt,
Computed: true,
Description: "The cooling duration of the AS group.",
},
"lbaas_listeners": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"pool_id": {
Type: schema.TypeString,
Computed: true,
Description: "The backend ECS group ID.",
},
"protocol_port": {
Type: schema.TypeInt,
Computed: true,
Description: "The backend protocol ID.",
},
"weight": {
Type: schema.TypeInt,
Computed: true,
Description: "The weight, which determines the portion of requests a backend " +
"ECS processes compared to other backend ECSs added to the same listener.",
},
},
},
Description: "The enhanced load balancers of the AS group.",
},
"availability_zones": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: "The AZ information of the AS group.",
},
"networks": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
Description: "The subnet ID.",
},
"ipv6_enable": {
Type: schema.TypeBool,
Computed: true,
Description: "Specifies whether to support IPv6 addresses.",
},
"ipv6_bandwidth_id": {
Type: schema.TypeString,
Computed: true,
Description: "The ID of the shared bandwidth of an IPv6 address.",
},
"source_dest_check": {
Type: schema.TypeBool,
Computed: true,
},
},
},
Description: "The network information of the AS group.",
},
"security_groups": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
Description: "The ID of the security group.",
},
},
},
Description: "The security group information of the AS group.",
},
"created_at": {
Type: schema.TypeString,
Computed: true,
Description: "The time when an AS group was created.",
},
"vpc_id": {
Type: schema.TypeString,
Computed: true,
Description: "The ID of the VPC to which the AS group belongs.",
},
"detail": {
Type: schema.TypeString,
Computed: true,
Description: "Details about the AS group.",
},
"is_scaling": {
Type: schema.TypeBool,
Computed: true,
Description: "The scaling flag of the AS group.",
},
"health_periodic_audit_method": {
Type: schema.TypeString,
Computed: true,
Description: "The health check method.",
},
"health_periodic_audit_time": {
Type: schema.TypeInt,
Computed: true,
Description: "The health check interval.",
},
"health_periodic_audit_grace_period": {
Type: schema.TypeInt,
Computed: true,
Description: "The grace period for health check.",
},
"instance_terminate_policy": {
Type: schema.TypeString,
Computed: true,
Description: "The instance removal policy.",
},
"delete_publicip": {
Type: schema.TypeBool,
Computed: true,
Description: "Specifies whether to delete the EIP bound to the ECS when deleting the ECS.",
},
"delete_volume": {
Type: schema.TypeBool,
Computed: true,
Description: "Specifies whether to delete the data disks attached to the ECS when " +
"deleting the ECS.",
},
"enterprise_project_id": {
Type: schema.TypeString,
Computed: true,
Description: "The enterprise project ID of the AS group.",
},
"activity_type": {
Type: schema.TypeString,
Computed: true,
Description: "The type of the AS action.",
},
"multi_az_scaling_policy": {
Type: schema.TypeString,
Computed: true,
Description: "The priority policy used to select target AZs when adjusting the number of" +
" instances in an AS group.",
},
"description": {
Type: schema.TypeString,
Computed: true,
Description: "The description of the AS group.",
},
"iam_agency_name": {
Type: schema.TypeString,
Computed: true,
Description: "The agency name.",
},
"tags": common.TagsComputedSchema(),
"instances": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: "The scaling group instances ids.",
},
},
},
Description: "A list of AS groups",
},
},
}
}
func dataSourceASGroupRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conf := config.GetHcsConfig(meta)
region := conf.GetRegion(d)
asClient, err := conf.AutoscalingV1Client(region)
if err != nil {
return diag.Errorf("error creating autoscaling client: %s", err)
}
opts := groups.ListOpts{
Name: d.Get("name").(string),
ConfigurationID: d.Get("scaling_configuration_id").(string),
Status: d.Get("status").(string),
EnterpriseProjectID: d.Get("enterprise_project_id").(string),
}
pages, err := groups.List(asClient, opts).AllPages()
if err != nil {
return diag.Errorf("error getting AS group list: %s", err)
}
groupList, err := pages.(groups.GroupPage).Extract()
if err != nil {
return diag.Errorf("error extract to AS group list: %s", err)
}
ids := make([]string, 0, len(groupList))
elements := make([]map[string]interface{}, 0, len(groupList))
for _, group := range groupList {
groupID := group.ID
allIns, err := getInstancesInGroup(asClient, groupID, nil)
if err != nil {
return diag.Errorf("can not get the instances in AS Group %s: %s", groupID, err)
}
groupMap := map[string]interface{}{
"scaling_group_name": group.Name,
"scaling_group_id": groupID,
"status": group.Status,
"scaling_configuration_id": group.ConfigurationID,
"scaling_configuration_name": group.ConfigurationName,
"current_instance_number": group.ActualInstanceNumber,
"desire_instance_number": group.DesireInstanceNumber,
"min_instance_number": group.MinInstanceNumber,
"max_instance_number": group.MaxInstanceNumber,
"cool_down_time": group.CoolDownTime,
"lbaas_listeners": flattenLBaaSListeners(group.LBaaSListeners),
"availability_zones": group.AvailableZones,
"networks": flattenNetworks(group.Networks),
"security_groups": flattenSecurityGroups(group.SecurityGroups),
"created_at": group.CreateTime,
"vpc_id": group.VpcID,
"detail": group.Detail,
"is_scaling": group.IsScaling,
"health_periodic_audit_method": group.HealthPeriodicAuditMethod,
"health_periodic_audit_time": group.HealthPeriodicAuditTime,
"health_periodic_audit_grace_period": group.HealthPeriodicAuditGrace,
"instance_terminate_policy": group.InstanceTerminatePolicy,
"delete_publicip": group.DeletePublicip,
"delete_volume": group.DeleteVolume,
"enterprise_project_id": group.EnterpriseProjectID,
"activity_type": group.ActivityType,
"multi_az_scaling_policy": group.MultiAZPriorityPolicy,
"description": group.Description,
"iam_agency_name": group.IamAgencyName,
"instances": getInstancesIDs(allIns),
}
// save group tags
if resourceTags, err := tags.Get(asClient, groupID).Extract(); err == nil {
tagMap := make(map[string]string)
for _, val := range resourceTags.Tags {
tagMap[val.Key] = val.Value
}
groupMap["tags"] = tagMap
} else {
log.Printf("[WARN] Error fetching tags of AS group (%s): %s", groupID, err)
}
elements = append(elements, groupMap)
ids = append(ids, groupID)
}
d.SetId(hashcode.Strings(ids))
mErr := multierror.Append(nil,
d.Set("groups", elements),
d.Set("region", region),
)
if mErr.ErrorOrNil() != nil {
return diag.Errorf("error setting AS group fields: %s", mErr)
}
return nil
}