/
resource_local_file.go
322 lines (290 loc) · 11.2 KB
/
resource_local_file.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
package provider
import (
"context"
"crypto/sha1"
"encoding/base64"
"encoding/hex"
"fmt"
"os"
"path/filepath"
"strconv"
"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"
"github.com/terraform-providers/terraform-provider-local/internal/localtypes"
"github.com/terraform-providers/terraform-provider-local/internal/modifiers/stringmodifier"
)
var (
_ resource.Resource = (*localFileResource)(nil)
)
func NewLocalFileResource() resource.Resource {
return &localFileResource{}
}
type localFileResource struct{}
func (n *localFileResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
Description: "Generates a local file with the given content.",
Attributes: map[string]schema.Attribute{
"filename": schema.StringAttribute{
Description: "The path to the file that will be created.\n " +
"Missing parent directories will be created.\n " +
"If the file already exists, it will be overridden with the given content.",
Required: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
},
"content": schema.StringAttribute{
Description: "Content to store in the file, expected to be a UTF-8 encoded string.\n " +
"Conflicts with `sensitive_content`, `content_base64` and `source`.\n " +
"Exactly one of these four arguments must be specified.",
Optional: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
Validators: []validator.String{
stringvalidator.ExactlyOneOf(
path.MatchRoot("sensitive_content"),
path.MatchRoot("content_base64"),
path.MatchRoot("source")),
},
},
"content_base64": schema.StringAttribute{
Description: "Content to store in the file, expected to be binary encoded as base64 string.\n " +
"Conflicts with `content`, `sensitive_content` and `source`.\n " +
"Exactly one of these four arguments must be specified.",
Optional: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
Validators: []validator.String{
stringvalidator.ExactlyOneOf(
path.MatchRoot("content"),
path.MatchRoot("sensitive_content"),
path.MatchRoot("source")),
},
},
"source": schema.StringAttribute{
Description: "Path to file to use as source for the one we are creating.\n " +
"Conflicts with `content`, `sensitive_content` and `content_base64`.\n " +
"Exactly one of these four arguments must be specified.",
Optional: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
Validators: []validator.String{
stringvalidator.ExactlyOneOf(
path.MatchRoot("content"),
path.MatchRoot("sensitive_content"),
path.MatchRoot("content_base64")),
},
},
"file_permission": schema.StringAttribute{
CustomType: localtypes.NewFilePermissionType(),
Description: "Permissions to set for the output file (before umask), expressed as string in\n " +
"[numeric notation](https://en.wikipedia.org/wiki/File-system_permissions#Numeric_notation).\n " +
"Default value is `\"0777\"`.",
Optional: true,
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
stringmodifier.StringDefault("0777"),
},
},
"directory_permission": schema.StringAttribute{
CustomType: localtypes.NewFilePermissionType(),
Description: "Permissions to set for directories created (before umask), expressed as string in\n " +
"[numeric notation](https://en.wikipedia.org/wiki/File-system_permissions#Numeric_notation).\n " +
"Default value is `\"0777\"`.",
Optional: true,
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
stringmodifier.StringDefault("0777"),
},
},
"id": schema.StringAttribute{
Description: "The hexadecimal encoding of the SHA1 checksum of the file content.",
Computed: true,
},
"sensitive_content": schema.StringAttribute{
DeprecationMessage: "Use the `local_sensitive_file` resource instead",
Description: "Sensitive content to store in the file, expected to be an UTF-8 encoded string.\n " +
"Will not be displayed in diffs.\n " +
"Conflicts with `content`, `content_base64` and `source`.\n " +
"Exactly one of these four arguments must be specified.\n " +
"If in need to use _sensitive_ content, please use the [`local_sensitive_file`](./sensitive_file.html)\n " +
"resource instead.",
Sensitive: true,
Optional: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
Validators: []validator.String{
stringvalidator.ExactlyOneOf(
path.MatchRoot("content"),
path.MatchRoot("content_base64"),
path.MatchRoot("source")),
},
},
"content_md5": schema.StringAttribute{
Description: "MD5 checksum of file content.",
Computed: true,
},
"content_sha1": schema.StringAttribute{
Description: "SHA1 checksum of file content.",
Computed: true,
},
"content_sha256": schema.StringAttribute{
Description: "SHA256 checksum of file content.",
Computed: true,
},
"content_base64sha256": schema.StringAttribute{
Description: "Base64 encoded SHA256 checksum of file content.",
Computed: true,
},
"content_sha512": schema.StringAttribute{
Description: "SHA512 checksum of file content.",
Computed: true,
},
"content_base64sha512": schema.StringAttribute{
Description: "Base64 encoded SHA512 checksum of file content.",
Computed: true,
},
},
}
}
func (n *localFileResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_file"
}
func (n *localFileResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
var plan localFileResourceModelV0
var filePerm, dirPerm string
diags := req.Plan.Get(ctx, &plan)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
content, err := parseLocalFileContent(plan)
if err != nil {
resp.Diagnostics.AddError(
"Create local file error",
"An unexpected error occurred while parsing local file content\n\n+"+
fmt.Sprintf("Original Error: %s", err),
)
return
}
destination := plan.Filename.ValueString()
destinationDir := filepath.Dir(destination)
if _, err := os.Stat(destinationDir); err != nil {
dirPerm = plan.DirectoryPermission.ValueString()
dirMode, _ := strconv.ParseInt(dirPerm, 8, 64)
if err := os.MkdirAll(destinationDir, os.FileMode(dirMode)); err != nil {
resp.Diagnostics.AddError(
"Create local file error",
"An unexpected error occurred while creating file directory\n\n+"+
fmt.Sprintf("Original Error: %s", err),
)
return
}
}
filePerm = plan.FilePermission.ValueString()
fileMode, _ := strconv.ParseInt(filePerm, 8, 64)
if err := os.WriteFile(destination, content, os.FileMode(fileMode)); err != nil {
resp.Diagnostics.AddError(
"Create local file error",
"An unexpected error occurred while writing the file\n\n+"+
fmt.Sprintf("Original Error: %s", err),
)
return
}
checksums := genFileChecksums(content)
plan.ContentMd5 = types.StringValue(checksums.md5Hex)
plan.ContentSha1 = types.StringValue(checksums.sha1Hex)
plan.ContentSha256 = types.StringValue(checksums.sha256Hex)
plan.ContentBase64sha256 = types.StringValue(checksums.sha256Base64)
plan.ContentSha512 = types.StringValue(checksums.sha512Hex)
plan.ContentBase64sha512 = types.StringValue(checksums.sha512Base64)
plan.ID = types.StringValue(checksums.sha1Hex)
diags = resp.State.Set(ctx, &plan)
resp.Diagnostics.Append(diags...)
}
func (n *localFileResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
var state localFileResourceModelV0
diags := req.State.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
// If the output file doesn't exist, mark the resource for creation.
outputPath := state.Filename.ValueString()
if _, err := os.Stat(outputPath); os.IsNotExist(err) {
resp.State.RemoveResource(ctx)
return
}
// Verify that the content of the destination file matches the content we
// expect. Otherwise, the file might have been modified externally, and we
// must reconcile.
outputContent, err := os.ReadFile(outputPath)
if err != nil {
resp.Diagnostics.AddError(
"Read local file error",
"An unexpected error occurred while reading the file\n\n+"+
fmt.Sprintf("Original Error: %s", err),
)
return
}
outputChecksum := sha1.Sum(outputContent)
if hex.EncodeToString(outputChecksum[:]) != state.ID.ValueString() {
resp.State.RemoveResource(ctx)
return
}
}
func (n *localFileResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
var plan localFileResourceModelV0
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
if resp.Diagnostics.HasError() {
return
}
resp.Diagnostics.Append(resp.State.Set(ctx, &plan)...)
}
func (n *localFileResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
var filename string
req.State.GetAttribute(ctx, path.Root("filename"), &filename)
os.Remove(filename)
}
func parseLocalFileContent(plan localFileResourceModelV0) ([]byte, error) {
if !plan.SensitiveContent.IsNull() && !plan.SensitiveContent.IsUnknown() {
return []byte(plan.SensitiveContent.ValueString()), nil
}
if !plan.ContentBase64.IsNull() && !plan.ContentBase64.IsUnknown() {
return base64.StdEncoding.DecodeString(plan.ContentBase64.ValueString())
}
if !plan.Source.IsNull() && !plan.Source.IsUnknown() {
sourceFileContent := plan.Source.ValueString()
return os.ReadFile(sourceFileContent)
}
content := plan.Content.ValueString()
return []byte(content), nil
}
type localFileResourceModelV0 struct {
Filename types.String `tfsdk:"filename"`
Content types.String `tfsdk:"content"`
ContentBase64 types.String `tfsdk:"content_base64"`
Source types.String `tfsdk:"source"`
FilePermission types.String `tfsdk:"file_permission"`
DirectoryPermission types.String `tfsdk:"directory_permission"`
ID types.String `tfsdk:"id"`
SensitiveContent types.String `tfsdk:"sensitive_content"`
ContentMd5 types.String `tfsdk:"content_md5"`
ContentSha1 types.String `tfsdk:"content_sha1"`
ContentSha256 types.String `tfsdk:"content_sha256"`
ContentBase64sha256 types.String `tfsdk:"content_base64sha256"`
ContentSha512 types.String `tfsdk:"content_sha512"`
ContentBase64sha512 types.String `tfsdk:"content_base64sha512"`
}