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