diff --git a/go.mod b/go.mod index 145bf941..1a16e81b 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/OctopusDeploy/terraform-provider-octopusdeploy go 1.21 require ( - github.com/OctopusDeploy/go-octopusdeploy/v2 v2.50.0 + github.com/OctopusDeploy/go-octopusdeploy/v2 v2.52.0 github.com/OctopusSolutionsEngineering/OctopusTerraformTestFramework v0.0.0-20240729041805-46db6fb717b4 github.com/google/uuid v1.6.0 github.com/hashicorp/go-cty v1.4.1-0.20200723130312-85980079f637 @@ -17,7 +17,6 @@ require ( github.com/hashicorp/terraform-plugin-testing v1.8.0 github.com/stretchr/testify v1.9.0 github.com/testcontainers/testcontainers-go v0.32.0 - golang.org/x/exp v0.0.0-20240707233637-46b078467d37 golang.org/x/text v0.16.0 k8s.io/utils v0.0.0-20230505201702-9f6742963106 software.sslmate.com/src/go-pkcs12 v0.4.0 @@ -130,6 +129,7 @@ require ( go.opentelemetry.io/otel/sdk v1.21.0 // indirect go.opentelemetry.io/otel/trace v1.24.0 // indirect golang.org/x/crypto v0.25.0 // indirect + golang.org/x/exp v0.0.0-20240707233637-46b078467d37 // indirect golang.org/x/mod v0.19.0 // indirect golang.org/x/net v0.27.0 // indirect golang.org/x/sync v0.7.0 // indirect @@ -142,5 +142,3 @@ require ( google.golang.org/protobuf v1.34.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) - -replace github.com/OctopusDeploy/go-octopusdeploy/v2 => ../go-octopusdeploy/ diff --git a/go.sum b/go.sum index 066bca24..bfd0603f 100644 --- a/go.sum +++ b/go.sum @@ -20,8 +20,8 @@ github.com/Microsoft/hcsshim v0.12.4 h1:Ev7YUMHAHoWNm+aDSPzc5W9s6E2jyL1szpVDJeZ/ github.com/Microsoft/hcsshim v0.12.4/go.mod h1:Iyl1WVpZzr+UkzjekHZbV8o5Z9ZkxNGx6CtY2Qg/JVQ= github.com/OctopusDeploy/go-octodiff v1.0.0 h1:U+ORg6azniwwYo+O44giOw6TiD5USk8S4VDhOQ0Ven0= github.com/OctopusDeploy/go-octodiff v1.0.0/go.mod h1:Mze0+EkOWTgTmi8++fyUc6r0aLZT7qD9gX+31t8MmIU= -github.com/OctopusDeploy/go-octopusdeploy/v2 v2.50.0 h1:rQiLEbqt/D3lPQw3pq9sXAW1C0WhVLrfN/h0cqUzaFY= -github.com/OctopusDeploy/go-octopusdeploy/v2 v2.50.0/go.mod h1:ggvOXzMnq+w0pLg6C9zdjz6YBaHfO3B3tqmmB7JQdaw= +github.com/OctopusDeploy/go-octopusdeploy/v2 v2.52.0 h1:X3Tdij/cGqmEtmZ0HqJFeHzTJVxFmYEAog4R4w6KFIw= +github.com/OctopusDeploy/go-octopusdeploy/v2 v2.52.0/go.mod h1:ggvOXzMnq+w0pLg6C9zdjz6YBaHfO3B3tqmmB7JQdaw= github.com/OctopusSolutionsEngineering/OctopusTerraformTestFramework v0.0.0-20240729041805-46db6fb717b4 h1:QfbVf0bOIRMp/WHAWsuVDB7KHoWnRsGbvDuOf2ua7k4= github.com/OctopusSolutionsEngineering/OctopusTerraformTestFramework v0.0.0-20240729041805-46db6fb717b4/go.mod h1:Oq9KbiRNDBB5jFmrwnrgLX0urIqR/1ptY18TzkqXm7M= github.com/ProtonMail/go-crypto v1.1.0-alpha.2 h1:bkyFVUP+ROOARdgCiJzNQo2V2kiB97LyUpzH9P6Hrlg= diff --git a/octopusdeploy_framework/resource_step_template_test.go b/octopusdeploy_framework/resource_step_template_test.go new file mode 100644 index 00000000..37f6f742 --- /dev/null +++ b/octopusdeploy_framework/resource_step_template_test.go @@ -0,0 +1,225 @@ +package octopusdeploy_framework + +import ( + "fmt" + "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/actiontemplates" + "github.com/hashicorp/terraform-plugin-testing/helper/acctest" + "github.com/hashicorp/terraform-plugin-testing/helper/resource" + "github.com/hashicorp/terraform-plugin-testing/terraform" + "testing" +) + +type stepTemplatePackagePropsTestData struct { + extract string + purpose string + selectionMode string +} + +type stepTemplatePackageTestData struct { + packageID string + acquisitonLocation string + feedID string + name string + properties stepTemplatePackagePropsTestData +} + +type stepTemplateParamTestData struct { + defaultValue string + displaySettings map[string]string + helpText string + label string + name string + id string +} + +type stepTemplateTestData struct { + localName string + prefix string + actionType string + name string + description string + stepPackageID string + packages []stepTemplatePackageTestData + parameters []stepTemplateParamTestData + properties map[string]string +} + +func TestAccOctopusStepTemplateBasic(t *testing.T) { + localName := acctest.RandStringFromCharSet(20, acctest.CharSetAlpha) + prefix := "octopusdeploy_step_template." + localName + data := stepTemplateTestData{ + localName: localName, + prefix: prefix, + actionType: "Octopus.Script", + name: acctest.RandStringFromCharSet(10, acctest.CharSetAlpha), + description: acctest.RandStringFromCharSet(20, acctest.CharSetAlpha), + stepPackageID: "Octopus.Script", + packages: []stepTemplatePackageTestData{ + { + packageID: "force", + acquisitonLocation: "Server", + feedID: "feeds-builtin", + name: "mypackage", + properties: stepTemplatePackagePropsTestData{ + extract: "True", + purpose: "", + selectionMode: "immediate", + }, + }, + }, + parameters: []stepTemplateParamTestData{ + { + defaultValue: "Hello World", + displaySettings: map[string]string{ + "Octopus.ControlType": "SingleLineText", + }, + helpText: acctest.RandStringFromCharSet(10, acctest.CharSetAlpha), + label: acctest.RandStringFromCharSet(10, acctest.CharSetAlpha), + name: acctest.RandStringFromCharSet(10, acctest.CharSetAlpha), + id: "621e1584-cdf3-4b67-9204-fc82430c908c", + }, + { + defaultValue: "Hello Earth", + displaySettings: map[string]string{ + "Octopus.ControlType": "SingleLineText", + }, + helpText: acctest.RandStringFromCharSet(10, acctest.CharSetAlpha), + label: acctest.RandStringFromCharSet(10, acctest.CharSetAlpha), + name: acctest.RandStringFromCharSet(10, acctest.CharSetAlpha), + id: "cd731d21-669a-42e1-81af-048681fd5c69", + }, + }, + properties: map[string]string{ + "Octopus.Action.Script.ScriptBody": "echo \"Hello World\"", + "Octopus.Action.Script.ScriptSource": "Inline", + "Octopus.Action.Script.Syntax": "Bash", + }, + } + + resource.Test(t, resource.TestCase{ + CheckDestroy: func(s *terraform.State) error { return testStepTemplateDestroy(s, localName) }, + PreCheck: func() { TestAccPreCheck(t) }, + ProtoV6ProviderFactories: ProtoV6ProviderFactories(), + Steps: []resource.TestStep{ + { + Config: testStepTemplateRunScriptBasic(data), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr(prefix, "name", data.name), + ), + }, + { + Config: testStepTemplateRunScriptUpdate(data), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr(prefix, "name", data.name+"-updated"), + ), + }, + }, + }) +} + +func testStepTemplateRunScriptBasic(data stepTemplateTestData) string { + return fmt.Sprintf(` + resource "octopusdeploy_step_template" "%s" { + action_type = "%s" + name = "%s" + description = "%s" + step_package_id = "%s" + packages = [ + { + package_id = "%s" + acquisition_location = "%s" + feed_id = "%s" + name = "%s" + properties = { + extract = "%s" + purpose = "%s" + selection_mode = "%s" + } + } + ] + parameters = [ + { + default_value = "%s" + display_settings = { + "Octopus.ControlType" : "%s" + } + help_text = "%s" + label = "%s" + name = "%s" + id = "%s" + }, + { + default_value = "%s" + display_settings = { + "Octopus.ControlType" : "%s" + } + help_text = "%s" + label = "%s" + name = "%s" + id = "%s" + }, + ] + properties = { + "Octopus.Action.Script.ScriptBody" : "%s" + "Octopus.Action.Script.ScriptSource" : "%s" + "Octopus.Action.Script.Syntax" : "%s" + } + } +`, + data.localName, + data.actionType, + data.name, + data.description, + data.stepPackageID, + data.packages[0].packageID, + data.packages[0].acquisitonLocation, + data.packages[0].feedID, + data.packages[0].name, + data.packages[0].properties.extract, + data.packages[0].properties.purpose, + data.packages[0].properties.selectionMode, + data.parameters[0].defaultValue, + data.parameters[0].displaySettings["Octopus.ControlType"], + data.parameters[0].helpText, + data.parameters[0].label, + data.parameters[0].name, + data.parameters[0].id, + data.parameters[1].defaultValue, + data.parameters[1].displaySettings["Octopus.ControlType"], + data.parameters[1].helpText, + data.parameters[1].label, + data.parameters[1].name, + data.parameters[1].id, + data.properties["Octopus.Action.Script.ScriptBody"], + data.properties["Octopus.Action.Script.ScriptSource"], + data.properties["Octopus.Action.Script.Syntax"], + ) +} + +func testStepTemplateRunScriptUpdate(data stepTemplateTestData) string { + data.name = data.name + "-updated" + + return testStepTemplateRunScriptBasic(data) +} + +func testStepTemplateDestroy(s *terraform.State, localName string) error { + var actionTemplateID string + + if rs, ok := s.RootModule().Resources[localName]; ok { + if rs.Type != "octopusdeploy_step_template" { + return fmt.Errorf("resource has unexpected type: %s", rs.Type) + } + actionTemplateID = rs.Primary.ID + } else { + return fmt.Errorf("resource octopusdeploy_template.%s not found in state", localName) + } + + actionTemplate, err := actiontemplates.GetByID(octoClient, octoClient.GetSpaceID(), actionTemplateID) + if err == nil { + if actionTemplate != nil { + return fmt.Errorf("step template (%s) still exists", actionTemplate.Name) + } + } + + return nil +} diff --git a/octopusdeploy_framework/util/util.go b/octopusdeploy_framework/util/util.go index a553a9fd..b67394f7 100644 --- a/octopusdeploy_framework/util/util.go +++ b/octopusdeploy_framework/util/util.go @@ -3,7 +3,6 @@ package util import ( "context" "fmt" - "github.com/hashicorp/terraform-plugin-framework/attr" "github.com/hashicorp/terraform-plugin-framework/diag" "github.com/hashicorp/terraform-plugin-framework/types"