|
| 1 | +package resource |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator" |
| 8 | + "github.com/hashicorp/terraform-plugin-framework/resource" |
| 9 | + "github.com/hashicorp/terraform-plugin-framework/resource/schema" |
| 10 | + "github.com/hashicorp/terraform-plugin-framework/schema/validator" |
| 11 | + |
| 12 | + "terraform-provider-plural/internal/client" |
| 13 | + "terraform-provider-plural/internal/common" |
| 14 | + "terraform-provider-plural/internal/model" |
| 15 | +) |
| 16 | + |
| 17 | +var _ resource.ResourceWithConfigure = &stackRunTriggerResource{} |
| 18 | + |
| 19 | +func NewStackRunTriggerResource() resource.Resource { |
| 20 | + return &stackRunTriggerResource{} |
| 21 | +} |
| 22 | + |
| 23 | +type stackRunTriggerResource struct { |
| 24 | + client *client.Client |
| 25 | +} |
| 26 | + |
| 27 | +func (in *stackRunTriggerResource) Metadata(_ context.Context, request resource.MetadataRequest, response *resource.MetadataResponse) { |
| 28 | + response.TypeName = request.ProviderTypeName + "_stack_run_trigger" |
| 29 | +} |
| 30 | + |
| 31 | +func (in *stackRunTriggerResource) Schema(_ context.Context, _ resource.SchemaRequest, response *resource.SchemaResponse) { |
| 32 | + response.Schema = schema.Schema{ |
| 33 | + Attributes: map[string]schema.Attribute{ |
| 34 | + "id": schema.StringAttribute{ |
| 35 | + Description: "ID of the Infrastructure Stack that should be used to start a new run from the newest SHA in the stack's run history.", |
| 36 | + MarkdownDescription: "ID of the Infrastructure Stack that should be used to start a new run from the newest SHA in the stack's run history.", |
| 37 | + Required: true, |
| 38 | + Validators: []validator.String{ |
| 39 | + stringvalidator.LengthAtLeast(1), |
| 40 | + }, |
| 41 | + }, |
| 42 | + }, |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +func (in *stackRunTriggerResource) Configure(_ context.Context, request resource.ConfigureRequest, response *resource.ConfigureResponse) { |
| 47 | + if request.ProviderData == nil { |
| 48 | + return |
| 49 | + } |
| 50 | + |
| 51 | + data, ok := request.ProviderData.(*common.ProviderData) |
| 52 | + if !ok { |
| 53 | + response.Diagnostics.AddError( |
| 54 | + "Unexpected Project Resource Configure Type", |
| 55 | + fmt.Sprintf("Expected *common.ProviderData, got: %T. Please report this issue to the provider developers.", request.ProviderData), |
| 56 | + ) |
| 57 | + return |
| 58 | + } |
| 59 | + |
| 60 | + in.client = data.Client |
| 61 | +} |
| 62 | + |
| 63 | +func (in *stackRunTriggerResource) Create(ctx context.Context, request resource.CreateRequest, response *resource.CreateResponse) { |
| 64 | + data := new(model.StackRunTrigger) |
| 65 | + response.Diagnostics.Append(request.Plan.Get(ctx, data)...) |
| 66 | + if response.Diagnostics.HasError() { |
| 67 | + return |
| 68 | + } |
| 69 | + |
| 70 | + _, err := in.client.TriggerRun( |
| 71 | + ctx, |
| 72 | + data.ID.ValueString(), |
| 73 | + ) |
| 74 | + if err != nil { |
| 75 | + response.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to trigger stack run, got error: %s", err)) |
| 76 | + return |
| 77 | + } |
| 78 | + |
| 79 | + response.Diagnostics.Append(response.State.Set(ctx, &data)...) |
| 80 | +} |
| 81 | + |
| 82 | +func (in *stackRunTriggerResource) Read(_ context.Context, _ resource.ReadRequest, _ *resource.ReadResponse) { |
| 83 | + // Since this is only a trigger, there is no read API. Ignore. |
| 84 | +} |
| 85 | + |
| 86 | +func (in *stackRunTriggerResource) Update(_ context.Context, _ resource.UpdateRequest, _ *resource.UpdateResponse) { |
| 87 | + // Since this is only a trigger, there is no update API. Ignore. |
| 88 | +} |
| 89 | + |
| 90 | +func (in *stackRunTriggerResource) Delete(_ context.Context, _ resource.DeleteRequest, _ *resource.DeleteResponse) { |
| 91 | + // Since this is only a trigger, there is no delete API. Ignore. |
| 92 | +} |
0 commit comments