Skip to content

Commit

Permalink
chore: fix template ids for project (#734)
Browse files Browse the repository at this point in the history
  • Loading branch information
benPearce1 authored Aug 13, 2024
1 parent c32f73f commit 8ef9e5f
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 35 deletions.
31 changes: 16 additions & 15 deletions octopusdeploy_framework/resource_project_expand.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/projects"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
"github.com/hashicorp/terraform-plugin-log/tflog"
"net/url"
)

Expand Down Expand Up @@ -50,9 +51,9 @@ func expandProject(ctx context.Context, model projectResourceModel) *projects.Pr
var gitLibrarySettingsList []gitLibraryPersistenceSettingsModel
diags := model.GitLibraryPersistenceSettings.ElementsAs(ctx, &gitLibrarySettingsList, false)
if diags.HasError() {
fmt.Printf("Error converting Git library persistence settings: %v\n", diags)
tflog.Error(ctx, fmt.Sprintf("Error converting Git library persistence settings: %v\n", diags))
} else {
fmt.Printf("Number of Git library persistence settings: %d\n", len(gitLibrarySettingsList))
tflog.Debug(ctx, fmt.Sprintf("Number of Git library persistence settings: %d\n", len(gitLibrarySettingsList)))
if len(gitLibrarySettingsList) > 0 {
project.PersistenceSettings = expandGitLibraryPersistenceSettings(ctx, gitLibrarySettingsList[0])
project.IsVersionControlled = true
Expand All @@ -62,9 +63,9 @@ func expandProject(ctx context.Context, model projectResourceModel) *projects.Pr
var gitUsernamePasswordSettingsList []gitUsernamePasswordPersistenceSettingsModel
diags := model.GitUsernamePasswordPersistenceSettings.ElementsAs(ctx, &gitUsernamePasswordSettingsList, false)
if diags.HasError() {
fmt.Printf("Error converting Git username/password persistence settings: %v\n", diags)
tflog.Error(ctx, fmt.Sprintf("Error converting Git username/password persistence settings: %v\n", diags))
} else {
fmt.Printf("Number of Git username/password persistence settings: %d\n", len(gitUsernamePasswordSettingsList))
tflog.Debug(ctx, fmt.Sprintf("Number of Git username/password persistence settings: %d\n", len(gitUsernamePasswordSettingsList)))
if len(gitUsernamePasswordSettingsList) > 0 {
project.PersistenceSettings = expandGitUsernamePasswordPersistenceSettings(ctx, gitUsernamePasswordSettingsList[0])
project.IsVersionControlled = true
Expand All @@ -74,9 +75,9 @@ func expandProject(ctx context.Context, model projectResourceModel) *projects.Pr
var gitAnonymousSettingsList []gitAnonymousPersistenceSettingsModel
diags := model.GitAnonymousPersistenceSettings.ElementsAs(ctx, &gitAnonymousSettingsList, false)
if diags.HasError() {
fmt.Printf("Error converting Git anonymous persistence settings: %v\n", diags)
tflog.Error(ctx, fmt.Sprintf("Error converting Git anonymous persistence settings: %v\n", diags))
} else {
fmt.Printf("Number of Git anonymous persistence settings: %d\n", len(gitAnonymousSettingsList))
tflog.Debug(ctx, fmt.Sprintf("Number of Git anonymous persistence settings: %d\n", len(gitAnonymousSettingsList)))
if len(gitAnonymousSettingsList) > 0 {
project.PersistenceSettings = expandGitAnonymousPersistenceSettings(ctx, gitAnonymousSettingsList[0])
project.IsVersionControlled = true
Expand Down Expand Up @@ -116,13 +117,13 @@ func expandProject(ctx context.Context, model projectResourceModel) *projects.Pr
var templates []templateModel
diags := model.Template.ElementsAs(ctx, &templates, false)
if diags.HasError() {
fmt.Printf("Error converting templates: %v\n", diags)
tflog.Error(ctx, fmt.Sprintf("Error converting templates: %v\n", diags))
} else {
fmt.Printf("Number of templates: %d\n", len(templates))
tflog.Info(ctx, fmt.Sprintf("Number of templates: %d\n", len(templates)))
project.Templates = expandTemplates(templates)
}
} else {
fmt.Println("Template is null")
tflog.Debug(ctx, "Template is null")
project.Templates = []actiontemplates.ActionTemplateParameter{}
}

Expand All @@ -138,7 +139,7 @@ func expandProject(ctx context.Context, model projectResourceModel) *projects.Pr
}

func expandGitLibraryPersistenceSettings(ctx context.Context, model gitLibraryPersistenceSettingsModel) projects.GitPersistenceSettings {
url, _ := url.Parse(model.URL.ValueString())
gitUrl, _ := url.Parse(model.URL.ValueString())
var protectedBranches []string
model.ProtectedBranches.ElementsAs(ctx, &protectedBranches, false)

Expand All @@ -149,12 +150,12 @@ func expandGitLibraryPersistenceSettings(ctx context.Context, model gitLibraryPe
},
model.DefaultBranch.ValueString(),
protectedBranches,
url,
gitUrl,
)
}

func expandGitUsernamePasswordPersistenceSettings(ctx context.Context, model gitUsernamePasswordPersistenceSettingsModel) projects.GitPersistenceSettings {
url, _ := url.Parse(model.URL.ValueString())
gitUrl, _ := url.Parse(model.URL.ValueString())
var protectedBranches []string
model.ProtectedBranches.ElementsAs(ctx, &protectedBranches, false)

Expand All @@ -168,12 +169,12 @@ func expandGitUsernamePasswordPersistenceSettings(ctx context.Context, model git
usernamePasswordCredential,
model.DefaultBranch.ValueString(),
protectedBranches,
url,
gitUrl,
)
}

func expandGitAnonymousPersistenceSettings(ctx context.Context, model gitAnonymousPersistenceSettingsModel) projects.GitPersistenceSettings {
url, _ := url.Parse(model.URL.ValueString())
gitUrl, _ := url.Parse(model.URL.ValueString())
var protectedBranches []string
model.ProtectedBranches.ElementsAs(ctx, &protectedBranches, false)

Expand All @@ -182,7 +183,7 @@ func expandGitAnonymousPersistenceSettings(ctx context.Context, model gitAnonymo
&credentials.Anonymous{},
model.DefaultBranch.ValueString(),
protectedBranches,
url,
gitUrl,
)
}

Expand Down
29 changes: 11 additions & 18 deletions octopusdeploy_framework/resource_project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func TestAccProjectWithUpdate(t *testing.T) {
resource.TestCheckResourceAttr(prefix, "description", description),
resource.TestCheckResourceAttr(prefix, "name", name),
),
Config: testAccProjectBasic(lifecycleLocalName, lifecycleName, projectGroupLocalName, projectGroupName, localName, name, description),
Config: testAccProjectBasic(lifecycleLocalName, lifecycleName, projectGroupLocalName, projectGroupName, localName, name, description, 2),
},
{
Check: resource.ComposeTestCheckFunc(
Expand All @@ -114,17 +114,22 @@ func TestAccProjectWithUpdate(t *testing.T) {
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),
Config: testAccProjectBasic(lifecycleLocalName, lifecycleName, projectGroupLocalName, projectGroupName, localName, name, description, 3),
},
},
})
}

