Skip to content

Commit b9a7ec5

Browse files
committed
add b64_url with padding
1 parent cab8157 commit b9a7ec5

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

docs/resources/id.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ resource "aws_instance" "server" {
7373
### Read-Only
7474

7575
- `b64_std` (String) The generated id presented in base64 without additional transformations.
76-
- `b64_url` (String) The generated id presented in base64, using the URL-friendly character set: case-sensitive letters, digits and the characters `_` and `-`.
76+
- `b64_url` (String) The generated id presented in non-padded base64, using the URL-friendly character set: case-sensitive letters, digits and the characters `_` and `-`.
77+
- `b64_url_pad` (String) The generated id presented in padded base64, using the URL-friendly character set: case-sensitive letters, digits, padding with `=` and the characters `_` and `-`.
7778
- `dec` (String) The generated id presented in non-padded decimal digits.
7879
- `hex` (String) The generated id presented in padded hexadecimal digits. This result will always be twice as long as the requested byte length.
7980
- `id` (String) The generated id presented in base64 without additional transformations or prefix.

internal/provider/resource_id.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,14 @@ exist concurrently.
8585
stringplanmodifier.UseStateForUnknown(),
8686
},
8787
},
88+
"b64_url_pad": schema.StringAttribute{
89+
Description: "The generated id presented in padded base64, using the URL-friendly character set: " +
90+
"case-sensitive letters, digits, padding with `=` and the characters `_` and `-`.",
91+
Computed: true,
92+
PlanModifiers: []planmodifier.String{
93+
stringplanmodifier.UseStateForUnknown(),
94+
},
95+
},
8896
"b64_std": schema.StringAttribute{
8997
Description: "The generated id presented in base64 without additional transformations.",
9098
Computed: true,
@@ -141,6 +149,7 @@ func (r *idResource) Create(ctx context.Context, req resource.CreateRequest, res
141149
}
142150

143151
id := base64.RawURLEncoding.EncodeToString(bytes)
152+
b64urlWithPading := base64.URLEncoding.EncodeToString(bytes)
144153
prefix := plan.Prefix.ValueString()
145154
b64Std := base64.StdEncoding.EncodeToString(bytes)
146155
hexStr := hex.EncodeToString(bytes)
@@ -155,6 +164,7 @@ func (r *idResource) Create(ctx context.Context, req resource.CreateRequest, res
155164
ByteLength: types.Int64Value(plan.ByteLength.ValueInt64()),
156165
Prefix: plan.Prefix,
157166
B64URL: types.StringValue(prefix + id),
167+
B64URLPad: types.StringValue(prefix + b64urlWithPading),
158168
B64Std: types.StringValue(prefix + b64Std),
159169
Hex: types.StringValue(prefix + hexStr),
160170
Dec: types.StringValue(prefix + dec),
@@ -210,6 +220,7 @@ func (r *idResource) ImportState(ctx context.Context, req resource.ImportStateRe
210220
return
211221
}
212222

223+
b64urlWithPading := base64.URLEncoding.EncodeToString(bytes)
213224
b64Std := base64.StdEncoding.EncodeToString(bytes)
214225
hexStr := hex.EncodeToString(bytes)
215226

@@ -225,6 +236,7 @@ func (r *idResource) ImportState(ctx context.Context, req resource.ImportStateRe
225236
state.Keepers = types.MapValueMust(types.StringType, nil)
226237
state.B64Std = types.StringValue(prefix + b64Std)
227238
state.B64URL = types.StringValue(prefix + id)
239+
state.B64URLPad = types.StringValue(prefix + b64urlWithPading)
228240
state.Hex = types.StringValue(prefix + hexStr)
229241
state.Dec = types.StringValue(prefix + dec)
230242

@@ -247,6 +259,7 @@ type idModelV0 struct {
247259
ByteLength types.Int64 `tfsdk:"byte_length"`
248260
Prefix types.String `tfsdk:"prefix"`
249261
B64URL types.String `tfsdk:"b64_url"`
262+
B64URLPad types.String `tfsdk:"b64_url_pad"`
250263
B64Std types.String `tfsdk:"b64_std"`
251264
Hex types.String `tfsdk:"hex"`
252265
Dec types.String `tfsdk:"dec"`

internal/provider/resource_id_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ func TestAccResourceID(t *testing.T) {
1616
}`,
1717
Check: resource.ComposeTestCheckFunc(
1818
resource.TestCheckResourceAttrWith("random_id.foo", "b64_url", testCheckLen(6)),
19+
resource.TestCheckResourceAttrWith("random_id.foo", "b64_url_pad", testCheckLen(8)),
1920
resource.TestCheckResourceAttrWith("random_id.foo", "b64_std", testCheckLen(8)),
2021
resource.TestCheckResourceAttrWith("random_id.foo", "hex", testCheckLen(8)),
2122
resource.TestCheckResourceAttrWith("random_id.foo", "dec", testCheckMinLen(1)),
@@ -41,6 +42,7 @@ func TestAccResourceID_ImportWithPrefix(t *testing.T) {
4142
}`,
4243
Check: resource.ComposeTestCheckFunc(
4344
resource.TestCheckResourceAttrWith("random_id.bar", "b64_url", testCheckLen(12)),
45+
resource.TestCheckResourceAttrWith("random_id.bar", "b64_url_pad", testCheckLen(14)),
4446
resource.TestCheckResourceAttrWith("random_id.bar", "b64_std", testCheckLen(14)),
4547
resource.TestCheckResourceAttrWith("random_id.bar", "hex", testCheckLen(14)),
4648
resource.TestCheckResourceAttrWith("random_id.bar", "dec", testCheckMinLen(1)),
@@ -67,6 +69,7 @@ func TestAccResourceID_UpgradeFromVersion3_3_2(t *testing.T) {
6769
}`,
6870
Check: resource.ComposeTestCheckFunc(
6971
resource.TestCheckResourceAttrWith("random_id.bar", "b64_url", testCheckLen(12)),
72+
resource.TestCheckResourceAttrWith("random_id.bar", "b64_url_pad", testCheckLen(14)),
7073
resource.TestCheckResourceAttrWith("random_id.bar", "b64_std", testCheckLen(14)),
7174
resource.TestCheckResourceAttrWith("random_id.bar", "hex", testCheckLen(14)),
7275
resource.TestCheckResourceAttrWith("random_id.bar", "dec", testCheckMinLen(1)),
@@ -88,6 +91,7 @@ func TestAccResourceID_UpgradeFromVersion3_3_2(t *testing.T) {
8891
}`,
8992
Check: resource.ComposeTestCheckFunc(
9093
resource.TestCheckResourceAttrWith("random_id.bar", "b64_url", testCheckLen(12)),
94+
resource.TestCheckResourceAttrWith("random_id.bar", "b64_url_pad", testCheckLen(14)),
9195
resource.TestCheckResourceAttrWith("random_id.bar", "b64_std", testCheckLen(14)),
9296
resource.TestCheckResourceAttrWith("random_id.bar", "hex", testCheckLen(14)),
9397
resource.TestCheckResourceAttrWith("random_id.bar", "dec", testCheckMinLen(1)),

0 commit comments

Comments
 (0)