/
table_alicloud_vpc_dhcp_options_set.go
257 lines (225 loc) · 7.26 KB
/
table_alicloud_vpc_dhcp_options_set.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
package alicloud
import (
"context"
"github.com/turbot/steampipe-plugin-sdk/v5/grpc/proto"
"github.com/turbot/steampipe-plugin-sdk/v5/plugin"
"github.com/turbot/steampipe-plugin-sdk/v5/plugin/transform"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
"github.com/aliyun/alibaba-cloud-sdk-go/services/vpc"
)
//// TABLE DEFINITION
func tableAlicloudVpcDhcpOptionsSet(ctx context.Context) *plugin.Table {
return &plugin.Table{
Name: "alicloud_vpc_dhcp_options_set",
Description: "Aliclod VPC DHCP Options Set",
Get: &plugin.GetConfig{
KeyColumns: plugin.SingleColumn("dhcp_options_set_id"),
Hydrate: getVpcDhcpOptionsSet,
},
List: &plugin.ListConfig{
Hydrate: listVpcDhcpOptionsSets,
KeyColumns: plugin.KeyColumnSlice{
{Name: "name", Require: plugin.Optional},
{Name: "domain_name", Require: plugin.Optional},
},
},
GetMatrixItemFunc: BuildRegionList,
Columns: []*plugin.Column{
{
Name: "name",
Description: "The name of the DHCP option set.",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("DhcpOptionsSetName"),
},
{
Name: "dhcp_options_set_id",
Description: "The ID of the DHCP option set.",
Type: proto.ColumnType_STRING,
},
{
Name: "status",
Description: "The status of the DHCP option set.",
Type: proto.ColumnType_STRING,
},
{
Name: "associate_vpc_count",
Description: "The number of VPCs associated with DHCP option set.",
Type: proto.ColumnType_INT,
},
{
Name: "boot_file_name",
Description: "The boot file name of DHCP option set.",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("DhcpOptions.BootFileName"),
},
{
Name: "description",
Description: "The description for the DHCP option set.",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("DhcpOptionsSetDescription"),
},
{
Name: "domain_name",
Description: "The root domain.",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("DhcpOptions.DomainName"),
},
{
Name: "domain_name_servers",
Description: "The IP addresses of your DNS servers.",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("DhcpOptions.DomainNameServers"),
},
{
Name: "owner_id",
Description: "The ID of the account to which the DHCP options set belongs.",
Type: proto.ColumnType_STRING,
},
{
Name: "tftp_server_name",
Description: "The tftp server name of the DHCP option set.",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("DhcpOptions.TFTPServerName"),
},
{
Name: "associate_vpcs",
Description: "The information of the VPC network that is associated with the DHCP options set.",
Type: proto.ColumnType_JSON,
Hydrate: getVpcDhcpOptionsSet,
},
// Steampipe standard columns
{
Name: "title",
Description: ColumnDescriptionTitle,
Type: proto.ColumnType_STRING,
Transform: transform.FromField("DhcpOptionsSetName"),
},
{
Name: "akas",
Description: ColumnDescriptionAkas,
Type: proto.ColumnType_JSON,
Hydrate: getVpcDhcpOptionSetAka,
Transform: transform.FromValue(),
},
// Alicloud common columns
{
Name: "region",
Description: ColumnDescriptionRegion,
Type: proto.ColumnType_STRING,
Hydrate: vpcDhcpOptionsetRegion,
Transform: transform.FromValue(),
},
{
Name: "account_id",
Description: ColumnDescriptionAccount,
Type: proto.ColumnType_STRING,
Hydrate: getCommonColumns,
Transform: transform.FromField("AccountID"),
},
},
}
}
//// LIST FUNCTION
func listVpcDhcpOptionsSets(ctx context.Context, d *plugin.QueryData, _ *plugin.HydrateData) (interface{}, error) {
// Create service connection
client, err := VpcService(ctx, d)
if err != nil {
plugin.Logger(ctx).Error("alicloud_vpc_dhcp_options_set.listVpcDhcpOptionsSets", "connection_error", err)
return nil, err
}
request := vpc.CreateListDhcpOptionsSetsRequest()
request.Scheme = "https"
request.MaxResults = requests.NewInteger(100)
if d.EqualsQualString("name") != "" {
request.DhcpOptionsSetName = d.EqualsQualString("name")
}
if d.EqualsQualString("domain_name") != "" {
request.DomainName = d.EqualsQualString("domain_name")
}
// Limiting the results
limit := d.QueryContext.Limit
if d.QueryContext.Limit != nil {
if *limit < 100 {
if *limit < 1 {
request.MaxResults = requests.NewInteger(1)
} else {
request.MaxResults = requests.NewInteger(int(*limit))
}
}
}
pageLeft := true
for pageLeft {
response, err := client.ListDhcpOptionsSets(request)
if err != nil {
plugin.Logger(ctx).Error("alicloud_vpc_dhcp_options_set.listVpcDhcpOptionsSets", "query_error", err, "request", request)
return nil, err
}
for _, dhcpOptionSet := range response.DhcpOptionsSets {
d.StreamListItem(ctx, dhcpOptionSet)
// Context can be cancelled due to manual cancellation or the limit has been hit
if d.RowsRemaining(ctx) == 0 {
return nil, nil
}
}
if response.NextToken != "" {
request.NextToken = response.NextToken
} else {
pageLeft = false
}
}
return nil, nil
}
//// HYDRATE FUNCTIONS
func getVpcDhcpOptionsSet(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
plugin.Logger(ctx).Trace("getVpcDhcpOptionsSet")
// Create service connection
client, err := VpcService(ctx, d)
if err != nil {
plugin.Logger(ctx).Error("alicloud_vpc_dhcp_options_set.getVpcDhcpOptionsSet", "connection_error", err)
return nil, err
}
var id string
if h.Item != nil {
id = h.Item.(vpc.DhcpOptionsSet).DhcpOptionsSetId
} else {
id = d.EqualsQuals["dhcp_options_set_id"].GetStringValue()
}
request := vpc.CreateGetDhcpOptionsSetRequest()
request.Scheme = "https"
request.DhcpOptionsSetId = id
response, err := client.GetDhcpOptionsSet(request)
if err != nil {
plugin.Logger(ctx).Error("alicloud_vpc_dhcp_options_set.getVpcDhcpOptionsSet", "query_error", err, "request", request)
return nil, nil
}
if response.DhcpOptionsSetId != "" {
return response, nil
}
return nil, nil
}
func getVpcDhcpOptionSetAka(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
plugin.Logger(ctx).Trace("getVpcDhcpOptionSetAka")
var id, region string
region = d.EqualsQualString(matrixKeyRegion)
switch item := h.Item.(type) {
case *vpc.GetDhcpOptionsSetResponse:
id = item.DhcpOptionsSetId
case vpc.DhcpOptionsSet:
id = item.DhcpOptionsSetId
}
// Get project details
getCommonColumnsCached := plugin.HydrateFunc(getCommonColumns).WithCache()
commonData, err := getCommonColumnsCached(ctx, d, h)
if err != nil {
return nil, err
}
commonColumnData := commonData.(*alicloudCommonColumnData)
accountID := commonColumnData.AccountID
akas := []string{"acs:vpc:" + region + ":" + accountID + ":dhcpoptionset/" + id}
return akas, nil
}
//// TRANSFORM FUNCTIONS
func vpcDhcpOptionsetRegion(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
region := d.EqualsQualString(matrixKeyRegion)
return region, nil
}