From 1dbb5c7d72c5da3d95f7992e82d60392870a48d3 Mon Sep 17 00:00:00 2001 From: Chris Kim Date: Tue, 24 Dec 2024 09:55:48 +1300 Subject: [PATCH 1/2] fix: Project persistence settings error when protected_branches is not provided --- .../resource_project_expand.go | 15 ++++++++++++++- octopusdeploy_framework/schemas/project.go | 7 ++++--- 2 files changed, 18 insertions(+), 4 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..61ea763b7 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" ) @@ -79,7 +80,7 @@ func (p ProjectSchema) GetResourceSchema() resourceSchema.Schema { "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(), "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.", @@ -91,7 +92,7 @@ func (p ProjectSchema) GetResourceSchema() resourceSchema.Schema { "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(), "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.", @@ -104,7 +105,7 @@ func (p ProjectSchema) GetResourceSchema() resourceSchema.Schema { "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(), "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.", From 8781a2fefe9f6a1504838b76f50dc86ae9e13ca3 Mon Sep 17 00:00:00 2001 From: Chris Kim Date: Tue, 24 Dec 2024 10:04:32 +1300 Subject: [PATCH 2/2] fix: Project persistence settings basepath should be set with a default(".octopus") if not provided --- octopusdeploy_framework/schemas/project.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/octopusdeploy_framework/schemas/project.go b/octopusdeploy_framework/schemas/project.go index 61ea763b7..60d9f9f1b 100644 --- a/octopusdeploy_framework/schemas/project.go +++ b/octopusdeploy_framework/schemas/project.go @@ -78,7 +78,7 @@ 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().Computed().PlanModifiers(setplanmodifier.UseStateForUnknown()).Description("A list of protected branch patterns.").Build(), }, @@ -90,7 +90,7 @@ 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().Computed().PlanModifiers(setplanmodifier.UseStateForUnknown()).Description("A list of protected branch patterns.").Build(), }, @@ -103,7 +103,7 @@ 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().Computed().PlanModifiers(setplanmodifier.UseStateForUnknown()).Description("A list of protected branch patterns.").Build(), },