Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/resources/id.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ resource "aws_instance" "server" {
### Read-Only

- `b64_std` (String) The generated id presented in base64 without additional transformations.
- `b64_url` (String) The generated id presented in base64, using the URL-friendly character set: case-sensitive letters, digits and the characters `_` and `-`.
- `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 `-`.
- `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 `-`.
- `dec` (String) The generated id presented in non-padded decimal digits.
- `hex` (String) The generated id presented in padded hexadecimal digits. This result will always be twice as long as the requested byte length.
- `id` (String) The generated id presented in base64 without additional transformations or prefix.
Expand Down
13 changes: 13 additions & 0 deletions internal/provider/resource_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,14 @@ exist concurrently.
stringplanmodifier.UseStateForUnknown(),
},
},
"b64_url_pad": schema.StringAttribute{
Description: "The generated id presented in padded base64, using the URL-friendly character set: " +
"case-sensitive letters, digits, padding with `=` 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,
Expand Down Expand Up @@ -141,6 +149,7 @@ func (r *idResource) Create(ctx context.Context, req resource.CreateRequest, res
}

id := base64.RawURLEncoding.EncodeToString(bytes)
b64urlWithPading := base64.URLEncoding.EncodeToString(bytes)
prefix := plan.Prefix.ValueString()
b64Std := base64.StdEncoding.EncodeToString(bytes)
hexStr := hex.EncodeToString(bytes)
Expand All @@ -155,6 +164,7 @@ func (r *idResource) Create(ctx context.Context, req resource.CreateRequest, res
ByteLength: types.Int64Value(plan.ByteLength.ValueInt64()),
Prefix: plan.Prefix,
B64URL: types.StringValue(prefix + id),
B64URLPad: types.StringValue(prefix + b64urlWithPading),
B64Std: types.StringValue(prefix + b64Std),
Hex: types.StringValue(prefix + hexStr),
Dec: types.StringValue(prefix + dec),
Expand Down Expand Up @@ -210,6 +220,7 @@ func (r *idResource) ImportState(ctx context.Context, req resource.ImportStateRe
return
}

b64urlWithPading := base64.URLEncoding.EncodeToString(bytes)
b64Std := base64.StdEncoding.EncodeToString(bytes)
hexStr := hex.EncodeToString(bytes)

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

Expand All @@ -247,6 +259,7 @@ type idModelV0 struct {
ByteLength types.Int64 `tfsdk:"byte_length"`
Prefix types.String `tfsdk:"prefix"`
B64URL types.String `tfsdk:"b64_url"`
B64URLPad types.String `tfsdk:"b64_url_pad"`
B64Std types.String `tfsdk:"b64_std"`
Hex types.String `tfsdk:"hex"`
Dec types.String `tfsdk:"dec"`
Expand Down
4 changes: 4 additions & 0 deletions internal/provider/resource_id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func TestAccResourceID(t *testing.T) {
}`,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrWith("random_id.foo", "b64_url", testCheckLen(6)),
resource.TestCheckResourceAttrWith("random_id.foo", "b64_url_pad", testCheckLen(8)),
resource.TestCheckResourceAttrWith("random_id.foo", "b64_std", testCheckLen(8)),
resource.TestCheckResourceAttrWith("random_id.foo", "hex", testCheckLen(8)),
resource.TestCheckResourceAttrWith("random_id.foo", "dec", testCheckMinLen(1)),
Expand All @@ -41,6 +42,7 @@ func TestAccResourceID_ImportWithPrefix(t *testing.T) {
}`,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrWith("random_id.bar", "b64_url", testCheckLen(12)),
resource.TestCheckResourceAttrWith("random_id.bar", "b64_url_pad", testCheckLen(14)),
resource.TestCheckResourceAttrWith("random_id.bar", "b64_std", testCheckLen(14)),
resource.TestCheckResourceAttrWith("random_id.bar", "hex", testCheckLen(14)),
resource.TestCheckResourceAttrWith("random_id.bar", "dec", testCheckMinLen(1)),
Expand All @@ -67,6 +69,7 @@ func TestAccResourceID_UpgradeFromVersion3_3_2(t *testing.T) {
}`,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrWith("random_id.bar", "b64_url", testCheckLen(12)),
resource.TestCheckResourceAttrWith("random_id.bar", "b64_url_pad", testCheckLen(14)),
resource.TestCheckResourceAttrWith("random_id.bar", "b64_std", testCheckLen(14)),
resource.TestCheckResourceAttrWith("random_id.bar", "hex", testCheckLen(14)),
resource.TestCheckResourceAttrWith("random_id.bar", "dec", testCheckMinLen(1)),
Expand All @@ -88,6 +91,7 @@ func TestAccResourceID_UpgradeFromVersion3_3_2(t *testing.T) {
}`,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrWith("random_id.bar", "b64_url", testCheckLen(12)),
resource.TestCheckResourceAttrWith("random_id.bar", "b64_url_pad", testCheckLen(14)),
resource.TestCheckResourceAttrWith("random_id.bar", "b64_std", testCheckLen(14)),
resource.TestCheckResourceAttrWith("random_id.bar", "hex", testCheckLen(14)),
resource.TestCheckResourceAttrWith("random_id.bar", "dec", testCheckMinLen(1)),
Expand Down