From 857bb8fe0a10cfcf36d9ca8ceed780d506bc4222 Mon Sep 17 00:00:00 2001 From: Chris Kim <97423717+mik-ky@users.noreply.github.com> Date: Tue, 24 Dec 2024 13:10:47 +1300 Subject: [PATCH] Fix project persistence settings (#844) * fix: Project persistence settings error when protected_branches is not provided * fix: Project persistence settings basepath should be set with a default(".octopus") if not provided --- .../resource_project_expand.go | 15 ++++++++++++++- octopusdeploy_framework/schemas/project.go | 13 +++++++------ 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/octopusdeploy_framework/resource_project_expand.go b/octopusdeploy_framework/resource_project_expand.go index aa6877bba..ae03462f0 100644 --- a/octopusdeploy_framework/resource_project_expand.go +++ b/octopusdeploy_framework/resource_project_expand.go @@ -3,6 +3,8 @@ package octopusdeploy_framework import ( "context" "fmt" + "net/url" + "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/actiontemplates" "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/core" "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/credentials" @@ -11,7 +13,6 @@ import ( "github.com/hashicorp/terraform-plugin-framework/types" "github.com/hashicorp/terraform-plugin-framework/types/basetypes" "github.com/hashicorp/terraform-plugin-log/tflog" - "net/url" ) func expandProject(ctx context.Context, model projectResourceModel) *projects.Project { @@ -143,6 +144,10 @@ func expandGitLibraryPersistenceSettings(ctx context.Context, model gitLibraryPe var protectedBranches []string model.ProtectedBranches.ElementsAs(ctx, &protectedBranches, false) + if protectedBranches == nil { + protectedBranches = []string{} + } + return projects.NewGitPersistenceSettings( model.BasePath.ValueString(), credentials.NewReference(model.GitCredentialID.ValueString()), @@ -157,6 +162,10 @@ func expandGitUsernamePasswordPersistenceSettings(ctx context.Context, model git var protectedBranches []string model.ProtectedBranches.ElementsAs(ctx, &protectedBranches, false) + if protectedBranches == nil { + protectedBranches = []string{} + } + return projects.NewGitPersistenceSettings( model.BasePath.ValueString(), credentials.NewUsernamePassword( @@ -174,6 +183,10 @@ func expandGitAnonymousPersistenceSettings(ctx context.Context, model gitAnonymo var protectedBranches []string model.ProtectedBranches.ElementsAs(ctx, &protectedBranches, false) + if protectedBranches == nil { + protectedBranches = []string{} + } + return projects.NewGitPersistenceSettings( model.BasePath.ValueString(), credentials.NewAnonymous(), diff --git a/octopusdeploy_framework/schemas/project.go b/octopusdeploy_framework/schemas/project.go index 93d7b32a9..60d9f9f1b 100644 --- a/octopusdeploy_framework/schemas/project.go +++ b/octopusdeploy_framework/schemas/project.go @@ -8,6 +8,7 @@ import ( "github.com/hashicorp/terraform-plugin-framework/resource/schema/listplanmodifier" "github.com/hashicorp/terraform-plugin-framework/resource/schema/objectplanmodifier" "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" + "github.com/hashicorp/terraform-plugin-framework/resource/schema/setplanmodifier" "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" "github.com/hashicorp/terraform-plugin-framework/types" ) @@ -77,9 +78,9 @@ func (p ProjectSchema) GetResourceSchema() resourceSchema.Schema { NestedObject: resourceSchema.NestedBlockObject{ Attributes: map[string]resourceSchema.Attribute{ "url": util.ResourceString().Required().Description("The URL associated with these version control settings.").Build(), - "base_path": util.ResourceString().Optional().Description("The base path associated with these version control settings.").Build(), + "base_path": util.ResourceString().Optional().Computed().Default(".octopus").Description("The base path associated with these version control settings.").Build(), "default_branch": util.ResourceString().Optional().Description("The default branch associated with these version control settings.").Build(), - "protected_branches": util.ResourceSet(types.StringType).Optional().Description("A list of protected branch patterns.").Build(), + "protected_branches": util.ResourceSet(types.StringType).Optional().Computed().PlanModifiers(setplanmodifier.UseStateForUnknown()).Description("A list of protected branch patterns.").Build(), }, }, Description: "Provides Git-related persistence settings for a version-controlled project.", @@ -89,9 +90,9 @@ func (p ProjectSchema) GetResourceSchema() resourceSchema.Schema { Attributes: map[string]resourceSchema.Attribute{ "git_credential_id": util.ResourceString().Required().Build(), "url": util.ResourceString().Required().Description("The URL associated with these version control settings.").Build(), - "base_path": util.ResourceString().Optional().Description("The base path associated with these version control settings.").Build(), + "base_path": util.ResourceString().Optional().Computed().Default(".octopus").Description("The base path associated with these version control settings.").Build(), "default_branch": util.ResourceString().Optional().Description("The default branch associated with these version control settings.").Build(), - "protected_branches": util.ResourceSet(types.StringType).Optional().Description("A list of protected branch patterns.").Build(), + "protected_branches": util.ResourceSet(types.StringType).Optional().Computed().PlanModifiers(setplanmodifier.UseStateForUnknown()).Description("A list of protected branch patterns.").Build(), }, }, Description: "Provides Git-related persistence settings for a version-controlled project.", @@ -102,9 +103,9 @@ func (p ProjectSchema) GetResourceSchema() resourceSchema.Schema { "url": util.ResourceString().Required().Description("The URL associated with these version control settings.").Build(), "username": util.ResourceString().Required().Description("The username for the Git credential.").Build(), "password": util.ResourceString().Sensitive().Required().Description("The password for the Git credential").Build(), //util.GetPasswordResourceSchema(false), - "base_path": util.ResourceString().Optional().Description("The base path associated with these version control settings.").Build(), + "base_path": util.ResourceString().Optional().Computed().Default(".octopus").Description("The base path associated with these version control settings.").Build(), "default_branch": util.ResourceString().Optional().Description("The default branch associated with these version control settings.").Build(), - "protected_branches": util.ResourceSet(types.StringType).Optional().Description("A list of protected branch patterns.").Build(), + "protected_branches": util.ResourceSet(types.StringType).Optional().Computed().PlanModifiers(setplanmodifier.UseStateForUnknown()).Description("A list of protected branch patterns.").Build(), }, }, Description: "Provides Git-related persistence settings for a version-controlled project.",