Skip to content

Commit

Permalink
Fix issue expand template
Browse files Browse the repository at this point in the history
  • Loading branch information
HuyPhanNguyen committed Jul 24, 2024
1 parent dd1ed03 commit 7806b35
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 56 deletions.
38 changes: 26 additions & 12 deletions octopusdeploy_framework/resource_project_expand.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package octopusdeploy_framework

import (
"context"
"fmt"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/actiontemplates"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/core"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/credentials"
Expand Down Expand Up @@ -88,8 +89,19 @@ func expandProject(ctx context.Context, model projectResourceModel) *projects.Pr

if !model.Template.IsNull() {
var templates []templateModel
model.Template.ElementsAs(ctx, &templates, false)
project.Templates = expandTemplates(templates)
diags := model.Template.ElementsAs(ctx, &templates, false)
if diags.HasError() {
// Handle or log the error
fmt.Printf("Error converting templates: %v\n", diags)
} else {
fmt.Printf("Number of templates: %d\n", len(templates))

// Now we can use the templates slice
project.Templates = expandTemplates(templates)
}
} else {
fmt.Println("Template is null")
project.Templates = []actiontemplates.ActionTemplateParameter{}
}

if !model.AutoDeployReleaseOverrides.IsNull() {
Expand Down Expand Up @@ -223,28 +235,30 @@ func expandDeploymentActionPackage(model deploymentActionPackageModel) *packages
PackageReference: model.PackageReference.ValueString(),
}
}

func expandTemplates(models []templateModel) []actiontemplates.ActionTemplateParameter {
templates := make([]actiontemplates.ActionTemplateParameter, len(models))
for i, model := range models {
defaultValue := core.NewPropertyValue(model.DefaultValue.ValueString(), false)

displaySettings := make(map[string]string)
if !model.DisplaySettings.IsNull() && !model.DisplaySettings.IsUnknown() {
for k, v := range model.DisplaySettings.Elements() {
if strVal, ok := v.(types.String); ok {
displaySettings[k] = strVal.ValueString()
}
}
model.DisplaySettings.ElementsAs(context.Background(), &displaySettings, false)
}

templates[i] = actiontemplates.ActionTemplateParameter{
Name: model.Name.ValueString(),
Label: model.Label.ValueString(),
HelpText: model.HelpText.ValueString(),
template := actiontemplates.ActionTemplateParameter{
DefaultValue: &defaultValue,
DisplaySettings: displaySettings,
HelpText: model.HelpText.ValueString(),
Label: model.Label.ValueString(),
Name: model.Name.ValueString(),
}

// Set the ID in the embedded Resource
if !model.ID.IsNull() {
template.Resource.ID = model.ID.ValueString()
}

templates[i] = template
}
return templates
}
67 changes: 35 additions & 32 deletions octopusdeploy_framework/resource_project_flatten.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,34 @@ func flattenProject(ctx context.Context, project *projects.Project, state *proje
model.VersioningStrategy = flattenVersioningStrategy(project.VersioningStrategy)
}

// Template
templateList, d := flattenTemplates(ctx, project.Templates)
diags.Append(d...)
model.Template = templateList
model.Template = flattenTemplates(project.Templates)

diags, resourceModel, diagnostics, done := processPersistenceSettings(ctx, project, model, diags)
if done {
return resourceModel, diagnostics
}

// Extension Settings
model.JiraServiceManagementExtensionSettings = flattenJiraServiceManagementExtensionSettings(nil)
model.ServiceNowExtensionSettings = flattenServiceNowExtensionSettings(nil)

for _, extensionSetting := range project.ExtensionSettings {
switch extensionSetting.ExtensionID() {
case extensions.JiraServiceManagementExtensionID:
if jsmSettings, ok := extensionSetting.(*projects.JiraServiceManagementExtensionSettings); ok {
model.JiraServiceManagementExtensionSettings = flattenJiraServiceManagementExtensionSettings(jsmSettings)
}
case extensions.ServiceNowExtensionID:
if snowSettings, ok := extensionSetting.(*projects.ServiceNowExtensionSettings); ok {
model.ServiceNowExtensionSettings = flattenServiceNowExtensionSettings(snowSettings)
}
}
}

return model, diags
}

