/
table_azure_network_watcher.go
152 lines (131 loc) · 4.4 KB
/
table_azure_network_watcher.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
package azure
import (
"context"
"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2020-05-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 tableAzureNetworkWatcher(_ context.Context) *plugin.Table {
return &plugin.Table{
Name: "azure_network_watcher",
Description: "Azure Network Watcher",
Get: &plugin.GetConfig{
KeyColumns: plugin.AllColumns([]string{"name", "resource_group"}),
Hydrate: getNetworkWatcher,
IgnoreConfig: &plugin.IgnoreConfig{
ShouldIgnoreErrorFunc: isNotFoundError([]string{"ResourceNotFound", "ResourceGroupNotFound", "404"}),
},
},
List: &plugin.ListConfig{
Hydrate: listNetworkWatchers,
},
Columns: azureColumns([]*plugin.Column{
{
Name: "name",
Description: "The friendly name that identifies the network watcher",
Type: proto.ColumnType_STRING,
},
{
Name: "id",
Description: "Contains ID to identify a network watcher uniquely",
Type: proto.ColumnType_STRING,
Transform: transform.FromGo(),
},
{
Name: "etag",
Description: "An unique read-only string that changes whenever the resource is updated",
Type: proto.ColumnType_STRING,
},
{
Name: "type",
Description: "The resource type of the network watcher",
Type: proto.ColumnType_STRING,
},
{
Name: "provisioning_state",
Description: "The provisioning state of the network watcher resource",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("WatcherPropertiesFormat.ProvisioningState").Transform(transform.ToString),
},
// Steampipe standard columns
{
Name: "title",
Description: ColumnDescriptionTitle,
Type: proto.ColumnType_STRING,
Transform: transform.FromField("Name"),
},
{
Name: "tags",
Description: ColumnDescriptionTags,
Type: proto.ColumnType_JSON,
},
{
Name: "akas",
Description: ColumnDescriptionAkas,
Type: proto.ColumnType_JSON,
Transform: transform.FromField("ID").Transform(idToAkas),
},
// Azure standard columns
{
Name: "region",
Description: ColumnDescriptionRegion,
Type: proto.ColumnType_STRING,
Transform: transform.FromField("Location").Transform(toLower),
},
{
Name: "resource_group",
Description: ColumnDescriptionResourceGroup,
Type: proto.ColumnType_STRING,
Transform: transform.FromField("ID").Transform(extractResourceGroupFromID),
},
}),
}
}
//// FETCH FUNCTIONS ////
func listNetworkWatchers(ctx context.Context, d *plugin.QueryData, _ *plugin.HydrateData) (interface{}, error) {
session, err := GetNewSession(ctx, d, "MANAGEMENT")
if err != nil {
return nil, err
}
subscriptionID := session.SubscriptionID
networkWatcherClient := network.NewWatchersClientWithBaseURI(session.ResourceManagerEndpoint, subscriptionID)
networkWatcherClient.Authorizer = session.Authorizer
result, err := networkWatcherClient.ListAll(ctx)
if err != nil {
return nil, err
}
for _, networkWatcher := range *result.Value {
d.StreamListItem(ctx, networkWatcher)
// 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 FUNCTIONS ////
func getNetworkWatcher(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
plugin.Logger(ctx).Trace("getNetworkWatcher")
name := d.EqualsQuals["name"].GetStringValue()
resourceGroup := d.EqualsQuals["resource_group"].GetStringValue()
session, err := GetNewSession(ctx, d, "MANAGEMENT")
if err != nil {
return nil, err
}
subscriptionID := session.SubscriptionID
networkWatcherClient := network.NewWatchersClientWithBaseURI(session.ResourceManagerEndpoint, subscriptionID)
networkWatcherClient.Authorizer = session.Authorizer
op, err := networkWatcherClient.Get(ctx, resourceGroup, name)
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
}