/
table_azure_lb_rule.go
257 lines (232 loc) · 9.64 KB
/
table_azure_lb_rule.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 azure
import (
"context"
"strings"
"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2021-02-01/network"
"github.com/turbot/steampipe-plugin-sdk/v5/grpc/proto"
"github.com/turbot/steampipe-plugin-sdk/v5/plugin/transform"
"github.com/turbot/steampipe-plugin-sdk/v5/plugin"
)
//// TABLE DEFINITION
func tableAzureLoadBalancerRule(_ context.Context) *plugin.Table {
return &plugin.Table{
Name: "azure_lb_rule",
Description: "Azure Load Balancer Rule",
Get: &plugin.GetConfig{
KeyColumns: plugin.AllColumns([]string{"load_balancer_name", "name", "resource_group"}),
Hydrate: getLoadBalancerRule,
IgnoreConfig: &plugin.IgnoreConfig{
ShouldIgnoreErrorFunc: isNotFoundError([]string{"ResourceNotFound", "ResourceGroupNotFound", "404"}),
},
},
List: &plugin.ListConfig{
Hydrate: listLoadBalancerRules,
ParentHydrate: listLoadBalancers,
},
Columns: azureColumns([]*plugin.Column{
{
Name: "name",
Description: "The name of the resource that is unique within the set of load balancing rules used by the load balancer. This name can be used to access the resource.",
Type: proto.ColumnType_STRING,
},
{
Name: "id",
Description: "The resource ID.",
Type: proto.ColumnType_STRING,
Transform: transform.FromGo(),
},
{
Name: "load_balancer_name",
Description: "The friendly name that identifies the load balancer.",
Type: proto.ColumnType_STRING,
Transform: transform.From(extractLoadBalancerNameFromRuleID),
},
{
Name: "provisioning_state",
Description: "The provisioning state of the load balancing rule resource. Possible values include: 'Succeeded', 'Updating', 'Deleting', 'Failed'.",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("LoadBalancingRulePropertiesFormat.ProvisioningState"),
},
{
Name: "type",
Description: "Type of the resource.",
Type: proto.ColumnType_STRING,
},
{
Name: "etag",
Description: "A unique read-only string that changes whenever the resource is updated.",
Type: proto.ColumnType_STRING,
},
{
Name: "backend_address_pool_id",
Description: "A reference to a pool of DIPs. Inbound traffic is randomly load balanced across IPs in the backend IPs.",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("LoadBalancingRulePropertiesFormat.BackendAddressPool.ID"),
},
{
Name: "backend_port",
Description: "The port used for internal connections on the endpoint. Acceptable values are between 0 and 65535. Note that value 0 enables 'Any Port'.",
Type: proto.ColumnType_INT,
Transform: transform.FromField("LoadBalancingRulePropertiesFormat.BackendPort"),
},
{
Name: "disable_outbound_snat",
Description: "Configures SNAT for the VMs in the backend pool to use the publicIP address specified in the frontend of the load balancing rule.",
Type: proto.ColumnType_BOOL,
Transform: transform.FromField("LoadBalancingRulePropertiesFormat.DisableOutboundSnat"),
Default: false,
},
{
Name: "enable_floating_ip",
Description: "Configures a virtual machine's endpoint for the floating IP capability required to configure a SQL AlwaysOn Availability Group. This setting is required when using the SQL AlwaysOn Availability Groups in SQL server. This setting can't be changed after you create the endpoint.",
Type: proto.ColumnType_BOOL,
Transform: transform.FromField("LoadBalancingRulePropertiesFormat.EnableFloatingIP"),
Default: false,
},
{
Name: "enable_tcp_reset",
Description: "Receive bidirectional TCP Reset on TCP flow idle timeout or unexpected connection termination. This element is only used when the protocol is set to TCP.",
Type: proto.ColumnType_BOOL,
Transform: transform.FromField("LoadBalancingRulePropertiesFormat.EnableTCPReset"),
Default: false,
},
{
Name: "frontend_ip_configuration_id",
Description: "A reference to frontend IP addresses.",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("LoadBalancingRulePropertiesFormat.FrontendIPConfiguration.ID"),
},
{
Name: "frontend_port",
Description: "The port for the external endpoint. Port numbers for each rule must be unique within the Load Balancer. Acceptable values are between 0 and 65534. Note that value 0 enables 'Any Port'.",
Type: proto.ColumnType_INT,
Transform: transform.FromField("LoadBalancingRulePropertiesFormat.FrontendPort"),
},
{
Name: "idle_timeout_in_minutes",
Description: "The timeout for the TCP idle connection. The value can be set between 4 and 30 minutes. The default value is 4 minutes. This element is only used when the protocol is set to TCP.",
Type: proto.ColumnType_INT,
Transform: transform.FromField("LoadBalancingRulePropertiesFormat.IdleTimeoutInMinutes"),
},
{
Name: "load_distribution",
Description: "The load distribution policy for this rule. Possible values include: 'Default', 'SourceIP', 'SourceIPProtocol'.",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("LoadBalancingRulePropertiesFormat.LoadDistribution"),
},
{
Name: "probe_id",
Description: "The reference to the load balancer probe used by the load balancing rule.",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("LoadBalancingRulePropertiesFormat.Probe.ID"),
},
{
Name: "protocol",
Description: "The reference to the transport protocol used by the load balancing rule. Possible values include: 'UDP', 'TCP', 'All'.",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("LoadBalancingRulePropertiesFormat.Protocol"),
},
{
Name: "backend_address_pools",
Description: "An array of references to pool of DIPs.",
Type: proto.ColumnType_JSON,
Transform: transform.FromField("LoadBalancingRulePropertiesFormat.BackendAddressPools"),
},
// Steampipe standard columns
{
Name: "title",
Description: ColumnDescriptionTitle,
Type: proto.ColumnType_STRING,
Transform: transform.FromField("Name"),
},
{
Name: "akas",
Description: ColumnDescriptionAkas,
Type: proto.ColumnType_JSON,
Transform: transform.FromField("ID").Transform(idToAkas),
},
// Azure standard columns
{
Name: "resource_group",
Description: ColumnDescriptionResourceGroup,
Type: proto.ColumnType_STRING,
Transform: transform.FromField("ID").Transform(extractResourceGroupFromID),
},
}),
}
}
//// LIST FUNCTION
func listLoadBalancerRules(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
// Get the details of load balancer
loadBalancer := h.Item.(network.LoadBalancer)
// Create session
session, err := GetNewSession(ctx, d, "MANAGEMENT")
if err != nil {
return nil, err
}
subscriptionID := session.SubscriptionID
resourceGroup := strings.Split(*loadBalancer.ID, "/")[4]
listLoadBalancerRulesClient := network.NewLoadBalancerLoadBalancingRulesClientWithBaseURI(session.ResourceManagerEndpoint, subscriptionID)
listLoadBalancerRulesClient.Authorizer = session.Authorizer
result, err := listLoadBalancerRulesClient.List(ctx, resourceGroup, *loadBalancer.Name)
if err != nil {
return nil, err
}
for _, rule := range result.Values() {
d.StreamListItem(ctx, rule)
// Check if context has been cancelled or if the limit has been hit (if specified)
// if there is a limit, it will return the number of rows required to reach this limit
if d.RowsRemaining(ctx) == 0 {
return nil, nil
}
}
for result.NotDone() {
err = result.NextWithContext(ctx)
if err != nil {
return nil, err
}
for _, rule := range result.Values() {
d.StreamListItem(ctx, rule)
// Check if context has been cancelled or if the limit has been hit (if specified)
// if there is a limit, it will return the number of rows required to reach this limit
if d.RowsRemaining(ctx) == 0 {
return nil, nil
}
}
}
return nil, err
}
//// HYDRATE FUNCTION
func getLoadBalancerRule(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
plugin.Logger(ctx).Trace("getLoadBalancerRule")
loadBalancerName := d.EqualsQuals["load_balancer_name"].GetStringValue()
loadBalancerRuleName := d.EqualsQuals["name"].GetStringValue()
resourceGroup := d.EqualsQuals["resource_group"].GetStringValue()
// Handle empty loadBalancerName, loadBalancerRuleName or resourceGroup
if loadBalancerName == "" || loadBalancerRuleName == "" || resourceGroup == "" {
return nil, nil
}
session, err := GetNewSession(ctx, d, "MANAGEMENT")
if err != nil {
return nil, err
}
subscriptionID := session.SubscriptionID
LoadBalancerRuleClient := network.NewLoadBalancerLoadBalancingRulesClientWithBaseURI(session.ResourceManagerEndpoint, subscriptionID)
LoadBalancerRuleClient.Authorizer = session.Authorizer
op, err := LoadBalancerRuleClient.Get(ctx, resourceGroup, loadBalancerName, loadBalancerRuleName)
if err != nil {
return nil, err
}
// In some cases resource does not give any notFound error
// instead of notFound error, it returns empty data
if op.ID != nil {
return op, nil
}
return nil, nil
}
//// TRANSFORM FUNCTION
func extractLoadBalancerNameFromRuleID(ctx context.Context, d *transform.TransformData) (interface{}, error) {
data := d.HydrateItem.(network.LoadBalancingRule)
loadBalancerName := strings.Split(*data.ID, "/")[8]
return loadBalancerName, nil
}