Skip to content

Commit 8e4e308

Browse files
mheapharshadixit12
andauthored
Replace periods underscore kong2tf (#1756)
* Add kong2tf failing test * Replace periods with underscores in resource names * fix: expand slugify to all unsupported chars --------- Co-authored-by: Harsha Dixit <harsha.dixit@konghq.com>
1 parent d48b55b commit 8e4e308

File tree

4 files changed

+75
-1
lines changed

4 files changed

+75
-1
lines changed

kong2tf/generate_resource.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"sort"
66
"strings"
7+
"unicode"
78
)
89

910
type importConfig struct {
@@ -113,7 +114,7 @@ resource "konnect_%s" "%s" {
113114
%s control_plane_id = var.control_plane_id%s
114115
}
115116
`,
116-
entityType, name,
117+
entityType, slugify(name),
117118
strings.TrimRight(output(entityType, entity, 1, true, "\n", customizations, oneOfFields), "\n"),
118119
generateParents(parents),
119120
generateLifecycle(lifecycle))
@@ -467,3 +468,28 @@ func contains(slice []string, item string) bool {
467468
}
468469
return false
469470
}
471+
472+
// This is a helper function used to convert characters not supported by terraform into underscores
473+
func keepRuneUnicode(r rune) rune {
474+
if unicode.IsLetter(r) || unicode.IsDigit(r) || r == '_' || r == '-' {
475+
return r
476+
}
477+
return '_'
478+
}
479+
480+
// slugify converts a string into a slug using underscores.
481+
// This replaces all characters that are not letters, numbers, dashes or underscores
482+
// based on https://developer.hashicorp.com/terraform/language/syntax/configuration#identifiers
483+
func slugify(input string) string {
484+
slug := strings.Map(keepRuneUnicode, input)
485+
486+
// Remove any consecutive underscores
487+
for strings.Contains(slug, "__") {
488+
slug = strings.ReplaceAll(slug, "__", "_")
489+
}
490+
491+
// Trim leading and trailing underscores
492+
slug = strings.Trim(slug, "_")
493+
494+
return slug
495+
}

kong2tf/kong2tf_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,12 @@ func Test_convertKongGatewayToTerraform(t *testing.T) {
191191
outputFilename: "partial-output-expected.tf",
192192
wantErr: false,
193193
},
194+
{
195+
name: "handles-unsupported-chars",
196+
inputFilename: "handles-unsupported-chars-input.yaml",
197+
outputFilename: "handles-unsupported-chars-expected.tf",
198+
wantErr: false,
199+
},
194200
}
195201
for _, tt := range tests {
196202
t.Run(tt.name, func(t *testing.T) {
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
variable "control_plane_id" {
2+
type = string
3+
default = "YOUR_CONTROL_PLANE_ID"
4+
}
5+
6+
resource "konnect_gateway_consumer" "my_consumer123_my_org_prod_2_example" {
7+
username = "my-consumer123@my-org.prod_2.example"
8+
custom_id = "1234567890"
9+
tags = ["internal"]
10+
11+
control_plane_id = var.control_plane_id
12+
}
13+
14+
resource "konnect_gateway_consumer" "宮本武蔵_my_org_prod_example" {
15+
username = "宮本武蔵@my-org.prod.example"
16+
custom_id = "1234567891"
17+
tags = ["internal"]
18+
19+
control_plane_id = var.control_plane_id
20+
}
21+
22+
resource "konnect_gateway_consumer" "my_consumer_124_my_org_prod_example" {
23+
username = "my-consumer©124@my-org.prod.example"
24+
custom_id = "1234567892"
25+
tags = ["internal"]
26+
27+
control_plane_id = var.control_plane_id
28+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
consumers:
2+
- username: my-consumer123@my-org.prod_2.example
3+
custom_id: "1234567890"
4+
tags:
5+
- internal
6+
- username: 宮本武蔵@my-org.prod.example # unicode letters
7+
custom_id: "1234567891"
8+
tags:
9+
- internal
10+
- username: my-consumer©124@my-org.prod.example #© is not a letter - it is a symbol
11+
custom_id: "1234567892"
12+
tags:
13+
- internal
14+

0 commit comments

Comments
 (0)