|
| 1 | +package keepalive |
| 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/types" |
| 10 | + |
| 11 | + "terraform-provider-kasm/internal/client" |
| 12 | +) |
| 13 | + |
| 14 | +// Ensure the implementation satisfies the expected interfaces. |
| 15 | +var ( |
| 16 | + _ resource.Resource = &keepaliveResource{} |
| 17 | +) |
| 18 | + |
| 19 | +// NewKeepaliveResource is a helper function to simplify the provider implementation. |
| 20 | +func NewKeepaliveResource() resource.Resource { |
| 21 | + return &keepaliveResource{} |
| 22 | +} |
| 23 | + |
| 24 | +// keepaliveResource is the resource implementation. |
| 25 | +type keepaliveResource struct { |
| 26 | + client *client.Client |
| 27 | +} |
| 28 | + |
| 29 | +// Metadata returns the resource type name. |
| 30 | +func (r *keepaliveResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { |
| 31 | + resp.TypeName = req.ProviderTypeName + "_keepalive" |
| 32 | +} |
| 33 | + |
| 34 | +// Schema defines the schema for the resource. |
| 35 | +func (r *keepaliveResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { |
| 36 | + resp.Schema = schema.Schema{ |
| 37 | + Attributes: map[string]schema.Attribute{ |
| 38 | + "id": schema.StringAttribute{ |
| 39 | + Computed: true, |
| 40 | + }, |
| 41 | + "kasm_id": schema.StringAttribute{ |
| 42 | + Required: true, |
| 43 | + }, |
| 44 | + }, |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +// Configure adds the provider configured client to the resource. |
| 49 | +func (r *keepaliveResource) Configure(_ context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) { |
| 50 | + if req.ProviderData == nil { |
| 51 | + return |
| 52 | + } |
| 53 | + |
| 54 | + client, ok := req.ProviderData.(*client.Client) |
| 55 | + if !ok { |
| 56 | + resp.Diagnostics.AddError( |
| 57 | + "Unexpected Resource Configure Type", |
| 58 | + fmt.Sprintf("Expected *client.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData), |
| 59 | + ) |
| 60 | + return |
| 61 | + } |
| 62 | + |
| 63 | + r.client = client |
| 64 | +} |
| 65 | + |
| 66 | +// Create creates the resource and sets the initial Terraform state. |
| 67 | +func (r *keepaliveResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { |
| 68 | + var plan KeepaliveResourceModel |
| 69 | + diags := req.Plan.Get(ctx, &plan) |
| 70 | + resp.Diagnostics.Append(diags...) |
| 71 | + if resp.Diagnostics.HasError() { |
| 72 | + return |
| 73 | + } |
| 74 | + |
| 75 | + // Set a unique ID before making the API call |
| 76 | + plan.ID = types.StringValue(plan.KasmID.ValueString()) |
| 77 | + |
| 78 | + // Make the keepalive API call |
| 79 | + _, err := r.client.Keepalive(plan.KasmID.ValueString()) |
| 80 | + if err != nil { |
| 81 | + resp.Diagnostics.AddError( |
| 82 | + "Error sending keepalive", |
| 83 | + fmt.Sprintf("Could not send keepalive: %v", err), |
| 84 | + ) |
| 85 | + return |
| 86 | + } |
| 87 | + |
| 88 | + diags = resp.State.Set(ctx, plan) |
| 89 | + resp.Diagnostics.Append(diags...) |
| 90 | + if resp.Diagnostics.HasError() { |
| 91 | + return |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +// Read refreshes the Terraform state with the latest data. |
| 96 | +func (r *keepaliveResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { |
| 97 | + var state KeepaliveResourceModel |
| 98 | + diags := req.State.Get(ctx, &state) |
| 99 | + resp.Diagnostics.Append(diags...) |
| 100 | + if resp.Diagnostics.HasError() { |
| 101 | + return |
| 102 | + } |
| 103 | + |
| 104 | + diags = resp.State.Set(ctx, state) |
| 105 | + resp.Diagnostics.Append(diags...) |
| 106 | + if resp.Diagnostics.HasError() { |
| 107 | + return |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +// Update updates the resource and sets the updated Terraform state on success. |
| 112 | +func (r *keepaliveResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { |
| 113 | + var plan KeepaliveResourceModel |
| 114 | + diags := req.Plan.Get(ctx, &plan) |
| 115 | + resp.Diagnostics.Append(diags...) |
| 116 | + if resp.Diagnostics.HasError() { |
| 117 | + return |
| 118 | + } |
| 119 | + |
| 120 | + // Make the keepalive API call |
| 121 | + _, err := r.client.Keepalive(plan.KasmID.ValueString()) |
| 122 | + if err != nil { |
| 123 | + resp.Diagnostics.AddError( |
| 124 | + "Error sending keepalive", |
| 125 | + fmt.Sprintf("Could not send keepalive: %v", err), |
| 126 | + ) |
| 127 | + return |
| 128 | + } |
| 129 | + |
| 130 | + diags = resp.State.Set(ctx, plan) |
| 131 | + resp.Diagnostics.Append(diags...) |
| 132 | + if resp.Diagnostics.HasError() { |
| 133 | + return |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +// Delete deletes the resource and removes the Terraform state on success. |
| 138 | +func (r *keepaliveResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { |
| 139 | + var state KeepaliveResourceModel |
| 140 | + diags := req.State.Get(ctx, &state) |
| 141 | + resp.Diagnostics.Append(diags...) |
| 142 | + if resp.Diagnostics.HasError() { |
| 143 | + return |
| 144 | + } |
| 145 | + |
| 146 | + // No API call needed for deletion |
| 147 | +} |
| 148 | + |
| 149 | +// KeepaliveResourceModel maps the resource schema data. |
| 150 | +type KeepaliveResourceModel struct { |
| 151 | + ID types.String `tfsdk:"id"` |
| 152 | + KasmID types.String `tfsdk:"kasm_id"` |
| 153 | +} |
0 commit comments