Skip to content

Commit b872c33

Browse files
committed
Fixed escaping dollar signs
1 parent e9f29d5 commit b872c33

File tree

18 files changed

+238
-5
lines changed

18 files changed

+238
-5
lines changed

cmd/internal/converters/channel_converter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ func (c ChannelConverter) toHcl(channel octopus2.Channel, recursive bool, terraf
105105
for t, r := range terraformDependencies {
106106
if t != "" && r != "" {
107107
dependency := dependencies.GetResource(t, r)
108-
dependency = hcl.RemoveInterpolation(dependency)
108+
dependency = hcl.RemoveId(hcl.RemoveInterpolation(dependency))
109109
manualDependencies = append(manualDependencies, dependency)
110110
}
111111
}

cmd/internal/converters/tenant_converter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ func (c TenantConverter) toHcl(tenant octopus2.Tenant, recursive bool, dependenc
137137
for resourceType, terraformDependencies := range tagSetDependencies {
138138
for _, terraformDependency := range terraformDependencies {
139139
dependency := dependencies.GetResource(resourceType, terraformDependency)
140-
dependency = hcl.RemoveInterpolation(dependency)
140+
dependency = hcl.RemoveId(hcl.RemoveInterpolation(dependency))
141141
dependsOn = append(dependsOn, dependency)
142142
}
143143
}

cmd/internal/converters/variable_set_converter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ func (c VariableSetConverter) toHcl(resource octopus2.VariableSet, recursive boo
222222
for resourceType, terraformDependencies := range tagSetDependencies {
223223
for _, terraformDependency := range terraformDependencies {
224224
dependency := dependencies.GetResource(resourceType, terraformDependency)
225-
hcl.RemoveInterpolation(dependency)
225+
dependency = hcl.RemoveId(hcl.RemoveInterpolation(dependency))
226226
dependsOn = append(dependsOn, dependency)
227227
}
228228
}

cmd/internal/hcl/unquoted_attribute_writer.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"github.com/hashicorp/hcl2/hcl"
77
"github.com/hashicorp/hcl2/hclwrite"
8+
"regexp"
89
"strings"
910
)
1011

@@ -28,7 +29,7 @@ func extractJsonAsMap(properties map[string]string) string {
2829
output := "{"
2930

3031
for key, value := range properties {
31-
output += "\n \"" + key + "\" = " + jsonStringToHcl(fmt.Sprint(value))
32+
output += "\n \"" + key + "\" = " + jsonStringToHcl(value)
3233
}
3334

3435
output += "\n }"
@@ -105,3 +106,9 @@ func RemoveInterpolation(value string) string {
105106
value = strings.Replace(value, "}", "", -1)
106107
return value
107108
}
109+
110+
func RemoveId(value string) string {
111+
regex := regexp.MustCompile(`\.id$`)
112+
value = regex.ReplaceAllString(value, "")
113+
return value
114+
}

cmd/internal/strutil/string_util.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package strutil
22

33
import (
4+
"regexp"
45
"strconv"
56
"strings"
67
)
@@ -92,8 +93,9 @@ func ParseBoolPointer(input *string) *bool {
9293

9394
func UnEscapeDollar(fileMap map[string]string) map[string]string {
9495
// Unescape dollar signs because of https://github.com/hashicorp/hcl/issues/323
96+
regex := regexp.MustCompile(`"\$\$\{(.*?)\}"`)
9597
for k, v := range fileMap {
96-
fileMap[k] = strings.ReplaceAll(v, "$${", "${")
98+
fileMap[k] = regex.ReplaceAllString(v, "\"${$1}\"")
9799
}
98100

99101
return fileMap

cmd/octoterra_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3403,3 +3403,46 @@ func TestProjectWithGitUsernameExport(t *testing.T) {
34033403
return nil
34043404
})
34053405
}
3406+
3407+
// TestProjectWithDollarSignsExport verifies that a project can be reimported with the correct git settings
3408+
func TestProjectWithDollarSignsExport(t *testing.T) {
3409+
performTest(t, func(t *testing.T, container *octopusContainer) error {
3410+
// Arrange
3411+
newSpaceId, err := arrange(t, container, "../test/terraform/40-escapedollar", []string{})
3412+
3413+
if err != nil {
3414+
return err
3415+
}
3416+
3417+
// Act
3418+
recreatedSpaceId, err := act(t, container, newSpaceId, []string{})
3419+
3420+
if err != nil {
3421+
return err
3422+
}
3423+
3424+
// Assert
3425+
octopusClient := createClient(container, recreatedSpaceId)
3426+
3427+
collection := octopus.GeneralCollection[octopus.Project]{}
3428+
err = octopusClient.GetAllResources("Projects", &collection)
3429+
3430+
if err != nil {
3431+
return err
3432+
}
3433+
3434+
resourceName := "Test"
3435+
found := false
3436+
for _, v := range collection.Items {
3437+
if v.Name == resourceName {
3438+
found = true
3439+
}
3440+
}
3441+
3442+
if !found {
3443+
t.Fatal("Space must have an project called \"" + resourceName + "\"")
3444+
}
3445+
3446+
return nil
3447+
})
3448+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
terraform {
2+
required_providers {
3+
octopusdeploy = { source = "OctopusDeployLabs/octopusdeploy", version = "0.10.1" }
4+
}
5+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
provider "octopusdeploy" {
2+
address = "${var.octopus_server}"
3+
api_key = "${var.octopus_apikey}"
4+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
variable "octopus_server" {
2+
type = string
3+
nullable = false
4+
sensitive = false
5+
description = "The URL of the Octopus server e.g. https://myinstance.octopus.app."
6+
}
7+
variable "octopus_apikey" {
8+
type = string
9+
nullable = false
10+
sensitive = true
11+
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."
12+
}
13+
variable "octopus_space_id" {
14+
type = string
15+
nullable = false
16+
sensitive = false
17+
description = "The space ID to populate"
18+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
resource "octopusdeploy_space" "octopus_space_test" {
2+
name = "${var.octopus_space_name}"
3+
is_default = false
4+
is_task_queue_stopped = false
5+
description = "My test space"
6+
space_managers_teams = ["teams-administrators"]
7+
}
8+
9+
output "octopus_space_id" {
10+
value = octopusdeploy_space.octopus_space_test.id
11+
}
12+
13+
variable "octopus_space_name" {
14+
type = string
15+
nullable = false
16+
sensitive = false
17+
description = "The name of the new space"
18+
default = "Test"
19+
}

0 commit comments

Comments
 (0)