Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support process scoped variables #593

Merged
merged 17 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/data-sources/variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ Optional:
- `channels` (List of String) A list of channels that are scoped to this variable value.
- `environments` (List of String) A list of environments that are scoped to this variable value.
- `machines` (List of String) A list of machines that are scoped to this variable value.
- `processes` (List of String) A list of processes that are scoped to this variable value.
- `roles` (List of String) A list of roles that are scoped to this variable value.
- `tenant_tags` (List of String) A list of tenant tags that are scoped to this variable value.

Expand Down
1 change: 1 addition & 0 deletions docs/resources/variable.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ Optional:
- `channels` (List of String) A list of channels that are scoped to this variable value.
- `environments` (List of String) A list of environments that are scoped to this variable value.
- `machines` (List of String) A list of machines that are scoped to this variable value.
- `processes` (List of String) A list of processes that are scoped to this variable value.
- `roles` (List of String) A list of roles that are scoped to this variable value.
- `tenant_tags` (List of String) A list of tenant tags that are scoped to this variable value.

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/OctopusDeploy/terraform-provider-octopusdeploy
go 1.20

require (
github.com/OctopusDeploy/go-octopusdeploy/v2 v2.36.1
github.com/OctopusDeploy/go-octopusdeploy/v2 v2.36.2
github.com/OctopusSolutionsEngineering/OctopusTerraformTestFramework v0.0.0-20230705105638-f5ef7c07973b
github.com/google/uuid v1.3.0
github.com/gruntwork-io/terratest v0.41.11
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ github.com/Microsoft/go-winio v0.4.16/go.mod h1:XB6nPKklQyQ7GC9LdcBEcBl8PF76WugX
github.com/Microsoft/go-winio v0.6.0 h1:slsWYD/zyx7lCXoZVlvQrj0hPTM1HI4+v1sIda2yDvg=
github.com/Microsoft/go-winio v0.6.0/go.mod h1:cTAf44im0RAYeL23bpB+fzCyDH2MJiz2BO69KH/soAE=
github.com/Microsoft/hcsshim v0.9.7 h1:mKNHW/Xvv1aFH87Jb6ERDzXTJTLPlmzfZ28VBFD/bfg=
github.com/OctopusDeploy/go-octopusdeploy/v2 v2.36.1 h1:irRBBh+rSCMqR0YDaUdfBnpl9UAU7tI8zD0j8xt2C8U=
github.com/OctopusDeploy/go-octopusdeploy/v2 v2.36.1/go.mod h1:GZmFu6LmN8Yg0tEoZx3ytk9FnaH+84cWm7u5TdWZC6E=
github.com/OctopusDeploy/go-octopusdeploy/v2 v2.36.2 h1:ovLCSy+I1mN0nynxNtOkrxUuf4DL2jZc79kkBH6698g=
github.com/OctopusDeploy/go-octopusdeploy/v2 v2.36.2/go.mod h1:GZmFu6LmN8Yg0tEoZx3ytk9FnaH+84cWm7u5TdWZC6E=
github.com/OctopusSolutionsEngineering/OctopusTerraformTestFramework v0.0.0-20230705105638-f5ef7c07973b h1:XOBPcVHeDUYIpcag0yI8IYKiBL+5LLL8suysvlavQwI=
github.com/OctopusSolutionsEngineering/OctopusTerraformTestFramework v0.0.0-20230705105638-f5ef7c07973b/go.mod h1:E0hYVpZd61fXhzTozkxjiWEy+/yTRxAnr2SIE7k8ZSM=
github.com/ProtonMail/go-crypto v0.0.0-20210428141323-04723f9f07d7 h1:YoJbenK9C67SkzkDfmQuVln04ygHj3vjZfd9FL+GmQQ=
Expand Down
60 changes: 60 additions & 0 deletions integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3350,3 +3350,63 @@ func TestK8sPodAuthTargetResource(t *testing.T) {
return nil
})
}

