Skip to content

Commit

Permalink
wip datasource step template
Browse files Browse the repository at this point in the history
  • Loading branch information
domenicsim1 committed Oct 3, 2024
1 parent 6e5de93 commit 9b47fdc
Show file tree
Hide file tree
Showing 4 changed files with 207 additions and 62 deletions.
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
}
1 change: 1 addition & 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 Down
33 changes: 10 additions & 23 deletions octopusdeploy_framework/resource_step_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ func NewStepTemplateResource() resource.Resource {
return &stepTemplateTypeResource{}
}

func (r *stepTemplateTypeResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
func (r *stepTemplateTypeResource) Metadata(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = util.GetTypeName("step_template")
}

func (r *stepTemplateTypeResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
func (r *stepTemplateTypeResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schemas.StepTemplateSchema{}.GetResourceSchema()
}

Expand All @@ -50,24 +50,19 @@ func (r *stepTemplateTypeResource) Create(ctx context.Context, req resource.Crea
return
}

newActionTemplate, dg := mapSchemaDataToActionTemplate(ctx, data)
newActionTemplate, dg := mapStepTemplateResourceModelToActionTemplate(ctx, data)
resp.Diagnostics.Append(dg...)
if resp.Diagnostics.HasError() {
return
}
newActionTemplate.SpaceID = data.SpaceID.ValueString()
newActionTemplate.Description = data.Description.ValueString()
if !data.CommunityActionTemplateId.IsNull() {
newActionTemplate.CommunityActionTemplateID = data.CommunityActionTemplateId.ValueString()
}

actionTemplate, err := actiontemplates.Add(r.Config.Client, newActionTemplate)
if err != nil {
resp.Diagnostics.AddError("unable to create step template", err.Error())
return
}

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

Expand All @@ -86,7 +81,7 @@ func (r *stepTemplateTypeResource) Read(ctx context.Context, req resource.ReadRe
return
}

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

Expand All @@ -105,7 +100,7 @@ func (r *stepTemplateTypeResource) Update(ctx context.Context, req resource.Upda
return
}

actionTemplateUpdate, dg := mapSchemaDataToActionTemplate(ctx, data)
actionTemplateUpdate, dg := mapStepTemplateResourceModelToActionTemplate(ctx, data)
resp.Diagnostics.Append(dg...)
if resp.Diagnostics.HasError() {
return
Expand All @@ -120,7 +115,7 @@ func (r *stepTemplateTypeResource) Update(ctx context.Context, req resource.Upda
return
}

resp.Diagnostics.Append(updateStepTemplate(&data, updatedActionTemplate)...)
resp.Diagnostics.Append(mapStepTemplateToResourceModel(&data, updatedActionTemplate)...)
if resp.Diagnostics.HasError() {
return
}
Expand All @@ -141,7 +136,7 @@ func (r *stepTemplateTypeResource) Delete(ctx context.Context, req resource.Dele
}
}

func updateStepTemplate(data *schemas.StepTemplateTypeResourceModel, at *actiontemplates.ActionTemplate) diag.Diagnostics {
func mapStepTemplateToResourceModel(data *schemas.StepTemplateTypeResourceModel, at *actiontemplates.ActionTemplate) diag.Diagnostics {
resp := diag.Diagnostics{}

data.ID = types.StringValue(at.ID)
Expand All @@ -155,9 +150,6 @@ func updateStepTemplate(data *schemas.StepTemplateTypeResourceModel, at *actiont
// Parameters
sParams, dg := convertStepTemplateToParameterAttributes(at.Parameters)
resp.Append(dg...)
if resp.HasError() {
return resp
}
data.Parameters = sParams

// Properties
Expand All @@ -167,26 +159,21 @@ func updateStepTemplate(data *schemas.StepTemplateTypeResourceModel, at *actiont
}
props, dg := types.MapValue(types.StringType, stringProps)
resp.Append(dg...)
if resp.HasError() {
return resp
}
data.Properties = props

// Packages
pkgs, dg := convertStepTemplateToPackageAttributes(at.Packages)
resp.Append(dg...)
if resp.HasError() {
return resp
}
data.Packages = pkgs

return resp
}

func mapSchemaDataToActionTemplate(ctx context.Context, data schemas.StepTemplateTypeResourceModel) (*actiontemplates.ActionTemplate, diag.Diagnostics) {
func mapStepTemplateResourceModelToActionTemplate(ctx context.Context, data schemas.StepTemplateTypeResourceModel) (*actiontemplates.ActionTemplate, diag.Diagnostics) {
resp := diag.Diagnostics{}
at := actiontemplates.NewActionTemplate(data.Name.ValueString(), data.ActionType.ValueString())

at.SpaceID = data.SpaceID.ValueString()
at.Description = data.Description.ValueString()
if !data.CommunityActionTemplateId.IsNull() {
at.CommunityActionTemplateID = data.CommunityActionTemplateId.ValueString()
Expand Down
Loading

0 comments on commit 9b47fdc

Please sign in to comment.