diff --git a/docs/resources/id.md b/docs/resources/id.md index 4beee0a5..5a79c83c 100644 --- a/docs/resources/id.md +++ b/docs/resources/id.md @@ -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. diff --git a/internal/provider/resource_id.go b/internal/provider/resource_id.go index 4613fc91..641969f0 100644 --- a/internal/provider/resource_id.go +++ b/internal/provider/resource_id.go @@ -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, @@ -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) @@ -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), @@ -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) @@ -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) @@ -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"` diff --git a/internal/provider/resource_id_test.go b/internal/provider/resource_id_test.go index 46735ef0..4da5889f 100644 --- a/internal/provider/resource_id_test.go +++ b/internal/provider/resource_id_test.go @@ -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)), @@ -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)), @@ -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)), @@ -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)),