This repository has been archived by the owner on Oct 2, 2020. It is now read-only.
forked from baidubce/terraform-provider-baiducloud
/
data_source_baiducloud_subnets.go
195 lines (175 loc) · 4.84 KB
/
data_source_baiducloud_subnets.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
/*
Use this data source to query subnet list.
Example Usage
```hcl
data "baiducloud_subnets" "default" {
vpc_id = "vpc-y4p102r3mz6m"
}
output "subnets" {
value = "${data.baiducloud_subnets.default.subnets}"
}
```
*/
package baiducloud
import (
"github.com/baidubce/bce-sdk-go/services/vpc"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/terraform-providers/terraform-provider-baiducloud/baiducloud/connectivity"
)
func dataSourceBaiduCloudSubnets() *schema.Resource {
return &schema.Resource{
Read: dataSourceBaiduCloudSubnetsRead,
Schema: map[string]*schema.Schema{
"filter": dataSourceFiltersSchema(),
"vpc_id": {
Type: schema.TypeString,
Description: "VPC ID for subnets to retrieve.",
Optional: true,
ForceNew: true,
},
"zone_name": {
Type: schema.TypeString,
Description: "Specify the zone name for subnets.",
Optional: true,
ForceNew: true,
},
"subnet_type": {
Type: schema.TypeString,
Description: "Specify the subnet type for subnets.",
Optional: true,
ForceNew: true,
ValidateFunc: validateSubnetType(),
},
"subnet_id": {
Type: schema.TypeString,
Description: "ID of the subnet.",
Optional: true,
ForceNew: true,
},
"output_file": {
Type: schema.TypeString,
Description: "Output file for saving result.",
Optional: true,
ForceNew: true,
},
// Attributes used for result
"subnets": {
Type: schema.TypeList,
Description: "Result of the subnets.",
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Description: "Name of the subnet.",
Computed: true,
},
"subnet_id": {
Type: schema.TypeString,
Description: "ID of the subnet.",
Computed: true,
},
"zone_name": {
Type: schema.TypeString,
Description: "Zone name of the subnet.",
Computed: true,
},
"cidr": {
Type: schema.TypeString,
Description: "CIDR block of the subnet.",
Computed: true,
},
"vpc_id": {
Type: schema.TypeString,
Description: "VPC ID of the subnet.",
Computed: true,
},
"subnet_type": {
Type: schema.TypeString,
Description: "Type of the subnet.",
Computed: true,
},
"description": {
Type: schema.TypeString,
Description: "Description of the subnet.",
Computed: true,
},
"available_ip": {
Type: schema.TypeInt,
Description: "Available IP address of the subnet.",
Computed: true,
},
"tags": tagsComputedSchema(),
},
},
},
},
}
}
func dataSourceBaiduCloudSubnetsRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.BaiduClient)
vpcService := VpcService{client}
var (
vpcID string
zoneName string
subnetType string
subnetID string
outputFile string
)
if v, ok := d.GetOk("vpc_id"); ok {
vpcID = v.(string)
}
if v, ok := d.GetOk("zone_name"); ok {
zoneName = v.(string)
}
if v, ok := d.GetOk("subnet_type"); ok {
subnetType = v.(string)
}
if v, ok := d.GetOk("subnet_id"); ok {
subnetID = v.(string)
}
if v, ok := d.GetOk("output_file"); ok {
outputFile = v.(string)
}
action := "Query subnets " + vpcID + "_" + zoneName + "_" + subnetType + "_" + subnetID
args := &vpc.ListSubnetArgs{
VpcId: vpcID,
ZoneName: zoneName,
SubnetType: vpc.SubnetType(subnetType),
}
subnets, err := vpcService.ListAllSubnets(args)
if err != nil {
return WrapErrorf(err, DefaultErrorMsg, "baiducloud_subnets", action, BCESDKGoERROR)
}
filter := NewDataSourceFilter(d)
subnetsResult := make([]map[string]interface{}, 0, len(subnets))
for _, subnet := range subnets {
if subnetID != "" && subnetID != subnet.SubnetId {
continue
}
subnetMap := make(map[string]interface{})
subnetMap["name"] = subnet.Name
subnetMap["subnet_id"] = subnet.SubnetId
subnetMap["zone_name"] = subnet.ZoneName
subnetMap["cidr"] = subnet.Cidr
subnetMap["vpc_id"] = subnet.VPCId
subnetMap["subnet_type"] = subnet.SubnetType
subnetMap["description"] = subnet.Description
subnetMap["available_ip"] = subnet.AvailableIp
subnetMap["tags"] = flattenTagsToMap(subnet.Tags)
if !filter.checkFilter(subnetMap) {
continue
}
subnetsResult = append(subnetsResult, subnetMap)
}
addDebug(action, subnetsResult)
d.Set("subnets", subnetsResult)
d.SetId(resource.UniqueId())
if outputFile != "" {
if err := writeToFile(outputFile, subnetsResult); err != nil {
return WrapErrorf(err, DefaultErrorMsg, "baiducloud_subnets", action, BCESDKGoERROR)
}
}
return nil
}