-
Notifications
You must be signed in to change notification settings - Fork 114
/
resource_id.go
256 lines (221 loc) · 8.12 KB
/
resource_id.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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package provider
import (
"context"
"crypto/rand"
"encoding/base64"
"encoding/hex"
"fmt"
"math/big"
"strings"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/int64planmodifier"
"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"
"github.com/terraform-providers/terraform-provider-random/internal/diagnostics"
mapplanmodifiers "github.com/terraform-providers/terraform-provider-random/internal/planmodifiers/map"
)
var (
_ resource.Resource = (*idResource)(nil)
_ resource.ResourceWithImportState = (*idResource)(nil)
)
func NewIdResource() resource.Resource {
return &idResource{}
}
type idResource struct{}
func (r *idResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_id"
}
func (r *idResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
Description: `
The resource ` + "`random_id`" + ` generates random numbers that are intended to be
used as unique identifiers for other resources.
This resource *does* use a cryptographic random number generator in order
to minimize the chance of collisions, making the results of this resource
when a 16-byte identifier is requested of equivalent uniqueness to a
type-4 UUID.
This resource can be used in conjunction with resources that have
the ` + "`create_before_destroy`" + ` lifecycle flag set to avoid conflicts with
unique names during the brief period where both the old and new resources
exist concurrently.
`,
Attributes: map[string]schema.Attribute{
"keepers": schema.MapAttribute{
Description: "Arbitrary map of values that, when changed, will trigger recreation of " +
"resource. See [the main provider documentation](../index.html) for more information.",
ElementType: types.StringType,
Optional: true,
PlanModifiers: []planmodifier.Map{
mapplanmodifiers.RequiresReplaceIfValuesNotNull(),
},
},
"byte_length": schema.Int64Attribute{
Description: "The number of random bytes to produce. The minimum value is 1, which produces " +
"eight bits of randomness.",
Required: true,
PlanModifiers: []planmodifier.Int64{
int64planmodifier.RequiresReplace(),
},
},
"prefix": schema.StringAttribute{
Description: "Arbitrary string to prefix the output value with. This string is supplied as-is, " +
"meaning it is not guaranteed to be URL-safe or base64 encoded.",
Optional: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
},
"b64_url": schema.StringAttribute{
Description: "The generated id presented in base64, using the URL-friendly character set: " +
"case-sensitive letters, digits and the characters `_` and `-`.",
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
"b64_std": schema.StringAttribute{
Description: "The generated id presented in base64 without additional transformations.",
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
"hex": schema.StringAttribute{
Description: "The generated id presented in padded hexadecimal digits. This result will " +
"always be twice as long as the requested byte length.",
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
"dec": schema.StringAttribute{
Description: "The generated id presented in non-padded decimal digits.",
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
"id": schema.StringAttribute{
Description: "The generated id presented in base64 without additional transformations or prefix.",
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
},
}
}
func (r *idResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
var plan idModelV0
diags := req.Plan.Get(ctx, &plan)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
byteLength := plan.ByteLength.ValueInt64()
bytes := make([]byte, byteLength)
n, err := rand.Reader.Read(bytes)
if int64(n) != byteLength {
resp.Diagnostics.Append(diagnostics.RandomnessGenerationError(err.Error())...)
return
}
if err != nil {
resp.Diagnostics.Append(diagnostics.RandomReadError(err.Error())...)
return
}
id := base64.RawURLEncoding.EncodeToString(bytes)
prefix := plan.Prefix.ValueString()
b64Std := base64.StdEncoding.EncodeToString(bytes)
hexStr := hex.EncodeToString(bytes)
bigInt := big.Int{}
bigInt.SetBytes(bytes)
dec := bigInt.String()
i := idModelV0{
ID: types.StringValue(id),
Keepers: plan.Keepers,
ByteLength: types.Int64Value(plan.ByteLength.ValueInt64()),
Prefix: plan.Prefix,
B64URL: types.StringValue(prefix + id),
B64Std: types.StringValue(prefix + b64Std),
Hex: types.StringValue(prefix + hexStr),
Dec: types.StringValue(prefix + dec),
}
diags = resp.State.Set(ctx, i)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}
// Read does not need to perform any operations as the state in ReadResourceResponse is already populated.
func (r *idResource) Read(context.Context, resource.ReadRequest, *resource.ReadResponse) {
}
// Update ensures the plan value is copied to the state to complete the update.
func (r *idResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
var model idModelV0
resp.Diagnostics.Append(req.Plan.Get(ctx, &model)...)
if resp.Diagnostics.HasError() {
return
}
resp.Diagnostics.Append(resp.State.Set(ctx, &model)...)
}
// Delete does not need to explicitly call resp.State.RemoveResource() as this is automatically handled by the
// [framework](https://github.com/hashicorp/terraform-plugin-framework/pull/301).
func (r *idResource) Delete(context.Context, resource.DeleteRequest, *resource.DeleteResponse) {
}
func (r *idResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
id := req.ID
var prefix string
sep := strings.LastIndex(id, ",")
if sep != -1 {
prefix = id[:sep]
id = id[sep+1:]
}
bytes, err := base64.RawURLEncoding.DecodeString(id)
if err != nil {
resp.Diagnostics.AddError(
"Import Random ID Error",
"While attempting to import a random id there was a decoding error.\n\n+"+
diagnostics.RetryMsg+
fmt.Sprintf("Original Error: %s", err),
)
return
}
b64Std := base64.StdEncoding.EncodeToString(bytes)
hexStr := hex.EncodeToString(bytes)
bigInt := big.Int{}
bigInt.SetBytes(bytes)
dec := bigInt.String()
var state idModelV0
state.ID = types.StringValue(id)
state.ByteLength = types.Int64Value(int64(len(bytes)))
// Using types.MapValueMust to ensure map is known.
state.Keepers = types.MapValueMust(types.StringType, nil)
state.B64Std = types.StringValue(prefix + b64Std)
state.B64URL = types.StringValue(prefix + id)
state.Hex = types.StringValue(prefix + hexStr)
state.Dec = types.StringValue(prefix + dec)
if prefix == "" {
state.Prefix = types.StringNull()
} else {
state.Prefix = types.StringValue(prefix)
}
diags := resp.State.Set(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}
type idModelV0 struct {
ID types.String `tfsdk:"id"`
Keepers types.Map `tfsdk:"keepers"`
ByteLength types.Int64 `tfsdk:"byte_length"`
Prefix types.String `tfsdk:"prefix"`
B64URL types.String `tfsdk:"b64_url"`
B64Std types.String `tfsdk:"b64_std"`
Hex types.String `tfsdk:"hex"`
Dec types.String `tfsdk:"dec"`
}