/
table_azure_automation_variable.go
202 lines (177 loc) · 6.16 KB
/
table_azure_automation_variable.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
package azure
import (
"context"
"strings"
"github.com/Azure/azure-sdk-for-go/services/automation/mgmt/2019-06-01/automation"
"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 tableAzureApAutomationVariable(_ context.Context) *plugin.Table {
return &plugin.Table{
Name: "azure_automation_variable",
Description: "Azure Automation Variable",
Get: &plugin.GetConfig{
KeyColumns: plugin.AllColumns([]string{"account_name", "name", "resource_group"}),
Hydrate: getAutomationVariable,
IgnoreConfig: &plugin.IgnoreConfig{
ShouldIgnoreErrorFunc: isNotFoundError([]string{"ResourceNotFound", "ResourceGroupNotFound", "404"}),
},
},
List: &plugin.ListConfig{
ParentHydrate: listAutomationAccounts,
Hydrate: listAutomationVariables,
},
Columns: azureColumns([]*plugin.Column{
{
Name: "name",
Type: proto.ColumnType_STRING,
Description: "The name of the resource.",
},
{
Name: "account_name",
Type: proto.ColumnType_STRING,
Description: "The name of the account.",
},
{
Name: "id",
Description: "Fully qualified resource ID.",
Type: proto.ColumnType_STRING,
Transform: transform.FromGo(),
},
{
Name: "description",
Description: "The description for the variable.",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("VariableProperties.Description"),
},
{
Name: "creation_time",
Description: "The creation time of the variable.",
Type: proto.ColumnType_TIMESTAMP,
Transform: transform.FromField("VariableProperties.CreationTime.Time"),
},
{
Name: "last_modified_time",
Description: "The last modified time of the variable.",
Type: proto.ColumnType_TIMESTAMP,
Transform: transform.FromField("VariableProperties.LastModifiedTime.Time"),
},
{
Name: "type",
Description: "The type of the resource.",
Type: proto.ColumnType_STRING,
},
{
Name: "is_encrypted",
Description: "The encrypted flag of the variable.",
Type: proto.ColumnType_BOOL,
Transform: transform.FromField("VariableProperties.IsEncrypted"),
},
{
Name: "value",
Description: "The value of the variable.",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("VariableProperties.Value"),
},
// 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),
},
}),
}
}
type VariableDetails struct {
AccountName string
automation.Variable
}
//// LIST FUNCTION ////
func listAutomationVariables(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
session, err := GetNewSession(ctx, d, "MANAGEMENT")
if err != nil {
plugin.Logger(ctx).Error("azure_automation_variable.listAutomationVariables", "session_error", err)
return nil, err
}
var account automation.Account
if h.Item != nil {
account = h.Item.(automation.Account)
} else {
return nil, nil
}
resourceGroupName := strings.Split(*account.ID, "/")[4]
accountName := account.Name
subscriptionID := session.SubscriptionID
accountClient := automation.NewVariableClientWithBaseURI(session.ResourceManagerEndpoint, subscriptionID)
accountClient.Authorizer = session.Authorizer
result, err := accountClient.ListByAutomationAccount(ctx, resourceGroupName, *accountName)
if err != nil {
plugin.Logger(ctx).Error("azure_automation_variable.listAutomationVariables", "api_error", err)
return nil, err
}
for _, variable := range result.Values() {
d.StreamListItem(ctx, &VariableDetails{*accountName, variable})
// 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 {
plugin.Logger(ctx).Error("azure_automation_variable.listAutomationVariables", "paginator_error", err)
return nil, err
}
for _, variable := range result.Values() {
d.StreamListItem(ctx, &VariableDetails{*accountName, variable})
// 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 getAutomationVariable(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
accountName := d.EqualsQuals["account_name"].GetStringValue()
name := d.EqualsQuals["name"].GetStringValue()
resourceGroup := d.EqualsQuals["resource_group"].GetStringValue()
session, err := GetNewSession(ctx, d, "MANAGEMENT")
if err != nil {
plugin.Logger(ctx).Error("azure_automation_variable.getAutomationVariable", "session_error", err)
return nil, err
}
subscriptionID := session.SubscriptionID
accountClient := automation.NewVariableClientWithBaseURI(session.ResourceManagerEndpoint, subscriptionID)
accountClient.Authorizer = session.Authorizer
op, err := accountClient.Get(ctx, resourceGroup, accountName, name)
if err != nil {
plugin.Logger(ctx).Error("azure_automation_variable.getAutomationVariable", "api_error", err)
return nil, err
}
// In some cases the API does not return any notFound error
// instead it returns empty data
if op.ID != nil {
return &VariableDetails{accountName, op}, nil
}
return nil, nil
}