Skip to content

Commit

Permalink
feat: step template resource and datasource (#793)
Browse files Browse the repository at this point in the history
  • Loading branch information
domenicsim1 authored Oct 16, 2024
1 parent 062ebe8 commit 337f569
Show file tree
Hide file tree
Showing 10 changed files with 1,199 additions and 5 deletions.
83 changes: 83 additions & 0 deletions docs/data-sources/step_template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "octopusdeploy_step_template Data Source - terraform-provider-octopusdeploy"
subcategory: ""
description: |-
Provides information about existing step_template.
---

# octopusdeploy_step_template (Data Source)

Provides information about existing step_template.



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

### Required

- `id` (String) Unique identifier of the step template

### Optional

- `space_id` (String) SpaceID of the Step Template

### Read-Only

- `step_template` (Object) (see [below for nested schema](#nestedatt--step_template))

<a id="nestedatt--step_template"></a>
### Nested Schema for `step_template`

Read-Only:

- `action_type` (String)
- `community_action_template_id` (String)
- `description` (String)
- `id` (String)
- `name` (String)
- `packages` (List of Object) (see [below for nested schema](#nestedobjatt--step_template--packages))
- `parameters` (List of Object) (see [below for nested schema](#nestedobjatt--step_template--parameters))
- `properties` (Map of String)
- `space_id` (String)
- `step_package_id` (String)
- `version` (Number)

<a id="nestedobjatt--step_template--packages"></a>
### Nested Schema for `step_template.packages`

Read-Only:

- `acquisition_location` (String)
- `feed_id` (String)
- `id` (String)
- `name` (String)
- `package_id` (String)
- `properties` (Object) (see [below for nested schema](#nestedobjatt--step_template--packages--properties))

<a id="nestedobjatt--step_template--packages--properties"></a>
### Nested Schema for `step_template.packages.properties`

Read-Only:

- `extract` (String)
- `package_parameter_name` (String)
- `purpose` (String)
- `selection_mode` (String)



<a id="nestedobjatt--step_template--parameters"></a>
### Nested Schema for `step_template.parameters`

Read-Only:

- `default_value` (String)
- `display_settings` (Map of String)
- `help_text` (String)
- `id` (String)
- `label` (String)
- `name` (String)


86 changes: 86 additions & 0 deletions docs/resources/step_template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "octopusdeploy_step_template Resource - terraform-provider-octopusdeploy"
subcategory: ""
description: |-
This resource manages step_templates in Octopus Deploy.
---

# octopusdeploy_step_template (Resource)

This resource manages step_templates in Octopus Deploy.



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

### Required

- `action_type` (String) The action type of the step template
- `name` (String) The name of this resource.
- `packages` (Attributes List) Package information for the step template (see [below for nested schema](#nestedatt--packages))
- `parameters` (Attributes List) List of parameters that can be used in Step Template. (see [below for nested schema](#nestedatt--parameters))
- `properties` (Map of String) Properties for the step template
- `step_package_id` (String) The ID of the step package

### Optional

- `community_action_template_id` (String) The ID of the community action template
- `description` (String) The description of this step_template.
- `space_id` (String) The space ID associated with this step_template.

### Read-Only

- `id` (String) The unique ID for this resource.
- `version` (Number) The version of the step template

<a id="nestedatt--packages"></a>
### Nested Schema for `packages`

Required:

- `feed_id` (String) ID of the feed.
- `name` (String) The name of this resource.
- `properties` (Attributes) Properties for the package. (see [below for nested schema](#nestedatt--packages--properties))

Optional:

- `acquisition_location` (String) Acquisition location for the package.
- `package_id` (String) The ID of the package to use.

Read-Only:

- `id` (String) The unique ID for this resource.

<a id="nestedatt--packages--properties"></a>
### Nested Schema for `packages.properties`

Required:

- `selection_mode` (String) The selection mode.

Optional:

- `extract` (String) If the package should extract.
- `package_parameter_name` (String) The name of the package parameter
- `purpose` (String) The purpose of this property.



<a id="nestedatt--parameters"></a>
### Nested Schema for `parameters`

Required:

- `id` (String) The id for the property.
- `name` (String) The name of the variable set by the parameter. The name can contain letters, digits, dashes and periods. Example: `ServerName`

Optional:

- `default_value` (String) A default value for the parameter, if applicable. This can be a hard-coded value or a variable reference.
- `display_settings` (Map of String) The display settings for the parameter.
- `help_text` (String) The help presented alongside the parameter input.
- `label` (String) The label shown beside the parameter when presented in the deployment process. Example: `Server name`.


4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/OctopusDeploy/terraform-provider-octopusdeploy
go 1.21

require (
github.com/OctopusDeploy/go-octopusdeploy/v2 v2.50.0
github.com/OctopusDeploy/go-octopusdeploy/v2 v2.52.0
github.com/OctopusSolutionsEngineering/OctopusTerraformTestFramework v0.0.0-20240729041805-46db6fb717b4
github.com/google/uuid v1.6.0
github.com/hashicorp/go-cty v1.4.1-0.20200723130312-85980079f637
Expand All @@ -17,7 +17,6 @@ require (
github.com/hashicorp/terraform-plugin-testing v1.8.0
github.com/stretchr/testify v1.9.0
github.com/testcontainers/testcontainers-go v0.32.0
golang.org/x/exp v0.0.0-20240707233637-46b078467d37
golang.org/x/text v0.16.0
k8s.io/utils v0.0.0-20230505201702-9f6742963106
software.sslmate.com/src/go-pkcs12 v0.4.0
Expand Down Expand Up @@ -130,6 +129,7 @@ require (
go.opentelemetry.io/otel/sdk v1.21.0 // indirect
go.opentelemetry.io/otel/trace v1.24.0 // indirect
golang.org/x/crypto v0.25.0 // indirect
golang.org/x/exp v0.0.0-20240707233637-46b078467d37 // indirect
golang.org/x/mod v0.19.0 // indirect
golang.org/x/net v0.27.0 // indirect
golang.org/x/sync v0.7.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ github.com/Microsoft/hcsshim v0.12.4 h1:Ev7YUMHAHoWNm+aDSPzc5W9s6E2jyL1szpVDJeZ/
github.com/Microsoft/hcsshim v0.12.4/go.mod h1:Iyl1WVpZzr+UkzjekHZbV8o5Z9ZkxNGx6CtY2Qg/JVQ=
github.com/OctopusDeploy/go-octodiff v1.0.0 h1:U+ORg6azniwwYo+O44giOw6TiD5USk8S4VDhOQ0Ven0=
github.com/OctopusDeploy/go-octodiff v1.0.0/go.mod h1:Mze0+EkOWTgTmi8++fyUc6r0aLZT7qD9gX+31t8MmIU=
github.com/OctopusDeploy/go-octopusdeploy/v2 v2.50.0 h1:rQiLEbqt/D3lPQw3pq9sXAW1C0WhVLrfN/h0cqUzaFY=
github.com/OctopusDeploy/go-octopusdeploy/v2 v2.50.0/go.mod h1:ggvOXzMnq+w0pLg6C9zdjz6YBaHfO3B3tqmmB7JQdaw=
github.com/OctopusDeploy/go-octopusdeploy/v2 v2.52.0 h1:X3Tdij/cGqmEtmZ0HqJFeHzTJVxFmYEAog4R4w6KFIw=
github.com/OctopusDeploy/go-octopusdeploy/v2 v2.52.0/go.mod h1:ggvOXzMnq+w0pLg6C9zdjz6YBaHfO3B3tqmmB7JQdaw=
github.com/OctopusSolutionsEngineering/OctopusTerraformTestFramework v0.0.0-20240729041805-46db6fb717b4 h1:QfbVf0bOIRMp/WHAWsuVDB7KHoWnRsGbvDuOf2ua7k4=
github.com/OctopusSolutionsEngineering/OctopusTerraformTestFramework v0.0.0-20240729041805-46db6fb717b4/go.mod h1:Oq9KbiRNDBB5jFmrwnrgLX0urIqR/1ptY18TzkqXm7M=
github.com/ProtonMail/go-crypto v1.1.0-alpha.2 h1:bkyFVUP+ROOARdgCiJzNQo2V2kiB97LyUpzH9P6Hrlg=
Expand Down
116 changes: 116 additions & 0 deletions octopusdeploy_framework/datasource_step_template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package octopusdeploy_framework

import (
"context"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/actiontemplates"
"github.com/OctopusDeploy/terraform-provider-octopusdeploy/octopusdeploy_framework/schemas"
"github.com/OctopusDeploy/terraform-provider-octopusdeploy/octopusdeploy_framework/util"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/types"
)

type stepTemplateDataSource struct {
*Config
}

func NewStepTemplateDataSource() datasource.DataSource {
return &stepTemplateDataSource{}
}
func (*stepTemplateDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = util.GetTypeName("step_template")
}

func (*stepTemplateDataSource) Schema(_ context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schemas.StepTemplateSchema{}.GetDatasourceSchema()
}

func (d *stepTemplateDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
d.Config = DataSourceConfiguration(req, resp)
}

func (d *stepTemplateDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var err error
var data schemas.StepTemplateTypeDataSourceModel
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}

query := struct {
ID string
SpaceID string
}{data.ID.ValueString(), data.SpaceID.ValueString()}

util.DatasourceReading(ctx, "step_template", query)

actionTemplate, err := actiontemplates.GetByID(d.Config.Client, query.SpaceID, query.ID)
if err != nil {
resp.Diagnostics.AddError("Unable to load step template", err.Error())
return
}

resp.Diagnostics.Append(mapStepTemplateToDatasourceModel(&data, actionTemplate)...)
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}

func mapStepTemplateToDatasourceModel(data *schemas.StepTemplateTypeDataSourceModel, at *actiontemplates.ActionTemplate) diag.Diagnostics {
resp := diag.Diagnostics{}

data.ID = types.StringValue(at.ID)
data.SpaceID = types.StringValue(at.SpaceID)
stepTemplate, dg := convertStepTemplateAttributes(at)
resp.Append(dg...)
data.StepTemplate = stepTemplate
return resp
}

func convertStepTemplateAttributes(at *actiontemplates.ActionTemplate) (types.Object, diag.Diagnostics) {
diags := diag.Diagnostics{}

params := make([]attr.Value, len(at.Parameters))
for i, param := range at.Parameters {
p, dg := convertStepTemplateParameterAttribute(param)
diags.Append(dg...)
params[i] = p
}
paramsListValue, dg := types.ListValue(types.ObjectType{AttrTypes: schemas.GetStepTemplateParameterTypeAttributes()}, params)
diags.Append(dg...)

pkgs := make([]attr.Value, len(at.Packages))
for i, pkg := range at.Packages {
p, dg := convertStepTemplatePackageAttribute(pkg)
diags.Append(dg...)
pkgs[i] = p
}
packageListValue, dg := types.ListValue(types.ObjectType{AttrTypes: schemas.GetStepTemplatePackageTypeAttributes()}, pkgs)
diags.Append(dg...)

props := make(map[string]attr.Value, len(at.Properties))
for key, val := range at.Properties {
props[key] = types.StringValue(val.Value)
}
propertiesMap, dg := types.MapValue(types.StringType, props)
diags.Append(dg...)

if diags.HasError() {
return types.ObjectNull(schemas.GetStepTemplateParameterTypeAttributes()), diags
}

stepTemplate, dg := types.ObjectValue(schemas.GetStepTemplateAttributes(), map[string]attr.Value{
"id": types.StringValue(at.ID),
"name": types.StringValue(at.Name),
"description": types.StringValue(at.Description),
"space_id": types.StringValue(at.SpaceID),
"version": types.Int32Value(at.Version),
"step_package_id": types.StringValue(at.ActionType),
"action_type": types.StringValue(at.ActionType),
"community_action_template_id": types.StringValue(at.CommunityActionTemplateID),
"packages": packageListValue,
"parameters": paramsListValue,
"properties": propertiesMap,
})
diags.Append(dg...)
return stepTemplate, diags
}
2 changes: 2 additions & 0 deletions octopusdeploy_framework/framework_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func (p *octopusDeployFrameworkProvider) DataSources(ctx context.Context) []func
NewSpacesDataSource,
NewLifecyclesDataSource,
NewEnvironmentsDataSource,
NewStepTemplateDataSource,
NewGitCredentialsDataSource,
NewFeedsDataSource,
NewLibraryVariableSetDataSource,
Expand All @@ -85,6 +86,7 @@ func (p *octopusDeployFrameworkProvider) Resources(ctx context.Context) []func()
NewMavenFeedResource,
NewLifecycleResource,
NewEnvironmentResource,
NewStepTemplateResource,
NewGitCredentialResource,
NewHelmFeedResource,
NewArtifactoryGenericFeedResource,
Expand Down
Loading

0 comments on commit 337f569

Please sign in to comment.