Skip to content

Commit

Permalink
Implement the regex and regexall function
Browse files Browse the repository at this point in the history
This should be implemented in the newly released `pulumi-std` 2.2.0, so
we can start generating invoke calls for it.

Blocked by pulumi/pulumi-std#90

Related #65
Fixes #191
Fixes pulumi/pulumi#18520
  • Loading branch information
brandonpollack23 committed Feb 13, 2025
1 parent 2e1d822 commit 8004905
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 29 deletions.
1 change: 1 addition & 0 deletions CHANGELOG_PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@
- Implement `anytrue` through the `pulumi-std` invoke of the same name
- Implement `contains` through the `pulumi-std` invoke of the same name
- Implement `chunklist` through the `pulumi-std` invoke of the same name
- Implement `regex(all)` through the `pulumi-std` invokes of the same name

### Bug Fixes
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,6 @@
"warning:builtin_functions/main.tf:670,15-41:Function not yet implemented:Function toset not yet implemented, see pulumi/pulumi-converter-terraform#137",
"warning:builtin_functions/main.tf:670,11-42:Function not yet implemented:Function one not yet implemented, see pulumi/pulumi-converter-terraform#65 (catch all bug)",
"warning:builtin_functions/main.tf:706,11-26:Function not yet implemented:Function plantimestamp not yet implemented, see pulumi/pulumi-converter-terraform#65 (catch all bug)",
"warning:builtin_functions/main.tf:742,11-59:Function not yet implemented:Function regex not yet implemented, see pulumi/pulumi-converter-terraform#65 (catch all bug)",
"warning:builtin_functions/main.tf:745,11-66:Function not yet implemented:Function regex not yet implemented, see pulumi/pulumi-converter-terraform#65 (catch all bug)",
"warning:builtin_functions/main.tf:748,11-106:Function not yet implemented:Function regex not yet implemented, see pulumi/pulumi-converter-terraform#65 (catch all bug)",
"warning:builtin_functions/main.tf:751,11-50:Function not yet implemented:Function regex not yet implemented, see pulumi/pulumi-converter-terraform#65 (catch all bug)",
"warning:builtin_functions/main.tf:757,11-50:Function not yet implemented:Function regexall not yet implemented, see pulumi/pulumi-converter-terraform#191",
"warning:builtin_functions/main.tf:760,18-57:Function not yet implemented:Function regexall not yet implemented, see pulumi/pulumi-converter-terraform#191",
"warning:builtin_functions/main.tf:763,18-49:Function not yet implemented:Function regexall not yet implemented, see pulumi/pulumi-converter-terraform#191",
"warning:builtin_functions/main.tf:778,11-29:Function not yet implemented:Function reverse not yet implemented, see pulumi/pulumi-converter-terraform#65 (catch all bug)",
"warning:builtin_functions/main.tf:808,11-67:Function not yet implemented:Function setproduct not yet implemented, see pulumi/pulumi-converter-terraform#65 (catch all bug)",
"warning:builtin_functions/main.tf:811,11-35:Function not yet implemented:Function setproduct not yet implemented, see pulumi/pulumi-converter-terraform#65 (catch all bug)",
Expand Down
35 changes: 28 additions & 7 deletions pkg/convert/testdata/programs/builtin_functions/pcl/main.pp
Original file line number Diff line number Diff line change
Expand Up @@ -1150,29 +1150,50 @@

# Examples for regex
output "funcRegex0" {
value = notImplemented("regex(\"[a-z]+\",\"53453453.345345aaabbbccc23454\")")
value = invoke("std:index:regex", {
pattern = "[a-z]+"
string = "53453453.345345aaabbbccc23454"
}).result
}
output "funcRegex1" {
value = notImplemented("regex(\"(\\\\d\\\\d\\\\d\\\\d)-(\\\\d\\\\d)-(\\\\d\\\\d)\",\"2019-02-01\")")
value = invoke("std:index:regex", {
pattern = "(\\d\\d\\d\\d)-(\\d\\d)-(\\d\\d)"
string = "2019-02-01"
}).result
}
output "funcRegex2" {
value = notImplemented("regex(\"^(?:(?P<scheme>[^:/?#]+):)?(?://(?P<authority>[^/?#]*))?\",\"https://terraform.io/docs/\")")
value = invoke("std:index:regex", {
pattern = "^(?:(?P<scheme>[^:/?#]+):)?(?://(?P<authority>[^/?#]*))?"
string = "https://terraform.io/docs/"
}).result
}
output "funcRegex3" {
value = notImplemented("regex(\"[a-z]+\",\"53453453.34534523454\")")
value = invoke("std:index:regex", {
pattern = "[a-z]+"
string = "53453453.34534523454"
}).result
}



