From ee24359afbe9cf34bfdea93b30c67d696f386d3c Mon Sep 17 00:00:00 2001 From: Lourens de Jager <165963988+lourens-octopus@users.noreply.github.com> Date: Tue, 3 Dec 2024 10:50:32 +1300 Subject: [PATCH] feat: Add Git trigger functionality --- docs/resources/git_trigger.md | 45 ++++ octopusdeploy_framework/framework_provider.go | 1 + .../resource_git_trigger.go | 240 ++++++++++++++++++ .../resource_git_trigger_test.go | 220 ++++++++++++++++ .../schemas/git_trigger.go | 66 +++++ .../45a-projectwithgitdependency/config.tf | 7 + .../45a-projectwithgitdependency/project.tf | 89 +++++++ .../project_group_test.tf | 4 + .../45a-projectwithgitdependency/provider.tf | 5 + .../provider_vars.tf | 18 ++ .../45a-projectwithgitdependency/space.tf | 3 + 11 files changed, 698 insertions(+) create mode 100644 docs/resources/git_trigger.md create mode 100644 octopusdeploy_framework/resource_git_trigger.go create mode 100644 octopusdeploy_framework/resource_git_trigger_test.go create mode 100644 octopusdeploy_framework/schemas/git_trigger.go create mode 100644 terraform/45a-projectwithgitdependency/config.tf create mode 100644 terraform/45a-projectwithgitdependency/project.tf create mode 100644 terraform/45a-projectwithgitdependency/project_group_test.tf create mode 100644 terraform/45a-projectwithgitdependency/provider.tf create mode 100644 terraform/45a-projectwithgitdependency/provider_vars.tf create mode 100644 terraform/45a-projectwithgitdependency/space.tf diff --git a/docs/resources/git_trigger.md b/docs/resources/git_trigger.md new file mode 100644 index 000000000..6376c9045 --- /dev/null +++ b/docs/resources/git_trigger.md @@ -0,0 +1,45 @@ +--- +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "octopusdeploy_git_trigger Resource - terraform-provider-octopusdeploy" +subcategory: "" +description: |- + This resource manages Git triggers in Octopus Deploy +--- + +# octopusdeploy_git_trigger (Resource) + +This resource manages Git triggers in Octopus Deploy + + + + +## Schema + +### Required + +- `channel_id` (String) The ID of the channel in which the release will be created if the action type is CreateRelease. +- `name` (String) The name of this resource. +- `project_id` (String) The ID of the project to attach the trigger. +- `sources` (Attributes List) (see [below for nested schema](#nestedatt--sources)) + +### Optional + +- `description` (String) The description of this Git trigger.. +- `is_disabled` (Boolean) Disables the trigger from being run when set. +- `space_id` (String) The space ID associated with this Git trigger. + +### Read-Only + +- `id` (String) The unique ID for this resource. + + +### Nested Schema for `sources` + +Required: + +- `deployment_action_slug` (String) The deployment action slug. +- `exclude_file_paths` (List of String) The file paths to exclude. +- `git_dependency_name` (String) The git dependency name. +- `include_file_paths` (List of String) The file paths to include. + + diff --git a/octopusdeploy_framework/framework_provider.go b/octopusdeploy_framework/framework_provider.go index d39492121..30595cac3 100644 --- a/octopusdeploy_framework/framework_provider.go +++ b/octopusdeploy_framework/framework_provider.go @@ -132,6 +132,7 @@ func (p *octopusDeployFrameworkProvider) Resources(ctx context.Context) []func() NewServiceAccountOIDCIdentity, NewGenericOidcResource, NewDeploymentFreezeTenantResource, + NewGitTriggerResource, } } diff --git a/octopusdeploy_framework/resource_git_trigger.go b/octopusdeploy_framework/resource_git_trigger.go new file mode 100644 index 000000000..453b63d5a --- /dev/null +++ b/octopusdeploy_framework/resource_git_trigger.go @@ -0,0 +1,240 @@ +package octopusdeploy_framework + +import ( + "context" + "fmt" + "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/actions" + "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/filters" + "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/projects" + "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/triggers" + "github.com/OctopusDeploy/terraform-provider-octopusdeploy/internal/errors" + "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/path" + "github.com/hashicorp/terraform-plugin-framework/resource" + "github.com/hashicorp/terraform-plugin-framework/types" + "github.com/hashicorp/terraform-plugin-log/tflog" +) + +type gitTriggerResource struct { + *Config +} + +func NewGitTriggerResource() resource.Resource { + return &gitTriggerResource{} +} + +var _ resource.ResourceWithImportState = &gitTriggerResource{} + +func (r *gitTriggerResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { + resp.TypeName = util.GetTypeName("git_trigger") +} + +func (r *gitTriggerResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) { + resp.Schema = schemas.GitTriggerSchema{}.GetResourceSchema() +} + +func (r *gitTriggerResource) Configure(_ context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) { + r.Config = ResourceConfiguration(req, resp) +} + +func (r *gitTriggerResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { + resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp) +} + +func (r *gitTriggerResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { + var data *schemas.GitTriggerResourceModel + resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...) + if resp.Diagnostics.HasError() { + return + } + + gitTriggerSources := convertListToGitTriggerSources(data.Sources) + + action := actions.NewCreateReleaseAction(data.ChannelId.ValueString()) + filter := filters.NewGitTriggerFilter(gitTriggerSources) + + client := r.Config.Client + + project, err := projects.GetByID(client, data.SpaceId.ValueString(), data.ProjectId.ValueString()) + + if err != nil { + resp.Diagnostics.AddError("error finding project", err.Error()) + return + } + + tflog.Info(ctx, fmt.Sprintf("creating Git trigger: %s", data.Name.ValueString())) + + projectTrigger := triggers.NewProjectTrigger(data.Name.ValueString(), data.Description.ValueString(), data.IsDisabled.ValueBool(), project, action, filter) + + createdGitTrigger, err := client.ProjectTriggers.Add(projectTrigger) + + if err != nil { + resp.Diagnostics.AddError("unable to create Git trigger", err.Error()) + return + } + + data.ID = types.StringValue(createdGitTrigger.GetID()) + data.Name = types.StringValue(createdGitTrigger.Name) + data.ProjectId = types.StringValue(createdGitTrigger.ProjectID) + data.SpaceId = types.StringValue(createdGitTrigger.SpaceID) + data.IsDisabled = types.BoolValue(createdGitTrigger.IsDisabled) + data.Sources = convertGitTriggerSourcesToList(createdGitTrigger.Filter.(*filters.GitTriggerFilter).Sources) + + tflog.Info(ctx, fmt.Sprintf("Git trigger created (%s)", data.ID)) + resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) +} + +func (r *gitTriggerResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { + var data *schemas.GitTriggerResourceModel + resp.Diagnostics.Append(req.State.Get(ctx, &data)...) + if resp.Diagnostics.HasError() { + return + } + + tflog.Info(ctx, fmt.Sprintf("reading Git Trigger (%s)", data.ID)) + + client := r.Config.Client + + gitTrigger, err := client.ProjectTriggers.GetByID(data.ID.ValueString()) + if err != nil { + if err := errors.ProcessApiErrorV2(ctx, resp, data, err, "error retrieving Git Trigger"); err != nil { + resp.Diagnostics.AddError("unable to load Git Trigger", err.Error()) + } + return + } + + data.ID = types.StringValue(gitTrigger.GetID()) + data.Name = types.StringValue(gitTrigger.Name) + data.ProjectId = types.StringValue(gitTrigger.ProjectID) + data.SpaceId = types.StringValue(gitTrigger.SpaceID) + data.IsDisabled = types.BoolValue(gitTrigger.IsDisabled) + data.Sources = convertGitTriggerSourcesToList(gitTrigger.Filter.(*filters.GitTriggerFilter).Sources) + + resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) +} + +func (r *gitTriggerResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { + var data, state *schemas.GitTriggerResourceModel + resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...) + resp.Diagnostics.Append(req.State.Get(ctx, &state)...) + if resp.Diagnostics.HasError() { + return + } + + tflog.Debug(ctx, fmt.Sprintf("updating Git Trigger '%s'", data.ID.ValueString())) + + client := r.Config.Client + + gitTrigger, err := client.ProjectTriggers.GetByID(data.ID.ValueString()) + if err != nil { + resp.Diagnostics.AddError("unable to load Git Trigger", err.Error()) + return + } + + gitTriggerSources := convertListToGitTriggerSources(data.Sources) + action := actions.NewCreateReleaseAction(data.ChannelId.ValueString()) + filter := filters.NewGitTriggerFilter(gitTriggerSources) + project, err := projects.GetByID(client, data.SpaceId.ValueString(), data.ProjectId.ValueString()) + + if err != nil { + resp.Diagnostics.AddError("error finding project", err.Error()) + return + } + + updatedGitTrigger := triggers.NewProjectTrigger(data.Name.ValueString(), data.Description.ValueString(), data.IsDisabled.ValueBool(), project, action, filter) + updatedGitTrigger.ID = gitTrigger.ID + + updatedGitTrigger, err = client.ProjectTriggers.Update(updatedGitTrigger) + tflog.Info(ctx, fmt.Sprintf("Git Trigger updated (%s)", data.ID)) + + resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) +} + +func (r *gitTriggerResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { + var data schemas.GitTriggerResourceModel + + resp.Diagnostics.Append(req.State.Get(ctx, &data)...) + if resp.Diagnostics.HasError() { + return + } + + client := r.Config.Client + + if err := client.ProjectTriggers.DeleteByID(data.ID.ValueString()); err != nil { + resp.Diagnostics.AddError("unable to delete Git Trigger", err.Error()) + return + } +} + +func convertListToGitTriggerSources(list types.List) []filters.GitTriggerSource { + var gitTriggerSources []filters.GitTriggerSource + + for _, elem := range list.Elements() { + obj := elem.(types.Object) + attrs := obj.Attributes() + + deploymentActionSlug := attrs["deployment_action_slug"].(types.String).ValueString() + gitDependencyName := attrs["git_dependency_name"].(types.String).ValueString() + includeFilePaths := convertToStringSlice(attrs["include_file_paths"].(types.List)) + excludeFilePaths := convertToStringSlice(attrs["exclude_file_paths"].(types.List)) + + gitTriggerSource := filters.GitTriggerSource{ + DeploymentActionSlug: deploymentActionSlug, + GitDependencyName: gitDependencyName, + IncludeFilePaths: includeFilePaths, + ExcludeFilePaths: excludeFilePaths, + } + + gitTriggerSources = append(gitTriggerSources, gitTriggerSource) + } + + return gitTriggerSources +} + +func convertToStringSlice(list types.List) []string { + var result []string + for _, elem := range list.Elements() { + result = append(result, elem.(types.String).ValueString()) + } + return result +} + +func convertGitTriggerSourcesToList(gitTriggerSources []filters.GitTriggerSource) types.List { + var elements []attr.Value + + for _, source := range gitTriggerSources { + attributes := map[string]attr.Value{ + "deployment_action_slug": types.StringValue(source.DeploymentActionSlug), + "git_dependency_name": types.StringValue(source.GitDependencyName), + "include_file_paths": convertStringSliceToList(source.IncludeFilePaths), + "exclude_file_paths": convertStringSliceToList(source.ExcludeFilePaths), + } + objectValue, _ := types.ObjectValue(sourcesObjectType(), attributes) + elements = append(elements, objectValue) + } + + listValue, _ := types.ListValue(types.ObjectType{AttrTypes: sourcesObjectType()}, elements) + return listValue +} + +func convertStringSliceToList(strings []string) types.List { + var elements []attr.Value + + for _, str := range strings { + elements = append(elements, types.StringValue(str)) + } + + listValue, _ := types.ListValue(types.StringType, elements) + return listValue +} + +func sourcesObjectType() map[string]attr.Type { + return map[string]attr.Type{ + "deployment_action_slug": types.StringType, + "git_dependency_name": types.StringType, + "include_file_paths": types.ListType{ElemType: types.StringType}, + "exclude_file_paths": types.ListType{ElemType: types.StringType}, + } +} diff --git a/octopusdeploy_framework/resource_git_trigger_test.go b/octopusdeploy_framework/resource_git_trigger_test.go new file mode 100644 index 000000000..53076844b --- /dev/null +++ b/octopusdeploy_framework/resource_git_trigger_test.go @@ -0,0 +1,220 @@ +package octopusdeploy_framework + +import ( + "fmt" + "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/channels" + "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/deployments" + "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/projects" + "github.com/OctopusSolutionsEngineering/OctopusTerraformTestFramework/test" + "github.com/hashicorp/terraform-plugin-testing/helper/acctest" + "github.com/hashicorp/terraform-plugin-testing/helper/resource" + "github.com/hashicorp/terraform-plugin-testing/terraform" + "path/filepath" + "strconv" + "strings" + "testing" +) + +type gitTriggerSourcesTestData struct { + deploymentActionSlug string + gitDependencyName string + includeFilePaths []string + excludeFilePaths []string +} + +type gitTriggerTestData struct { + name string + description string + projectId string + spaceId string + channelId string + isDisabled bool + sources []gitTriggerSourcesTestData +} + +func TestAccOctopusDeployGitTrigger(t *testing.T) { + projectId, spaceId, actionSlug, channelId := setupTestSpace(t) + + localName := acctest.RandStringFromCharSet(20, acctest.CharSetAlpha) + prefix := "octopusdeploy_git_trigger." + localName + createData := gitTriggerTestData{ + name: acctest.RandStringFromCharSet(20, acctest.CharSetAlpha), + description: acctest.RandStringFromCharSet(20, acctest.CharSetAlpha), + projectId: projectId, + channelId: channelId, + spaceId: spaceId, + isDisabled: false, + sources: []gitTriggerSourcesTestData{ + { + deploymentActionSlug: actionSlug, + gitDependencyName: "", + includeFilePaths: []string{acctest.RandStringFromCharSet(20, acctest.CharSetAlpha)}, + excludeFilePaths: []string{acctest.RandStringFromCharSet(20, acctest.CharSetAlpha)}, + }, + }, + } + updateData := gitTriggerTestData{ + name: createData.name + "-updated", + description: createData.description + "-updated", + projectId: createData.projectId, + channelId: createData.channelId, + spaceId: createData.spaceId, + isDisabled: true, + sources: []gitTriggerSourcesTestData{ + { + deploymentActionSlug: createData.sources[0].deploymentActionSlug, + gitDependencyName: createData.sources[0].gitDependencyName, + includeFilePaths: []string{createData.sources[0].includeFilePaths[0] + "-updated"}, + excludeFilePaths: []string{createData.sources[0].excludeFilePaths[0] + "-updated"}, + }, + }, + } + + resource.Test(t, resource.TestCase{ + CheckDestroy: func(s *terraform.State) error { return testGitTriggerCheckDestroy(s) }, + PreCheck: func() { TestAccPreCheck(t) }, + ProtoV6ProviderFactories: ProtoV6ProviderFactories(), + Steps: []resource.TestStep{ + { + Config: testGitTriggerBasic(createData, localName), + Check: testAssertGitTriggerAttributes(createData, prefix), + }, + { + Config: testGitTriggerBasic(updateData, localName), + Check: testAssertGitTriggerAttributes(updateData, prefix), + }, + }, + }) +} + +func setupTestSpace(t *testing.T) (string, string, string, string) { + testFramework := test.OctopusContainerTest{} + + spaceId := octoClient.GetSpaceID() + + err := testFramework.TerraformInitAndApply(t, octoContainer, filepath.Join("../terraform", "45a-projectwithgitdependency"), spaceId, []string{}) + + if err != nil { + t.Fatal(err.Error()) + } + + query := projects.ProjectsQuery{ + PartialName: "Test", + Skip: 0, + Take: 1, + } + + projectResources, err := octoClient.Projects.Get(query) + if err != nil { + t.Fatal(err.Error()) + } + + if len(projectResources.Items) == 0 { + t.Fatalf("Space must have a project called \"Test\"") + } + project := projectResources.Items[0] + + channelResources, err := channels.GetAll(octoClient, spaceId) + + if err != nil { + t.Fatal(err.Error()) + } + + if len(channelResources) == 0 { + t.Fatalf("Space must have a channel") + } + + var projectChannel *channels.Channel + + for _, channel := range channelResources { + if channel.ProjectID == project.ID { + projectChannel = channel + break + } + } + + if projectChannel == nil { + t.Fatalf("No channel found for project ID: %s", project.ID) + } + + deploymentProccessResource, err := deployments.GetDeploymentProcessByID(octoClient, spaceId, project.DeploymentProcessID) + + if err != nil { + t.Fatal(err.Error()) + } + + actionSlug := deploymentProccessResource.Steps[0].Actions[0].Slug + + return project.ID, spaceId, actionSlug, projectChannel.ID +} + +func testGitTriggerBasic(data gitTriggerTestData, localName string) string { + return fmt.Sprintf(` + resource "octopusdeploy_git_trigger" "%s" { + name = "%s" + space_id = "%s" + project_id = "%s" + channel_id = "%s" + is_disabled = "%t" + sources = [%s] + } + `, + localName, + data.name, + data.spaceId, + data.projectId, + data.channelId, + data.isDisabled, + convertGitTriggerSourcesToString(data.sources), + ) +} + +func testAssertGitTriggerAttributes(expected gitTriggerTestData, prefix string) resource.TestCheckFunc { + return resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr(prefix, "name", expected.name), + resource.TestCheckResourceAttr(prefix, "space_id", expected.spaceId), + resource.TestCheckResourceAttr(prefix, "project_id", expected.projectId), + resource.TestCheckResourceAttr(prefix, "channel_id", expected.channelId), + resource.TestCheckResourceAttr(prefix, "is_disabled", strconv.FormatBool(expected.isDisabled)), + resource.TestCheckResourceAttr(prefix, "sources.0.include_file_paths.0", expected.sources[0].includeFilePaths[0]), + resource.TestCheckResourceAttr(prefix, "sources.0.exclude_file_paths.0", expected.sources[0].excludeFilePaths[0]), + ) +} + +func testGitTriggerCheckDestroy(s *terraform.State) error { + for _, rs := range s.RootModule().Resources { + if rs.Type != "octopusdeploy_git_trigger" { + continue + } + + projectTrigger, err := octoClient.ProjectTriggers.GetByID(rs.Primary.ID) + if err == nil && projectTrigger != nil { + return fmt.Errorf("git trigger (%s) still exists", rs.Primary.ID) + } + } + + return nil +} + +func convertGitTriggerSourcesToString(sources []gitTriggerSourcesTestData) string { + var result string + for _, source := range sources { + result += fmt.Sprintf(` + { + deployment_action_slug = "%s" + git_dependency_name = "%s" + include_file_paths = [%s] + exclude_file_paths = [%s] + }`, + source.deploymentActionSlug, + source.gitDependencyName, + convertStringSliceToString(source.includeFilePaths), + convertStringSliceToString(source.excludeFilePaths), + ) + } + return result +} + +func convertStringSliceToString(slice []string) string { + return fmt.Sprintf(`"%s"`, strings.Join(slice, `", "`)) +} diff --git a/octopusdeploy_framework/schemas/git_trigger.go b/octopusdeploy_framework/schemas/git_trigger.go new file mode 100644 index 000000000..b4fe25c4e --- /dev/null +++ b/octopusdeploy_framework/schemas/git_trigger.go @@ -0,0 +1,66 @@ +package schemas + +import ( + "github.com/OctopusDeploy/terraform-provider-octopusdeploy/octopusdeploy_framework/util" + datasourceSchema "github.com/hashicorp/terraform-plugin-framework/datasource/schema" + resourceSchema "github.com/hashicorp/terraform-plugin-framework/resource/schema" + "github.com/hashicorp/terraform-plugin-framework/types" +) + +type GitTriggerSchema struct{} + +var _ EntitySchema = GitTriggerSchema{} + +func (d GitTriggerSchema) GetResourceSchema() resourceSchema.Schema { + return resourceSchema.Schema{ + Description: "This resource manages Git triggers in Octopus Deploy", + Attributes: map[string]resourceSchema.Attribute{ + "id": GetIdResourceSchema(), + "name": GetNameResourceSchema(true), + "description": GetDescriptionResourceSchema("Git trigger."), + "space_id": GetSpaceIdResourceSchema("Git trigger"), + "project_id": GetRequiredStringResourceSchema("The ID of the project to attach the trigger."), + "channel_id": GetRequiredStringResourceSchema("The ID of the channel in which the release will be created if the action type is CreateRelease."), + "sources": GetSourcesAttributeSchema(), + "is_disabled": GetOptionalBooleanResourceAttribute("Disables the trigger from being run when set.", false), + }, + } +} + +func GetSourcesAttributeSchema() datasourceSchema.ListNestedAttribute { + return datasourceSchema.ListNestedAttribute{ + Required: true, + NestedObject: datasourceSchema.NestedAttributeObject{ + Attributes: map[string]datasourceSchema.Attribute{ + "deployment_action_slug": util.DataSourceString().Required().Description("The deployment action slug.").Build(), + "git_dependency_name": util.DataSourceString().Required().Description("The git dependency name.").Build(), + "include_file_paths": resourceSchema.ListAttribute{ + Required: true, + ElementType: types.StringType, + Description: "The file paths to include.", + }, + "exclude_file_paths": resourceSchema.ListAttribute{ + Required: true, + ElementType: types.StringType, + Description: "The file paths to exclude.", + }, + }, + }, + } +} + +func (d GitTriggerSchema) GetDatasourceSchema() datasourceSchema.Schema { + return datasourceSchema.Schema{} +} + +type GitTriggerResourceModel struct { + Name types.String `tfsdk:"name"` + Description types.String `tfsdk:"description"` + SpaceId types.String `tfsdk:"space_id"` + ProjectId types.String `tfsdk:"project_id"` + ChannelId types.String `tfsdk:"channel_id"` + Sources types.List `tfsdk:"sources"` + IsDisabled types.Bool `tfsdk:"is_disabled"` + + ResourceModel +} diff --git a/terraform/45a-projectwithgitdependency/config.tf b/terraform/45a-projectwithgitdependency/config.tf new file mode 100644 index 000000000..96443e7a1 --- /dev/null +++ b/terraform/45a-projectwithgitdependency/config.tf @@ -0,0 +1,7 @@ +terraform { + required_providers { + octopusdeploy = { source = "OctopusDeployLabs/octopusdeploy", version = "0.18.1" } + // Use the option below when debugging + // octopusdeploy = { source = "octopus.com/com/octopusdeploy" } + } +} diff --git a/terraform/45a-projectwithgitdependency/project.tf b/terraform/45a-projectwithgitdependency/project.tf new file mode 100644 index 000000000..bb5236fd7 --- /dev/null +++ b/terraform/45a-projectwithgitdependency/project.tf @@ -0,0 +1,89 @@ +data "octopusdeploy_lifecycles" "lifecycle_default_lifecycle" { + ids = null + partial_name = "Default Lifecycle" + skip = 0 + take = 1 +} + + +resource "octopusdeploy_project" "deploy_frontend_project" { + auto_create_release = false + default_guided_failure_mode = "EnvironmentDefault" + default_to_skip_if_already_installed = false + description = "Test project" + discrete_channel_release = false + is_disabled = false + is_discrete_channel_release = false + is_version_controlled = false + lifecycle_id = data.octopusdeploy_lifecycles.lifecycle_default_lifecycle.lifecycles[0].id + name = "Test" + project_group_id = octopusdeploy_project_group.project_group_test.id + tenanted_deployment_participation = "Untenanted" + space_id = var.octopus_space_id + included_library_variable_sets = [] + versioning_strategy { + template = "#{Octopus.Version.LastMajor}.#{Octopus.Version.LastMinor}.#{Octopus.Version.LastPatch}.#{Octopus.Version.NextRevision}" + } + + connectivity_policy { + allow_deployments_to_no_targets = false + exclude_unhealthy_targets = false + skip_machine_behavior = "SkipUnavailableMachines" + } +} + +resource "octopusdeploy_channel" "test_channel_1" { + name = "Test Channel 1" + project_id = "${octopusdeploy_project.deploy_frontend_project.id}" + space_id = var.octopus_space_id + lifecycle_id = data.octopusdeploy_lifecycles.lifecycle_default_lifecycle.lifecycles[0].id +} + +resource "octopusdeploy_deployment_process" "deployment_process_project_noopterraform" { + project_id = "${octopusdeploy_project.deploy_frontend_project.id}" + + step { + condition = "Success" + name = "Git Dependency" + package_requirement = "LetOctopusDecide" + start_trigger = "StartAfterPrevious" + properties = {} + target_roles = ["bread"] + + action { + action_type = "Octopus.KubernetesDeployRawYaml" + name = "Git Dependency" + sort_order = 1 + condition = "Success" + run_on_server = true + is_disabled = false + can_be_used_for_project_versioning = false + is_required = true + worker_pool_id = "" + properties = { + "Octopus.Action.GitRepository.Source": "External", + "Octopus.Action.Kubernetes.DeploymentTimeout" = "180", + "Octopus.Action.Kubernetes.ResourceStatusCheck" = "True", + "Octopus.Action.Kubernetes.ServerSideApply.Enabled" = "True", + "Octopus.Action.Kubernetes.ServerSideApply.ForceConflicts" = "True", + "Octopus.Action.KubernetesContainers.CustomResourceYamlFileName" = "test", + "Octopus.Action.Script.ScriptSource" = "GitRepository" + } + environments = [] + excluded_environments = [] + channels = [] + tenant_tags = [] + features = [] + + git_dependency { + default_branch = "main" + file_path_filters = [ + "test" + ] + git_credential_id = "GitCredentials-1" + git_credential_type = "Library" + repository_uri = "https://github.com/octopus/testing.git" + } + } + } +} \ No newline at end of file diff --git a/terraform/45a-projectwithgitdependency/project_group_test.tf b/terraform/45a-projectwithgitdependency/project_group_test.tf new file mode 100644 index 000000000..438a4f9c7 --- /dev/null +++ b/terraform/45a-projectwithgitdependency/project_group_test.tf @@ -0,0 +1,4 @@ +resource "octopusdeploy_project_group" "project_group_test" { + name = "Test" + description = "Test Description" +} diff --git a/terraform/45a-projectwithgitdependency/provider.tf b/terraform/45a-projectwithgitdependency/provider.tf new file mode 100644 index 000000000..a04197720 --- /dev/null +++ b/terraform/45a-projectwithgitdependency/provider.tf @@ -0,0 +1,5 @@ +provider "octopusdeploy" { + address = "${var.octopus_server}" + api_key = "${var.octopus_apikey}" + space_id = "${var.octopus_space_id}" +} diff --git a/terraform/45a-projectwithgitdependency/provider_vars.tf b/terraform/45a-projectwithgitdependency/provider_vars.tf new file mode 100644 index 000000000..c7d93fe40 --- /dev/null +++ b/terraform/45a-projectwithgitdependency/provider_vars.tf @@ -0,0 +1,18 @@ +variable "octopus_server" { + type = string + nullable = false + sensitive = false + description = "The URL of the Octopus server e.g. https://myinstance.octopus.app." +} +variable "octopus_apikey" { + type = string + nullable = false + sensitive = true + description = "The API key used to access the Octopus server. See https://octopus.com/docs/octopus-rest-api/how-to-create-an-api-key for details on creating an API key." +} +variable "octopus_space_id" { + type = string + nullable = false + sensitive = false + description = "The space ID to populate" +} diff --git a/terraform/45a-projectwithgitdependency/space.tf b/terraform/45a-projectwithgitdependency/space.tf new file mode 100644 index 000000000..ee59bdc80 --- /dev/null +++ b/terraform/45a-projectwithgitdependency/space.tf @@ -0,0 +1,3 @@ +output "octopus_space_id" { + value = var.octopus_space_id +}