From a0c5d42fa41c4c3d38f74d493fa7922028aa267f Mon Sep 17 00:00:00 2001 From: James Nugent Date: Sun, 6 Nov 2016 16:57:15 -0600 Subject: [PATCH] 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. --- builtin/providers/random/resource_id.go | 24 ++++++++++++++------ builtin/providers/random/resource_id_test.go | 10 +++++++- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/builtin/providers/random/resource_id.go b/builtin/providers/random/resource_id.go index 0521540ed..56baca9f5 100644 --- a/builtin/providers/random/resource_id.go +++ b/builtin/providers/random/resource_id.go @@ -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{ @@ -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) @@ -73,6 +72,19 @@ 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) @@ -80,10 +92,8 @@ func CreateID(d *schema.ResourceData, _ interface{}) error { 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) diff --git a/builtin/providers/random/resource_id_test.go b/builtin/providers/random/resource_id_test.go index ed6b8af8d..f3d5870b8 100644 --- a/builtin/providers/random/resource_id_test.go +++ b/builtin/providers/random/resource_id_test.go @@ -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"), @@ -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) }