From 47528ac93a0c2440f865a0b18b6ee0cb3e6698c9 Mon Sep 17 00:00:00 2001 From: Tom Straub Date: Sat, 30 Jun 2018 13:08:57 -0500 Subject: [PATCH 1/2] New Resource random_uuid --- random/provider.go | 1 + random/resource_uuid.go | 62 ++++++++++++++++++++++++++++++++ random/resource_uuid_test.go | 68 ++++++++++++++++++++++++++++++++++++ website/docs/r/uuid.html.md | 53 ++++++++++++++++++++++++++++ 4 files changed, 184 insertions(+) create mode 100644 random/resource_uuid.go create mode 100644 random/resource_uuid_test.go create mode 100644 website/docs/r/uuid.html.md diff --git a/random/provider.go b/random/provider.go index 5b1f667d..cbbe8cec 100644 --- a/random/provider.go +++ b/random/provider.go @@ -16,6 +16,7 @@ func Provider() terraform.ResourceProvider { "random_pet": resourcePet(), "random_string": resourceString(), "random_integer": resourceInteger(), + "random_uuid": resourceUuid(), }, } } diff --git a/random/resource_uuid.go b/random/resource_uuid.go new file mode 100644 index 00000000..e199b1a2 --- /dev/null +++ b/random/resource_uuid.go @@ -0,0 +1,62 @@ +package random + +import ( + "github.com/hashicorp/errwrap" + "github.com/hashicorp/go-uuid" + "github.com/hashicorp/terraform/helper/schema" +) + +func resourceUuid() *schema.Resource { + return &schema.Resource{ + Create: CreateUuid, + Read: RepopulateUuid, + Delete: schema.RemoveFromState, + Importer: &schema.ResourceImporter{ + State: ImportUuid, + }, + + Schema: map[string]*schema.Schema{ + "keepers": { + Type: schema.TypeMap, + Optional: true, + ForceNew: true, + }, + + "result": { + Type: schema.TypeString, + Computed: true, + }, + }, + } +} + +func CreateUuid(d *schema.ResourceData, meta interface{}) error { + result, err := uuid.GenerateUUID() + if err != nil { + return errwrap.Wrapf("error generating uuid: {{err}}", err) + } + d.Set("result", result) + d.SetId(result) + return nil +} + +func RepopulateUuid(d *schema.ResourceData, _ interface{}) error { + return nil +} + +func ImportUuid(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { + id := d.Id() + bytes, err := uuid.ParseUUID(id) + if err != nil { + return nil, errwrap.Wrapf("error parsing uuid bytes: {{err}}", err) + } + result, err2 := uuid.FormatUUID(bytes) + if err2 != nil { + return nil, errwrap.Wrapf("error formatting uuid bytes: {{err2}}", err2) + } + + d.Set("result", result) + d.SetId(result) + + return []*schema.ResourceData{d}, nil +} diff --git a/random/resource_uuid_test.go b/random/resource_uuid_test.go new file mode 100644 index 00000000..bd02c0ab --- /dev/null +++ b/random/resource_uuid_test.go @@ -0,0 +1,68 @@ +package random + +import ( + "fmt" + "regexp" + "testing" + + "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/terraform" +) + +type uuidLens struct { + result string +} + +func TestAccResourceUUID(t *testing.T) { + resource.UnitTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccResourceUUIDConfig, + Check: resource.ComposeTestCheckFunc( + testAccResourceUUIDCheck("random_uuid.foo"), + ), + }, + { + ResourceName: "random_uuid.foo", + ImportState: true, + ImportStateVerify: true, + }, + { + ResourceName: "random_uuid.bar", + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +func testAccResourceUUIDCheck(id string) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[id] + if !ok { + return fmt.Errorf("Not found: %s", id) + } + if rs.Primary.ID == "" { + return fmt.Errorf("No ID is set") + } + + result := rs.Primary.Attributes["result"] + matched, err := regexp.MatchString( + "[\\da-f]{8}-[\\da-f]{4}-[\\da-f]{4}-[\\da-f]{4}-[\\da-f]{12}", result) + if !matched || err != nil { + return fmt.Errorf("result string format incorrect, is %s", result) + } + + return nil + } +} + +const ( + testAccResourceUUIDConfig = ` +resource "random_uuid" "foo" { } + +resource "random_uuid" "bar" { } +` +) diff --git a/website/docs/r/uuid.html.md b/website/docs/r/uuid.html.md new file mode 100644 index 00000000..d5482459 --- /dev/null +++ b/website/docs/r/uuid.html.md @@ -0,0 +1,53 @@ +--- +layout: "random" +page_title: "Random: random_uuid" +sidebar_current: "docs-random-resource-id" +description: |- + Generates a random identifier. +--- + +# random\_id + +The resource `random_uuid` generates random uuid string that is intended to be +used as unique identifiers for other resources. + +This resource uses the `hashicorp/go-uuid` to generate a UUID-formatted string +for use with services needed a unique string identifier. + + +## Example Usage + +The following example shows how to generate a unique name for an Azure Resource Group. + +```hcl +resource "random_uuid" "test" { } + +resource "azurerm_resource_group" "test" { + name = "${random_uuid.test.result}-rg" + location = "Central US" +} +``` + +## Argument Reference + +The following arguments are supported: + +* `keepers` - (Optional) Arbitrary map of values that, when changed, will + trigger a new uuid to be generated. See + [the main provider documentation](../index.html) for more information. + +## Attributes Reference + +The following attributes are exported: + +* `result` - The generated uuid presented in string format. + +## Import + +Random UUID's can be imported. This can be used to replace a config value with a value +interpolated from the random provider without experiencing diffs. + +Example: +``` +$ terraform import random_uuid.main aabbccdd-eeff-0011-2233-445566778899 +``` From 24020cd94aa81d2146c24d6c7995aa4e1b3c49c8 Mon Sep 17 00:00:00 2001 From: Tom Straub Date: Thu, 19 Jul 2018 12:04:56 -0400 Subject: [PATCH 2/2] Update docs to contain `uuid`. --- website/docs/r/uuid.html.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/docs/r/uuid.html.md b/website/docs/r/uuid.html.md index d5482459..587ca90f 100644 --- a/website/docs/r/uuid.html.md +++ b/website/docs/r/uuid.html.md @@ -6,7 +6,7 @@ description: |- Generates a random identifier. --- -# random\_id +# random\_uuid The resource `random_uuid` generates random uuid string that is intended to be used as unique identifiers for other resources.