func testAccProjectBasic(lifecycleLocalName string, lifecycleName string, projectGroupLocalName string, projectGroupName string, localName string, name string, description string) string {
func testAccProjectBasic(lifecycleLocalName string, lifecycleName string, projectGroupLocalName string, projectGroupName string, localName string, name string, description string, templateCount int) string {
projectGroup := internaltest.NewProjectGroupTestOptions()
projectGroup.LocalName = projectGroupLocalName
projectGroup.Resource.Name = projectGroupName

var templates string
for i := 0; i < templateCount; i++ {
templates += fmt.Sprintf("\ntemplate {\n\t\t\t\tname = \"%d\"\n\t\t\t\tdisplay_settings = {\n\t\t\t\t\t\"Octopus.ControlType\": \"SingleLineText\"\n\t\t\t\t}\n\t\t\t}\n", i)
}

return fmt.Sprintf(testAccLifecycle(lifecycleLocalName, lifecycleName)+"\n"+
internaltest.ProjectGroupConfiguration(projectGroup)+"\n"+
`resource "octopusdeploy_project" "%s" {
Expand All @@ -133,20 +138,8 @@ func testAccProjectBasic(lifecycleLocalName string, lifecycleName string, projec
name = "%s"
project_group_id = octopusdeploy_project_group.%s.id
template {
name = "2"
display_settings = {
"Octopus.ControlType": "SingleLineText"
}
}
template {
name = "1"
display_settings = {
"Octopus.ControlType": "SingleLineText"
}
}
%s
versioning_strategy {
template = "#{Octopus.Version.LastMajor}.#{Octopus.Version.LastMinor}.#{Octopus.Version.LastPatch}.#{Octopus.Version.NextRevision}"
}
Expand All @@ -156,7 +149,7 @@ func testAccProjectBasic(lifecycleLocalName string, lifecycleName string, projec
skip_machine_behavior = "None"
}
}`, localName, description, lifecycleLocalName, name, projectGroupLocalName)
}`, localName, description, lifecycleLocalName, name, projectGroupLocalName, templates)
}

func testAccProjectCheckDestroy(s *terraform.State) error {
Expand Down
2 changes: 1 addition & 1 deletion octopusdeploy_framework/resource_tenant_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func testAccTenantBasic(lifecycleLocalName string, lifecycleName string, project
sortOrder := acctest.RandIntRange(0, 10)
useGuidedFailure := false

return fmt.Sprintf(testAccProjectBasic(lifecycleLocalName, lifecycleName, projectGroupLocalName, projectGroupName, projectLocalName, projectName, projectDescription)+"\n"+
return fmt.Sprintf(testAccProjectBasic(lifecycleLocalName, lifecycleName, projectGroupLocalName, projectGroupName, projectLocalName, projectName, projectDescription, 2)+"\n"+
testAccEnvironment(environmentLocalName, environmentName, environmentDescription, allowDynamicInfrastructure, sortOrder, useGuidedFailure)+"\n"+`
resource "octopusdeploy_tenant" "%s" {
description = "%s"
Expand Down
2 changes: 1 addition & 1 deletion octopusdeploy_framework/schemas/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func GetProjectResourceSchema() resourceSchema.Schema {
"template": resourceSchema.ListNestedBlock{
NestedObject: resourceSchema.NestedBlockObject{
Attributes: map[string]resourceSchema.Attribute{
"id": util.ResourceString().Optional().Computed().Description("The ID of the template parameter.").Build(),
"id": util.ResourceString().Optional().Computed().PlanModifiers(stringplanmodifier.UseStateForUnknown()).Description("The ID of the template parameter.").Build(),
"name": util.ResourceString().Required().Description("The name of the variable set by the parameter. The name can contain letters, digits, dashes and periods.").Build(),
"label": util.ResourceString().Optional().Description("The label shown beside the parameter when presented in the deployment process.").Build(),
"help_text": util.ResourceString().Optional().Description("The help presented alongside the parameter input.").Build(),
Expand Down

0 comments on commit 8ef9e5f

Please sign in to comment.