/
table_azure_bastion_host.go
186 lines (165 loc) · 5.57 KB
/
table_azure_bastion_host.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
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 tableAzureBastionHost(_ context.Context) *plugin.Table {
return &plugin.Table{
Name: "azure_bastion_host",
Description: "Azure Bastion Host",
Get: &plugin.GetConfig{
KeyColumns: plugin.AllColumns([]string{"name", "resource_group"}),
Hydrate: getBastionHost,
IgnoreConfig: &plugin.IgnoreConfig{
ShouldIgnoreErrorFunc: isNotFoundError([]string{"ResourceGroupNotFound", "ResourceNotFound", "404"}),
},
},
List: &plugin.ListConfig{
Hydrate: listBastionHosts,
},
Columns: azureColumns([]*plugin.Column{
{
Name: "name",
Type: proto.ColumnType_STRING,
Description: "The friendly name that identifies the bastion host.",
},
{
Name: "id",
Description: "Contains ID to identify a bastion host uniquely.",
Transform: transform.FromGo(),
Type: proto.ColumnType_STRING,
},
{
Name: "dns_name",
Description: "FQDN for the endpoint on which bastion host is accessible.",
Transform: transform.FromField("BastionHostPropertiesFormat.DNSName"),
Type: proto.ColumnType_STRING,
},
{
Name: "etag",
Description: "A unique read-only string that changes whenever the resource is updated.",
Type: proto.ColumnType_STRING,
},
{
Name: "provisioning_state",
Description: "The provisioning state of the bastion host resource.",
Transform: transform.FromField("BastionHostPropertiesFormat.ProvisioningState"),
Type: proto.ColumnType_STRING,
},
{
Name: "type",
Description: "The resource type of the bastion host.",
Type: proto.ColumnType_STRING,
},
{
Name: "ip_configurations",
Description: "IP configuration of the bastion host resource.",
Transform: transform.FromField("BastionHostPropertiesFormat.IPConfigurations"),
Type: proto.ColumnType_JSON,
},
// Steampipe standard columns
{
Name: "akas",
Description: ColumnDescriptionAkas,
Transform: transform.FromField("ID").Transform(idToAkas),
Type: proto.ColumnType_JSON,
},
{
Name: "tags",
Description: ColumnDescriptionTags,
Type: proto.ColumnType_JSON,
},
{
Name: "title",
Description: ColumnDescriptionTitle,
Transform: transform.FromField("Name"),
Type: proto.ColumnType_STRING,
},
// Azure standard columns
{
Name: "region",
Description: ColumnDescriptionRegion,
Transform: transform.FromField("Location").Transform(toLower),
Type: proto.ColumnType_STRING,
},
{
Name: "resource_group",
Description: ColumnDescriptionResourceGroup,
Transform: transform.FromField("ID").Transform(extractResourceGroupFromID),
Type: proto.ColumnType_STRING,
},
}),
}
}
//// FETCH FUNCTIONS ////
func listBastionHosts(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
logger := plugin.Logger(ctx)
session, err := GetNewSession(ctx, d, "MANAGEMENT")
if err != nil {
logger.Error("azure_bastion_host.listBastionHosts", "client_error", err)
return nil, err
}
subscriptionID := session.SubscriptionID
bastionClient := network.NewBastionHostsClientWithBaseURI(session.ResourceManagerEndpoint, subscriptionID)
bastionClient.Authorizer = session.Authorizer
result, err := bastionClient.List(ctx)
if err != nil {
logger.Error("azure_bastion_host.listBastionHosts", "api_error", err)
return nil, err
}
for _, host := range result.Values() {
d.StreamListItem(ctx, host)
// 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 {
logger.Error("azure_bastion_host.listBastionHosts", "api_error", err)
return nil, err
}
for _, host := range result.Values() {
d.StreamListItem(ctx, host)
// 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, nil
}
func getBastionHost(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
logger := plugin.Logger(ctx)
name := d.EqualsQuals["name"].GetStringValue()
resourceGroup := d.EqualsQuals["resource_group"].GetStringValue()
if name == "" || resourceGroup == "" {
return nil, nil
}
session, err := GetNewSession(ctx, d, "MANAGEMENT")
if err != nil {
logger.Error("azure_bastion_host.getBastionHost", "client_error", err)
return nil, err
}
subscriptionID := session.SubscriptionID
bastionClient := network.NewBastionHostsClientWithBaseURI(session.ResourceManagerEndpoint, subscriptionID)
bastionClient.Authorizer = session.Authorizer
result, err := bastionClient.Get(ctx, resourceGroup, name)
if err != nil {
logger.Error("azure_bastion_host.getBastionHost", "api_error", err)
return nil, err
}
// In some cases resource does not give any notFound error
// instead of notFound error, it returns empty data
if result.ID != nil {
return result, nil
}
return nil, nil
}