Skip to content

Commit 7789736

Browse files
committed
Support terraform datasources
1 parent 13ca38f commit 7789736

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

lua/gx/handlers/terraform.lua

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,24 @@ local providers = {
1616
{ prefix = "kubernetes_", provider = "kubernetes" },
1717
}
1818

19-
local function match_terraform_resource(line, prefix)
20-
local pattern = string.format('resource "%s([^"]*)"', prefix)
19+
local function match_terraform_resource(line, kind, prefix)
20+
local pattern = string.format('%s "%s([^"]*)"', kind, prefix)
2121
return helper.find(line, nil, pattern)
2222
end
2323

2424
function M.handle(_, line, _)
25-
local base_url = "https://registry.terraform.io/providers/hashicorp/%s/latest/docs/resources/"
25+
local base_url = "https://registry.terraform.io/providers/hashicorp/%s/latest/docs"
2626

2727
-- Check each provider in our table
2828
for _, provider in ipairs(providers) do
29-
local resource = match_terraform_resource(line, provider.prefix)
29+
local resource = match_terraform_resource(line, "resource", provider.prefix)
3030
if resource then
31-
return base_url:format(provider.provider) .. resource
31+
return base_url:format(provider.provider) .. "/resources/" .. resource
32+
end
33+
34+
resource = match_terraform_resource(line, "data", provider.prefix)
35+
if resource then
36+
return base_url:format(provider.provider) .. "/data-sources/" .. resource
3237
end
3338
end
3439
end

test/spec/gx/handlers/terraform_spec.lua

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,17 @@ describe("terraform_handler", function()
1616
)
1717
end)
1818

19+
it("aws datasources", function()
20+
assert.equals(
21+
"https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/route",
22+
handler.handle("v", 'data "aws_route" "example" {')
23+
)
24+
assert.equals(
25+
"https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/region",
26+
handler.handle("v", 'data "aws_region" "current" {')
27+
)
28+
end)
29+
1930
it("azure resources", function()
2031
assert.equals(
2132
"https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/resource_group",
@@ -49,6 +60,13 @@ describe("terraform_handler", function()
4960
)
5061
end)
5162

63+
it("kubernetes datasources", function()
64+
assert.equals(
65+
"https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs/data-sources/mutating_webhook_configuration_v1",
66+
handler.handle("v", 'data "kubernetes_mutating_webhook_configuration_v1" "example" {')
67+
)
68+
end)
69+
5270
it("non-terraform lines", function()
5371
assert.is_nil(handler.handle("v", "This is not a terraform resource"))
5472
assert.is_nil(handler.handle("v", 'variable "example" {'))

0 commit comments

Comments
 (0)