func TestVariableResource(t *testing.T) {
testFramework := test.OctopusContainerTest{}
testFramework.ArrangeTest(t, func(t *testing.T, container *test.OctopusContainer, spaceClient *client.Client) error {
// Act
newSpaceId, err := testFramework.Act(t, container, "./terraform", "49-variables", []string{})

if err != nil {
return err
}

// Assert
client, err := octoclient.CreateClient(container.URI, newSpaceId, test.ApiKey)
project, err := client.Projects.GetByName("Test")
variableSet, err := client.Variables.GetAll(project.ID)

if err != nil {
return err
}

if len(variableSet.Variables) != 7 {
t.Fatalf("Expected 7 variables to be created.")
}

for _, variable := range variableSet.Variables {
switch variable.Name {
case "UnscopedVariable":
if !variable.Scope.IsEmpty() {
t.Fatalf("Expected UnscopedVariable to have no scope values.")
}
case "ActionScopedVariable":
if len(variable.Scope.Actions) == 0 {
t.Fatalf("Expected ActionScopedVariable to have action scope.")
}
case "ChannelScopedVariable":
if len(variable.Scope.Channels) == 0 {
t.Fatalf("Expected ChannelScopedVariable to have channel scope.")
}
case "EnvironmentScopedVariable":
if len(variable.Scope.Environments) == 0 {
t.Fatalf("Expected EnvironmentScopedVariable to have environment scope.")
}
case "MachineScopedVariable":
if len(variable.Scope.Machines) == 0 {
t.Fatalf("Expected MachineScopedVariable to have machine scope.")
}
case "ProcessScopedVariable":
if len(variable.Scope.ProcessOwners) == 0 {
t.Fatalf("Expected ProcessScopedVariable to have process scope.")
}
case "RoleScopedVariable":
if len(variable.Scope.Roles) == 0 {
t.Fatalf("Expected RoleScopedVariable to have role scope.")
}
}
}

return nil
})
}
23 changes: 17 additions & 6 deletions octopusdeploy/schema_variable_scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ func expandVariableScope(flattenedVariableScope interface{}) variables.VariableS

if flattenedMap, ok := list[0].(map[string]interface{}); ok {
return variables.VariableScope{
Actions: getSliceFromTerraformTypeList(flattenedMap["actions"]),
Channels: getSliceFromTerraformTypeList(flattenedMap["channels"]),
Environments: getSliceFromTerraformTypeList(flattenedMap["environments"]),
Machines: getSliceFromTerraformTypeList(flattenedMap["machines"]),
Roles: getSliceFromTerraformTypeList(flattenedMap["roles"]),
TenantTags: getSliceFromTerraformTypeList(flattenedMap["tenant_tags"]),
Actions: getSliceFromTerraformTypeList(flattenedMap["actions"]),
Channels: getSliceFromTerraformTypeList(flattenedMap["channels"]),
Environments: getSliceFromTerraformTypeList(flattenedMap["environments"]),
Machines: getSliceFromTerraformTypeList(flattenedMap["machines"]),
ProcessOwners: getSliceFromTerraformTypeList(flattenedMap["processes"]),
Roles: getSliceFromTerraformTypeList(flattenedMap["roles"]),
TenantTags: getSliceFromTerraformTypeList(flattenedMap["tenant_tags"]),
}
}

Expand Down Expand Up @@ -52,6 +53,10 @@ func flattenVariableScope(scope variables.VariableScope) []interface{} {
flattenedScope["machines"] = scope.Machines
}

if len(scope.ProcessOwners) > 0 {
flattenedScope["processes"] = scope.ProcessOwners
}

if len(scope.Roles) > 0 {
flattenedScope["roles"] = scope.Roles
}
Expand Down Expand Up @@ -89,6 +94,12 @@ func getVariableScopeSchema() map[string]*schema.Schema {
Optional: true,
Type: schema.TypeList,
},
"processes": {
Description: "A list of processes that are scoped to this variable value.",
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
Type: schema.TypeList,
},
"roles": {
Description: "A list of roles that are scoped to this variable value.",
Elem: &schema.Schema{Type: schema.TypeString},
Expand Down
7 changes: 7 additions & 0 deletions terraform/49-variables/config.tf
Original file line number Diff line number Diff line change
@@ -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" }
}
}
20 changes: 20 additions & 0 deletions terraform/49-variables/environments.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
resource "octopusdeploy_environment" "development_environment" {
allow_dynamic_infrastructure = true
description = "A test environment"
name = "Development"
use_guided_failure = false
}

resource "octopusdeploy_environment" "test_environment" {
allow_dynamic_infrastructure = true
description = "A test environment"
name = "Test"
use_guided_failure = false
}

resource "octopusdeploy_environment" "production_environment" {
allow_dynamic_infrastructure = true
description = "A test environment"
name = "Production"
use_guided_failure = false
}
22 changes: 22 additions & 0 deletions terraform/49-variables/infrastructure.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
data "octopusdeploy_machine_policies" "default_machine_policy" {
ids = null
partial_name = "Default Machine Policy"
skip = 0
take = 1
}

