Skip to content

Commit

Permalink
provider/random: Separate read from create
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jen20 committed Nov 6, 2016
1 parent 5ba1c4e commit a0c5d42
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
24 changes: 17 additions & 7 deletions builtin/providers/random/resource_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
func resourceId() *schema.Resource {
return &schema.Resource{
Create: CreateID,
Read: schema.Noop,
Read: RepopulateEncodings,
Delete: schema.RemoveFromState,

Schema: map[string]*schema.Schema{
Expand Down Expand Up @@ -59,8 +59,7 @@ func resourceId() *schema.Resource {
}
}

func CreateID(d *schema.ResourceData, _ interface{}) error {

func CreateID(d *schema.ResourceData, meta interface{}) error {
byteLength := d.Get("byte_length").(int)
bytes := make([]byte, byteLength)

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

b64Str := base64.RawURLEncoding.EncodeToString(bytes)
d.SetId(b64Str)

return RepopulateEncodings(d, meta)
}

func RepopulateEncodings(d *schema.ResourceData, _ interface{}) error {
base64Str := d.Id()

bytes, err := base64.RawURLEncoding.DecodeString(base64Str)
if err != nil {
return errwrap.Wrapf("Error decoding ID: {{err}}", err)
}

b64StdStr := base64.StdEncoding.EncodeToString(bytes)
hexStr := hex.EncodeToString(bytes)

bigInt := big.Int{}
bigInt.SetBytes(bytes)
decStr := bigInt.String()

d.SetId(b64Str)

d.Set("b64", b64Str)
d.Set("b64_url", b64Str)
d.Set("b64", base64Str)
d.Set("b64_url", base64Str)
d.Set("b64_std", b64StdStr)

d.Set("hex", hexStr)
Expand Down
10 changes: 9 additions & 1 deletion builtin/providers/random/resource_id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func TestAccResourceID(t *testing.T) {
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
resource.TestStep{
{
Config: testAccResourceIDConfig,
Check: resource.ComposeTestCheckFunc(
testAccResourceIDCheck("random_id.foo"),
Expand All @@ -34,12 +34,20 @@ func testAccResourceIDCheck(id string) resource.TestCheckFunc {
}

b64Str := rs.Primary.Attributes["b64"]
b64UrlStr := rs.Primary.Attributes["b64_url"]
b64StdStr := rs.Primary.Attributes["b64_std"]
hexStr := rs.Primary.Attributes["hex"]
decStr := rs.Primary.Attributes["dec"]

if got, want := len(b64Str), 6; got != want {
return fmt.Errorf("base64 string length is %d; want %d", got, want)
}
if got, want := len(b64UrlStr), 6; got != want {
return fmt.Errorf("base64 URL string length is %d; want %d", got, want)
}
if got, want := len(b64StdStr), 8; got != want {
return fmt.Errorf("base64 STD string length is %d; want %d", got, want)
}
if got, want := len(hexStr), 8; got != want {
return fmt.Errorf("hex string length is %d; want %d", got, want)
}
Expand Down

0 comments on commit a0c5d42

Please sign in to comment.