Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions lua/gx/handlers/terraform.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,24 @@ local providers = {
{ prefix = "kubernetes_", provider = "kubernetes" },
}

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

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

-- Check each provider in our table
for _, provider in ipairs(providers) do
local resource = match_terraform_resource(line, provider.prefix)
local resource = match_terraform_resource(line, "resource", provider.prefix)
if resource then
return base_url:format(provider.provider) .. resource
return base_url:format(provider.provider) .. "/resources/" .. resource
end

resource = match_terraform_resource(line, "data", provider.prefix)
if resource then
return base_url:format(provider.provider) .. "/data-sources/" .. resource
end
end
end
Expand Down
37 changes: 36 additions & 1 deletion test/spec/gx/handlers/terraform_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@ describe("terraform_handler", function()
)
end)

it("aws datasources", function()
assert.equals(
"https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/route",
handler.handle("v", 'data "aws_route" "example" {')
)
assert.equals(
"https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/region",
handler.handle("v", 'data "aws_region" "current" {')
)
end)

it("azure resources", function()
assert.equals(
"https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/resource_group",
Expand All @@ -27,6 +38,17 @@ describe("terraform_handler", function()
)
end)

it("azure datasources", function()
assert.equals(
"https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/data-sources/location",
handler.handle("v", 'data "azurerm_location" "example" {')
)
assert.equals(
"https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/data-sources/resources",
handler.handle("v", 'data "azurerm_resources" "spokes" {')
)
end)

it("google resources", function()
assert.equals(
"https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_instance",
Expand All @@ -38,6 +60,13 @@ describe("terraform_handler", function()
)
end)

it("google datasources", function()
assert.equals(
"https://registry.terraform.io/providers/hashicorp/google/latest/docs/data-sources/compute_images",
handler.handle("v", 'data "google_compute_images" "example" {')
)
end)

it("kubernetes resources", function()
assert.equals(
"https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs/resources/deployment",
Expand All @@ -49,10 +78,16 @@ describe("terraform_handler", function()
)
end)

it("kubernetes datasources", function()
assert.equals(
"https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs/data-sources/mutating_webhook_configuration_v1",
handler.handle("v", 'data "kubernetes_mutating_webhook_configuration_v1" "example" {')
)
end)

it("non-terraform lines", function()
assert.is_nil(handler.handle("v", "This is not a terraform resource"))
assert.is_nil(handler.handle("v", 'variable "example" {'))
assert.is_nil(handler.handle("v", 'data "aws_ami" "example" {'))
assert.is_nil(handler.handle("v", 'resource "unknown_provider_resource" "example" {'))
end)
end)