-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from pluralsh/marcin/eng-2167-add-custom-stack…
…-runs-to-terraform-provider feat: Add custom stack run resource
- Loading branch information
Showing
11 changed files
with
646 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
--- | ||
# generated by https://github.com/hashicorp/terraform-plugin-docs | ||
page_title: "plural_custom_stack_run Resource - terraform-provider-plural" | ||
subcategory: "" | ||
description: |- | ||
--- | ||
|
||
# plural_custom_stack_run (Resource) | ||
|
||
|
||
|
||
|
||
|
||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Required | ||
|
||
- `name` (String) Human-readable name of this custom run. | ||
|
||
### Optional | ||
|
||
- `commands` (Attributes Set) The commands for this custom run. (see [below for nested schema](#nestedatt--commands)) | ||
- `configuration` (Attributes Set) Self-service configuration which will be presented in UI before triggering. (see [below for nested schema](#nestedatt--configuration)) | ||
- `documentation` (String) Extended documentation to explain what this will do. | ||
- `stack_id` (String) The ID of the stack to attach it to. | ||
|
||
### Read-Only | ||
|
||
- `id` (String) Internal identifier of this custom run. | ||
|
||
<a id="nestedatt--commands"></a> | ||
### Nested Schema for `commands` | ||
|
||
Required: | ||
|
||
- `cmd` (String) Command to run. | ||
|
||
Optional: | ||
|
||
- `args` (Set of String) Arguments to pass to the command when executing it. | ||
- `dir` (String) | ||
|
||
|
||
<a id="nestedatt--configuration"></a> | ||
### Nested Schema for `configuration` | ||
|
||
Required: | ||
|
||
- `name` (String) | ||
- `type` (String) | ||
|
||
Optional: | ||
|
||
- `condition` (Attributes) (see [below for nested schema](#nestedatt--configuration--condition)) | ||
- `default` (String) | ||
- `documentation` (String) | ||
- `longform` (String) | ||
- `optional` (Boolean) | ||
- `placeholder` (String) | ||
|
||
<a id="nestedatt--configuration--condition"></a> | ||
### Nested Schema for `configuration.condition` | ||
|
||
Required: | ||
|
||
- `field` (String) | ||
- `operation` (String) | ||
|
||
Optional: | ||
|
||
- `value` (String) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
terraform { | ||
required_providers { | ||
plural = { | ||
source = "pluralsh/plural" | ||
version = "0.2.1" | ||
} | ||
} | ||
} | ||
|
||
provider "plural" { | ||
use_cli = true | ||
} | ||
|
||
data "plural_cluster" "cluster" { | ||
handle = "mgmt" | ||
} | ||
|
||
data "plural_git_repository" "repository" { | ||
url = "https://github.com/zreigz/tf-hello.git" | ||
} | ||
|
||
resource "random_string" "random" { | ||
length = 5 | ||
upper = false | ||
special = false | ||
} | ||
|
||
resource "plural_infrastructure_stack" "stack" { | ||
name = "stack-tf-${random_string.random.result}" | ||
type = "TERRAFORM" | ||
detach = true | ||
cluster_id = data.plural_cluster.cluster.id | ||
repository = { | ||
id = data.plural_git_repository.repository.id | ||
ref = "main" | ||
folder = "terraform" | ||
} | ||
configuration = { | ||
version = "1.8.1" | ||
} | ||
} | ||
|
||
resource "plural_custom_stack_run" "run-full" { | ||
name = "run-tf-full-${random_string.random.result}" | ||
documentation = "test" | ||
stack_id = plural_infrastructure_stack.stack.id | ||
commands = [{ | ||
cmd = "ls" | ||
args = ["-al"] | ||
dir = "/" | ||
}] | ||
configuration = [{ | ||
type = "STRING" | ||
name = "author" | ||
default = "john" | ||
documentation = "author name" | ||
longform = "author name" | ||
placeholder = "author name, i.e. john" | ||
optional = true | ||
condition = { | ||
operation = "PREFIX" | ||
field = "author" | ||
value = "j" | ||
} | ||
}] | ||
} | ||
|
||
resource "plural_custom_stack_run" "run-minimal" { | ||
name = "run-tf-minimal-${random_string.random.result}" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
package resource | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"terraform-provider-plural/internal/client" | ||
"terraform-provider-plural/internal/common" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/path" | ||
"github.com/hashicorp/terraform-plugin-framework/resource" | ||
) | ||
|
||
var _ resource.Resource = &CustomStackRunResource{} | ||
var _ resource.ResourceWithImportState = &CustomStackRunResource{} | ||
|
||
func NewCustomStackRunResource() resource.Resource { | ||
return &CustomStackRunResource{} | ||
} | ||
|
||
// CustomStackRunResource defines the custom stack run resource implementation. | ||
type CustomStackRunResource struct { | ||
client *client.Client | ||
} | ||
|
||
func (r *CustomStackRunResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { | ||
resp.TypeName = req.ProviderTypeName + "_custom_stack_run" | ||
} | ||
|
||
func (r *CustomStackRunResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { | ||
resp.Schema = r.schema() | ||
} | ||
|
||
func (r *CustomStackRunResource) Configure(_ context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) { | ||
if req.ProviderData == nil { | ||
return | ||
} | ||
|
||
data, ok := req.ProviderData.(*common.ProviderData) | ||
if !ok { | ||
resp.Diagnostics.AddError( | ||
"Unexpected Custom Stack Run Resource Configure Type", | ||
fmt.Sprintf("Expected *common.ProviderData, got: %T. Please report this issue to the provider developers.", req.ProviderData), | ||
) | ||
|
||
return | ||
} | ||
|
||
r.client = data.Client | ||
} | ||
|
||
func (r *CustomStackRunResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { | ||
data := new(customStackRun) | ||
resp.Diagnostics.Append(req.Plan.Get(ctx, data)...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
attr, err := data.Attributes(ctx, resp.Diagnostics, r.client) | ||
if err != nil { | ||
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to get attributes, got error: %s", err)) | ||
return | ||
} | ||
sd, err := r.client.CreateCustomStackRun(ctx, *attr) | ||
if err != nil { | ||
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to create custom stack run, got error: %s", err)) | ||
return | ||
} | ||
|
||
data.From(sd.CreateCustomStackRun, ctx, resp.Diagnostics) | ||
resp.Diagnostics.Append(resp.State.Set(ctx, data)...) | ||
} | ||
|
||
func (r *CustomStackRunResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { | ||
data := new(customStackRun) | ||
resp.Diagnostics.Append(req.State.Get(ctx, data)...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
response, err := r.client.GetCustomStackRun(ctx, data.Id.ValueString()) | ||
if err != nil { | ||
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read custom stack run, got error: %s", err)) | ||
return | ||
} | ||
|
||
data.From(response.CustomStackRun, ctx, resp.Diagnostics) | ||
resp.Diagnostics.Append(resp.State.Set(ctx, data)...) | ||
} | ||
|
||
func (r *CustomStackRunResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { | ||
data := new(customStackRun) | ||
resp.Diagnostics.Append(req.Plan.Get(ctx, data)...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
attr, err := data.Attributes(ctx, resp.Diagnostics, r.client) | ||
if err != nil { | ||
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to get attributes, got error: %s", err)) | ||
return | ||
} | ||
_, err = r.client.UpdateCustomStackRun(ctx, data.Id.ValueString(), *attr) | ||
if err != nil { | ||
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to update custom stack run, got error: %s", err)) | ||
return | ||
} | ||
|
||
resp.Diagnostics.Append(resp.State.Set(ctx, data)...) | ||
} | ||
|
||
func (r *CustomStackRunResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { | ||
data := new(customStackRun) | ||
resp.Diagnostics.Append(req.State.Get(ctx, data)...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
_, err := r.client.DeleteCustomStackRun(ctx, data.Id.ValueString()) | ||
if err != nil { | ||
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to delete custom stack run, got error: %s", err)) | ||
return | ||
} | ||
} | ||
|
||
func (r *CustomStackRunResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { | ||
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp) | ||
} |
Oops, something went wrong.