forked from aliyun/terraform-provider-alicloud
/
data_source_alicloud_vpcs.go
252 lines (231 loc) · 6.45 KB
/
data_source_alicloud_vpcs.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
package alicloud
import (
"regexp"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
"github.com/aliyun/alibaba-cloud-sdk-go/services/vpc"
"github.com/hashicorp/terraform/helper/schema"
"github.com/terraform-providers/terraform-provider-alicloud/alicloud/connectivity"
)
func dataSourceAlicloudVpcs() *schema.Resource {
return &schema.Resource{
Read: dataSourceAlicloudVpcsRead,
Schema: map[string]*schema.Schema{
"cidr_block": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"status": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"name_regex": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validateNameRegex,
ForceNew: true,
},
"is_default": {
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
},
"vswitch_id": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"output_file": {
Type: schema.TypeString,
Optional: true,
},
"ids": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"names": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
// Computed values
"vpcs": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
},
"region_id": {
Type: schema.TypeString,
Computed: true,
},
"status": {
Type: schema.TypeString,
Computed: true,
},
"vpc_name": {
Type: schema.TypeString,
Computed: true,
},
"vswitch_ids": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"cidr_block": {
Type: schema.TypeString,
Computed: true,
},
"vrouter_id": {
Type: schema.TypeString,
Computed: true,
},
"route_table_id": {
Type: schema.TypeString,
Computed: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
"is_default": {
Type: schema.TypeBool,
Computed: true,
},
"creation_time": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
}
}
func dataSourceAlicloudVpcsRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
request := vpc.CreateDescribeVpcsRequest()
request.RegionId = string(client.Region)
request.PageSize = requests.NewInteger(PageSizeLarge)
request.PageNumber = requests.NewInteger(1)
var allVpcs []vpc.Vpc
invoker := NewInvoker()
for {
var raw interface{}
var err error
if err = invoker.Run(func() error {
raw, err = client.WithVpcClient(func(vpcClient *vpc.Client) (interface{}, error) {
return vpcClient.DescribeVpcs(request)
})
addDebug(request.GetActionName(), raw)
return err
}); err != nil {
return WrapErrorf(err, DataDefaultErrorMsg, "alicloud_vpcs", request.GetActionName(), AlibabaCloudSdkGoERROR)
}
response, _ := raw.(*vpc.DescribeVpcsResponse)
if len(response.Vpcs.Vpc) < 1 {
break
}
allVpcs = append(allVpcs, response.Vpcs.Vpc...)
if len(response.Vpcs.Vpc) < PageSizeLarge {
break
}
if page, err := getNextpageNumber(request.PageNumber); err != nil {
return WrapError(err)
} else {
request.PageNumber = page
}
}
var filteredVpcs []vpc.Vpc
var route_tables []string
var r *regexp.Regexp
if nameRegex, ok := d.GetOk("name_regex"); ok && nameRegex.(string) != "" {
r = regexp.MustCompile(nameRegex.(string))
}
for _, v := range allVpcs {
if r != nil && !r.MatchString(v.VpcName) {
continue
}
if cidrBlock, ok := d.GetOk("cidr_block"); ok && v.CidrBlock != cidrBlock.(string) {
continue
}
if status, ok := d.GetOk("status"); ok && string(v.Status) != status.(string) {
continue
}
if isDefault, ok := d.GetOk("is_default"); ok && v.IsDefault != isDefault.(bool) {
continue
}
if vswitchId, ok := d.GetOk("vswitch_id"); ok && !vpcVswitchIdListContains(v.VSwitchIds.VSwitchId, vswitchId.(string)) {
continue
}
request := vpc.CreateDescribeVRoutersRequest()
request.VRouterId = v.VRouterId
request.RegionId = string(client.Region)
raw, err := client.WithVpcClient(func(vpcClient *vpc.Client) (interface{}, error) {
return vpcClient.DescribeVRouters(request)
})
if err != nil {
return WrapErrorf(err, DataDefaultErrorMsg, "alicloud_vpcs", request.GetActionName(), AlibabaCloudSdkGoERROR)
}
addDebug(request.GetActionName(), raw)
response, _ := raw.(*vpc.DescribeVRoutersResponse)
if len(response.VRouters.VRouter) > 0 {
route_tables = append(route_tables, response.VRouters.VRouter[0].RouteTableIds.RouteTableId[0])
} else {
route_tables = append(route_tables, "")
}
filteredVpcs = append(filteredVpcs, v)
}
return vpcsDecriptionAttributes(d, filteredVpcs, route_tables, meta)
}
func vpcVswitchIdListContains(vswitchIdList []string, vswitchId string) bool {
for _, idListItem := range vswitchIdList {
if idListItem == vswitchId {
return true
}
}
return false
}
func vpcsDecriptionAttributes(d *schema.ResourceData, vpcSetTypes []vpc.Vpc, route_tables []string, meta interface{}) error {
var ids []string
var names []string
var s []map[string]interface{}
for index, vpc := range vpcSetTypes {
mapping := map[string]interface{}{
"id": vpc.VpcId,
"region_id": vpc.RegionId,
"status": vpc.Status,
"vpc_name": vpc.VpcName,
"vswitch_ids": vpc.VSwitchIds.VSwitchId,
"cidr_block": vpc.CidrBlock,
"vrouter_id": vpc.VRouterId,
"route_table_id": route_tables[index],
"description": vpc.Description,
"is_default": vpc.IsDefault,
"creation_time": vpc.CreationTime,
}
ids = append(ids, vpc.VpcId)
names = append(names, vpc.VpcName)
s = append(s, mapping)
}
d.SetId(dataResourceIdHash(ids))
if err := d.Set("vpcs", s); err != nil {
return WrapError(err)
}
if err := d.Set("names", names); err != nil {
return WrapError(err)
}
if err := d.Set("ids", ids); err != nil {
return WrapError(err)
}
// create a json file in current directory and write data source to it.
if output, ok := d.GetOk("output_file"); ok && output.(string) != "" {
writeToFile(output.(string), s)
}
return nil
}