Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add stack run trigger resource #51

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions docs/resources/stack_run_trigger.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "plural_stack_run_trigger Resource - terraform-provider-plural"
subcategory: ""
description: |-

---

# plural_stack_run_trigger (Resource)





<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `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.
16 changes: 16 additions & 0 deletions example/stackruntrigger/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
terraform {
required_providers {
plural = {
source = "pluralsh/plural"
version = "0.2.1"
}
}
}

provider "plural" {
use_cli = true
}

resource "plural_stack_run_trigger" "trigger" {
id = "ecc8966a-edfe-4e48-b5ea-f87c3e97d0a3"
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ require (
github.com/hashicorp/terraform-plugin-framework-validators v0.13.0
github.com/hashicorp/terraform-plugin-log v0.9.0
github.com/mitchellh/go-homedir v1.1.0
github.com/pluralsh/console/go/client v1.5.0
github.com/pluralsh/console/go/client v1.6.0
github.com/pluralsh/plural-cli v0.9.14-0.20240730152129-7ce540b144fd
github.com/pluralsh/polly v0.1.10
github.com/samber/lo v1.46.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -657,8 +657,8 @@ github.com/pjbgf/sha1cd v0.3.0/go.mod h1:nZ1rrWOcGJ5uZgEEVL1VUM9iRQiZvWdbZjkKyFz
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pluralsh/console/go/client v1.5.0 h1:O5pHJUXqBJWUt66MiCRlyatarNFQGy1Fy6QaC5P0avY=
github.com/pluralsh/console/go/client v1.5.0/go.mod h1:lpoWASYsM9keNePS3dpFiEisUHEfObIVlSL3tzpKn8k=
github.com/pluralsh/console/go/client v1.6.0 h1:uh6aWaZNmgDW0Wk172FQd+uJKltUmB1K8SvRDwAjYNM=
github.com/pluralsh/console/go/client v1.6.0/go.mod h1:lpoWASYsM9keNePS3dpFiEisUHEfObIVlSL3tzpKn8k=
github.com/pluralsh/gqlclient v1.12.1 h1:JDOkP9jjqkPdTYdpH5hooG4F8T6FDH90XfipeXJmJFY=
github.com/pluralsh/gqlclient v1.12.1/go.mod h1:OEjN9L63x8m3A3eQBv5kVkFgiY9fp2aZ0cgOF0uII58=
github.com/pluralsh/plural-cli v0.9.14-0.20240730152129-7ce540b144fd h1:gg+5AUbiip8WzAQE+avozXLBdP5eS19J6x6+BhtkGNY=
Expand Down
9 changes: 9 additions & 0 deletions internal/model/stack_run_trigger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package model

import (
"github.com/hashicorp/terraform-plugin-framework/types"
)

type StackRunTrigger struct {
ID types.String `tfsdk:"id"`
}
1 change: 1 addition & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ func (p *PluralProvider) Resources(_ context.Context) []func() resource.Resource
r.NewGroupMemberResource,
r.NewGroupResource,
r.NewPrAutomationTriggerResource,
r.NewStackRunTriggerResource,
}
}

Expand Down
92 changes: 92 additions & 0 deletions internal/resource/stack_run_trigger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package resource

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"

"terraform-provider-plural/internal/client"
"terraform-provider-plural/internal/common"
"terraform-provider-plural/internal/model"
)

var _ resource.ResourceWithConfigure = &stackRunTriggerResource{}

func NewStackRunTriggerResource() resource.Resource {
return &stackRunTriggerResource{}
}

type stackRunTriggerResource struct {
client *client.Client
}

func (in *stackRunTriggerResource) Metadata(_ context.Context, request resource.MetadataRequest, response *resource.MetadataResponse) {
response.TypeName = request.ProviderTypeName + "_stack_run_trigger"
}

func (in *stackRunTriggerResource) Schema(_ context.Context, _ resource.SchemaRequest, response *resource.SchemaResponse) {
response.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
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.",
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.",
Required: true,
Validators: []validator.String{
stringvalidator.LengthAtLeast(1),
},
},
},
}
}

func (in *stackRunTriggerResource) Configure(_ context.Context, request resource.ConfigureRequest, response *resource.ConfigureResponse) {
if request.ProviderData == nil {
return
}

data, ok := request.ProviderData.(*common.ProviderData)
if !ok {
response.Diagnostics.AddError(
"Unexpected Project Resource Configure Type",
fmt.Sprintf("Expected *common.ProviderData, got: %T. Please report this issue to the provider developers.", request.ProviderData),
)
return
}

in.client = data.Client
}

func (in *stackRunTriggerResource) Create(ctx context.Context, request resource.CreateRequest, response *resource.CreateResponse) {
data := new(model.StackRunTrigger)
response.Diagnostics.Append(request.Plan.Get(ctx, data)...)
if response.Diagnostics.HasError() {
return
}

_, err := in.client.TriggerRun(
ctx,
data.ID.ValueString(),
)
if err != nil {
response.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to trigger stack run, got error: %s", err))
return
}

response.Diagnostics.Append(response.State.Set(ctx, &data)...)
}

func (in *stackRunTriggerResource) Read(_ context.Context, _ resource.ReadRequest, _ *resource.ReadResponse) {
// Since this is only a trigger, there is no read API. Ignore.
}

func (in *stackRunTriggerResource) Update(_ context.Context, _ resource.UpdateRequest, _ *resource.UpdateResponse) {
// Since this is only a trigger, there is no update API. Ignore.
}

func (in *stackRunTriggerResource) Delete(_ context.Context, _ resource.DeleteRequest, _ *resource.DeleteResponse) {
// Since this is only a trigger, there is no delete API. Ignore.
}
Loading