Skip to content

Commit 8411ece

Browse files
masongup-mdsolrainkwan
authored andcommitted
Add resources for uuid v4 and v7
1 parent 3b9cb36 commit 8411ece

File tree

12 files changed

+1535
-0
lines changed

12 files changed

+1535
-0
lines changed

docs/resources/uuid4.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "random_uuid4 Resource - terraform-provider-random"
4+
subcategory: ""
5+
description: |-
6+
The resource random_uuid4 generates a random version 4 uuid string that is intended to be used as a unique identifier for other resources.
7+
This resource uses google/uuid https://github.com/google/uuid to generate a valid V4 UUID for use with services needing a unique string identifier.
8+
---
9+
10+
# random_uuid4 (Resource)
11+
12+
The resource `random_uuid4` generates a random version 4 uuid string that is intended to be used as a unique identifier for other resources.
13+
14+
This resource uses [google/uuid](https://github.com/google/uuid) to generate a valid V4 UUID for use with services needing a unique string identifier.
15+
16+
## Example Usage
17+
18+
```terraform
19+
# The following example shows how to generate a unique name for an Azure Resource Group.
20+
21+
resource "random_uuid4" "test" {
22+
}
23+
24+
resource "azurerm_resource_group" "test" {
25+
name = "${random_uuid4.test.result}-rg"
26+
location = "Central US"
27+
}
28+
```
29+
30+
<!-- schema generated by tfplugindocs -->
31+
## Schema
32+
33+
### Optional
34+
35+
- `keepers` (Map of String) Arbitrary map of values that, when changed, will trigger recreation of resource. See [the main provider documentation](../index.html) for more information.
36+
37+
### Read-Only
38+
39+
- `id` (String) The generated uuid presented in string format.
40+
- `result` (String) The generated uuid presented in string format.
41+
42+
## Import
43+
44+
Import is supported using the following syntax:
45+
46+
```shell
47+
# Random UUID's can be imported. This can be used to replace a config
48+
# value with a value interpolated from the random provider without
49+
# experiencing diffs.
50+
51+
terraform import random_uuid4.main 7e4436da-7c71-486e-a57c-830b25fff7bd
52+
```

docs/resources/uuid7.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "random_uuid7 Resource - terraform-provider-random"
4+
subcategory: ""
5+
description: |-
6+
The resource random_uuid7 generates a random version 7 uuid string that is intended to be used as a unique identifier for other resources.
7+
This resource uses google/uuid https://github.com/google/uuid to generate a valid V7 UUID for use with services needing a unique string identifier.
8+
---
9+
10+
# random_uuid7 (Resource)
11+
12+
The resource `random_uuid7` generates a random version 7 uuid string that is intended to be used as a unique identifier for other resources.
13+
14+
This resource uses [google/uuid](https://github.com/google/uuid) to generate a valid V7 UUID for use with services needing a unique string identifier.
15+
16+
## Example Usage
17+
18+
```terraform
19+
# The following example shows how to generate a unique name for an Azure Resource Group.
20+
21+
resource "random_uuid7" "test" {
22+
}
23+
24+
resource "azurerm_resource_group" "test" {
25+
name = "${random_uuid7.test.result}-rg"
26+
location = "Central US"
27+
}
28+
```
29+
30+
<!-- schema generated by tfplugindocs -->
31+
## Schema
32+
33+
### Optional
34+
35+
- `keepers` (Map of String) Arbitrary map of values that, when changed, will trigger recreation of resource. See [the main provider documentation](../index.html) for more information.
36+
37+
### Read-Only
38+
39+
- `id` (String) The generated uuid presented in string format.
40+
- `result` (String) The generated uuid presented in string format.
41+
42+
## Import
43+
44+
Import is supported using the following syntax:
45+
46+
```shell
47+
# Random UUID's can be imported. This can be used to replace a config
48+
# value with a value interpolated from the random provider without
49+
# experiencing diffs.
50+
51+
terraform import random_uuid7.main 0197ad85-fe6e-7e92-a2f5-7550daa83030
52+
```
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Random UUID's can be imported. This can be used to replace a config
2+
# value with a value interpolated from the random provider without
3+
# experiencing diffs.
4+
5+
terraform import random_uuid4.main 7e4436da-7c71-486e-a57c-830b25fff7bd
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# The following example shows how to generate a unique name for an Azure Resource Group.
2+
3+
resource "random_uuid4" "test" {
4+
}
5+
6+
resource "azurerm_resource_group" "test" {
7+
name = "${random_uuid4.test.result}-rg"
8+
location = "Central US"
9+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Random UUID's can be imported. This can be used to replace a config
2+
# value with a value interpolated from the random provider without
3+
# experiencing diffs.
4+
5+
terraform import random_uuid7.main 0197ad85-fe6e-7e92-a2f5-7550daa83030
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# The following example shows how to generate a unique name for an Azure Resource Group.
2+
3+
resource "random_uuid7" "test" {
4+
}
5+
6+
resource "azurerm_resource_group" "test" {
7+
name = "${random_uuid7.test.result}-rg"
8+
location = "Central US"
9+
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.23.7
55
require (
66
github.com/dustinkirkland/golang-petname v0.0.0-20240428194347-eebcea082ee0
77
github.com/google/go-cmp v0.7.0
8+
github.com/google/uuid v1.6.0
89
github.com/hashicorp/go-uuid v1.0.3
910
github.com/hashicorp/terraform-json v0.25.0
1011
github.com/hashicorp/terraform-plugin-framework v1.15.0

internal/provider/provider.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ func (p *randomProvider) Resources(context.Context) []func() resource.Resource {
4141
NewShuffleResource,
4242
NewStringResource,
4343
NewUuidResource,
44+
NewUuidV4Resource,
45+
NewUuidV7Resource,
4446
}
4547
}
4648

internal/provider/resource_uuid4.go

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package provider
5+
6+
import (
7+
"context"
8+
"fmt"
9+
10+
"github.com/google/uuid"
11+
"github.com/hashicorp/terraform-plugin-framework/resource"
12+
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
13+
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
14+
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
15+
"github.com/hashicorp/terraform-plugin-framework/types"
16+
17+
"github.com/terraform-providers/terraform-provider-random/internal/diagnostics"
18+
mapplanmodifiers "github.com/terraform-providers/terraform-provider-random/internal/planmodifiers/map"
19+
)
20+
21+
var (
22+
_ resource.Resource = (*uuidV4Resource)(nil)
23+
_ resource.ResourceWithImportState = (*uuidV4Resource)(nil)
24+
)
25+
26+
func NewUuidV4Resource() resource.Resource {
27+
return &uuidV4Resource{}
28+
}
29+
30+
type uuidV4Resource struct{}
31+
32+
func (r *uuidV4Resource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
33+
resp.TypeName = req.ProviderTypeName + "_uuid4"
34+
}
35+
36+
func (r *uuidV4Resource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
37+
resp.Schema = schema.Schema{
38+
Description: "The resource `random_uuid4` generates a random version 4 uuid string that is intended " +
39+
"to be used as a unique identifier for other resources.\n" +
40+
"\n" +
41+
"This resource uses [google/uuid](https://github.com/google/uuid) to generate a " +
42+
"valid V4 UUID for use with services needing a unique string identifier.",
43+
Attributes: map[string]schema.Attribute{
44+
"keepers": schema.MapAttribute{
45+
Description: "Arbitrary map of values that, when changed, will trigger recreation of " +
46+
"resource. See [the main provider documentation](../index.html) for more information.",
47+
ElementType: types.StringType,
48+
Optional: true,
49+
PlanModifiers: []planmodifier.Map{
50+
mapplanmodifiers.RequiresReplaceIfValuesNotNull(),
51+
},
52+
},
53+
"result": schema.StringAttribute{
54+
Description: "The generated uuid presented in string format.",
55+
Computed: true,
56+
PlanModifiers: []planmodifier.String{
57+
stringplanmodifier.UseStateForUnknown(),
58+
},
59+
},
60+
"id": schema.StringAttribute{
61+
Description: "The generated uuid presented in string format.",
62+
Computed: true,
63+
PlanModifiers: []planmodifier.String{
64+
stringplanmodifier.UseStateForUnknown(),
65+
},
66+
},
67+
},
68+
}
69+
}
70+
71+
func (r *uuidV4Resource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
72+
result, err := uuid.NewRandom()
73+
if err != nil {
74+
resp.Diagnostics.AddError(
75+
"Create Random UUID v4 error",
76+
"There was an error during generation of a UUID.\n\n"+
77+
diagnostics.RetryMsg+
78+
fmt.Sprintf("Original Error: %s", err),
79+
)
80+
return
81+
}
82+
83+
var plan uuidModelV4
84+
85+
diags := req.Plan.Get(ctx, &plan)
86+
resp.Diagnostics.Append(diags...)
87+
if resp.Diagnostics.HasError() {
88+
return
89+
}
90+
91+
u := &uuidModelV4{
92+
ID: types.StringValue(result.String()),
93+
Result: types.StringValue(result.String()),
94+
Keepers: plan.Keepers,
95+
}
96+
97+
diags = resp.State.Set(ctx, u)
98+
resp.Diagnostics.Append(diags...)
99+
if resp.Diagnostics.HasError() {
100+
return
101+
}
102+
}
103+
104+
// Read does not need to perform any operations as the state in ReadResourceResponse is already populated.
105+
func (r *uuidV4Resource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
106+
}
107+
108+
// Update ensures the plan value is copied to the state to complete the update.
109+
func (r *uuidV4Resource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
110+
var model uuidModelV4
111+
112+
resp.Diagnostics.Append(req.Plan.Get(ctx, &model)...)
113+
114+
if resp.Diagnostics.HasError() {
115+
return
116+
}
117+
118+
resp.Diagnostics.Append(resp.State.Set(ctx, &model)...)
119+
}
120+
121+
// Delete does not need to explicitly call resp.State.RemoveResource() as this is automatically handled by the
122+
// [framework](https://github.com/hashicorp/terraform-plugin-framework/pull/301).
123+
func (r *uuidV4Resource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
124+
}
125+
126+
func (r *uuidV4Resource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
127+
parsedUuid, err := uuid.Parse(req.ID)
128+
if err != nil {
129+
resp.Diagnostics.AddError(
130+
"Import Random UUID Error",
131+
"There was an error during the parsing of the UUID.\n\n"+
132+
diagnostics.RetryMsg+
133+
fmt.Sprintf("Original Error: %s", err),
134+
)
135+
return
136+
}
137+
138+
var state uuidModelV4
139+
140+
state.ID = types.StringValue(parsedUuid.String())
141+
state.Result = types.StringValue(parsedUuid.String())
142+
state.Keepers = types.MapNull(types.StringType)
143+
144+
diags := resp.State.Set(ctx, &state)
145+
resp.Diagnostics.Append(diags...)
146+
if resp.Diagnostics.HasError() {
147+
return
148+
}
149+
}
150+
151+
type uuidModelV4 struct {
152+
ID types.String `tfsdk:"id"`
153+
Keepers types.Map `tfsdk:"keepers"`
154+
Result types.String `tfsdk:"result"`
155+
}

0 commit comments

Comments
 (0)