Skip to content

Commit

Permalink
Merge pull request #38 from straubt1/newresource_randomuuid
Browse files Browse the repository at this point in the history
New Resource random_uuid
  • Loading branch information
mildwonkey authored Jul 19, 2018
2 parents 9582277 + 24020cd commit 355b0c7
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 0 deletions.
1 change: 1 addition & 0 deletions random/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func Provider() terraform.ResourceProvider {
"random_pet": resourcePet(),
"random_string": resourceString(),
"random_integer": resourceInteger(),
"random_uuid": resourceUuid(),
},
}
}
62 changes: 62 additions & 0 deletions random/resource_uuid.go
Original file line number Diff line number Diff line change
@@ -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
}
68 changes: 68 additions & 0 deletions random/resource_uuid_test.go
Original file line number Diff line number Diff line change
@@ -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" { }
`
)
53 changes: 53 additions & 0 deletions website/docs/r/uuid.html.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
---
layout: "random"
page_title: "Random: random_uuid"
sidebar_current: "docs-random-resource-id"
description: |-
Generates a random identifier.
---

# random\_uuid

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
```

0 comments on commit 355b0c7

Please sign in to comment.