diff --git a/docs/resources/deployment_process.md b/docs/resources/deployment_process.md index 788f45a6f..dbcc57c9d 100644 --- a/docs/resources/deployment_process.md +++ b/docs/resources/deployment_process.md @@ -194,6 +194,7 @@ Optional: - `environments` (List of String) The environments within which this deployment action will run. - `excluded_environments` (List of String) The environments that this step will be skipped in - `features` (List of String) A list of enabled features for this action. +- `git_dependency` (Block Set, Max: 1) Configuration for resource sourcing from a git repository. (see [below for nested schema](#nestedblock--step--action--git_dependency)) - `id` (String) The unique ID for this resource. - `is_disabled` (Boolean) Indicates the disabled status of this deployment action. - `is_required` (Boolean) Indicates the required status of this deployment action. @@ -229,6 +230,21 @@ Optional: - `image` (String) + +### Nested Schema for `step.action.git_dependency` + +Required: + +- `default_branch` (String) Name of the default branch of the repository. +- `git_credential_type` (String) The Git credential authentication type. +- `repository_uri` (String) The Git URI for the repository where this resource is sourced from. + +Optional: + +- `file_path_filters` (List of String) List of file path filters used to narrow down the directory where files are to be sourced from. Supports glob patten syntax. +- `git_credential_id` (String) ID of an existing Git credential. + + ### Nested Schema for `step.action.package` @@ -283,6 +299,7 @@ Optional: - `environments` (List of String) The environments within which this deployment action will run. - `excluded_environments` (List of String) The environments that this step will be skipped in - `features` (List of String) A list of enabled features for this action. +- `git_dependency` (Block Set, Max: 1) Configuration for resource sourcing from a git repository. (see [below for nested schema](#nestedblock--step--apply_terraform_template_action--git_dependency)) - `google_cloud_account` (Block Set, Max: 1) (see [below for nested schema](#nestedblock--step--apply_terraform_template_action--google_cloud_account)) - `id` (String) The unique ID for this resource. - `inline_template` (String) @@ -362,6 +379,21 @@ Optional: - `image` (String) + +### Nested Schema for `step.apply_terraform_template_action.git_dependency` + +Required: + +- `default_branch` (String) Name of the default branch of the repository. +- `git_credential_type` (String) The Git credential authentication type. +- `repository_uri` (String) The Git URI for the repository where this resource is sourced from. + +Optional: + +- `file_path_filters` (List of String) List of file path filters used to narrow down the directory where files are to be sourced from. Supports glob patten syntax. +- `git_credential_id` (String) ID of an existing Git credential. + + ### Nested Schema for `step.apply_terraform_template_action.google_cloud_account` @@ -439,6 +471,7 @@ Optional: - `environments` (List of String) The environments within which this deployment action will run. - `excluded_environments` (List of String) The environments that this step will be skipped in - `features` (List of String) A list of enabled features for this action. +- `git_dependency` (Block Set, Max: 1) Configuration for resource sourcing from a git repository. (see [below for nested schema](#nestedblock--step--deploy_kubernetes_secret_action--git_dependency)) - `id` (String) The unique ID for this resource. - `is_disabled` (Boolean) Indicates the disabled status of this deployment action. - `is_required` (Boolean) Indicates the required status of this deployment action. @@ -473,6 +506,21 @@ Optional: - `image` (String) + +### Nested Schema for `step.deploy_kubernetes_secret_action.git_dependency` + +Required: + +- `default_branch` (String) Name of the default branch of the repository. +- `git_credential_type` (String) The Git credential authentication type. +- `repository_uri` (String) The Git URI for the repository where this resource is sourced from. + +Optional: + +- `file_path_filters` (List of String) List of file path filters used to narrow down the directory where files are to be sourced from. Supports glob patten syntax. +- `git_credential_id` (String) ID of an existing Git credential. + + ### Nested Schema for `step.deploy_kubernetes_secret_action.package` @@ -508,6 +556,7 @@ Optional: - `environments` (List of String) The environments within which this deployment action will run. - `excluded_environments` (List of String) The environments that this step will be skipped in - `features` (List of String) A list of enabled features for this action. +- `git_dependency` (Block Set, Max: 1) Configuration for resource sourcing from a git repository. (see [below for nested schema](#nestedblock--step--deploy_package_action--git_dependency)) - `id` (String) The unique ID for this resource. - `is_disabled` (Boolean) Indicates the disabled status of this deployment action. - `is_required` (Boolean) Indicates the required status of this deployment action. @@ -556,6 +605,21 @@ Optional: - `image` (String) + +### Nested Schema for `step.deploy_package_action.git_dependency` + +Required: + +- `default_branch` (String) Name of the default branch of the repository. +- `git_credential_type` (String) The Git credential authentication type. +- `repository_uri` (String) The Git URI for the repository where this resource is sourced from. + +Optional: + +- `file_path_filters` (List of String) List of file path filters used to narrow down the directory where files are to be sourced from. Supports glob patten syntax. +- `git_credential_id` (String) ID of an existing Git credential. + + ### Nested Schema for `step.deploy_package_action.package` @@ -621,6 +685,7 @@ Optional: - `environments` (List of String) The environments within which this deployment action will run. - `excluded_environments` (List of String) The environments that this step will be skipped in - `features` (List of String) A list of enabled features for this action. +- `git_dependency` (Block Set, Max: 1) Configuration for resource sourcing from a git repository. (see [below for nested schema](#nestedblock--step--deploy_windows_service_action--git_dependency)) - `id` (String) The unique ID for this resource. - `is_disabled` (Boolean) Indicates the disabled status of this deployment action. - `is_required` (Boolean) Indicates the required status of this deployment action. @@ -670,6 +735,21 @@ Optional: - `image` (String) + +### Nested Schema for `step.deploy_windows_service_action.git_dependency` + +Required: + +- `default_branch` (String) Name of the default branch of the repository. +- `git_credential_type` (String) The Git credential authentication type. +- `repository_uri` (String) The Git URI for the repository where this resource is sourced from. + +Optional: + +- `file_path_filters` (List of String) List of file path filters used to narrow down the directory where files are to be sourced from. Supports glob patten syntax. +- `git_credential_id` (String) ID of an existing Git credential. + + ### Nested Schema for `step.deploy_windows_service_action.package` @@ -705,6 +785,7 @@ Optional: - `environments` (List of String) The environments within which this deployment action will run. - `excluded_environments` (List of String) The environments that this step will be skipped in - `features` (List of String) A list of enabled features for this action. +- `git_dependency` (Block Set, Max: 1) Configuration for resource sourcing from a git repository. (see [below for nested schema](#nestedblock--step--manual_intervention_action--git_dependency)) - `id` (String) The unique ID for this resource. - `is_disabled` (Boolean) Indicates the disabled status of this deployment action. - `is_required` (Boolean) Indicates the required status of this deployment action. @@ -737,6 +818,21 @@ Optional: - `image` (String) + +### Nested Schema for `step.manual_intervention_action.git_dependency` + +Required: + +- `default_branch` (String) Name of the default branch of the repository. +- `git_credential_type` (String) The Git credential authentication type. +- `repository_uri` (String) The Git URI for the repository where this resource is sourced from. + +Optional: + +- `file_path_filters` (List of String) List of file path filters used to narrow down the directory where files are to be sourced from. Supports glob patten syntax. +- `git_credential_id` (String) ID of an existing Git credential. + + ### Nested Schema for `step.manual_intervention_action.package` @@ -771,6 +867,7 @@ Optional: - `environments` (List of String) The environments within which this deployment action will run. - `excluded_environments` (List of String) The environments that this step will be skipped in - `features` (List of String) A list of enabled features for this action. +- `git_dependency` (Block Set, Max: 1) Configuration for resource sourcing from a git repository. (see [below for nested schema](#nestedblock--step--run_kubectl_script_action--git_dependency)) - `id` (String) The unique ID for this resource. - `is_disabled` (Boolean) Indicates the disabled status of this deployment action. - `is_required` (Boolean) Indicates the required status of this deployment action. @@ -812,6 +909,21 @@ Optional: - `image` (String) + +### Nested Schema for `step.run_kubectl_script_action.git_dependency` + +Required: + +- `default_branch` (String) Name of the default branch of the repository. +- `git_credential_type` (String) The Git credential authentication type. +- `repository_uri` (String) The Git URI for the repository where this resource is sourced from. + +Optional: + +- `file_path_filters` (List of String) List of file path filters used to narrow down the directory where files are to be sourced from. Supports glob patten syntax. +- `git_credential_id` (String) ID of an existing Git credential. + + ### Nested Schema for `step.run_kubectl_script_action.package` @@ -863,6 +975,7 @@ Optional: - `environments` (List of String) The environments within which this deployment action will run. - `excluded_environments` (List of String) The environments that this step will be skipped in - `features` (List of String) A list of enabled features for this action. +- `git_dependency` (Block Set, Max: 1) Configuration for resource sourcing from a git repository. (see [below for nested schema](#nestedblock--step--run_script_action--git_dependency)) - `id` (String) The unique ID for this resource. - `is_disabled` (Boolean) Indicates the disabled status of this deployment action. - `is_required` (Boolean) Indicates the required status of this deployment action. @@ -904,6 +1017,21 @@ Optional: - `image` (String) + +### Nested Schema for `step.run_script_action.git_dependency` + +Required: + +- `default_branch` (String) Name of the default branch of the repository. +- `git_credential_type` (String) The Git credential authentication type. +- `repository_uri` (String) The Git URI for the repository where this resource is sourced from. + +Optional: + +- `file_path_filters` (List of String) List of file path filters used to narrow down the directory where files are to be sourced from. Supports glob patten syntax. +- `git_credential_id` (String) ID of an existing Git credential. + + ### Nested Schema for `step.run_script_action.package` diff --git a/docs/resources/runbook_process.md b/docs/resources/runbook_process.md index 0089dab08..a90b83b00 100644 --- a/docs/resources/runbook_process.md +++ b/docs/resources/runbook_process.md @@ -72,6 +72,7 @@ Optional: - `environments` (List of String) The environments within which this deployment action will run. - `excluded_environments` (List of String) The environments that this step will be skipped in - `features` (List of String) A list of enabled features for this action. +- `git_dependency` (Block Set, Max: 1) Configuration for resource sourcing from a git repository. (see [below for nested schema](#nestedblock--step--action--git_dependency)) - `id` (String) The unique ID for this resource. - `is_disabled` (Boolean) Indicates the disabled status of this deployment action. - `is_required` (Boolean) Indicates the required status of this deployment action. @@ -107,6 +108,21 @@ Optional: - `image` (String) + +### Nested Schema for `step.action.git_dependency` + +Required: + +- `default_branch` (String) Name of the default branch of the repository. +- `git_credential_type` (String) The Git credential authentication type. +- `repository_uri` (String) The Git URI for the repository where this resource is sourced from. + +Optional: + +- `file_path_filters` (List of String) List of file path filters used to narrow down the directory where files are to be sourced from. Supports glob patten syntax. +- `git_credential_id` (String) ID of an existing Git credential. + + ### Nested Schema for `step.action.package` @@ -161,6 +177,7 @@ Optional: - `environments` (List of String) The environments within which this deployment action will run. - `excluded_environments` (List of String) The environments that this step will be skipped in - `features` (List of String) A list of enabled features for this action. +- `git_dependency` (Block Set, Max: 1) Configuration for resource sourcing from a git repository. (see [below for nested schema](#nestedblock--step--apply_terraform_template_action--git_dependency)) - `google_cloud_account` (Block Set, Max: 1) (see [below for nested schema](#nestedblock--step--apply_terraform_template_action--google_cloud_account)) - `id` (String) The unique ID for this resource. - `inline_template` (String) @@ -240,6 +257,21 @@ Optional: - `image` (String) + +### Nested Schema for `step.apply_terraform_template_action.git_dependency` + +Required: + +- `default_branch` (String) Name of the default branch of the repository. +- `git_credential_type` (String) The Git credential authentication type. +- `repository_uri` (String) The Git URI for the repository where this resource is sourced from. + +Optional: + +- `file_path_filters` (List of String) List of file path filters used to narrow down the directory where files are to be sourced from. Supports glob patten syntax. +- `git_credential_id` (String) ID of an existing Git credential. + + ### Nested Schema for `step.apply_terraform_template_action.google_cloud_account` @@ -317,6 +349,7 @@ Optional: - `environments` (List of String) The environments within which this deployment action will run. - `excluded_environments` (List of String) The environments that this step will be skipped in - `features` (List of String) A list of enabled features for this action. +- `git_dependency` (Block Set, Max: 1) Configuration for resource sourcing from a git repository. (see [below for nested schema](#nestedblock--step--deploy_kubernetes_secret_action--git_dependency)) - `id` (String) The unique ID for this resource. - `is_disabled` (Boolean) Indicates the disabled status of this deployment action. - `is_required` (Boolean) Indicates the required status of this deployment action. @@ -351,6 +384,21 @@ Optional: - `image` (String) + +### Nested Schema for `step.deploy_kubernetes_secret_action.git_dependency` + +Required: + +- `default_branch` (String) Name of the default branch of the repository. +- `git_credential_type` (String) The Git credential authentication type. +- `repository_uri` (String) The Git URI for the repository where this resource is sourced from. + +Optional: + +- `file_path_filters` (List of String) List of file path filters used to narrow down the directory where files are to be sourced from. Supports glob patten syntax. +- `git_credential_id` (String) ID of an existing Git credential. + + ### Nested Schema for `step.deploy_kubernetes_secret_action.package` @@ -386,6 +434,7 @@ Optional: - `environments` (List of String) The environments within which this deployment action will run. - `excluded_environments` (List of String) The environments that this step will be skipped in - `features` (List of String) A list of enabled features for this action. +- `git_dependency` (Block Set, Max: 1) Configuration for resource sourcing from a git repository. (see [below for nested schema](#nestedblock--step--deploy_package_action--git_dependency)) - `id` (String) The unique ID for this resource. - `is_disabled` (Boolean) Indicates the disabled status of this deployment action. - `is_required` (Boolean) Indicates the required status of this deployment action. @@ -434,6 +483,21 @@ Optional: - `image` (String) + +### Nested Schema for `step.deploy_package_action.git_dependency` + +Required: + +- `default_branch` (String) Name of the default branch of the repository. +- `git_credential_type` (String) The Git credential authentication type. +- `repository_uri` (String) The Git URI for the repository where this resource is sourced from. + +Optional: + +- `file_path_filters` (List of String) List of file path filters used to narrow down the directory where files are to be sourced from. Supports glob patten syntax. +- `git_credential_id` (String) ID of an existing Git credential. + + ### Nested Schema for `step.deploy_package_action.package` @@ -499,6 +563,7 @@ Optional: - `environments` (List of String) The environments within which this deployment action will run. - `excluded_environments` (List of String) The environments that this step will be skipped in - `features` (List of String) A list of enabled features for this action. +- `git_dependency` (Block Set, Max: 1) Configuration for resource sourcing from a git repository. (see [below for nested schema](#nestedblock--step--deploy_windows_service_action--git_dependency)) - `id` (String) The unique ID for this resource. - `is_disabled` (Boolean) Indicates the disabled status of this deployment action. - `is_required` (Boolean) Indicates the required status of this deployment action. @@ -548,6 +613,21 @@ Optional: - `image` (String) + +### Nested Schema for `step.deploy_windows_service_action.git_dependency` + +Required: + +- `default_branch` (String) Name of the default branch of the repository. +- `git_credential_type` (String) The Git credential authentication type. +- `repository_uri` (String) The Git URI for the repository where this resource is sourced from. + +Optional: + +- `file_path_filters` (List of String) List of file path filters used to narrow down the directory where files are to be sourced from. Supports glob patten syntax. +- `git_credential_id` (String) ID of an existing Git credential. + + ### Nested Schema for `step.deploy_windows_service_action.package` @@ -583,6 +663,7 @@ Optional: - `environments` (List of String) The environments within which this deployment action will run. - `excluded_environments` (List of String) The environments that this step will be skipped in - `features` (List of String) A list of enabled features for this action. +- `git_dependency` (Block Set, Max: 1) Configuration for resource sourcing from a git repository. (see [below for nested schema](#nestedblock--step--manual_intervention_action--git_dependency)) - `id` (String) The unique ID for this resource. - `is_disabled` (Boolean) Indicates the disabled status of this deployment action. - `is_required` (Boolean) Indicates the required status of this deployment action. @@ -615,6 +696,21 @@ Optional: - `image` (String) + +### Nested Schema for `step.manual_intervention_action.git_dependency` + +Required: + +- `default_branch` (String) Name of the default branch of the repository. +- `git_credential_type` (String) The Git credential authentication type. +- `repository_uri` (String) The Git URI for the repository where this resource is sourced from. + +Optional: + +- `file_path_filters` (List of String) List of file path filters used to narrow down the directory where files are to be sourced from. Supports glob patten syntax. +- `git_credential_id` (String) ID of an existing Git credential. + + ### Nested Schema for `step.manual_intervention_action.package` @@ -649,6 +745,7 @@ Optional: - `environments` (List of String) The environments within which this deployment action will run. - `excluded_environments` (List of String) The environments that this step will be skipped in - `features` (List of String) A list of enabled features for this action. +- `git_dependency` (Block Set, Max: 1) Configuration for resource sourcing from a git repository. (see [below for nested schema](#nestedblock--step--run_kubectl_script_action--git_dependency)) - `id` (String) The unique ID for this resource. - `is_disabled` (Boolean) Indicates the disabled status of this deployment action. - `is_required` (Boolean) Indicates the required status of this deployment action. @@ -690,6 +787,21 @@ Optional: - `image` (String) + +### Nested Schema for `step.run_kubectl_script_action.git_dependency` + +Required: + +- `default_branch` (String) Name of the default branch of the repository. +- `git_credential_type` (String) The Git credential authentication type. +- `repository_uri` (String) The Git URI for the repository where this resource is sourced from. + +Optional: + +- `file_path_filters` (List of String) List of file path filters used to narrow down the directory where files are to be sourced from. Supports glob patten syntax. +- `git_credential_id` (String) ID of an existing Git credential. + + ### Nested Schema for `step.run_kubectl_script_action.package` @@ -741,6 +853,7 @@ Optional: - `environments` (List of String) The environments within which this deployment action will run. - `excluded_environments` (List of String) The environments that this step will be skipped in - `features` (List of String) A list of enabled features for this action. +- `git_dependency` (Block Set, Max: 1) Configuration for resource sourcing from a git repository. (see [below for nested schema](#nestedblock--step--run_script_action--git_dependency)) - `id` (String) The unique ID for this resource. - `is_disabled` (Boolean) Indicates the disabled status of this deployment action. - `is_required` (Boolean) Indicates the required status of this deployment action. @@ -782,6 +895,21 @@ Optional: - `image` (String) + +### Nested Schema for `step.run_script_action.git_dependency` + +Required: + +- `default_branch` (String) Name of the default branch of the repository. +- `git_credential_type` (String) The Git credential authentication type. +- `repository_uri` (String) The Git URI for the repository where this resource is sourced from. + +Optional: + +- `file_path_filters` (List of String) List of file path filters used to narrow down the directory where files are to be sourced from. Supports glob patten syntax. +- `git_credential_id` (String) ID of an existing Git credential. + + ### Nested Schema for `step.run_script_action.package` @@ -813,3 +941,5 @@ Optional: - `id` (String) The unique ID for this resource. - `name` (String) The name of this resource. - `properties` (Map of String) A list of properties associated with this package. + + diff --git a/integration_test.go b/integration_test.go index 0456c0caf..dbf1b55af 100644 --- a/integration_test.go +++ b/integration_test.go @@ -3472,3 +3472,55 @@ func TestTerraformApplyStepWithWorkerPool(t *testing.T) { return nil }) } + +func TestDeploymentProcessWithGitDependency(t *testing.T) { + testFramework := test.OctopusContainerTest{} + testFramework.ArrangeTest(t, func(t *testing.T, container *test.OctopusContainer, spaceClient *client.Client) error { + newSpaceId, err := testFramework.Act(t, container, "./terraform", "50-deploymentprocesswithgitdependency", []string{}) + + if err != nil { + return err + } + + client, err := octoclient.CreateClient(container.URI, newSpaceId, test.ApiKey) + project, err := client.Projects.GetByName("Test") + deploymentProcess, err := deployments.GetDeploymentProcessByID(client, newSpaceId, project.DeploymentProcessID) + + if len(deploymentProcess.Steps) == 0 { + t.Fatalf("Expected deployment process to have steps.") + } + + expectedGitUri := "https://github.com/OctopusSamples/OctoPetShop.git" + expectedDefaultBranch := "main" + + for _, step := range deploymentProcess.Steps { + action := step.Actions[0] + + if len(action.GitDependencies) == 0 { + t.Fatalf(fmt.Sprint(action.Name) + " - Expected action to have git dependency configured.") + } + + gitDependency := action.GitDependencies[0] + + if fmt.Sprint(gitDependency.RepositoryUri) != expectedGitUri { + t.Fatalf(fmt.Sprint(action.Name) + " - Expected git dependency to have repository uri equal to " + fmt.Sprint(expectedGitUri)) + } + + if fmt.Sprint(gitDependency.DefaultBranch) != expectedDefaultBranch { + t.Fatalf(fmt.Sprint(action.Name) + " - Expected git dependency to have default branch equal to " + fmt.Sprint(expectedDefaultBranch)) + } + + if fmt.Sprint(gitDependency.GitCredentialType) == "Library" { + if len(strings.TrimSpace(gitDependency.GitCredentialId)) == 0 { + t.Fatalf(fmt.Sprint(action.Name) + " - Expected git dependency library type to have a defined git credential id.") + } + } else { + if len(strings.TrimSpace(gitDependency.GitCredentialId)) > 0 { + t.Fatalf(fmt.Sprint(action.Name) + " - Expected git dependency of non-library type to not have a defined git credential id.") + } + } + } + + return nil + }) +} diff --git a/octopusdeploy/schema_deployment_action.go b/octopusdeploy/schema_deployment_action.go index d54454119..33ccea1a5 100644 --- a/octopusdeploy/schema_deployment_action.go +++ b/octopusdeploy/schema_deployment_action.go @@ -2,6 +2,7 @@ package octopusdeploy import ( "fmt" + "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/gitdependencies" "strconv" "strings" @@ -128,6 +129,10 @@ func flattenAction(action *deployments.DeploymentAction) map[string]interface{} flattenedAction["package"] = flattenedPackageReferences } + if len(action.GitDependencies) > 0 { + flattenedAction["git_dependency"] = flattenGitDependency(action.GitDependencies[0]) + } + return flattenedAction } @@ -138,6 +143,7 @@ func getDeploymentActionSchema() *schema.Schema { addWorkerPoolSchema(element) addWorkerPoolVariableSchema(element) addPackagesSchema(element, false) + addGitDependencySchema(element) return actionSchema } @@ -216,7 +222,8 @@ func getActionSchema() (*schema.Schema, *schema.Resource) { Optional: true, Type: schema.TypeList, }, - "id": getIDSchema(), + "git_dependency": getGitDependencySchema(false), + "id": getIDSchema(), "is_disabled": { Default: false, Description: "Indicates the disabled status of this deployment action.", @@ -494,5 +501,12 @@ func expandAction(flattenedAction map[string]interface{}) *deployments.Deploymen } } + if v, ok := flattenedAction["git_dependency"]; ok { + expandedGitDependency := expandGitDependency(v.(*schema.Set)) + if expandedGitDependency != nil { + action.GitDependencies = []*gitdependencies.GitDependency{expandedGitDependency} + } + } + return action } diff --git a/octopusdeploy/schema_git_dependency.go b/octopusdeploy/schema_git_dependency.go new file mode 100644 index 000000000..fcfa29d8b --- /dev/null +++ b/octopusdeploy/schema_git_dependency.go @@ -0,0 +1,100 @@ +package octopusdeploy + +import ( + "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/gitdependencies" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" +) + +func flattenGitDependency(gitDependency *gitdependencies.GitDependency) []interface{} { + if gitDependency == nil { + return nil + } + + return []interface{}{map[string]interface{}{ + "repository_uri": gitDependency.RepositoryUri, + "default_branch": gitDependency.DefaultBranch, + "git_credential_type": gitDependency.GitCredentialType, + "file_path_filters": flattenArray(gitDependency.FilePathFilters), + "git_credential_id": gitDependency.GitCredentialId, + }} +} + +func expandGitDependency(set *schema.Set) *gitdependencies.GitDependency { + if set == nil || len(set.List()) == 0 { + return nil + } + + flattenedMap := set.List()[0].(map[string]interface{}) + gitDependency := &gitdependencies.GitDependency{} + + if repositoryUri := flattenedMap["repository_uri"]; repositoryUri != nil { + gitDependency.RepositoryUri = repositoryUri.(string) + } + + if defaultBranch := flattenedMap["default_branch"]; defaultBranch != nil { + gitDependency.DefaultBranch = defaultBranch.(string) + } + + if gitCredentialType := flattenedMap["git_credential_type"]; gitCredentialType != nil { + gitDependency.GitCredentialType = gitCredentialType.(string) + } + + if filePathFilters := flattenedMap["file_path_filters"]; filePathFilters != nil { + gitDependency.FilePathFilters = expandArray(filePathFilters.([]interface{})) + } + + if gitCredentialId := flattenedMap["git_credential_id"]; gitCredentialId != nil { + gitDependency.GitCredentialId = gitCredentialId.(string) + } + + return gitDependency +} + +func getGitDependencySchema(required bool) *schema.Schema { + return &schema.Schema{ + Computed: !required, + Description: "Configuration for resource sourcing from a git repository.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "repository_uri": { + Description: "The Git URI for the repository where this resource is sourced from.", + Required: true, + Type: schema.TypeString, + ValidateDiagFunc: validation.ToDiagFunc(validation.StringIsNotWhiteSpace), + }, + "default_branch": { + Description: "Name of the default branch of the repository.", + Required: true, + Type: schema.TypeString, + ValidateDiagFunc: validation.ToDiagFunc(validation.StringIsNotWhiteSpace), + }, + "git_credential_type": { + Description: "The Git credential authentication type.", + Required: true, + Type: schema.TypeString, + ValidateDiagFunc: validation.ToDiagFunc(validation.StringIsNotWhiteSpace), + }, + "file_path_filters": { + Description: "List of file path filters used to narrow down the directory where files are to be sourced from. Supports glob patten syntax.", + Optional: true, + Type: schema.TypeList, + Elem: &schema.Schema{Type: schema.TypeString}, + }, + "git_credential_id": { + Description: "ID of an existing Git credential.", + Optional: true, + Type: schema.TypeString, + }, + }, + }, + Optional: !required, + Required: required, + MaxItems: 1, + Type: schema.TypeSet, + } +} + +func addGitDependencySchema(element *schema.Resource) { + element.Schema["git_dependency"] = getGitDependencySchema(false) +} diff --git a/terraform/50-deploymentprocesswithgitdependency/config.tf b/terraform/50-deploymentprocesswithgitdependency/config.tf new file mode 100644 index 000000000..8a9eaf8a9 --- /dev/null +++ b/terraform/50-deploymentprocesswithgitdependency/config.tf @@ -0,0 +1,7 @@ +terraform { + required_providers { + octopusdeploy = { source = "OctopusDeployLabs/octopusdeploy", version = "0.11.3" } + // Use the option below when debugging + // octopusdeploy = { source = "octopus.com/com/octopusdeploy" } + } +} diff --git a/terraform/50-deploymentprocesswithgitdependency/deployment_process.tf b/terraform/50-deploymentprocesswithgitdependency/deployment_process.tf new file mode 100644 index 000000000..566405bd1 --- /dev/null +++ b/terraform/50-deploymentprocesswithgitdependency/deployment_process.tf @@ -0,0 +1,156 @@ +locals { + git_uri = "https://github.com/OctopusSamples/OctoPetShop.git" + default_branch = "main" + git_credential_type = "Library" +} + +resource "octopusdeploy_deployment_process" "test_deployment_process" { + project_id = octopusdeploy_project.test_project.id + +# Supported steps + step { + condition = "Success" + name = "Generic Action" + package_requirement = "LetOctopusDecide" + start_trigger = "StartAfterPrevious" + action { + action_type = "Octopus.Script" + name = "Generic Action" + git_dependency { + repository_uri = local.git_uri + default_branch = local.default_branch + git_credential_type = local.git_credential_type + git_credential_id = octopusdeploy_git_credential.test_git_credential.id + } + can_be_used_for_project_versioning = false + condition = "Success" + is_disabled = false + is_required = true + run_on_server = true + properties = { + "Octopus.Action.EnabledFeatures" : "Octopus.Features.SubstituteInFiles", + "Octopus.Action.GitRepository.Source" : "External", + "Octopus.Action.Script.ScriptFileName" : "Test.sh", + "Octopus.Action.Script.ScriptSource" : "GitRepository", + "Octopus.Action.SubstituteInFiles.Enabled" : "True" + } + } + } + + step { + condition = "Success" + name = "Run Script" + package_requirement = "LetOctopusDecide" + start_trigger = "StartAfterPrevious" + run_script_action { + git_dependency { + repository_uri = local.git_uri + default_branch = local.default_branch + git_credential_type = local.git_credential_type + git_credential_id = octopusdeploy_git_credential.test_git_credential.id + } + script_source = "GitRepository" + script_file_name = "Test.sh" + can_be_used_for_project_versioning = false + condition = "Success" + is_disabled = false + is_required = true + name = "Run Script" + run_on_server = true + } + } + + step { + condition = "Success" + name = "Run Script - Anon" + package_requirement = "LetOctopusDecide" + start_trigger = "StartAfterPrevious" + run_script_action { + git_dependency { + repository_uri = local.git_uri + default_branch = local.default_branch + git_credential_type = "Anonymous" + } + script_source = "GitRepository" + script_file_name = "Test.sh" + can_be_used_for_project_versioning = false + condition = "Success" + is_disabled = false + is_required = true + name = "Run Script - Anon" + run_on_server = true + } + } + + step { + condition = "Success" + name = "Kubectl Action" + package_requirement = "LetOctopusDecide" + start_trigger = "StartAfterPrevious" + run_kubectl_script_action { + name = "Kubectl Action" + run_on_server = true + script_source = "GitRepository" + script_file_name = "Test.sh" + git_dependency { + repository_uri = local.git_uri + default_branch = local.default_branch + git_credential_type = local.git_credential_type + git_credential_id = octopusdeploy_git_credential.test_git_credential.id + } + } + } + + step { + condition = "Success" + name = "Raw Yaml Action" + package_requirement = "LetOctopusDecide" + start_trigger = "StartAfterPrevious" + properties = { "Octopus.Action.TargetRoles" : "Qwerty" } + action { + action_type = "Octopus.KubernetesDeployRawYaml" + name = "Raw Yaml Action" + git_dependency { + repository_uri = local.git_uri + default_branch = local.default_branch + git_credential_type = local.git_credential_type + git_credential_id = octopusdeploy_git_credential.test_git_credential.id + } + can_be_used_for_project_versioning = false + condition = "Success" + is_disabled = false + is_required = true + run_on_server = true + properties = { + "Octopus.Action.Script.ScriptFileName" : "Test.sh", + "Octopus.Action.Script.ScriptSource" : "GitRepository", + "Octopus.Action.SubstituteInFiles.Enabled" : "True", + "Octopus.Action.KubernetesContainers.CustomResourceYamlFileName" : "files/*", + "Octopus.Action.KubernetesContainers.DeploymentWait" : "NoWait", + "Octopus.Action.KubernetesContainers.Namespace" : "default" + } + } + } + +# Step that doesn't support git dependencies + step { + condition = "Success" + name = "Deploy Secret" + package_requirement = "LetOctopusDecide" + start_trigger = "StartAfterPrevious" + properties = { "Octopus.Action.TargetRoles" : "Qwerty" } + deploy_kubernetes_secret_action { + name = "Deploy secret" + secret_name = "name" + secret_values = { + "val" = "123" + } + git_dependency { + repository_uri = local.git_uri + default_branch = local.default_branch + git_credential_type = local.git_credential_type + git_credential_id = octopusdeploy_git_credential.test_git_credential.id + } + } + } +} \ No newline at end of file diff --git a/terraform/50-deploymentprocesswithgitdependency/environments.tf b/terraform/50-deploymentprocesswithgitdependency/environments.tf new file mode 100644 index 000000000..590fe4e93 --- /dev/null +++ b/terraform/50-deploymentprocesswithgitdependency/environments.tf @@ -0,0 +1,6 @@ +resource "octopusdeploy_environment" "development_environment" { + allow_dynamic_infrastructure = true + description = "A test environment" + name = "Development" + use_guided_failure = false +} \ No newline at end of file diff --git a/terraform/50-deploymentprocesswithgitdependency/git_credential.tf b/terraform/50-deploymentprocesswithgitdependency/git_credential.tf new file mode 100644 index 000000000..dcb081c10 --- /dev/null +++ b/terraform/50-deploymentprocesswithgitdependency/git_credential.tf @@ -0,0 +1,7 @@ +resource "octopusdeploy_git_credential" "test_git_credential" { + description = "test git credential" + name = "test" + type = "UsernamePassword" + username = "admin" + password = "Password01!" +} \ No newline at end of file diff --git a/terraform/50-deploymentprocesswithgitdependency/project.tf b/terraform/50-deploymentprocesswithgitdependency/project.tf new file mode 100644 index 000000000..11900e47d --- /dev/null +++ b/terraform/50-deploymentprocesswithgitdependency/project.tf @@ -0,0 +1,43 @@ +data "octopusdeploy_lifecycles" "lifecycle_default_lifecycle" { + ids = null + partial_name = "Default Lifecycle" + skip = 0 + take = 1 +} + +resource "octopusdeploy_project_group" "project_group_test" { + name = "Test" + description = "Test Description" +} + +resource "octopusdeploy_project" "test_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" + } + template { + name = "Project Template Variable" + label = "Test" + default_value = "Test" + display_settings = { "Octopus.ControlType" = "SingleLineText" } + } +} \ No newline at end of file diff --git a/terraform/50-deploymentprocesswithgitdependency/provider.tf b/terraform/50-deploymentprocesswithgitdependency/provider.tf new file mode 100644 index 000000000..a04197720 --- /dev/null +++ b/terraform/50-deploymentprocesswithgitdependency/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/50-deploymentprocesswithgitdependency/provider_vars.tf b/terraform/50-deploymentprocesswithgitdependency/provider_vars.tf new file mode 100644 index 000000000..c7d93fe40 --- /dev/null +++ b/terraform/50-deploymentprocesswithgitdependency/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/50-deploymentprocesswithgitdependency/space.tf b/terraform/50-deploymentprocesswithgitdependency/space.tf new file mode 100644 index 000000000..ee59bdc80 --- /dev/null +++ b/terraform/50-deploymentprocesswithgitdependency/space.tf @@ -0,0 +1,3 @@ +output "octopus_space_id" { + value = var.octopus_space_id +}