/
resource_label_permission.go
286 lines (257 loc) · 10.9 KB
/
resource_label_permission.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*
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"
"strconv"
"strings"
"github.com/hanneshayashi/gsm/gsmdrivelabels"
"github.com/hanneshayashi/gsm/gsmhelpers"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"google.golang.org/api/drivelabels/v2"
)
// Ensure provider defined types fully satisfy framework interfaces.
var _ resource.Resource = &gdriveLabelPermissionResourceModel{}
var _ resource.ResourceWithImportState = &gdriveLabelPermissionResourceModel{}
func newLabelPermission() resource.Resource {
return &gdriveLabelPermissionResourceModel{}
}
// gdriveLabelPermissionResourceModel defines the resource implementation.
type gdriveLabelPermissionResource struct {
client *http.Client
}
// gdriveLabelPermissionResourceModel describes the resource data model.
type gdriveLabelPermissionResourceModel struct {
Id types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
Parent types.String `tfsdk:"parent"`
Email types.String `tfsdk:"email"`
Audience types.String `tfsdk:"audience"`
Role types.String `tfsdk:"role"`
UseAdminAccess types.Bool `tfsdk:"use_admin_access"`
}
func (r *gdriveLabelPermissionResourceModel) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_label_permission"
}
func (r *gdriveLabelPermissionResourceModel) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
MarkdownDescription: `Updates a Label's permissions.
If a permission for the indicated principal doesn't exist, a new Label Permission is created, otherwise the existing permission is updated.
Permissions affect the Label resource as a whole, are not revisioned, and do not require publishing.`,
Attributes: map[string]schema.Attribute{
"id": rsId(),
"name": schema.StringAttribute{
MarkdownDescription: "Resource name of this permission.",
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
"parent": schema.StringAttribute{
MarkdownDescription: "The ID of the label.",
Required: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
},
"use_admin_access": schema.BoolAttribute{
Optional: true,
MarkdownDescription: `Set to true in order to use the user's admin credentials.
The server verifies that the user is an admin for the label before allowing access.`,
},
"email": schema.StringAttribute{
Optional: true,
MarkdownDescription: `Specifies the email address for a user or group pricinpal.
User and Group permissions may only be inserted using email address.`,
Validators: []validator.String{
stringvalidator.ExactlyOneOf(path.Expressions{
path.MatchRoot("audience"),
path.MatchRoot("email"),
}...),
},
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
},
"audience": schema.StringAttribute{
MarkdownDescription: `Audience to grant a role to.
The magic value of 'audiences/default' may be used to apply the role to the default audience in the context of the organization that owns the Label.`,
Optional: true,
Validators: []validator.String{
stringvalidator.ExactlyOneOf(path.Expressions{
path.MatchRoot("audience"),
path.MatchRoot("email"),
}...),
},
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
},
"role": schema.StringAttribute{
Required: true,
MarkdownDescription: `The role the principal should have. Possible values are:
* READER - A reader can read the label and associated metadata applied to Drive items.
* APPLIER - An applier can write associated metadata on Drive items in which they also have write access to. Implies READER.
* ORGANIZER - An organizer can pin this label in shared drives they manage and add new appliers to the label.
* EDITOR - Editors can make any update including deleting the label which also deletes the associated Drive item metadata. Implies APPLIER.`,
},
},
}
}
func (r *gdriveLabelPermissionResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.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 Resource Configure Type",
fmt.Sprintf("Expected *http.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
return
}
r.client = client
}
func (permissionModel *gdriveLabelPermissionResourceModel) toPermission() *drivelabels.GoogleAppsDriveLabelsV2LabelPermission {
permission := &drivelabels.GoogleAppsDriveLabelsV2LabelPermission{
Role: permissionModel.Role.ValueString(),
}
if !permissionModel.Audience.IsNull() {
permission.Audience = permissionModel.Audience.ValueString()
}
if !permissionModel.Email.IsNull() {
permission.Email = permissionModel.Email.ValueString()
}
return permission
}
func (r *gdriveLabelPermissionResourceModel) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
plan := &gdriveLabelPermissionResourceModel{}
resp.Diagnostics.Append(req.Plan.Get(ctx, plan)...)
if resp.Diagnostics.HasError() {
return
}
p, err := gsmdrivelabels.CreateLabelPermission(gsmhelpers.EnsurePrefix(plan.Parent.ValueString(), "labels/"), "", plan.UseAdminAccess.ValueBool(), plan.toPermission())
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to create label permission, got error: %s", err))
return
}
plan.Name = types.StringValue(p.Name)
plan.Id = plan.Name
resp.Diagnostics.Append(resp.State.Set(ctx, &plan)...)
}
func (r *gdriveLabelPermissionResourceModel) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
state := &gdriveLabelPermissionResourceModel{}
resp.Diagnostics.Append(req.State.Get(ctx, state)...)
if resp.Diagnostics.HasError() {
return
}
name := state.Id.ValueString()
currentP, err := gsmdrivelabels.ListLabelPermissions(gsmhelpers.EnsurePrefix(state.Parent.ValueString(), "labels/"), "", state.UseAdminAccess.ValueBool(), 1)
for i := range currentP {
if i.Name == name {
if i.Email != "" {
state.Email = types.StringValue(i.Email)
}
if i.Audience != "" && state.Audience.ValueString() != "audiences/default" {
state.Audience = types.StringValue(i.Audience)
}
state.Role = types.StringValue(i.Role)
}
}
e := <-err
if e != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to list permissions on label, got error: %s", e))
}
state.Name = state.Id
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
}
func (r *gdriveLabelPermissionResourceModel) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
plan := &gdriveLabelPermissionResourceModel{}
resp.Diagnostics.Append(req.Plan.Get(ctx, plan)...)
if resp.Diagnostics.HasError() {
return
}
state := &gdriveLabelPermissionResourceModel{}
resp.Diagnostics.Append(req.State.Get(ctx, state)...)
if resp.Diagnostics.HasError() {
return
}
useAdminAccess := plan.UseAdminAccess.ValueBool()
parent := gsmhelpers.EnsurePrefix(plan.Parent.ValueString(), "labels/")
updateReq := &drivelabels.GoogleAppsDriveLabelsV2BatchUpdateLabelPermissionsRequest{
UseAdminAccess: useAdminAccess,
Requests: []*drivelabels.GoogleAppsDriveLabelsV2UpdateLabelPermissionRequest{
{
UseAdminAccess: useAdminAccess,
Parent: parent,
LabelPermission: plan.toPermission(),
},
},
}
_, err := gsmdrivelabels.BatchUpdateLabelPermissions(parent, "", updateReq)
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to update permissions on label, got error: %s", err))
}
resp.Diagnostics.Append(resp.State.Set(ctx, &plan)...)
}
func (r *gdriveLabelPermissionResourceModel) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
state := &gdriveLabelPermissionResourceModel{}
resp.Diagnostics.Append(req.State.Get(ctx, state)...)
if resp.Diagnostics.HasError() {
return
}
useAdminAccess := state.UseAdminAccess.ValueBool()
deleteReq := &drivelabels.GoogleAppsDriveLabelsV2BatchDeleteLabelPermissionsRequest{
UseAdminAccess: useAdminAccess,
Requests: []*drivelabels.GoogleAppsDriveLabelsV2DeleteLabelPermissionRequest{
{
Name: state.Name.ValueString(),
UseAdminAccess: useAdminAccess,
},
},
}
gsmdrivelabels.BatchDeleteLabelPermissions(gsmhelpers.EnsurePrefix(state.Parent.ValueString(), "labels/"), deleteReq)
}
func (r *gdriveLabelPermissionResourceModel) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
idParts := strings.Split(req.ID, ",")
if len(idParts) < 2 {
resp.Diagnostics.AddError("Unexpected Import Identifier", fmt.Sprintf("Expected import identifier with format: 'useAdminAccess,name'. Got: %q", req.ID))
return
}
for i := range idParts {
if idParts[i] == "" {
resp.Diagnostics.AddError("Unexpected Import Identifier", fmt.Sprintf("Expected import identifier with format: 'useAdminAccess,name'. Got: %q", req.ID))
return
}
}
useAdminAccess, err := strconv.ParseBool(idParts[0])
if err != nil {
resp.Diagnostics.AddError("Unexpected Import Identifier", fmt.Sprintf("Unable to parse '%s' as bool: %v", idParts[0], err))
return
}
id := strings.Join(idParts[1:], "")
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("parent"), id[:strings.LastIndex(id, "/permissions/")])...)
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("use_admin_access"), useAdminAccess)...)
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("id"), id)...)
}