/
data_source_permissions.go
177 lines (156 loc) · 6.57 KB
/
data_source_permissions.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
/*
Copyright © 2021-2023 Hannes Hayashi
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package provider
import (
"context"
"fmt"
"net/http"
"github.com/hanneshayashi/gsm/gsmdrive"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
)
// Ensure provider defined types fully satisfy framework interfaces.
var _ datasource.DataSource = &permissionsDataSource{}
func newPermissionsDataSource() datasource.DataSource {
return &permissionsDataSource{}
}
// permissionsDataSource defines the data source implementation.
type permissionsDataSource struct {
client *http.Client
}
type gdrivePermissionsDataSourcePermissionModel struct {
PermissionId types.String `tfsdk:"permission_id"`
DisplayName types.String `tfsdk:"display_name"`
Domain types.String `tfsdk:"domain"`
EmailAddress types.String `tfsdk:"email_address"`
ExpirationTime types.String `tfsdk:"expiration_time"`
Role types.String `tfsdk:"role"`
Type types.String `tfsdk:"type"`
Deleted types.Bool `tfsdk:"deleted"`
}
type gdrivePermissionsDataSourceModel struct {
Id types.String `tfsdk:"id"`
FileId types.String `tfsdk:"file_id"`
Permissions []*gdrivePermissionsDataSourcePermissionModel `tfsdk:"permissions"`
UseDomainAdminAccess types.Bool `tfsdk:"use_domain_admin_access"`
}
func (d *permissionsDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_permissions"
}
func (d *permissionsDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
MarkdownDescription: "Returns the list of all permissions on a file or Shared Drive",
Attributes: map[string]schema.Attribute{
"id": dsId(),
"file_id": schema.StringAttribute{
Required: true,
MarkdownDescription: "ID of the file or Shared Drive.",
},
"use_domain_admin_access": schema.BoolAttribute{
Optional: true,
MarkdownDescription: "Use domain admin access.",
},
"permissions": schema.SetNestedAttribute{
Computed: true,
MarkdownDescription: "The list of permissions set on this file or Shared Drive.",
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"permission_id": schema.StringAttribute{
Computed: true,
MarkdownDescription: "ID of the permission.",
},
"display_name": schema.StringAttribute{
Computed: true,
MarkdownDescription: `The "pretty" name of the value of the permission.
The following is a list of examples for each type of permission:
* user - User's full name, as defined for their Google account, such as "Joe Smith."
* group - Name of the Google Group, such as "The Company Administrators."
* domain - String domain name, such as "thecompany.com."
* anyone - No displayName is present.`,
},
"domain": schema.StringAttribute{
Computed: true,
MarkdownDescription: "The domain if the type of this permissions is 'domain'.",
},
"deleted": schema.BoolAttribute{
Computed: true,
MarkdownDescription: `Whether the account associated with this permission has been deleted.
This field only pertains to user and group permissions.`,
},
"email_address": schema.StringAttribute{
Computed: true,
MarkdownDescription: "The email address if the type of this permissions is 'user' or 'group'.",
},
"expiration_time": schema.StringAttribute{
Computed: true,
MarkdownDescription: "The time at which this permission will expire (RFC 3339 date-time).",
},
"role": schema.StringAttribute{
Computed: true,
MarkdownDescription: "The role that this trustee is granted.",
},
"type": schema.StringAttribute{
Computed: true,
MarkdownDescription: "The type of the trustee. Can be 'user', 'domain', 'group' or 'anyone'.",
},
},
},
},
},
}
}
func (d *permissionsDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
// Prevent panic if the provider has not been configured.
if req.ProviderData == nil {
return
}
client, ok := req.ProviderData.(*http.Client)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
fmt.Sprintf("Expected *http.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
return
}
d.client = client
}
func (ds *permissionsDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
config := &gdrivePermissionsDataSourceModel{}
resp.Diagnostics.Append(req.Config.Get(ctx, config)...)
if resp.Diagnostics.HasError() {
return
}
fileID := config.FileId.ValueString()
r, err := gsmdrive.ListPermissions(fileID, "", "permissions(id,displayName,domain,deleted,emailAddress,expirationTime,role,type),nextPageToken", config.UseDomainAdminAccess.ValueBool(), 1)
for p := range r {
config.Permissions = append(config.Permissions, &gdrivePermissionsDataSourcePermissionModel{
PermissionId: types.StringValue(p.Id),
DisplayName: types.StringValue(p.DisplayName),
Domain: types.StringValue(p.Domain),
EmailAddress: types.StringValue(p.EmailAddress),
ExpirationTime: types.StringValue(p.ExpirationTime),
Role: types.StringValue(p.Role),
Type: types.StringValue(p.Type),
Deleted: types.BoolValue(p.Deleted),
})
}
e := <-err
if e != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to list permissions, got error: %s", e))
return
}
config.Id = config.FileId
resp.Diagnostics.Append(resp.State.Set(ctx, &config)...)
}