This repository has been archived by the owner on Jul 17, 2020. It is now read-only.
/
data_source_tc_ccn_instances.go
226 lines (200 loc) · 6.17 KB
/
data_source_tc_ccn_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
/*
Use this data source to query detailed information of CCN instances.
Example Usage
```hcl
resource "tencentcloud_ccn" "main" {
name = "ci-temp-test-ccn"
description = "ci-temp-test-ccn-des"
qos = "AG"
}
data "tencentcloud_ccn_instances" "id_instances" {
ccn_id = tencentcloud_ccn.main.id
}
data "tencentcloud_ccn_instances" "name_instances" {
name = tencentcloud_ccn.main.name
}
```
*/
package tencentcloud
import (
"context"
"crypto/md5"
"fmt"
"log"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)
func dataSourceTencentCloudCcnInstances() *schema.Resource {
return &schema.Resource{
Read: dataSourceTencentCloudCcnInstancesRead,
Schema: map[string]*schema.Schema{
"ccn_id": {
Type: schema.TypeString,
Optional: true,
Description: "ID of the CCN to be queried.",
},
"name": {
Type: schema.TypeString,
Optional: true,
Description: "Name of the CCN to be queried.",
},
"result_output_file": {
Type: schema.TypeString,
Optional: true,
Description: "Used to save results.",
},
// Computed values
"instance_list": {
Type: schema.TypeList,
Computed: true,
Description: "Information list of CCN.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"ccn_id": {
Type: schema.TypeString,
Computed: true,
Description: "ID of the CCN.",
},
"name": {
Type: schema.TypeString,
Computed: true,
Description: "Name of the CCN.",
},
"description": {
Type: schema.TypeString,
Computed: true,
Description: "Description of the CCN.",
},
"qos": {
Type: schema.TypeString,
Computed: true,
Description: "Service quality of CCN, and the available value include 'PT', 'AU', 'AG'. The default is 'AU'.",
},
"state": {
Type: schema.TypeString,
Computed: true,
Description: "States of instance. The available value include 'ISOLATED'(arrears) and 'AVAILABLE'.",
},
"attachment_list": {
Type: schema.TypeList,
Computed: true,
Description: "Information list of instance is attached.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"instance_type": {
Type: schema.TypeString,
Computed: true,
Description: "Type of attached instance network, and available values include VPC, DIRECTCONNECT, BMVPC and VPNGW.",
},
"instance_region": {
Type: schema.TypeString,
Computed: true,
Description: "The region that the instance locates at.",
},
"instance_id": {
Type: schema.TypeString,
Computed: true,
Description: "ID of instance is attached.",
},
"state": {
Type: schema.TypeString,
Computed: true,
Description: "States of instance is attached, and available values include PENDING, ACTIVE, EXPIRED, REJECTED, DELETED, FAILED(asynchronous forced disassociation after 2 hours), ATTACHING, DETACHING and DETACHFAILED(asynchronous forced disassociation after 2 hours).",
},
"attached_time": {
Type: schema.TypeString,
Computed: true,
Description: "Time of attaching.",
},
"cidr_block": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Description: "A network address block of the instance that is attached.",
},
},
},
},
"create_time": {
Type: schema.TypeString,
Computed: true,
Description: "Creation time of resource.",
},
},
},
},
},
}
}
func dataSourceTencentCloudCcnInstancesRead(d *schema.ResourceData, meta interface{}) error {
defer logElapsed("data_source.tencentcloud_ccn_instances.read")()
logId := getLogId(contextNil)
ctx := context.WithValue(context.TODO(), logIdKey, logId)
service := VpcService{client: meta.(*TencentCloudClient).apiV3Conn}
var (
ccnId = ""
name = ""
)
if temp, ok := d.GetOk("ccn_id"); ok {
if tempStr := temp.(string); tempStr != "" {
ccnId = tempStr
}
}
if temp, ok := d.GetOk("name"); ok {
if tempStr := temp.(string); tempStr != "" {
name = tempStr
}
}
var infos, err = service.DescribeCcns(ctx, ccnId, name)
if err != nil {
return err
}
var infoList = make([]map[string]interface{}, 0, len(infos))
for _, item := range infos {
var infoMap = make(map[string]interface{})
infoMap["ccn_id"] = item.ccnId
infoMap["name"] = item.name
infoMap["description"] = item.description
infoMap["qos"] = item.qos
infoMap["state"] = strings.ToUpper(item.state)
infoMap["create_time"] = item.createTime
infoList = append(infoList, infoMap)
instances, err := service.DescribeCcnAttachedInstances(ctx, item.ccnId)
if err != nil {
return err
}
attachmentList := make([]interface{}, 0, len(instances))
for _, instance := range instances {
instanceMap := map[string]interface{}{
"instance_type": instance.instanceType,
"instance_region": instance.instanceRegion,
"instance_id": instance.instanceId,
"state": strings.ToUpper(instance.state),
"attached_time": instance.attachedTime,
"cidr_block": instance.cidrBlock,
}
attachmentList = append(attachmentList, instanceMap)
}
infoMap["attachment_list"] = attachmentList
}
if err := d.Set("instance_list", infoList); err != nil {
log.Printf("[CRITAL]%s provider set ccn instances fail, reason:%s\n ", logId, err.Error())
return err
}
m := md5.New()
_, err = m.Write([]byte("ccn_instances" + ccnId + "_" + name))
if err != nil {
return err
}
d.SetId(fmt.Sprintf("%x", m.Sum(nil)))
if output, ok := d.GetOk("result_output_file"); ok && output.(string) != "" {
if err := writeToFile(output.(string), infoList); err != nil {
log.Printf("[CRITAL]%s output file[%s] fail, reason[%s]\n",
logId, output.(string), err.Error())
return err
}
}
return nil
}