|
| 1 | +package resource |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "terraform-provider-plural/internal/client" |
| 8 | + "terraform-provider-plural/internal/common" |
| 9 | + |
| 10 | + "github.com/hashicorp/terraform-plugin-framework/path" |
| 11 | + "github.com/hashicorp/terraform-plugin-framework/resource" |
| 12 | +) |
| 13 | + |
| 14 | +var _ resource.Resource = &CustomStackRunResource{} |
| 15 | +var _ resource.ResourceWithImportState = &CustomStackRunResource{} |
| 16 | + |
| 17 | +func NewCustomStackRunResource() resource.Resource { |
| 18 | + return &CustomStackRunResource{} |
| 19 | +} |
| 20 | + |
| 21 | +// CustomStackRunResource defines the custom stack run resource implementation. |
| 22 | +type CustomStackRunResource struct { |
| 23 | + client *client.Client |
| 24 | +} |
| 25 | + |
| 26 | +func (r *CustomStackRunResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { |
| 27 | + resp.TypeName = req.ProviderTypeName + "_custom_stack_run" |
| 28 | +} |
| 29 | + |
| 30 | +func (r *CustomStackRunResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { |
| 31 | + resp.Schema = r.schema() |
| 32 | +} |
| 33 | + |
| 34 | +func (r *CustomStackRunResource) Configure(_ context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) { |
| 35 | + if req.ProviderData == nil { |
| 36 | + return |
| 37 | + } |
| 38 | + |
| 39 | + data, ok := req.ProviderData.(*common.ProviderData) |
| 40 | + if !ok { |
| 41 | + resp.Diagnostics.AddError( |
| 42 | + "Unexpected Custom Stack Run Resource Configure Type", |
| 43 | + fmt.Sprintf("Expected *common.ProviderData, got: %T. Please report this issue to the provider developers.", req.ProviderData), |
| 44 | + ) |
| 45 | + |
| 46 | + return |
| 47 | + } |
| 48 | + |
| 49 | + r.client = data.Client |
| 50 | +} |
| 51 | + |
| 52 | +func (r *CustomStackRunResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { |
| 53 | + data := new(customStackRun) |
| 54 | + resp.Diagnostics.Append(req.Plan.Get(ctx, data)...) |
| 55 | + if resp.Diagnostics.HasError() { |
| 56 | + return |
| 57 | + } |
| 58 | + |
| 59 | + attr, err := data.Attributes(ctx, resp.Diagnostics, r.client) |
| 60 | + if err != nil { |
| 61 | + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to get attributes, got error: %s", err)) |
| 62 | + return |
| 63 | + } |
| 64 | + sd, err := r.client.CreateCustomStackRun(ctx, *attr) |
| 65 | + if err != nil { |
| 66 | + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to create custom stack run, got error: %s", err)) |
| 67 | + return |
| 68 | + } |
| 69 | + |
| 70 | + data.From(sd.CreateCustomStackRun, ctx, resp.Diagnostics) |
| 71 | + resp.Diagnostics.Append(resp.State.Set(ctx, data)...) |
| 72 | +} |
| 73 | + |
| 74 | +func (r *CustomStackRunResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { |
| 75 | + data := new(customStackRun) |
| 76 | + resp.Diagnostics.Append(req.State.Get(ctx, data)...) |
| 77 | + if resp.Diagnostics.HasError() { |
| 78 | + return |
| 79 | + } |
| 80 | + |
| 81 | + response, err := r.client.GetCustomStackRun(ctx, data.Id.ValueString()) |
| 82 | + if err != nil { |
| 83 | + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read custom stack run, got error: %s", err)) |
| 84 | + return |
| 85 | + } |
| 86 | + |
| 87 | + data.From(response.CustomStackRun, ctx, resp.Diagnostics) |
| 88 | + resp.Diagnostics.Append(resp.State.Set(ctx, data)...) |
| 89 | +} |
| 90 | + |
| 91 | +func (r *CustomStackRunResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { |
| 92 | + data := new(customStackRun) |
| 93 | + resp.Diagnostics.Append(req.Plan.Get(ctx, data)...) |
| 94 | + if resp.Diagnostics.HasError() { |
| 95 | + return |
| 96 | + } |
| 97 | + |
| 98 | + attr, err := data.Attributes(ctx, resp.Diagnostics, r.client) |
| 99 | + if err != nil { |
| 100 | + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to get attributes, got error: %s", err)) |
| 101 | + return |
| 102 | + } |
| 103 | + _, err = r.client.UpdateCustomStackRun(ctx, data.Id.ValueString(), *attr) |
| 104 | + if err != nil { |
| 105 | + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to update custom stack run, got error: %s", err)) |
| 106 | + return |
| 107 | + } |
| 108 | + |
| 109 | + resp.Diagnostics.Append(resp.State.Set(ctx, data)...) |
| 110 | +} |
| 111 | + |
| 112 | +func (r *CustomStackRunResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { |
| 113 | + data := new(customStackRun) |
| 114 | + resp.Diagnostics.Append(req.State.Get(ctx, data)...) |
| 115 | + if resp.Diagnostics.HasError() { |
| 116 | + return |
| 117 | + } |
| 118 | + |
| 119 | + _, err := r.client.DeleteCustomStackRun(ctx, data.Id.ValueString()) |
| 120 | + if err != nil { |
| 121 | + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to delete custom stack run, got error: %s", err)) |
| 122 | + return |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +func (r *CustomStackRunResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { |
| 127 | + resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp) |
| 128 | +} |
0 commit comments