func processPersistenceSettings(ctx context.Context, project *projects.Project, model *projectResourceModel, diags diag.Diagnostics) (diag.Diagnostics, *projectResourceModel, diag.Diagnostics, bool) {
if project.PersistenceSettings != nil {
if project.PersistenceSettings.Type() == projects.PersistenceSettingsTypeVersionControlled {
gitSettings := project.PersistenceSettings.(projects.GitPersistenceSettings)
Expand All @@ -97,7 +120,7 @@ func flattenProject(ctx context.Context, project *projects.Project, state *proje
}

if diags.HasError() {
return nil, diags
return nil, nil, diags, true
}
} else {
model.GitLibraryPersistenceSettings = types.ListNull(types.ObjectType{AttrTypes: getGitLibraryPersistenceSettingsAttrTypes()})
Expand All @@ -109,25 +132,7 @@ func flattenProject(ctx context.Context, project *projects.Project, state *proje
model.GitUsernamePasswordPersistenceSettings = types.ListNull(types.ObjectType{AttrTypes: getGitUsernamePasswordPersistenceSettingsAttrTypes()})
model.GitAnonymousPersistenceSettings = types.ListNull(types.ObjectType{AttrTypes: getGitAnonymousPersistenceSettingsAttrTypes()})
}

// Extension Settings
model.JiraServiceManagementExtensionSettings = flattenJiraServiceManagementExtensionSettings(nil)
model.ServiceNowExtensionSettings = flattenServiceNowExtensionSettings(nil)

for _, extensionSetting := range project.ExtensionSettings {
switch extensionSetting.ExtensionID() {
case extensions.JiraServiceManagementExtensionID:
if jsmSettings, ok := extensionSetting.(*projects.JiraServiceManagementExtensionSettings); ok {
model.JiraServiceManagementExtensionSettings = flattenJiraServiceManagementExtensionSettings(jsmSettings)
}
case extensions.ServiceNowExtensionID:
if snowSettings, ok := extensionSetting.(*projects.ServiceNowExtensionSettings); ok {
model.ServiceNowExtensionSettings = flattenServiceNowExtensionSettings(snowSettings)
}
}
}

return model, diags
return diags, nil, nil, false
}

func flattenConnectivityPolicy(policy *core.ConnectivityPolicy) types.List {
Expand Down Expand Up @@ -241,15 +246,15 @@ func flattenServiceNowExtensionSettings(settings *projects.ServiceNowExtensionSe
return types.ListValueMust(types.ObjectType{AttrTypes: getServiceNowExtensionSettingsAttrTypes()}, []attr.Value{obj})
}

func flattenTemplates(ctx context.Context, templates []actiontemplates.ActionTemplateParameter) (types.List, diag.Diagnostics) {
func flattenTemplates(templates []actiontemplates.ActionTemplateParameter) types.List {
if len(templates) == 0 {
return types.ListNull(types.ObjectType{AttrTypes: getTemplateAttrTypes()}), nil
return types.ListNull(types.ObjectType{AttrTypes: getTemplateAttrTypes()})
}

templateList := make([]attr.Value, 0, len(templates))
for _, template := range templates {
obj, diags := types.ObjectValueFrom(ctx, getTemplateAttrTypes(), map[string]attr.Value{
"id": types.StringValue(template.ID),
obj := types.ObjectValueMust(getTemplateAttrTypes(), map[string]attr.Value{
"id": types.StringValue(template.Resource.ID),
"name": types.StringValue(template.Name),
"label": types.StringValue(template.Label),
"help_text": types.StringValue(template.HelpText),
Expand All @@ -259,13 +264,11 @@ func flattenTemplates(ctx context.Context, templates []actiontemplates.ActionTem
convertMapStringToMapAttrValue(template.DisplaySettings),
),
})
if diags.HasError() {
return types.ListNull(types.ObjectType{AttrTypes: getTemplateAttrTypes()}), diags
}

templateList = append(templateList, obj)
}

return types.ListValueMust(types.ObjectType{AttrTypes: getTemplateAttrTypes()}, templateList), nil
return types.ListValueMust(types.ObjectType{AttrTypes: getTemplateAttrTypes()}, templateList)
}

func flattenAutoDeployReleaseOverrides(overrides []projects.AutoDeployReleaseOverride) types.List {
Expand Down
1 change: 1 addition & 0 deletions octopusdeploy_framework/resource_project_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ type deploymentActionPackageModel struct {
}

type templateModel struct {
ID types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
Label types.String `tfsdk:"label"`
HelpText types.String `tfsdk:"help_text"`
Expand Down
14 changes: 2 additions & 12 deletions octopusdeploy_framework/resource_project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,17 +105,6 @@ func TestAccProjectWithUpdate(t *testing.T) {
),
Config: testAccProjectBasic(lifecycleLocalName, lifecycleName, projectGroupLocalName, projectGroupName, localName, name, description),
},
{
Check: resource.ComposeTestCheckFunc(
testAccProjectCheckExists(),
resource.TestCheckResourceAttr(prefix, "description", description),
resource.TestCheckResourceAttr(prefix, "name", name),
resource.TestCheckNoResourceAttr(prefix, "deployment_step.0.windows_service.0.step_name"),
resource.TestCheckNoResourceAttr(prefix, "deployment_step.0.windows_service.1.step_name"),
resource.TestCheckNoResourceAttr(prefix, "deployment_step.0.iis_website.0.step_name"),
),
Config: testAccProjectBasic(lifecycleLocalName, lifecycleName, projectGroupLocalName, projectGroupName, localName, name, description),
},
},
})
}
Expand All @@ -125,7 +114,7 @@ func testAccProjectBasic(lifecycleLocalName string, lifecycleName string, projec
projectGroup.LocalName = projectGroupLocalName
projectGroup.Resource.Name = projectGroupName

return fmt.Sprintf(testAccLifecycle(lifecycleLocalName, lifecycleName)+"\n"+
var cf = fmt.Sprintf(testAccLifecycle(lifecycleLocalName, lifecycleName)+"\n"+
internaltest.ProjectGroupConfiguration(projectGroup)+"\n"+
`resource "octopusdeploy_project" "%s" {
description = "%s"
Expand Down Expand Up @@ -170,6 +159,7 @@ func testAccProjectBasic(lifecycleLocalName string, lifecycleName string, projec
// template = "alskdjaslkdj"
// }
}`, localName, description, lifecycleLocalName, name, projectGroupLocalName)
return cf
}

func testAccProjectCheckDestroy(s *terraform.State) error {
Expand Down

0 comments on commit 7806b35

Please sign in to comment.