Skip to content

Commit fc17f30

Browse files
authored
Merge pull request #104 from artem-nefedov/terraform-datasources
2 parents 13ca38f + a79ee94 commit fc17f30

File tree

2 files changed

+46
-6
lines changed

2 files changed

+46
-6
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: 36 additions & 1 deletion
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",
@@ -27,6 +38,17 @@ describe("terraform_handler", function()
2738
)
2839
end)
2940

41+
it("azure datasources", function()
42+
assert.equals(
43+
"https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/data-sources/location",
44+
handler.handle("v", 'data "azurerm_location" "example" {')
45+
)
46+
assert.equals(
47+
"https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/data-sources/resources",
48+
handler.handle("v", 'data "azurerm_resources" "spokes" {')
49+
)
50+
end)
51+
3052
it("google resources", function()
3153
assert.equals(
3254
"https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_instance",
@@ -38,6 +60,13 @@ describe("terraform_handler", function()
3860
)
3961
end)
4062

63+
it("google datasources", function()
64+
assert.equals(
65+
"https://registry.terraform.io/providers/hashicorp/google/latest/docs/data-sources/compute_images",
66+
handler.handle("v", 'data "google_compute_images" "example" {')
67+
)
68+
end)
69+
4170
it("kubernetes resources", function()
4271
assert.equals(
4372
"https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs/resources/deployment",
@@ -49,10 +78,16 @@ describe("terraform_handler", function()
4978
)
5079
end)
5180

81+
it("kubernetes datasources", function()
82+
assert.equals(
83+
"https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs/data-sources/mutating_webhook_configuration_v1",
84+
handler.handle("v", 'data "kubernetes_mutating_webhook_configuration_v1" "example" {')
85+
)
86+
end)
87+
5288
it("non-terraform lines", function()
5389
assert.is_nil(handler.handle("v", "This is not a terraform resource"))
5490
assert.is_nil(handler.handle("v", 'variable "example" {'))
55-
assert.is_nil(handler.handle("v", 'data "aws_ami" "example" {'))
5691
assert.is_nil(handler.handle("v", 'resource "unknown_provider_resource" "example" {'))
5792
end)
5893
end)

0 commit comments

Comments
 (0)