|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +package framework |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "fmt" |
| 9 | + |
| 10 | + "github.com/hashicorp/terraform-plugin-framework/resource" |
| 11 | + "github.com/hashicorp/terraform-plugin-framework/resource/identityschema" |
| 12 | + "github.com/hashicorp/terraform-plugin-framework/resource/schema" |
| 13 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 14 | +) |
| 15 | + |
| 16 | +var _ resource.Resource = IdentityResource{} |
| 17 | +var _ resource.ResourceWithIdentity = IdentityResource{} |
| 18 | + |
| 19 | +func NewIdentityResource() resource.Resource { |
| 20 | + return &IdentityResource{} |
| 21 | +} |
| 22 | + |
| 23 | +type IdentityResource struct{} |
| 24 | + |
| 25 | +func (r IdentityResource) IdentitySchema(ctx context.Context, req resource.IdentitySchemaRequest, resp *resource.IdentitySchemaResponse) { |
| 26 | + resp.IdentitySchema = identityschema.Schema{ |
| 27 | + Attributes: map[string]identityschema.Attribute{ |
| 28 | + "id": identityschema.StringAttribute{ |
| 29 | + RequiredForImport: true, |
| 30 | + }, |
| 31 | + "name": identityschema.StringAttribute{ |
| 32 | + OptionalForImport: true, |
| 33 | + }, |
| 34 | + }, |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +func (r IdentityResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { |
| 39 | + resp.TypeName = req.ProviderTypeName + "_identity" |
| 40 | +} |
| 41 | + |
| 42 | +func (r IdentityResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { |
| 43 | + resp.Schema = schema.Schema{ |
| 44 | + Attributes: map[string]schema.Attribute{ |
| 45 | + "name": schema.StringAttribute{ |
| 46 | + Required: true, |
| 47 | + }, |
| 48 | + }, |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +func (r IdentityResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { |
| 53 | + var data IdentityResourceModel |
| 54 | + |
| 55 | + resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...) |
| 56 | + if resp.Diagnostics.HasError() { |
| 57 | + return |
| 58 | + } |
| 59 | + |
| 60 | + resp.Diagnostics.Append(resp.Identity.Set(ctx, IdentityResourceIdentityModel{ |
| 61 | + ID: types.StringValue("id-123"), |
| 62 | + Name: types.StringValue(fmt.Sprintf("my name is %s", data.Name.ValueString())), |
| 63 | + })...) |
| 64 | + resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) |
| 65 | +} |
| 66 | + |
| 67 | +func (r IdentityResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { |
| 68 | + var data IdentityResourceModel |
| 69 | + |
| 70 | + resp.Diagnostics.Append(req.State.Get(ctx, &data)...) |
| 71 | + |
| 72 | + if resp.Diagnostics.HasError() { |
| 73 | + return |
| 74 | + } |
| 75 | + |
| 76 | + resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) |
| 77 | +} |
| 78 | + |
| 79 | +func (r IdentityResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { |
| 80 | + var data IdentityResourceModel |
| 81 | + |
| 82 | + resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...) |
| 83 | + |
| 84 | + if resp.Diagnostics.HasError() { |
| 85 | + return |
| 86 | + } |
| 87 | + |
| 88 | + resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) |
| 89 | +} |
| 90 | + |
| 91 | +func (r IdentityResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { |
| 92 | +} |
| 93 | + |
| 94 | +type IdentityResourceModel struct { |
| 95 | + Name types.String `tfsdk:"name"` |
| 96 | +} |
| 97 | + |
| 98 | +type IdentityResourceIdentityModel struct { |
| 99 | + ID types.String `tfsdk:"id"` |
| 100 | + Name types.String `tfsdk:"name"` |
| 101 | +} |
0 commit comments