/
table_googledirectory_group.go
208 lines (182 loc) · 5.29 KB
/
table_googledirectory_group.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
package googledirectory
import (
"context"
"fmt"
"strings"
"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"
admin "google.golang.org/api/admin/directory/v1"
)
//// TABLE DEFINITION
func tableGoogleDirectoryGroup(_ context.Context) *plugin.Table {
return &plugin.Table{
Name: "googledirectory_group",
Description: "Groups defined in the Google Workspace directory.",
List: &plugin.ListConfig{
Hydrate: listDirectoryGroups,
KeyColumns: []*plugin.KeyColumn{
{
Name: "customer_id",
Require: plugin.Optional,
},
{
Name: "name",
Require: plugin.Optional,
},
{
Name: "query",
Require: plugin.Optional,
},
},
ShouldIgnoreError: isNotFoundError([]string{"404"}),
},
Get: &plugin.GetConfig{
KeyColumns: plugin.AnyColumn([]string{"id", "email"}),
Hydrate: getDirectoryGroup,
},
Columns: []*plugin.Column{
{
Name: "name",
Description: "The group's display name.",
Type: proto.ColumnType_STRING,
},
{
Name: "id",
Description: "The unique ID of a group.",
Type: proto.ColumnType_STRING,
},
{
Name: "email",
Description: "Specifies the group's email address.",
Type: proto.ColumnType_STRING,
},
{
Name: "admin_created",
Description: "Indicates whether the group is created by an administrator, or by an user.",
Type: proto.ColumnType_BOOL,
},
{
Name: "customer_id",
Description: "The customer ID to retrieve all account groups.",
Type: proto.ColumnType_STRING,
Transform: transform.FromQual("customer_id"),
},
{
Name: "description",
Description: "An extended description to help users determine the purpose of a group.",
Type: proto.ColumnType_STRING,
},
{
Name: "direct_members_count",
Description: "The number of users that are direct members of the group.",
Type: proto.ColumnType_INT,
},
{
Name: "etag",
Description: "A hash of the metadata, used to ensure there were no concurrent modifications to the resource when attempting an update.",
Type: proto.ColumnType_STRING,
},
{
Name: "kind",
Description: "The type of the API resource.",
Type: proto.ColumnType_STRING,
},
{
Name: "query",
Description: "Filter string to [filter](https://developers.google.com/admin-sdk/directory/v1/guides/search-groups) groups.",
Type: proto.ColumnType_STRING,
Transform: transform.FromQual("query"),
},
{
Name: "aliases",
Description: "A list of the group's alias email addresses.",
Type: proto.ColumnType_JSON,
},
{
Name: "non_editable_aliases",
Description: "A list of the group's non-editable alias email addresses that are outside of the account's primary domain or subdomains.",
Type: proto.ColumnType_JSON,
},
},
}
}
//// LIST FUNCTION
func listDirectoryGroups(ctx context.Context, d *plugin.QueryData, _ *plugin.HydrateData) (interface{}, error) {
// Create service
service, err := AdminService(ctx, d)
if err != nil {
return nil, err
}
var queryFilter, query string
var filter []string
if d.EqualsQuals["name"] != nil {
filter = append(filter, fmt.Sprintf("name='%s'", d.EqualsQuals["name"].GetStringValue()))
}
if d.EqualsQuals["query"] != nil {
queryFilter = d.EqualsQuals["query"].GetStringValue()
}
if queryFilter != "" {
query = queryFilter
} else if len(filter) > 0 {
query = strings.Join(filter, " ")
}
// Since, query parameter can't be empty, set default param name:**, to return all groups
if query == "" {
query = "name:**"
}
// Set default value to my_customer, to represent current account
customerID := "my_customer"
if d.EqualsQuals["customer_id"] != nil {
customerID = d.EqualsQuals["customer_id"].GetStringValue()
}
// By default, API can return maximum 200 records in a single page
maxResult := int64(200)
limit := d.QueryContext.Limit
if d.QueryContext.Limit != nil {
if *limit < maxResult {
maxResult = *limit
}
}
resp := service.Groups.List().Customer(customerID).Query(query).MaxResults(maxResult)
if err := resp.Pages(ctx, func(page *admin.Groups) error {
for _, group := range page.Groups {
d.StreamListItem(ctx, group)
// Context can be cancelled due to manual cancellation or the limit has been hit
if plugin.IsCancelled(ctx) {
page.NextPageToken = ""
break
}
}
return nil
}); err != nil {
return nil, err
}
return nil, err
}
//// HYDRATE FUNCTIONS
func getDirectoryGroup(ctx context.Context, d *plugin.QueryData, _ *plugin.HydrateData) (interface{}, error) {
plugin.Logger(ctx).Trace("getDirectoryGroup")
// Create service
service, err := AdminService(ctx, d)
if err != nil {
return nil, err
}
id := d.EqualsQuals["id"].GetStringValue()
email := d.EqualsQuals["email"].GetStringValue()
// Return nil, if no input provided
if id == "" && email == "" {
return nil, nil
}
var inputStr string
if id == "" {
inputStr = email
} else {
inputStr = id
}
resp, err := service.Groups.Get(inputStr).Do()
if err != nil {
return nil, err
}
return resp, nil
}