Skip to content

Commit

Permalink
uuidv5: added support for custom namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
lscheidler committed May 10, 2019
1 parent d946f7e commit 0ce5da8
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 2 deletions.
4 changes: 3 additions & 1 deletion lang/funcs/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ var UUIDV5Func = function.New(&function.Spec{
case args[0].AsString() == "x500":
namespace = uuidv5.NamespaceX500
default:
return cty.UnknownVal(cty.String), fmt.Errorf("uuidv5() doesn't support namespace %s", args[0].AsString())
if namespace, err = uuidv5.FromString(args[0].AsString()); err != nil {
return cty.UnknownVal(cty.String), fmt.Errorf("uuidv5() doesn't support namespace %s (%v)", args[0].AsString(), err)
}
}
val := args[1].AsString()
return cty.StringVal(uuidv5.NewV5(namespace, val).String()), nil
Expand Down
6 changes: 6 additions & 0 deletions lang/funcs/crypto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ func TestUUIDV5(t *testing.T) {
cty.StringVal("7e12415e-f7c9-57c3-9e43-52dc9950d264"),
false,
},
{
cty.StringVal("6ba7b810-9dad-11d1-80b4-00c04fd430c8"),
cty.StringVal("tada"),
cty.StringVal("faa898db-9b9d-5b75-86a9-149e7bb8e3b8"),
false,
},
{
cty.StringVal("tada"),
cty.StringVal("tada"),
Expand Down
4 changes: 4 additions & 0 deletions lang/functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,10 @@ func TestFunctions(t *testing.T) {
`uuidv5("x500", "tada")`,
cty.StringVal("7e12415e-f7c9-57c3-9e43-52dc9950d264"),
},
{
`uuidv5("6ba7b810-9dad-11d1-80b4-00c04fd430c8", "tada")`,
cty.StringVal("faa898db-9b9d-5b75-86a9-149e7bb8e3b8"),
},
},

"values": {
Expand Down
6 changes: 5 additions & 1 deletion website/docs/configuration/functions/uuidv5.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ description: |-
earlier, see
[0.11 Configuration Language: Interpolation Syntax](../../configuration-0-11/interpolation.html).

`uuidv5` generates a uuid v5 string representation of the value in the specified namespace.
`uuidv5` generates a uuid v5 string representation of the value in the specified namespace. Following namespace shortcuts are defined:
`dns`, `url`, `oid`, `x500`, but You can also specify a custom namespace in uuid format, e.g. `6ba7b810-9dad-11d1-80b4-00c04fd430c8`.

The id is a generated and formatted as required by
[RFC 4122 section 4.3](https://tools.ietf.org/html/rfc4122#section-4.3),
Expand All @@ -33,4 +34,7 @@ af9d40a5-7a36-5c07-b23a-851cd99fbfa5
> uuidv5("x500", "io.terraform.www")
3ebe33fd-b2a1-546b-9867-2ca38768d5d6
> uuidv5("6ba7b810-9dad-11d1-80b4-00c04fd430c8", "www.terraform.io")
a5008fae-b28c-5ba5-96cd-82b4c53552d6
```

0 comments on commit 0ce5da8

Please sign in to comment.