# Examples for regexall
output "funcRegexall0" {
value = notImplemented("regexall(\"[a-z]+\",\"1234abcd5678efgh9\")")
value = invoke("std:index:regex", {
pattern = "[a-z]+"
string = "1234abcd5678efgh9"
}).result
}
output "funcRegexall1" {
value = length(notImplemented("regexall(\"[a-z]+\",\"1234abcd5678efgh9\")"))
value = length(invoke("std:index:regex", {
pattern = "[a-z]+"
string = "1234abcd5678efgh9"
}).result)
}
output "funcRegexall2" {
value = length(notImplemented("regexall(\"[a-z]+\",\"123456789\")")) > 0
value = length(invoke("std:index:regex", {
pattern = "[a-z]+"
string = "123456789"
}).result) > 0
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"warning:/files.tf:3,23-6,5:Function not yet implemented:Function toset not yet implemented, see pulumi/pulumi-converter-terraform#137",
"warning:/files.tf:14,51-107:Function not yet implemented:Function templatefile not yet implemented, see pulumi/pulumi-converter-terraform#192",
"warning:/files.tf:20,23-89:Function not yet implemented:Function setunion not yet implemented, see pulumi/pulumi-converter-terraform#65 (catch all bug)",
"warning:/files.tf:23,45-73:Function not yet implemented:Function regexall not yet implemented, see pulumi/pulumi-converter-terraform#191",
"warning:/files.tf:36,24-38:Function not yet implemented:Function tostring not yet implemented, see pulumi/pulumi-converter-terraform#65 (catch all bug)",
"warning:/files.tf:38,19-45,11:Function not yet implemented:Function tomap not yet implemented, see pulumi/pulumi-converter-terraform#65 (catch all bug)",
"warning:/files.tf:52,24-38:Function not yet implemented:Function tostring not yet implemented, see pulumi/pulumi-converter-terraform#65 (catch all bug)",
Expand All @@ -12,7 +11,6 @@
"warning:/files.tf:3,23-6,5:Function not yet implemented:Function toset not yet implemented, see pulumi/pulumi-converter-terraform#137",
"warning:/files.tf:14,51-107:Function not yet implemented:Function templatefile not yet implemented, see pulumi/pulumi-converter-terraform#192",
"warning:/files.tf:20,23-89:Function not yet implemented:Function setunion not yet implemented, see pulumi/pulumi-converter-terraform#65 (catch all bug)",
"warning:/files.tf:23,45-73:Function not yet implemented:Function regexall not yet implemented, see pulumi/pulumi-converter-terraform#191",
"warning:/files.tf:36,24-38:Function not yet implemented:Function tostring not yet implemented, see pulumi/pulumi-converter-terraform#65 (catch all bug)",
"warning:/files.tf:38,19-45,11:Function not yet implemented:Function tomap not yet implemented, see pulumi/pulumi-converter-terraform#65 (catch all bug)",
"warning:/files.tf:52,24-38:Function not yet implemented:Function tostring not yet implemented, see pulumi/pulumi-converter-terraform#65 (catch all bug)",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@
templateFileContents = { for p, sp in templateFilePaths : p => notImplemented("templatefile(\"$${var.base_dir}/$${sp}\",var.template_vars)") }
staticFileLocalPaths = { for p in staticFilePaths : p => "${baseDir}/${p}" }
outputFilePaths = notImplemented("setunion(keys(local.template_file_paths),local.static_file_paths)")
fileSuffixMatches = { for p in outputFilePaths : p => notImplemented("regexall(\"\\\\.[^\\\\.]+\\\\z\",p)") }
fileSuffixes = { for p, ms in fileSuffixMatches : p => length(ms) > 0 ? ms[0] : "" }
fileSuffixMatches = { for p in outputFilePaths : p => invoke("std:index:regex", {
pattern = "\\.[^\\.]+\\z"
string = p
}).result }
fileSuffixes = { for p, ms in fileSuffixMatches : p => length(ms) > 0 ? ms[0] : "" }
myFileTypes = { for p in outputFilePaths : p => invoke("std:index:lookup", {
map = fileTypes
key = fileSuffixes[p]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@
templateFileContents = { for p, sp in templateFilePaths : p => notImplemented("templatefile(\"$${var.base_dir}/$${sp}\",var.template_vars)") }
staticFileLocalPaths = { for p in staticFilePaths : p => "${baseDir}/${p}" }
outputFilePaths = notImplemented("setunion(keys(local.template_file_paths),local.static_file_paths)")
fileSuffixMatches = { for p in outputFilePaths : p => notImplemented("regexall(\"\\\\.[^\\\\.]+\\\\z\",p)") }
fileSuffixes = { for p, ms in fileSuffixMatches : p => length(ms) > 0 ? ms[0] : "" }
fileSuffixMatches = { for p in outputFilePaths : p => invoke("std:index:regex", {
pattern = "\\.[^\\.]+\\z"
string = p
}).result }
fileSuffixes = { for p, ms in fileSuffixMatches : p => length(ms) > 0 ? ms[0] : "" }
myFileTypes = { for p in outputFilePaths : p => invoke("std:index:lookup", {
map = fileTypes
key = fileSuffixes[p]
Expand Down
61 changes: 61 additions & 0 deletions pkg/convert/testdata/schemas/std.json
Original file line number Diff line number Diff line change
Expand Up @@ -1692,6 +1692,67 @@
"type": "object"
}
},
"std:index:regex": {
"description": "Returns the first match of a regular expression in a string (including named or indexed groups).",
"inputs": {
"properties": {
"pattern": {
"type": "string"
},
"string": {
"type": "string"
}
},
"type": "object",
"required": [
"pattern",
"string"
]
},
"outputs": {
"properties": {
"result": {
"$ref": "pulumi.json#/Any"
}
},
"required": [
"result"
],
"type": "object"
}
},
"std:index:regexall": {
"description": "Returns a list of all matches of a regular expression in a string (including named or indexed groups).",
"inputs": {
"properties": {
"pattern": {
"type": "string"
},
"string": {
"type": "string"
}
},
"type": "object",
"required": [
"pattern",
"string"
]
},
"outputs": {
"properties": {
"result": {
"items": {
"$ref": "pulumi.json#/Any"
},
"type": "array"
}
},
"required": [
"result"
],
"type": "object"
}
},
"std:index:replace": {
"description": "Does a search and replace on the given string.\nAll instances of search are replaced with the value of replace.\nIf search is wrapped in forward slashes, it is treated as a regular expression.\nIf using a regular expression, replace can reference subcaptures in the regular expression by\nusing $n where n is the index or name of the subcapture. If using a regular expression,\nthe syntax conforms to the re2 regular expression syntax.",
"inputs": {
Expand Down
8 changes: 4 additions & 4 deletions pkg/convert/testdata/states/count/import.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[
{
"Type": "simple:index:resource",
"Name": "a_resource-0",
"ID": "abc123",
"Name": "a_resource-1",
"ID": "def456",
"LogicalName": "",
"IsComponent": false,
"IsRemote": false,
Expand All @@ -11,8 +11,8 @@
},
{
"Type": "simple:index:resource",
"Name": "a_resource-1",
"ID": "def456",
"Name": "a_resource-0",
"ID": "abc123",
"LogicalName": "",
"IsComponent": false,
"IsRemote": false,
Expand Down
8 changes: 4 additions & 4 deletions pkg/convert/testdata/states/range/import.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[
{
"Type": "simple:index:resource",
"Name": "a_resource-hello",
"ID": "abc123",
"Name": "a_resource-goodbye",
"ID": "def456",
"LogicalName": "",
"IsComponent": false,
"IsRemote": false,
Expand All @@ -11,8 +11,8 @@
},
{
"Type": "simple:index:resource",
"Name": "a_resource-goodbye",
"ID": "def456",
"Name": "a_resource-hello",
"ID": "abc123",
"LogicalName": "",
"IsComponent": false,
"IsRemote": false,
Expand Down
11 changes: 10 additions & 1 deletion pkg/convert/tf.go
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,16 @@ var tfFunctionStd = map[string]struct {
inputs: []string{"limit", "start", "step"},
output: "result",
},
"regex": {
token: "std:index:regex",
inputs: []string{"pattern", "string"},
output: "result",
},
"regexall": {
token: "std:index:regex",
inputs: []string{"pattern", "string"},
output: "result",
},
"replace": {
token: "std:index:replace",
inputs: []string{"text", "search", "replace"},
Expand Down Expand Up @@ -3660,7 +3670,6 @@ func componentProgramBinderFromAfero(fs afero.Fs) pcl.ComponentProgramBinder {

var unimplementedFunctionBugs = map[string]string{
"formatdate": "pulumi/pulumi-converter-terraform#196",
"regexall": "pulumi/pulumi-converter-terraform#191",
"slice": "pulumi/pulumi-converter-terraform#65",
"templatefile": "pulumi/pulumi-converter-terraform#192",
"toset": "pulumi/pulumi-converter-terraform#137",
Expand Down

0 comments on commit 8004905

Please sign in to comment.