|
| 1 | +package provider |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/hashicorp/terraform-plugin-framework/resource" |
| 8 | + "github.com/hashicorp/terraform-plugin-framework/resource/schema" |
| 9 | + "github.com/hashicorp/terraform-plugin-framework/resource/schema/mapplanmodifier" |
| 10 | + "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" |
| 11 | + "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" |
| 12 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 13 | + |
| 14 | + "github.com/coder/coder/v2/codersdk" |
| 15 | +) |
| 16 | + |
| 17 | +// Ensure provider defined types fully satisfy framework interfaces. |
| 18 | +var _ resource.Resource = &ProvisionerKeyResource{} |
| 19 | + |
| 20 | +func NewProvisionerKeyResource() resource.Resource { |
| 21 | + return &ProvisionerKeyResource{} |
| 22 | +} |
| 23 | + |
| 24 | +// ProvisionerKeyResource defines the resource implementation. |
| 25 | +type ProvisionerKeyResource struct { |
| 26 | + *CoderdProviderData |
| 27 | +} |
| 28 | + |
| 29 | +// ProvisionerKeyResourceModel describes the resource data model. |
| 30 | +type ProvisionerKeyResourceModel struct { |
| 31 | + OrganizationID UUID `tfsdk:"organization_id"` |
| 32 | + Name types.String `tfsdk:"name"` |
| 33 | + Tags types.Map `tfsdk:"tags"` |
| 34 | + Key types.String `tfsdk:"key"` |
| 35 | +} |
| 36 | + |
| 37 | +func (r *ProvisionerKeyResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { |
| 38 | + resp.TypeName = req.ProviderTypeName + "_provisioner_key" |
| 39 | +} |
| 40 | + |
| 41 | +func (r *ProvisionerKeyResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) { |
| 42 | + resp.Schema = schema.Schema{ |
| 43 | + MarkdownDescription: "A provisioner key for a Coder deployment.", |
| 44 | + |
| 45 | + Attributes: map[string]schema.Attribute{ |
| 46 | + "organization_id": schema.StringAttribute{ |
| 47 | + CustomType: UUIDType, |
| 48 | + MarkdownDescription: "The organization that provisioners connected with this key will be connected to.", |
| 49 | + Required: true, |
| 50 | + PlanModifiers: []planmodifier.String{ |
| 51 | + stringplanmodifier.RequiresReplace(), |
| 52 | + }, |
| 53 | + }, |
| 54 | + "name": schema.StringAttribute{ |
| 55 | + MarkdownDescription: "The name of the key.", |
| 56 | + Required: true, |
| 57 | + PlanModifiers: []planmodifier.String{ |
| 58 | + stringplanmodifier.RequiresReplace(), |
| 59 | + }, |
| 60 | + }, |
| 61 | + "tags": schema.MapAttribute{ |
| 62 | + MarkdownDescription: "The tags that provisioners connected with this key will accept jobs for.", |
| 63 | + ElementType: types.StringType, |
| 64 | + Optional: true, |
| 65 | + PlanModifiers: []planmodifier.Map{ |
| 66 | + mapplanmodifier.RequiresReplace(), |
| 67 | + }, |
| 68 | + }, |
| 69 | + "key": schema.StringAttribute{ |
| 70 | + MarkdownDescription: "The acquired provisioner key", |
| 71 | + Computed: true, |
| 72 | + Sensitive: true, |
| 73 | + }, |
| 74 | + }, |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +func (r *ProvisionerKeyResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) { |
| 79 | + // Prevent panic if the provider has not been configured. |
| 80 | + if req.ProviderData == nil { |
| 81 | + return |
| 82 | + } |
| 83 | + |
| 84 | + data, ok := req.ProviderData.(*CoderdProviderData) |
| 85 | + |
| 86 | + if !ok { |
| 87 | + resp.Diagnostics.AddError( |
| 88 | + "Unexpected Resource Configure Type", |
| 89 | + fmt.Sprintf("Expected *CoderdProviderData, got: %T. Please report this issue to the provider developers.", req.ProviderData), |
| 90 | + ) |
| 91 | + |
| 92 | + return |
| 93 | + } |
| 94 | + |
| 95 | + r.CoderdProviderData = data |
| 96 | +} |
| 97 | + |
| 98 | +func (r *ProvisionerKeyResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { |
| 99 | + // Read Terraform plan data into the model |
| 100 | + var data ProvisionerKeyResourceModel |
| 101 | + resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...) |
| 102 | + if resp.Diagnostics.HasError() { |
| 103 | + return |
| 104 | + } |
| 105 | + |
| 106 | + var tags map[string]string |
| 107 | + resp.Diagnostics.Append(data.Tags.ElementsAs(ctx, &tags, false)...) |
| 108 | + createKeyResult, err := r.Client.CreateProvisionerKey(ctx, data.OrganizationID.ValueUUID(), codersdk.CreateProvisionerKeyRequest{ |
| 109 | + Name: data.Name.ValueString(), |
| 110 | + Tags: tags, |
| 111 | + }) |
| 112 | + if err != nil { |
| 113 | + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to create provisioner_key, got error: %s", err)) |
| 114 | + return |
| 115 | + } |
| 116 | + |
| 117 | + data.Key = types.StringValue(createKeyResult.Key) |
| 118 | + // Save data into Terraform state |
| 119 | + resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) |
| 120 | +} |
| 121 | + |
| 122 | +func (r *ProvisionerKeyResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { |
| 123 | + // Read Terraform prior state data into the model |
| 124 | + var data ProvisionerKeyResourceModel |
| 125 | + resp.Diagnostics.Append(req.State.Get(ctx, &data)...) |
| 126 | + if resp.Diagnostics.HasError() { |
| 127 | + return |
| 128 | + } |
| 129 | + |
| 130 | + // Provisioner keys are immutable, no reading necessary. |
| 131 | + |
| 132 | + // Save updated data into Terraform state |
| 133 | + resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) |
| 134 | +} |
| 135 | + |
| 136 | +func (r *ProvisionerKeyResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { |
| 137 | + // Provisioner keys are immutable, updating is always invalid. |
| 138 | + resp.Diagnostics.AddError("Invalid Update", "Terraform is attempting to update a resource which must be replaced") |
| 139 | +} |
| 140 | + |
| 141 | +func (r *ProvisionerKeyResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { |
| 142 | + // Read Terraform prior state data into the model |
| 143 | + var data ProvisionerKeyResourceModel |
| 144 | + resp.Diagnostics.Append(req.State.Get(ctx, &data)...) |
| 145 | + if resp.Diagnostics.HasError() { |
| 146 | + return |
| 147 | + } |
| 148 | + |
| 149 | + err := r.Client.DeleteProvisionerKey(ctx, data.OrganizationID.ValueUUID(), data.Name.ValueString()) |
| 150 | + if err != nil { |
| 151 | + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to delete provisionerkey, got error: %s", err)) |
| 152 | + return |
| 153 | + } |
| 154 | +} |
0 commit comments