add terraform snippets
This commit is contained in:
297
nvim/.config/nvim/snippets/terraform.lua
Normal file
297
nvim/.config/nvim/snippets/terraform.lua
Normal file
@@ -0,0 +1,297 @@
|
|||||||
|
-- Terraform snippets
|
||||||
|
local ls = require("luasnip")
|
||||||
|
local s = ls.snippet
|
||||||
|
local t = ls.text_node
|
||||||
|
local i = ls.insert_node
|
||||||
|
local f = ls.function_node
|
||||||
|
local c = ls.choice_node
|
||||||
|
local d = ls.dynamic_node
|
||||||
|
local sn = ls.snippet_node
|
||||||
|
local fmt = require("luasnip.extras.fmt").fmt
|
||||||
|
local rep = require("luasnip.extras").rep
|
||||||
|
|
||||||
|
-- Return a table of snippets for Terraform
|
||||||
|
-- stylua: ignore start
|
||||||
|
return {
|
||||||
|
-- Terraform block
|
||||||
|
s("terraform", fmt([[
|
||||||
|
terraform {{
|
||||||
|
required_version = ">= {}"
|
||||||
|
|
||||||
|
required_providers {{
|
||||||
|
{}
|
||||||
|
}}
|
||||||
|
|
||||||
|
{}
|
||||||
|
}}
|
||||||
|
]], {
|
||||||
|
i(1, "1.0.0"),
|
||||||
|
i(2, 'aws = {\n source = "hashicorp/aws"\n version = "~> 4.0"\n }'),
|
||||||
|
i(3, ""),
|
||||||
|
})),
|
||||||
|
|
||||||
|
-- Provider block
|
||||||
|
s("provider", fmt([[
|
||||||
|
provider "{}" {{
|
||||||
|
{}
|
||||||
|
}}
|
||||||
|
]], {
|
||||||
|
i(1, "aws"),
|
||||||
|
i(2, 'region = "us-west-2"'),
|
||||||
|
})),
|
||||||
|
|
||||||
|
-- AWS Provider with profile
|
||||||
|
s("aws", fmt([[
|
||||||
|
provider "aws" {{
|
||||||
|
region = "{}"
|
||||||
|
profile = "{}"
|
||||||
|
}}
|
||||||
|
]], {
|
||||||
|
i(1, "us-west-2"),
|
||||||
|
i(2, "default"),
|
||||||
|
})),
|
||||||
|
|
||||||
|
-- Resource block
|
||||||
|
s("resource", fmt([[
|
||||||
|
resource "{}" "{}" {{
|
||||||
|
{}
|
||||||
|
}}
|
||||||
|
]], {
|
||||||
|
i(1, "aws_s3_bucket"),
|
||||||
|
i(2, "example"),
|
||||||
|
i(3, ""),
|
||||||
|
})),
|
||||||
|
|
||||||
|
-- Data source block
|
||||||
|
s("data", fmt([[
|
||||||
|
data "{}" "{}" {{
|
||||||
|
{}
|
||||||
|
}}
|
||||||
|
]], {
|
||||||
|
i(1, "aws_ami"),
|
||||||
|
i(2, "example"),
|
||||||
|
i(3, 'owners = ["amazon"]'),
|
||||||
|
})),
|
||||||
|
|
||||||
|
-- Variable block
|
||||||
|
s("variable", fmt([[
|
||||||
|
variable "{}" {{
|
||||||
|
description = "{}"
|
||||||
|
type = {}
|
||||||
|
default = {}
|
||||||
|
{}
|
||||||
|
}}
|
||||||
|
]], {
|
||||||
|
i(1, "example"),
|
||||||
|
i(2, "Example variable"),
|
||||||
|
c(3, {
|
||||||
|
t("string"),
|
||||||
|
t("number"),
|
||||||
|
t("bool"),
|
||||||
|
t("list(string)"),
|
||||||
|
t("map(string)"),
|
||||||
|
t("object({})"),
|
||||||
|
sn(nil, {t("list(object({"), i(1, "name = string, value = string"), t("}))")}),
|
||||||
|
}),
|
||||||
|
c(4, {
|
||||||
|
t('null'),
|
||||||
|
sn(nil, {t('"'), i(1, "default_value"), t('"')}),
|
||||||
|
sn(nil, {t('true')}),
|
||||||
|
sn(nil, {t('false')}),
|
||||||
|
sn(nil, {t('123')}),
|
||||||
|
sn(nil, {t('['), i(1, '"item1", "item2"'), t(']')}),
|
||||||
|
sn(nil, {t('{'), i(1, 'key = "value"'), t('}')}),
|
||||||
|
}),
|
||||||
|
i(5, ""),
|
||||||
|
})),
|
||||||
|
|
||||||
|
-- Output block
|
||||||
|
s("output", fmt([[
|
||||||
|
output "{}" {{
|
||||||
|
description = "{}"
|
||||||
|
value = {}
|
||||||
|
{}
|
||||||
|
}}
|
||||||
|
]], {
|
||||||
|
i(1, "example"),
|
||||||
|
i(2, "Example output"),
|
||||||
|
i(3, "aws_instance.example.id"),
|
||||||
|
c(4, {
|
||||||
|
t(""),
|
||||||
|
t("sensitive = true"),
|
||||||
|
}),
|
||||||
|
})),
|
||||||
|
|
||||||
|
-- Local block
|
||||||
|
s("locals", fmt([[
|
||||||
|
locals {{
|
||||||
|
{} = {}
|
||||||
|
}}
|
||||||
|
]], {
|
||||||
|
i(1, "example"),
|
||||||
|
i(2, '"value"'),
|
||||||
|
})),
|
||||||
|
|
||||||
|
-- Module block
|
||||||
|
s("module", fmt([[
|
||||||
|
module "{}" {{
|
||||||
|
source = "{}"
|
||||||
|
|
||||||
|
{}
|
||||||
|
}}
|
||||||
|
]], {
|
||||||
|
i(1, "example"),
|
||||||
|
i(2, "./modules/example"),
|
||||||
|
i(3, "# Module inputs"),
|
||||||
|
})),
|
||||||
|
|
||||||
|
-- Backend block
|
||||||
|
s("backend", fmt([[
|
||||||
|
backend "{}" {{
|
||||||
|
{}
|
||||||
|
}}
|
||||||
|
]], {
|
||||||
|
i(1, "s3"),
|
||||||
|
i(2, 'bucket = "terraform-state"\nkey = "path/to/state.tfstate"\nregion = "us-west-2"'),
|
||||||
|
})),
|
||||||
|
|
||||||
|
-- Conditional expression
|
||||||
|
s("condition", fmt([[
|
||||||
|
{} ? {} : {}
|
||||||
|
]], {
|
||||||
|
i(1, "var.environment == \"prod\""),
|
||||||
|
i(2, "\"production\""),
|
||||||
|
i(3, "\"development\""),
|
||||||
|
})),
|
||||||
|
|
||||||
|
-- For expression
|
||||||
|
s("for", fmt([[
|
||||||
|
[for {} in {} : {}]
|
||||||
|
]], {
|
||||||
|
i(1, "item"),
|
||||||
|
i(2, "var.list"),
|
||||||
|
i(3, "item.name"),
|
||||||
|
})),
|
||||||
|
|
||||||
|
-- For expression with index
|
||||||
|
s("fori", fmt([[
|
||||||
|
[for {}, {} in {} : {}]
|
||||||
|
]], {
|
||||||
|
i(1, "index"),
|
||||||
|
i(2, "item"),
|
||||||
|
i(3, "var.list"),
|
||||||
|
i(4, "\"${index}-${item.name}\""),
|
||||||
|
})),
|
||||||
|
|
||||||
|
-- For expression with map
|
||||||
|
s("formap", fmt([[
|
||||||
|
{{for {} in {} : {} => {}}}
|
||||||
|
]], {
|
||||||
|
i(1, "item"),
|
||||||
|
i(2, "var.list"),
|
||||||
|
i(3, "item.key"),
|
||||||
|
i(4, "item.value"),
|
||||||
|
})),
|
||||||
|
|
||||||
|
-- Count parameter
|
||||||
|
s("count", fmt([[
|
||||||
|
count = {}
|
||||||
|
|
||||||
|
{}[count.index]
|
||||||
|
]], {
|
||||||
|
i(1, "var.create_resource ? 1 : 0"),
|
||||||
|
i(2, "# Reference this resource elsewhere using aws_instance.example"),
|
||||||
|
})),
|
||||||
|
|
||||||
|
-- For each parameter
|
||||||
|
s("foreach", fmt([[
|
||||||
|
for_each = {}
|
||||||
|
|
||||||
|
{}
|
||||||
|
]], {
|
||||||
|
c(1, {
|
||||||
|
sn(nil, {t("toset(["), i(1, "\"item1\", \"item2\""), t("])")}),
|
||||||
|
sn(nil, {t("{"), i(1, "key1 = \"value1\", key2 = \"value2\""), t("}")}),
|
||||||
|
sn(nil, {t("var."), i(1, "map_variable")}),
|
||||||
|
}),
|
||||||
|
i(2, "# Reference this resource elsewhere using aws_instance.example[each.key]"),
|
||||||
|
})),
|
||||||
|
|
||||||
|
-- Dynamic block
|
||||||
|
s("dynamic", fmt([[
|
||||||
|
dynamic "{}" {{
|
||||||
|
for_each = {}
|
||||||
|
content {{
|
||||||
|
{}
|
||||||
|
}}
|
||||||
|
}}
|
||||||
|
]], {
|
||||||
|
i(1, "ingress"),
|
||||||
|
i(2, "var.ingress_rules"),
|
||||||
|
i(3, "from_port = ingress.value.from_port\n to_port = ingress.value.to_port\n protocol = ingress.value.protocol\n cidr_blocks = ingress.value.cidr_blocks"),
|
||||||
|
})),
|
||||||
|
|
||||||
|
-- Lifecycle block
|
||||||
|
s("lifecycle", fmt([[
|
||||||
|
lifecycle {{
|
||||||
|
{}
|
||||||
|
}}
|
||||||
|
]], {
|
||||||
|
c(1, {
|
||||||
|
t("create_before_destroy = true"),
|
||||||
|
t("prevent_destroy = true"),
|
||||||
|
t("ignore_changes = [tags]"),
|
||||||
|
sn(nil, {t("ignore_changes = ["), i(1, "attribute"), t("]")}),
|
||||||
|
t("create_before_destroy = true\n prevent_destroy = true\n ignore_changes = [tags]"),
|
||||||
|
}),
|
||||||
|
})),
|
||||||
|
|
||||||
|
-- Terraform workspace
|
||||||
|
s("workspace", fmt([[
|
||||||
|
terraform.workspace
|
||||||
|
]], {})),
|
||||||
|
|
||||||
|
-- Depends on
|
||||||
|
s("depends", fmt([[
|
||||||
|
depends_on = [
|
||||||
|
{},
|
||||||
|
]
|
||||||
|
]], {
|
||||||
|
i(1, "aws_iam_role_policy.example"),
|
||||||
|
})),
|
||||||
|
|
||||||
|
-- Provisioner
|
||||||
|
s("provisioner", fmt([[
|
||||||
|
provisioner "{}" {{
|
||||||
|
{}
|
||||||
|
}}
|
||||||
|
]], {
|
||||||
|
c(1, {
|
||||||
|
t("local-exec"),
|
||||||
|
t("remote-exec"),
|
||||||
|
t("file"),
|
||||||
|
}),
|
||||||
|
c(2, {
|
||||||
|
sn(nil, {t('command = "'), i(1, "echo hello"), t('"')}),
|
||||||
|
sn(nil, {t('inline = [\n "'), i(1, "echo hello"), t('",\n ]')}),
|
||||||
|
sn(nil, {t('source = "'), i(1, "scripts/setup.sh"), t('"\n destination = "'), i(2, "/tmp/setup.sh"), t('"')}),
|
||||||
|
}),
|
||||||
|
})),
|
||||||
|
|
||||||
|
-- Null resource
|
||||||
|
s("null", fmt([[
|
||||||
|
resource "null_resource" "{}" {{
|
||||||
|
triggers = {{
|
||||||
|
{} = {}
|
||||||
|
}}
|
||||||
|
|
||||||
|
{}
|
||||||
|
}}
|
||||||
|
]], {
|
||||||
|
i(1, "example"),
|
||||||
|
i(2, "trigger"),
|
||||||
|
i(3, "uuid()"),
|
||||||
|
i(4, "# Provisioners or depends_on go here"),
|
||||||
|
})),
|
||||||
|
}
|
||||||
|
-- stylua: ignore end
|
||||||
Reference in New Issue
Block a user