Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions docs/resources/uuid4.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "random_uuid4 Resource - terraform-provider-random"
subcategory: ""
description: |-
The resource random_uuid4 generates a random version 4 uuid string that is intended to be used as a unique identifier for other resources.
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.
---

# random_uuid4 (Resource)

The resource `random_uuid4` generates a random version 4 uuid string that is intended to be used as a unique identifier for other resources.

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.

## Example Usage

```terraform
# The following example shows how to generate a unique name for an Azure Resource Group.

resource "random_uuid4" "test" {
}

resource "azurerm_resource_group" "test" {
name = "${random_uuid4.test.result}-rg"
location = "Central US"
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Optional

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

### Read-Only

- `id` (String) The generated uuid presented in string format.
- `result` (String) The generated uuid presented in string format.

## Import

Import is supported using the following syntax:

```shell
# 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.

terraform import random_uuid4.main 7e4436da-7c71-486e-a57c-830b25fff7bd
```
52 changes: 52 additions & 0 deletions docs/resources/uuid7.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "random_uuid7 Resource - terraform-provider-random"
subcategory: ""
description: |-
The resource random_uuid7 generates a random version 7 uuid string that is intended to be used as a unique identifier for other resources.
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.
---

# random_uuid7 (Resource)

The resource `random_uuid7` generates a random version 7 uuid string that is intended to be used as a unique identifier for other resources.

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.

## Example Usage

```terraform
# The following example shows how to generate a unique name for an Azure Resource Group.

resource "random_uuid7" "test" {
}

resource "azurerm_resource_group" "test" {
name = "${random_uuid7.test.result}-rg"
location = "Central US"
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Optional

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

### Read-Only

- `id` (String) The generated uuid presented in string format.
- `result` (String) The generated uuid presented in string format.

## Import

Import is supported using the following syntax:

```shell
# 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.

terraform import random_uuid7.main 0197ad85-fe6e-7e92-a2f5-7550daa83030
```
5 changes: 5 additions & 0 deletions examples/resources/random_uuid4/import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# 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.

terraform import random_uuid4.main 7e4436da-7c71-486e-a57c-830b25fff7bd
9 changes: 9 additions & 0 deletions examples/resources/random_uuid4/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# The following example shows how to generate a unique name for an Azure Resource Group.

resource "random_uuid4" "test" {
}

resource "azurerm_resource_group" "test" {
name = "${random_uuid4.test.result}-rg"
location = "Central US"
}
5 changes: 5 additions & 0 deletions examples/resources/random_uuid7/import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# 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.

terraform import random_uuid7.main 0197ad85-fe6e-7e92-a2f5-7550daa83030
9 changes: 9 additions & 0 deletions examples/resources/random_uuid7/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# The following example shows how to generate a unique name for an Azure Resource Group.

resource "random_uuid7" "test" {
}

resource "azurerm_resource_group" "test" {
name = "${random_uuid7.test.result}-rg"
location = "Central US"
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.23.7
require (
github.com/dustinkirkland/golang-petname v0.0.0-20240428194347-eebcea082ee0
github.com/google/go-cmp v0.7.0
github.com/google/uuid v1.6.0
github.com/hashicorp/go-uuid v1.0.3
github.com/hashicorp/terraform-json v0.25.0
github.com/hashicorp/terraform-plugin-framework v1.15.0
Expand Down
2 changes: 2 additions & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ func (p *randomProvider) Resources(context.Context) []func() resource.Resource {
NewShuffleResource,
NewStringResource,
NewUuidResource,
NewUuidV4Resource,
NewUuidV7Resource,
}
}

Expand Down
155 changes: 155 additions & 0 deletions internal/provider/resource_uuid4.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package provider

import (
"context"
"fmt"

"github.com/google/uuid"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/types"

"github.com/terraform-providers/terraform-provider-random/internal/diagnostics"
mapplanmodifiers "github.com/terraform-providers/terraform-provider-random/internal/planmodifiers/map"
)

var (
_ resource.Resource = (*uuidV4Resource)(nil)
_ resource.ResourceWithImportState = (*uuidV4Resource)(nil)
)

func NewUuidV4Resource() resource.Resource {
return &uuidV4Resource{}
}

type uuidV4Resource struct{}

func (r *uuidV4Resource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_uuid4"
}

func (r *uuidV4Resource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
Description: "The resource `random_uuid4` generates a random version 4 uuid string that is intended " +
"to be used as a unique identifier for other resources.\n" +
"\n" +
"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.",
Attributes: map[string]schema.Attribute{
"keepers": schema.MapAttribute{
Description: "Arbitrary map of values that, when changed, will trigger recreation of " +
"resource. See [the main provider documentation](../index.html) for more information.",
ElementType: types.StringType,
Optional: true,
PlanModifiers: []planmodifier.Map{
mapplanmodifiers.RequiresReplaceIfValuesNotNull(),
},
},
"result": schema.StringAttribute{
Description: "The generated uuid presented in string format.",
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
"id": schema.StringAttribute{
Description: "The generated uuid presented in string format.",
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
},
}
}

func (r *uuidV4Resource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
result, err := uuid.NewRandom()
if err != nil {
resp.Diagnostics.AddError(
"Create Random UUID v4 error",
"There was an error during generation of a UUID.\n\n"+
diagnostics.RetryMsg+
fmt.Sprintf("Original Error: %s", err),
)
return
}

var plan uuidModelV4

diags := req.Plan.Get(ctx, &plan)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}

u := &uuidModelV4{
ID: types.StringValue(result.String()),
Result: types.StringValue(result.String()),
Keepers: plan.Keepers,
}

diags = resp.State.Set(ctx, u)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}

// Read does not need to perform any operations as the state in ReadResourceResponse is already populated.
func (r *uuidV4Resource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
}

// Update ensures the plan value is copied to the state to complete the update.
func (r *uuidV4Resource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
var model uuidModelV4

resp.Diagnostics.Append(req.Plan.Get(ctx, &model)...)

if resp.Diagnostics.HasError() {
return
}

resp.Diagnostics.Append(resp.State.Set(ctx, &model)...)
}

// Delete does not need to explicitly call resp.State.RemoveResource() as this is automatically handled by the
// [framework](https://github.com/hashicorp/terraform-plugin-framework/pull/301).
func (r *uuidV4Resource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
}

func (r *uuidV4Resource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
parsedUuid, err := uuid.Parse(req.ID)
if err != nil {
resp.Diagnostics.AddError(
"Import Random UUID Error",
"There was an error during the parsing of the UUID.\n\n"+
diagnostics.RetryMsg+
fmt.Sprintf("Original Error: %s", err),
)
return
}

var state uuidModelV4

state.ID = types.StringValue(parsedUuid.String())
state.Result = types.StringValue(parsedUuid.String())
state.Keepers = types.MapNull(types.StringType)

diags := resp.State.Set(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}

type uuidModelV4 struct {
ID types.String `tfsdk:"id"`
Keepers types.Map `tfsdk:"keepers"`
Result types.String `tfsdk:"result"`
}
Loading
Loading