Skip to content

Commit 4c32b78

Browse files
authored
Merge pull request #51 from pluralsh/sebastian/prod-2337-stack-run-trigger-terraform-resource
feat: add stack run trigger resource
2 parents 6efcd1b + fa3cdfb commit 4c32b78

File tree

7 files changed

+141
-3
lines changed

7 files changed

+141
-3
lines changed

docs/resources/stack_run_trigger.md

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "plural_stack_run_trigger Resource - terraform-provider-plural"
4+
subcategory: ""
5+
description: |-
6+
7+
---
8+
9+
# plural_stack_run_trigger (Resource)
10+
11+
12+
13+
14+
15+
<!-- schema generated by tfplugindocs -->
16+
## Schema
17+
18+
### Required
19+
20+
- `id` (String) ID of the Infrastructure Stack that should be used to start a new run from the newest SHA in the stack's run history.

example/stackruntrigger/main.tf

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
terraform {
2+
required_providers {
3+
plural = {
4+
source = "pluralsh/plural"
5+
version = "0.2.1"
6+
}
7+
}
8+
}
9+
10+
provider "plural" {
11+
use_cli = true
12+
}
13+
14+
resource "plural_stack_run_trigger" "trigger" {
15+
id = "ecc8966a-edfe-4e48-b5ea-f87c3e97d0a3"
16+
}

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ require (
1212
github.com/hashicorp/terraform-plugin-framework-validators v0.13.0
1313
github.com/hashicorp/terraform-plugin-log v0.9.0
1414
github.com/mitchellh/go-homedir v1.1.0
15-
github.com/pluralsh/console/go/client v1.5.0
15+
github.com/pluralsh/console/go/client v1.6.0
1616
github.com/pluralsh/plural-cli v0.9.14-0.20240730152129-7ce540b144fd
1717
github.com/pluralsh/polly v0.1.10
1818
github.com/samber/lo v1.46.0

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -657,8 +657,8 @@ github.com/pjbgf/sha1cd v0.3.0/go.mod h1:nZ1rrWOcGJ5uZgEEVL1VUM9iRQiZvWdbZjkKyFz
657657
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
658658
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
659659
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
660-
github.com/pluralsh/console/go/client v1.5.0 h1:O5pHJUXqBJWUt66MiCRlyatarNFQGy1Fy6QaC5P0avY=
661-
github.com/pluralsh/console/go/client v1.5.0/go.mod h1:lpoWASYsM9keNePS3dpFiEisUHEfObIVlSL3tzpKn8k=
660+
github.com/pluralsh/console/go/client v1.6.0 h1:uh6aWaZNmgDW0Wk172FQd+uJKltUmB1K8SvRDwAjYNM=
661+
github.com/pluralsh/console/go/client v1.6.0/go.mod h1:lpoWASYsM9keNePS3dpFiEisUHEfObIVlSL3tzpKn8k=
662662
github.com/pluralsh/gqlclient v1.12.1 h1:JDOkP9jjqkPdTYdpH5hooG4F8T6FDH90XfipeXJmJFY=
663663
github.com/pluralsh/gqlclient v1.12.1/go.mod h1:OEjN9L63x8m3A3eQBv5kVkFgiY9fp2aZ0cgOF0uII58=
664664
github.com/pluralsh/plural-cli v0.9.14-0.20240730152129-7ce540b144fd h1:gg+5AUbiip8WzAQE+avozXLBdP5eS19J6x6+BhtkGNY=

internal/model/stack_run_trigger.go

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package model
2+
3+
import (
4+
"github.com/hashicorp/terraform-plugin-framework/types"
5+
)
6+
7+
type StackRunTrigger struct {
8+
ID types.String `tfsdk:"id"`
9+
}

internal/provider/provider.go

+1
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ func (p *PluralProvider) Resources(_ context.Context) []func() resource.Resource
192192
r.NewGroupMemberResource,
193193
r.NewGroupResource,
194194
r.NewPrAutomationTriggerResource,
195+
r.NewStackRunTriggerResource,
195196
}
196197
}
197198

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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

Comments
 (0)