Skip to content

Commit a0c5d42

Browse files
committed
provider/random: Separate read from create
We now generate the read operation which sets the various encodings of the random value such that adding new ones does not require generating a new random value. We also verify that these are set correctly via the acceptance tests.
1 parent 5ba1c4e commit a0c5d42

2 files changed

Lines changed: 26 additions & 8 deletions

File tree

builtin/providers/random/resource_id.go

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
func resourceId() *schema.Resource {
1515
return &schema.Resource{
1616
Create: CreateID,
17-
Read: schema.Noop,
17+
Read: RepopulateEncodings,
1818
Delete: schema.RemoveFromState,
1919

2020
Schema: map[string]*schema.Schema{
@@ -59,8 +59,7 @@ func resourceId() *schema.Resource {
5959
}
6060
}
6161

62-
func CreateID(d *schema.ResourceData, _ interface{}) error {
63-
62+
func CreateID(d *schema.ResourceData, meta interface{}) error {
6463
byteLength := d.Get("byte_length").(int)
6564
bytes := make([]byte, byteLength)
6665

@@ -73,17 +72,28 @@ func CreateID(d *schema.ResourceData, _ interface{}) error {
7372
}
7473

7574
b64Str := base64.RawURLEncoding.EncodeToString(bytes)
75+
d.SetId(b64Str)
76+
77+
return RepopulateEncodings(d, meta)
78+
}
79+
80+
func RepopulateEncodings(d *schema.ResourceData, _ interface{}) error {
81+
base64Str := d.Id()
82+
83+
bytes, err := base64.RawURLEncoding.DecodeString(base64Str)
84+
if err != nil {
85+
return errwrap.Wrapf("Error decoding ID: {{err}}", err)
86+
}
87+
7688
b64StdStr := base64.StdEncoding.EncodeToString(bytes)
7789
hexStr := hex.EncodeToString(bytes)
7890

7991
bigInt := big.Int{}
8092
bigInt.SetBytes(bytes)
8193
decStr := bigInt.String()
8294

83-
d.SetId(b64Str)
84-
85-
d.Set("b64", b64Str)
86-
d.Set("b64_url", b64Str)
95+
d.Set("b64", base64Str)
96+
d.Set("b64_url", base64Str)
8797
d.Set("b64_std", b64StdStr)
8898

8999
d.Set("hex", hexStr)

builtin/providers/random/resource_id_test.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ func TestAccResourceID(t *testing.T) {
1313
PreCheck: func() { testAccPreCheck(t) },
1414
Providers: testAccProviders,
1515
Steps: []resource.TestStep{
16-
resource.TestStep{
16+
{
1717
Config: testAccResourceIDConfig,
1818
Check: resource.ComposeTestCheckFunc(
1919
testAccResourceIDCheck("random_id.foo"),
@@ -34,12 +34,20 @@ func testAccResourceIDCheck(id string) resource.TestCheckFunc {
3434
}
3535

3636
b64Str := rs.Primary.Attributes["b64"]
37+
b64UrlStr := rs.Primary.Attributes["b64_url"]
38+
b64StdStr := rs.Primary.Attributes["b64_std"]
3739
hexStr := rs.Primary.Attributes["hex"]
3840
decStr := rs.Primary.Attributes["dec"]
3941

4042
if got, want := len(b64Str), 6; got != want {
4143
return fmt.Errorf("base64 string length is %d; want %d", got, want)
4244
}
45+
if got, want := len(b64UrlStr), 6; got != want {
46+
return fmt.Errorf("base64 URL string length is %d; want %d", got, want)
47+
}
48+
if got, want := len(b64StdStr), 8; got != want {
49+
return fmt.Errorf("base64 STD string length is %d; want %d", got, want)
50+
}
4351
if got, want := len(hexStr), 8; got != want {
4452
return fmt.Errorf("hex string length is %d; want %d", got, want)
4553
}

0 commit comments

Comments
 (0)