diff --git a/lua/gx/handlers/terraform.lua b/lua/gx/handlers/terraform.lua index 3d62119..61321ee 100644 --- a/lua/gx/handlers/terraform.lua +++ b/lua/gx/handlers/terraform.lua @@ -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 diff --git a/test/spec/gx/handlers/terraform_spec.lua b/test/spec/gx/handlers/terraform_spec.lua index fb3c76e..c7dacbd 100644 --- a/test/spec/gx/handlers/terraform_spec.lua +++ b/test/spec/gx/handlers/terraform_spec.lua @@ -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", @@ -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", @@ -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", @@ -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)