resource "octopusdeploy_cloud_region_deployment_target" "test_target" {
environments = ["${octopusdeploy_environment.development_environment.id}"]
name = "Test"
roles = ["cloud"]
default_worker_pool_id = ""
health_status = "Healthy"
is_disabled = false
machine_policy_id = "${data.octopusdeploy_machine_policies.default_machine_policy.machine_policies[0].id}"
shell_name = "Unknown"
shell_version = "Unknown"
tenant_tags = []
tenanted_deployment_participation = "Untenanted"
tenants = []
thumbprint = ""
}
75 changes: 75 additions & 0 deletions terraform/49-variables/project.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
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" }
}
}

resource "octopusdeploy_deployment_process" "test_deployment_process" {
project_id = octopusdeploy_project.test_project.id
step {
condition = "Success"
name = "Hello world (using PowerShell)"
package_requirement = "LetOctopusDecide"
start_trigger = "StartAfterPrevious"
run_script_action {
can_be_used_for_project_versioning = false
condition = "Success"
is_disabled = false
is_required = true
name = "Hello world (using PowerShell)"
script_body = <<-EOT
Write-Host 'Hello world, using PowerShell'
#TODO: Experiment with steps of your own :)
Write-Host '[Learn more about the types of steps available in Octopus](https://g.octopushq.com/OnboardingAddStepsLearnMore)'
EOT
run_on_server = true
}
}
}

resource "octopusdeploy_channel" "test_channel" {
depends_on = [octopusdeploy_deployment_process.test_deployment_process]
description = "Test Channel"
name = "Test Channel"
project_id = octopusdeploy_project.test_project.id
}
5 changes: 5 additions & 0 deletions terraform/49-variables/provider.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
provider "octopusdeploy" {
address = "${var.octopus_server}"
api_key = "${var.octopus_apikey}"
space_id = "${var.octopus_space_id}"
}
18 changes: 18 additions & 0 deletions terraform/49-variables/provider_vars.tf
Original file line number Diff line number Diff line change
@@ -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"
}
3 changes: 3 additions & 0 deletions terraform/49-variables/space.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
output "octopus_space_id" {
value = var.octopus_space_id
}
66 changes: 66 additions & 0 deletions terraform/49-variables/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
resource "octopusdeploy_variable" "unscoped_project_variable" {
owner_id = octopusdeploy_project.test_project.id
type = "String"
name = "UnscopedVariable"
value = "UnscopedVariable"
}

resource "octopusdeploy_variable" "scoped_project_variable_action" {
owner_id = octopusdeploy_project.test_project.id
type = "String"
name = "ActionScopedVariable"
value = "unscoped variable"
scope {
actions = [octopusdeploy_deployment_process.test_deployment_process.step[0].run_script_action[0].id]
}
}

resource "octopusdeploy_variable" "scoped_project_variable_channel" {
owner_id = octopusdeploy_project.test_project.id
type = "String"
name = "ChannelScopedVariable"
value = "ChannelScopedVariable"
scope {
channels = [octopusdeploy_channel.test_channel.id]
}
}

resource "octopusdeploy_variable" "scoped_project_variable_environment" {
owner_id = octopusdeploy_project.test_project.id
type = "String"
name = "EnvironmentScopedVariable"
value = "EnvironmentScopedVariable"
scope {
environments = [octopusdeploy_environment.development_environment.id]
}
}

resource "octopusdeploy_variable" "scoped_project_variable_machine" {
owner_id = octopusdeploy_project.test_project.id
type = "String"
name = "MachineScopedVariable"
value = "MachineScopedVariable"
scope {
machines = [octopusdeploy_cloud_region_deployment_target.test_target.id]
}
}

resource "octopusdeploy_variable" "scoped_project_variable_process" {
owner_id = octopusdeploy_project.test_project.id
type = "String"
name = "ProcessScopedVariable"
value = "ProcessScopedVariable"
scope {
processes = [octopusdeploy_deployment_process.test_deployment_process.id]
}
}

resource "octopusdeploy_variable" "scoped_project_variable_role" {
owner_id = octopusdeploy_project.test_project.id
type = "String"
name = "RoleScopedVariable"
value = "RoleScopedVariable"
scope {
roles = ["role"]
}
}
Loading