/
resource_label_text_field.go
252 lines (213 loc) · 9.01 KB
/
resource_label_text_field.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
/*
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/gsmdrivelabels"
"github.com/hanneshayashi/gsm/gsmhelpers"
"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/types"
"google.golang.org/api/drivelabels/v2"
)
// Ensure provider defined types fully satisfy framework interfaces.
var _ resource.Resource = &gdriveLabelTextFieldResource{}
var _ resource.ResourceWithImportState = &gdriveLabelTextFieldResource{}
func newLabelTextField() resource.Resource {
return &gdriveLabelTextFieldResource{}
}
// gdriveLabelTextFieldResource defines the resource implementation.
type gdriveLabelTextFieldResource struct {
client *http.Client
}
type gdriveLabelTextFieldResourceModel struct {
LifeCycle *gdriveLabelLifeCycleModel `tfsdk:"life_cycle"`
Properties *gdriveLabelFieldPropertieseModel `tfsdk:"properties"`
Id types.String `tfsdk:"id"`
FieldId types.String `tfsdk:"field_id"`
LabelId types.String `tfsdk:"label_id"`
QueryKey types.String `tfsdk:"query_key"`
LanguageCode types.String `tfsdk:"language_code"`
UseAdminAccess types.Bool `tfsdk:"use_admin_access"`
}
func (fieldModel *gdriveLabelTextFieldResourceModel) toField() (field *drivelabels.GoogleAppsDriveLabelsV2Field) {
field = &drivelabels.GoogleAppsDriveLabelsV2Field{
TextOptions: &drivelabels.GoogleAppsDriveLabelsV2FieldTextOptions{},
}
if fieldModel.LifeCycle != nil {
field.Lifecycle = fieldModel.LifeCycle.toLifecycle()
}
if fieldModel.Properties != nil {
field.Properties = fieldModel.Properties.toProperties()
}
return
}
func (fieldModel *gdriveLabelTextFieldResourceModel) getLifeCycle() (lifecycle lifeCycleInterface) {
return fieldModel.LifeCycle
}
func (fieldModel *gdriveLabelTextFieldResourceModel) setProperties(properties *gdriveLabelFieldPropertieseModel) {
fieldModel.Properties = properties
}
func (fieldModel *gdriveLabelTextFieldResourceModel) setIds(labelId, fieldId, queryKey string) {
fieldModel.Id = types.StringValue(combineId(labelId, fieldId))
fieldModel.LabelId = types.StringValue(labelId)
fieldModel.FieldId = types.StringValue(fieldId)
fieldModel.QueryKey = types.StringValue(queryKey)
}
func (fieldModel *gdriveLabelTextFieldResourceModel) getProperties() *gdriveLabelFieldPropertieseModel {
return fieldModel.Properties
}
func (fieldModel *gdriveLabelTextFieldResourceModel) getId() string {
return fieldModel.Id.ValueString()
}
func (fieldModel *gdriveLabelTextFieldResourceModel) getFieldId() string {
return fieldModel.FieldId.ValueString()
}
func (fieldModel *gdriveLabelTextFieldResourceModel) getLabelId() string {
return fieldModel.LabelId.ValueString()
}
func (fieldModel *gdriveLabelTextFieldResourceModel) getLanguageCode() string {
return fieldModel.LanguageCode.ValueString()
}
func (fieldModel *gdriveLabelTextFieldResourceModel) getUseAdminAccess() bool {
return fieldModel.UseAdminAccess.ValueBool()
}
func (r *gdriveLabelTextFieldResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_label_text_field"
}
func (r *gdriveLabelTextFieldResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
MarkdownDescription: `Creates a Text Field for a Drive Label.
Changes made to a Field must be published via the Field's Label before they are available for files.
Publishing can only be done via the Label resource, NOT the Field resources.
This means that, if you have Labels and Fields in the same Terraform configuration and you make changes
to the Fields you may have to apply twice in order to
1. Apply the changes to the Fields.
2. Publish the changes via the Label.
A Field must be deactivated before it can be deleted.`,
Attributes: map[string]schema.Attribute{
"id": rsId(),
"field_id": schema.StringAttribute{
MarkdownDescription: `The key of the field, unique within a label or library.
This value is autogenerated. Matches the regex: ([a-zA-Z0-9])+`,
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
"query_key": schema.StringAttribute{
MarkdownDescription: `The key to use when constructing Drive search queries to find files based on values defined for this field on files. For example, "{queryKey} > 2001-01-01".`,
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
"label_id": schema.StringAttribute{
MarkdownDescription: "The ID of the label.",
Required: true,
},
"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.`,
},
"language_code": schema.StringAttribute{
MarkdownDescription: `The BCP-47 language code to use for evaluating localized field labels.
When not specified, values in the default configured language are used.`,
Optional: true,
},
},
Blocks: map[string]schema.Block{
"life_cycle": lifeCycleRS(),
"properties": fieldProperties(),
},
}
}
func (r *gdriveLabelTextFieldResource) 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 (r *gdriveLabelTextFieldResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
plan := &gdriveLabelTextFieldResourceModel{}
resp.Diagnostics.Append(req.Plan.Get(ctx, plan)...)
if resp.Diagnostics.HasError() {
return
}
resp.Diagnostics.Append(createLabelField(plan)...)
if resp.Diagnostics.HasError() {
return
}
resp.Diagnostics.Append(resp.State.Set(ctx, &plan)...)
}
func (r *gdriveLabelTextFieldResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
state := &gdriveLabelTextFieldResourceModel{}
resp.Diagnostics.Append(req.State.Get(ctx, state)...)
if resp.Diagnostics.HasError() {
return
}
_, err := populateField(state)
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to get label field, got error: %s", err))
return
}
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
}
func (r *gdriveLabelTextFieldResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
plan := &gdriveLabelTextFieldResourceModel{}
resp.Diagnostics.Append(req.Plan.Get(ctx, plan)...)
if resp.Diagnostics.HasError() {
return
}
state := &gdriveLabelTextFieldResourceModel{}
resp.Diagnostics.Append(req.State.Get(ctx, state)...)
if resp.Diagnostics.HasError() {
return
}
updateLabelRequest := newUpdateFieldRequest(plan, state)
_, err := gsmdrivelabels.Delta(gsmhelpers.EnsurePrefix(plan.getLabelId(), "labels/"), "*", updateLabelRequest)
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to update text field, got error: %s", err))
return
}
resp.Diagnostics.Append(resp.State.Set(ctx, &plan)...)
}
func (r *gdriveLabelTextFieldResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
state := &gdriveLabelTextFieldResourceModel{}
resp.Diagnostics.Append(req.State.Get(ctx, state)...)
if resp.Diagnostics.HasError() {
return
}
resp.Diagnostics.Append(deleteLabelField(state)...)
if resp.Diagnostics.HasError() {
return
}
}
func (r *gdriveLabelTextFieldResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
resp.Diagnostics.Append(importSplitId(ctx, req, resp, adminAttributeLabels, "label_id/field_id")...)
}