Skip to content

Commit

Permalink
Added machine proxy export
Browse files Browse the repository at this point in the history
  • Loading branch information
mcasperson committed Feb 28, 2025
1 parent 799a8e9 commit 3173106
Show file tree
Hide file tree
Showing 13 changed files with 161 additions and 1 deletion.
2 changes: 1 addition & 1 deletion cmd/internal/converters/machine_proxy_converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
7 changes: 7 additions & 0 deletions cmd/internal/converters/space_converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type SpaceConverter struct {
KubernetesAgentWorkerConverter Converter
ListeningWorkerConverter Converter
SshWorkerConverter Converter
MachineProxyConverter Converter
ErrGroup *errgroup.Group
ExcludeSpaceCreation bool
}
Expand Down Expand Up @@ -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()
}

Expand Down Expand Up @@ -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()
}

Expand Down
17 changes: 17 additions & 0 deletions cmd/internal/entry/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -1003,6 +1019,7 @@ func ConvertSpaceToTerraform(args args.Arguments, version string) (*data.Resourc
KubernetesAgentWorkerConverter: k8sAgentWorkerConverter,
ListeningWorkerConverter: listeningWorkerConverter,
SshWorkerConverter: sshWorkerConverter,
MachineProxyConverter: machineProxyConverter,
}

octopusActionProcessor := converters.OctopusActionProcessor{
Expand Down
43 changes: 43 additions & 0 deletions cmd/octoterra_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
}
5 changes: 5 additions & 0 deletions test/terraform/87-machineproxy/space_creation/config.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
terraform {
required_providers {
octopusdeploy = { source = "OctopusDeployLabs/octopusdeploy", version = "0.40.4" }
}
}
4 changes: 4 additions & 0 deletions test/terraform/87-machineproxy/space_creation/provider.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
provider "octopusdeploy" {
address = "${var.octopus_server}"
api_key = "${var.octopus_apikey}"
}
18 changes: 18 additions & 0 deletions test/terraform/87-machineproxy/space_creation/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"
}
28 changes: 28 additions & 0 deletions test/terraform/87-machineproxy/space_creation/space.tf
Original file line number Diff line number Diff line change
@@ -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"
}

5 changes: 5 additions & 0 deletions test/terraform/87-machineproxy/space_population/config.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
terraform {
required_providers {
octopusdeploy = { source = "OctopusDeployLabs/octopusdeploy", version = "0.40.4" }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
resource "octopusdeploy_machine_proxy" "machineproxy" {
name = "Test"
host = "localhost"
username = "admin"
password = "password"
port = 100
}
5 changes: 5 additions & 0 deletions test/terraform/87-machineproxy/space_population/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 test/terraform/87-machineproxy/space_population/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 test/terraform/87-machineproxy/space_population/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
}

0 comments on commit 3173106

Please sign in to comment.