From 317310663537aa985aa861e0cd71b595f6417d00 Mon Sep 17 00:00:00 2001 From: Matthew Casperson Date: Fri, 28 Feb 2025 11:17:12 +1000 Subject: [PATCH] Added machine proxy export --- .../converters/machine_proxy_converter.go | 2 +- cmd/internal/converters/space_converter.go | 7 +++ cmd/internal/entry/entry.go | 17 ++++++++ cmd/octoterra_test.go | 43 +++++++++++++++++++ .../87-machineproxy/space_creation/config.tf | 5 +++ .../space_creation/provider.tf | 4 ++ .../space_creation/provider_vars.tf | 18 ++++++++ .../87-machineproxy/space_creation/space.tf | 28 ++++++++++++ .../space_population/config.tf | 5 +++ .../space_population/machine_proxy.tf | 7 +++ .../space_population/provider.tf | 5 +++ .../space_population/provider_vars.tf | 18 ++++++++ .../87-machineproxy/space_population/space.tf | 3 ++ 13 files changed, 161 insertions(+), 1 deletion(-) create mode 100644 test/terraform/87-machineproxy/space_creation/config.tf create mode 100644 test/terraform/87-machineproxy/space_creation/provider.tf create mode 100644 test/terraform/87-machineproxy/space_creation/provider_vars.tf create mode 100644 test/terraform/87-machineproxy/space_creation/space.tf create mode 100644 test/terraform/87-machineproxy/space_population/config.tf create mode 100644 test/terraform/87-machineproxy/space_population/machine_proxy.tf create mode 100644 test/terraform/87-machineproxy/space_population/provider.tf create mode 100644 test/terraform/87-machineproxy/space_population/provider_vars.tf create mode 100644 test/terraform/87-machineproxy/space_population/space.tf diff --git a/cmd/internal/converters/machine_proxy_converter.go b/cmd/internal/converters/machine_proxy_converter.go index 3485efc6..d4d156ba 100644 --- a/cmd/internal/converters/machine_proxy_converter.go +++ b/cmd/internal/converters/machine_proxy_converter.go @@ -374,7 +374,7 @@ func (c MachineProxyConverter) toHcl(resource octopus.MachineProxy, recursive bo file.Body().AppendBlock(block) secretVariableResource := terraform.TerraformVariable{ - Name: machineProxyName, + Name: machineProxyName + "_password", Type: "string", Nullable: false, Sensitive: true, diff --git a/cmd/internal/converters/space_converter.go b/cmd/internal/converters/space_converter.go index 73e5d06e..ddca0948 100644 --- a/cmd/internal/converters/space_converter.go +++ b/cmd/internal/converters/space_converter.go @@ -48,6 +48,7 @@ type SpaceConverter struct { KubernetesAgentWorkerConverter Converter ListeningWorkerConverter Converter SshWorkerConverter Converter + MachineProxyConverter Converter ErrGroup *errgroup.Group ExcludeSpaceCreation bool } @@ -154,6 +155,9 @@ func (c SpaceConverter) AllToHcl(dependencies *data.ResourceDetailsCollection) e // Include the space if it was requested c.SpacePopulateConverter.AllToHcl(dependencies) + // Convert the machine proxies + c.MachineProxyConverter.AllToHcl(dependencies) + return c.ErrGroup.Wait() } @@ -246,6 +250,9 @@ func (c SpaceConverter) AllToStatelessHcl(dependencies *data.ResourceDetailsColl // convert SSH workers c.SshWorkerConverter.AllToStatelessHcl(dependencies) + // Convert the machine proxies + c.MachineProxyConverter.AllToStatelessHcl(dependencies) + return c.ErrGroup.Wait() } diff --git a/cmd/internal/entry/entry.go b/cmd/internal/entry/entry.go index 0ac88447..a883976c 100644 --- a/cmd/internal/entry/entry.go +++ b/cmd/internal/entry/entry.go @@ -434,6 +434,22 @@ func ConvertSpaceToTerraform(args args.Arguments, version string) (*data.Resourc GenerateImportScripts: args.GenerateImportScripts, } + machineProxyConverter := converters.MachineProxyConverter{ + Client: &octopusClient, + ErrGroup: &group, + ExcludeMachineProxies: nil, + ExcludeMachineProxiesRegex: nil, + ExcludeMachineProxiesExcept: nil, + ExcludeAllMachineProxies: false, + Excluder: converters.DefaultExcluder{}, + LimitResourceCount: args.LimitResourceCount, + IncludeSpaceInPopulation: args.IncludeSpaceInPopulation, + IncludeIds: args.IncludeIds, + GenerateImportScripts: args.GenerateImportScripts, + DummySecretVariableValues: args.DummySecretVariableValues, + DummySecretGenerator: dummySecretGenerator, + } + certificateConverter := converters.CertificateConverter{ Client: &octopusClient, DummySecretVariableValues: args.DummySecretVariableValues, @@ -1003,6 +1019,7 @@ func ConvertSpaceToTerraform(args args.Arguments, version string) (*data.Resourc KubernetesAgentWorkerConverter: k8sAgentWorkerConverter, ListeningWorkerConverter: listeningWorkerConverter, SshWorkerConverter: sshWorkerConverter, + MachineProxyConverter: machineProxyConverter, } octopusActionProcessor := converters.OctopusActionProcessor{ diff --git a/cmd/octoterra_test.go b/cmd/octoterra_test.go index fe1ebab9..a6171adc 100644 --- a/cmd/octoterra_test.go +++ b/cmd/octoterra_test.go @@ -10069,3 +10069,46 @@ func TestS3FeedExport(t *testing.T) { return nil }) } + +// TestMachineProxies verifies that a machine proxy can be reimported with the correct settings +func TestMachineProxies(t *testing.T) { + + exportSpaceImportAndTest( + t, + "../test/terraform/87-machineproxy/space_creation", + "../test/terraform/87-machineproxy/space_population", + []string{}, + []string{ + "-var=machine_proxy_test_password=whatever", + }, + args2.Arguments{ + ExcludeProjectGroupsExcept: []string{"Test"}, + }, + func(t *testing.T, container *test.OctopusContainer, recreatedSpaceId string, terraformStateDir string) error { + // Assert + octopusClient := createClient(container, recreatedSpaceId) + + collection := octopus.GeneralCollection[octopus.MachineProxy]{} + err := octopusClient.GetAllResources("Proxies", &collection) + + if err != nil { + return err + } + + found := false + for _, v := range collection.Items { + if v.Name == "Test" { + found = true + if v.Host != "localhost" { + return errors.New("the machine proxy must be have a host of \"localhost\"") + } + } + } + + if !found { + return errors.New("space must have a machine proxy called \"Test\"") + } + + return nil + }) +} diff --git a/test/terraform/87-machineproxy/space_creation/config.tf b/test/terraform/87-machineproxy/space_creation/config.tf new file mode 100644 index 00000000..556b69ea --- /dev/null +++ b/test/terraform/87-machineproxy/space_creation/config.tf @@ -0,0 +1,5 @@ +terraform { + required_providers { + octopusdeploy = { source = "OctopusDeployLabs/octopusdeploy", version = "0.40.4" } + } +} diff --git a/test/terraform/87-machineproxy/space_creation/provider.tf b/test/terraform/87-machineproxy/space_creation/provider.tf new file mode 100644 index 00000000..8020467e --- /dev/null +++ b/test/terraform/87-machineproxy/space_creation/provider.tf @@ -0,0 +1,4 @@ +provider "octopusdeploy" { + address = "${var.octopus_server}" + api_key = "${var.octopus_apikey}" +} diff --git a/test/terraform/87-machineproxy/space_creation/provider_vars.tf b/test/terraform/87-machineproxy/space_creation/provider_vars.tf new file mode 100644 index 00000000..c7d93fe4 --- /dev/null +++ b/test/terraform/87-machineproxy/space_creation/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/test/terraform/87-machineproxy/space_creation/space.tf b/test/terraform/87-machineproxy/space_creation/space.tf new file mode 100644 index 00000000..9a5f39bf --- /dev/null +++ b/test/terraform/87-machineproxy/space_creation/space.tf @@ -0,0 +1,28 @@ +resource "octopusdeploy_space" "octopus_space_test" { + name = var.octopus_space_name + is_default = false + is_task_queue_stopped = false + description = var.octopus_space_description + space_managers_teams = ["teams-administrators"] +} + +output "octopus_space_id" { + value = octopusdeploy_space.octopus_space_test.id +} + +variable "octopus_space_name" { + type = string + nullable = false + sensitive = false + description = "The name of the new space" + default = "Test" +} + +variable "octopus_space_description" { + type = string + nullable = false + sensitive = false + description = "The description of the new space" + default = "My test space" +} + diff --git a/test/terraform/87-machineproxy/space_population/config.tf b/test/terraform/87-machineproxy/space_population/config.tf new file mode 100644 index 00000000..556b69ea --- /dev/null +++ b/test/terraform/87-machineproxy/space_population/config.tf @@ -0,0 +1,5 @@ +terraform { + required_providers { + octopusdeploy = { source = "OctopusDeployLabs/octopusdeploy", version = "0.40.4" } + } +} diff --git a/test/terraform/87-machineproxy/space_population/machine_proxy.tf b/test/terraform/87-machineproxy/space_population/machine_proxy.tf new file mode 100644 index 00000000..9223638a --- /dev/null +++ b/test/terraform/87-machineproxy/space_population/machine_proxy.tf @@ -0,0 +1,7 @@ +resource "octopusdeploy_machine_proxy" "machineproxy" { + name = "Test" + host = "localhost" + username = "admin" + password = "password" + port = 100 +} diff --git a/test/terraform/87-machineproxy/space_population/provider.tf b/test/terraform/87-machineproxy/space_population/provider.tf new file mode 100644 index 00000000..a0419772 --- /dev/null +++ b/test/terraform/87-machineproxy/space_population/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/test/terraform/87-machineproxy/space_population/provider_vars.tf b/test/terraform/87-machineproxy/space_population/provider_vars.tf new file mode 100644 index 00000000..c7d93fe4 --- /dev/null +++ b/test/terraform/87-machineproxy/space_population/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/test/terraform/87-machineproxy/space_population/space.tf b/test/terraform/87-machineproxy/space_population/space.tf new file mode 100644 index 00000000..ee59bdc8 --- /dev/null +++ b/test/terraform/87-machineproxy/space_population/space.tf @@ -0,0 +1,3 @@ +output "octopus_space_id" { + value = var.octopus_space_id +}