/
data_source_ddoscoo_instances.go
318 lines (287 loc) · 10.8 KB
/
data_source_ddoscoo_instances.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
package alicloud
import (
"context"
"regexp"
"time"
"github.com/cenkalti/backoff/v4"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
alicloudAntiddosClient "github.com/alibabacloud-go/ddoscoo-20200101/v2/client"
util "github.com/alibabacloud-go/tea-utils/v2/service"
"github.com/alibabacloud-go/tea/tea"
)
var (
_ datasource.DataSource = &ddoscooInstancesDataSource{}
_ datasource.DataSourceWithConfigure = &ddoscooInstancesDataSource{}
)
func NewDdosCooInstancesDataSource() datasource.DataSource {
return &ddoscooInstancesDataSource{}
}
type ddoscooInstancesDataSource struct {
client *alicloudAntiddosClient.Client
}
type ddoscooInstancesDataSourceModel struct {
Remark types.String `tfsdk:"remark_regex"`
IDs types.List `tfsdk:"ids"`
Instances []*antiddosCooInstance `tfsdk:"instances"`
}
type antiddosCooInstance struct {
ID types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
BaseBandwidth types.Int64 `tfsdk:"base_bandwidth"`
Bandwidth types.Int64 `tfsdk:"bandwidth"`
ServiceBandwidth types.Int64 `tfsdk:"service_bandwidth"`
PortCount types.Int64 `tfsdk:"port_count"`
DomainCount types.Int64 `tfsdk:"domain_count"`
Remark types.String `tfsdk:"remark"`
IpMode types.String `tfsdk:"ip_mode"`
DebtStatus types.Int64 `tfsdk:"debt_status"`
Edition types.Int64 `tfsdk:"edition"`
IpVersion types.String `tfsdk:"ip_version"`
Status types.Int64 `tfsdk:"status"`
Enabled types.Int64 `tfsdk:"enabled"`
ExpireTime types.Int64 `tfsdk:"expire_time"`
CreateTime types.Int64 `tfsdk:"create_time"`
Eip types.List `tfsdk:"eip"`
}
func (d *ddoscooInstancesDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_ddoscoo_instances"
}
func (d *ddoscooInstancesDataSource) Schema(_ context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Description: "This data source provides the AntiDDoS domain resources of the current AliCloud user.",
Attributes: map[string]schema.Attribute{
"remark_regex": schema.StringAttribute{
Description: "Remark of AntiDDoS instances.",
Optional: true,
},
"ids": schema.ListAttribute{
Description: "List of IDs of AntiDDoS instance.",
Optional: true,
ElementType: types.StringType,
},
"instances": schema.ListNestedAttribute{
Description: "A list of Anti-DDoS instances",
Computed: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Description: "ID of AntiDDoS instance.",
Computed: true,
},
"name": schema.StringAttribute{
Description: "Name of AntiDDoS instance.",
Computed: true,
},
"base_bandwidth": schema.Int64Attribute{
Description: "Base bandwidth of AntiDDoS instance.",
Computed: true,
},
"bandwidth": schema.Int64Attribute{
Description: "Bandwidth of AntiDDoS instacne.",
Computed: true,
},
"service_bandwidth": schema.Int64Attribute{
Description: "Service bandwidth of AntiDDoS instance.",
Computed: true,
},
"port_count": schema.Int64Attribute{
Description: "Port count of AntiDDoS instance.",
Computed: true,
},
"domain_count": schema.Int64Attribute{
Description: "Domain count of AntiDDoS instance.",
Computed: true,
},
"remark": schema.StringAttribute{
Description: "Remark of AntiDDoS instance.",
Computed: true,
},
"ip_mode": schema.StringAttribute{
Description: "IP Mode of AntiDDoS instance.",
Computed: true,
},
"debt_status": schema.Int64Attribute{
Description: "Debt status of AntiDDoS instance.",
Computed: true,
},
"edition": schema.Int64Attribute{
Description: "Edition of AntiDDoS instance.",
Computed: true,
},
"ip_version": schema.StringAttribute{
Description: "IP version of AntiDDoS instance.",
Computed: true,
},
"status": schema.Int64Attribute{
Description: "Status of AntiDDoS instance.",
Computed: true,
},
"enabled": schema.Int64Attribute{
Description: "If the AntiDDoS instance is enabled",
Computed: true,
},
"expire_time": schema.Int64Attribute{
Description: "Expire time of AntiDDoS instance.",
Computed: true,
},
"create_time": schema.Int64Attribute{
Description: "Create time of AntiDDoS instance.",
Computed: true,
},
"eip": schema.ListAttribute{
Description: "EIP of AntiDDoS instance.",
Computed: true,
ElementType: types.StringType,
},
},
},
},
},
}
}
func (d *ddoscooInstancesDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
if req.ProviderData == nil {
return
}
d.client = req.ProviderData.(alicloudClients).antiddosClient
}
func (d *ddoscooInstancesDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var plan, state ddoscooInstancesDataSourceModel
diags := req.Config.Get(ctx, &plan)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
state.IDs = plan.IDs
state.Remark = plan.Remark
state.Instances = []*antiddosCooInstance{}
describeInstancesRequest := &alicloudAntiddosClient.DescribeInstancesRequest{
PageNumber: tea.String("1"),
PageSize: tea.String("20"),
}
describeInstanceSpecsRequest := &alicloudAntiddosClient.DescribeInstanceSpecsRequest{}
describeInstanceDetailsRequest := &alicloudAntiddosClient.DescribeInstanceDetailsRequest{}
var nameRegex *regexp.Regexp
// Describe Instances
readInstances := func() error {
runtime := &util.RuntimeOptions{}
if !(plan.IDs.IsNull() || plan.IDs.IsUnknown()) {
// Plan ID List to List of String
var planIdsList []string
for _, x := range plan.IDs.Elements() {
planIdsList = append(planIdsList, trimStringQuotes(x.String()))
}
describeInstancesRequest.InstanceIds = tea.StringSlice(planIdsList)
}
if !(plan.Remark.IsNull() || plan.Remark.IsUnknown()) {
// Convert Remark Input to Regex
v := plan.Remark.ValueString()
if r, err := regexp.Compile(v); err != nil {
resp.Diagnostics.AddError(
"[API ERROR] Failed to Convert Remark Input to Regex",
err.Error(),
)
} else {
nameRegex = r
}
}
// Describe Instances List
antiddosInstances, err := d.client.DescribeInstancesWithOptions(describeInstancesRequest, runtime)
if err != nil {
if _t, ok := err.(*tea.SDKError); ok {
if isAbleToRetry(*_t.Code) {
return err
} else {
return backoff.Permanent(err)
}
} else {
return err
}
}
var antiddosInstancesList []string
if *antiddosInstances.Body.TotalCount > int64(0) {
for _, instances := range antiddosInstances.Body.Instances {
// Regex Filter
if nameRegex != nil && !(nameRegex.MatchString(*instances.Remark)) {
continue
}
antiddosInstancesList = append(antiddosInstancesList, *instances.InstanceId)
}
// Describe Instance Specs
describeInstanceSpecsRequest.InstanceIds = tea.StringSlice(antiddosInstancesList)
antiddosInstanceSpecs, err := d.client.DescribeInstanceSpecsWithOptions(describeInstanceSpecsRequest, runtime)
if err != nil {
if _t, ok := err.(*tea.SDKError); ok {
if isAbleToRetry(*_t.Code) {
return err
} else {
return backoff.Permanent(err)
}
} else {
return err
}
}
// Describe Instance Details
describeInstanceDetailsRequest.InstanceIds = tea.StringSlice(antiddosInstancesList)
antiddosInstanceDetails, err := d.client.DescribeInstanceDetailsWithOptions(describeInstanceDetailsRequest, runtime)
if err != nil {
if _t, ok := err.(*tea.SDKError); ok {
if isAbleToRetry(*_t.Code) {
return err
} else {
return backoff.Permanent(err)
}
} else {
return err
}
}
// Assign all values into instances
for i := 0; i < len(antiddosInstancesList); i++ {
var instanceEipList []attr.Value
for _, instanceDetailsEip := range antiddosInstanceDetails.Body.InstanceDetails[i].EipInfos {
instanceEipList = append(instanceEipList, types.StringValue(*instanceDetailsEip.Eip))
}
instanceDetail := &antiddosCooInstance{
ID: types.StringValue(antiddosInstancesList[i]),
Name: types.StringValue(*antiddosInstances.Body.Instances[i].Remark),
BaseBandwidth: types.Int64Value(int64(*antiddosInstanceSpecs.Body.InstanceSpecs[i].BaseBandwidth)),
Bandwidth: types.Int64Value(int64(*antiddosInstanceSpecs.Body.InstanceSpecs[i].ElasticBandwidth)),
ServiceBandwidth: types.Int64Value(int64(*antiddosInstanceSpecs.Body.InstanceSpecs[i].BandwidthMbps)),
PortCount: types.Int64Value(int64(*antiddosInstanceSpecs.Body.InstanceSpecs[i].PortLimit)),
DomainCount: types.Int64Value(int64(*antiddosInstanceSpecs.Body.InstanceSpecs[i].DomainLimit)),
Remark: types.StringValue(*antiddosInstances.Body.Instances[i].Remark),
IpMode: types.StringValue(*antiddosInstances.Body.Instances[i].IpMode),
DebtStatus: types.Int64Value(int64(*antiddosInstances.Body.Instances[i].DebtStatus)),
Edition: types.Int64Value(int64(*antiddosInstances.Body.Instances[i].Edition)),
IpVersion: types.StringValue(*antiddosInstances.Body.Instances[i].IpVersion),
Status: types.Int64Value(int64(*antiddosInstances.Body.Instances[i].Status)),
Enabled: types.Int64Value(int64(*antiddosInstances.Body.Instances[i].Enabled)),
ExpireTime: types.Int64Value(*antiddosInstances.Body.Instances[i].ExpireTime),
CreateTime: types.Int64Value(*antiddosInstances.Body.Instances[i].CreateTime),
Eip: types.ListValueMust(types.StringType, instanceEipList),
}
state.Instances = append(state.Instances, instanceDetail)
}
}
return nil
}
// Retry backoff
reconnectBackoff := backoff.NewExponentialBackOff()
reconnectBackoff.MaxElapsedTime = 60 * time.Second
err := backoff.Retry(readInstances, reconnectBackoff)
if err != nil {
resp.Diagnostics.AddError(
"[API ERROR] Failed to read Anti-DDoS Instances",
err.Error(),
)
return
}
diags = resp.State.Set(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}