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.
This commit is contained in:
parent
5ba1c4ed2c
commit
a0c5d42fa4
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue