Merge pull request #12016 from hashicorp/sethvargo/prefix_random_id

Add a prefix option to random_id provider
This commit is contained in:
Jake Champlin 2017-02-16 11:13:20 -05:00 committed by GitHub
commit 76b41e6efe
3 changed files with 49 additions and 13 deletions

View File

@ -30,6 +30,12 @@ func resourceId() *schema.Resource {
ForceNew: true, ForceNew: true,
}, },
"prefix": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"b64": { "b64": {
Type: schema.TypeString, Type: schema.TypeString,
Computed: true, Computed: true,
@ -78,6 +84,7 @@ func CreateID(d *schema.ResourceData, meta interface{}) error {
} }
func RepopulateEncodings(d *schema.ResourceData, _ interface{}) error { func RepopulateEncodings(d *schema.ResourceData, _ interface{}) error {
prefix := d.Get("prefix").(string)
base64Str := d.Id() base64Str := d.Id()
bytes, err := base64.RawURLEncoding.DecodeString(base64Str) bytes, err := base64.RawURLEncoding.DecodeString(base64Str)
@ -92,12 +99,12 @@ func RepopulateEncodings(d *schema.ResourceData, _ interface{}) error {
bigInt.SetBytes(bytes) bigInt.SetBytes(bytes)
decStr := bigInt.String() decStr := bigInt.String()
d.Set("b64", base64Str) d.Set("b64", prefix+base64Str)
d.Set("b64_url", base64Str) d.Set("b64_url", prefix+base64Str)
d.Set("b64_std", b64StdStr) d.Set("b64_std", prefix+b64StdStr)
d.Set("hex", hexStr) d.Set("hex", prefix+hexStr)
d.Set("dec", decStr) d.Set("dec", prefix+decStr)
return nil return nil
} }

View File

@ -8,6 +8,13 @@ import (
"github.com/hashicorp/terraform/terraform" "github.com/hashicorp/terraform/terraform"
) )
type idLens struct {
b64Len int
b64UrlLen int
b64StdLen int
hexLen int
}
func TestAccResourceID(t *testing.T) { func TestAccResourceID(t *testing.T) {
resource.Test(t, resource.TestCase{ resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) }, PreCheck: func() { testAccPreCheck(t) },
@ -16,14 +23,25 @@ func TestAccResourceID(t *testing.T) {
{ {
Config: testAccResourceIDConfig, Config: testAccResourceIDConfig,
Check: resource.ComposeTestCheckFunc( Check: resource.ComposeTestCheckFunc(
testAccResourceIDCheck("random_id.foo"), testAccResourceIDCheck("random_id.foo", &idLens{
b64Len: 6,
b64UrlLen: 6,
b64StdLen: 8,
hexLen: 8,
}),
testAccResourceIDCheck("random_id.bar", &idLens{
b64Len: 12,
b64UrlLen: 12,
b64StdLen: 14,
hexLen: 14,
}),
), ),
}, },
}, },
}) })
} }
func testAccResourceIDCheck(id string) resource.TestCheckFunc { func testAccResourceIDCheck(id string, want *idLens) resource.TestCheckFunc {
return func(s *terraform.State) error { return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[id] rs, ok := s.RootModule().Resources[id]
if !ok { if !ok {
@ -39,16 +57,16 @@ func testAccResourceIDCheck(id string) resource.TestCheckFunc {
hexStr := rs.Primary.Attributes["hex"] hexStr := rs.Primary.Attributes["hex"]
decStr := rs.Primary.Attributes["dec"] decStr := rs.Primary.Attributes["dec"]
if got, want := len(b64Str), 6; got != want { if got, want := len(b64Str), want.b64Len; got != want {
return fmt.Errorf("base64 string length is %d; want %d", got, want) return fmt.Errorf("base64 string length is %d; want %d", got, want)
} }
if got, want := len(b64UrlStr), 6; got != want { if got, want := len(b64UrlStr), want.b64UrlLen; got != want {
return fmt.Errorf("base64 URL string length is %d; want %d", got, want) return fmt.Errorf("base64 URL string length is %d; want %d", got, want)
} }
if got, want := len(b64StdStr), 8; got != want { if got, want := len(b64StdStr), want.b64StdLen; got != want {
return fmt.Errorf("base64 STD string length is %d; want %d", got, want) return fmt.Errorf("base64 STD string length is %d; want %d", got, want)
} }
if got, want := len(hexStr), 8; got != want { if got, want := len(hexStr), want.hexLen; got != want {
return fmt.Errorf("hex string length is %d; want %d", got, want) return fmt.Errorf("hex string length is %d; want %d", got, want)
} }
if len(decStr) < 1 { if len(decStr) < 1 {
@ -59,8 +77,15 @@ func testAccResourceIDCheck(id string) resource.TestCheckFunc {
} }
} }
const testAccResourceIDConfig = ` const (
testAccResourceIDConfig = `
resource "random_id" "foo" { resource "random_id" "foo" {
byte_length = 4 byte_length = 4
} }
resource "random_id" "bar" {
byte_length = 4
prefix = "cloud-"
}
` `
)

View File

@ -60,6 +60,10 @@ The following arguments are supported:
trigger a new id to be generated. See trigger a new id to be generated. See
[the main provider documentation](../index.html) for more information. [the main provider documentation](../index.html) for more information.
* `prefix` - (Optional) Arbitrary string to prefix the output value with. This
string is supplied as-is, meaning it is not guaranteed to be URL-safe or
base64 encoded.
## Attributes Reference ## Attributes Reference
The following attributes are exported: The following attributes are exported: