From 87a2bceadc08c4e287dcf48699cd32d979d7017e Mon Sep 17 00:00:00 2001 From: Danny Steenman Date: Thu, 14 Nov 2024 20:40:04 +0100 Subject: [PATCH] feat: add support for terraform --- .vscode-test/tf-template.tf | 22 ++++++++++++++++++++++ README.md | 4 ++-- package.json | 16 ++++++++++++++-- src/extension.ts | 18 ++++++++++++------ 4 files changed, 50 insertions(+), 10 deletions(-) create mode 100644 .vscode-test/tf-template.tf diff --git a/.vscode-test/tf-template.tf b/.vscode-test/tf-template.tf new file mode 100644 index 0000000..67ca722 --- /dev/null +++ b/.vscode-test/tf-template.tf @@ -0,0 +1,22 @@ +resource "aws_iam_policy" "policy" { + name = "test_policy" + path = "/" + description = "My test policy" + + # Terraform's "jsonencode" function converts a + # Terraform expression result to valid JSON syntax. + policy = jsonencode({ + Version = "2012-10-17" + Statement = [ + { + Action = [ + "ec2:Describe*", + "ec2:CreateTags", + "ec2:DeleteTags" + ] + Effect = "Allow" + Resource = "*" + } + ] + }) +} diff --git a/README.md b/README.md index 6ba273b..e8964aa 100644 --- a/README.md +++ b/README.md @@ -41,14 +41,14 @@ This AWS IAM Actions Snippets extension equips Visual Studio Code with comprehen 1. **Comprehensive Coverage**: Offers snippets for **all** AWS IAM actions available across various AWS services. 2. **Auto-completion**: Provides intelligent auto-completion for IAM actions as you type. 3. **Documentation Links**: Quick access to AWS documentation for each IAM action directly from the snippet. -4. **Flexible Support**: Works seamlessly with both YAML and JSON IAM policy documents. +4. **Flexible Format Support**: Supports IAM policies in JSON, but also IAM Policies defined in CloudFormation templates (`.json, .yaml`), and Terraform files (`.tf`). 5. **Up-to-Date**: Regularly updated to reflect the latest AWS IAM actions. 6. **Smart Hover Information**: When hovering over wildcard actions, displays all matching IAM actions, providing a comprehensive view of the permissions covered. ## Usage 1. Install the AWS IAM Actions Snippets extension in VS Code. -2. Open or create a new `.json` or `.yml` file for your IAM policy. +2. Open or create a new `.json`, `.yml`, or `.tf` file for your IAM policy. 3. Start typing an IAM action name (e.g., `s3:Get`) in the appropriate place in your policy. 4. The extension will provide auto-completion suggestions for matching IAM actions. 5. Select the desired action to insert it into your policy. diff --git a/package.json b/package.json index 9edbc59..ce46566 100644 --- a/package.json +++ b/package.json @@ -48,7 +48,8 @@ ], "activationEvents": [ "onLanguage:yaml", - "onLanguage:json" + "onLanguage:json", + "onLanguage:terraform" ], "main": "./out/extension.js", "scripts": { @@ -78,13 +79,24 @@ "id": "yaml", "aliases": [ "YAML", - "json" + "yaml" ], "extensions": [ ".yaml", ".yml", ".template" ] + }, + { + "id": "terraform", + "aliases": [ + "Terraform", + "terraform" + ], + "extensions": [ + ".tf", + ".tfvars" + ] } ] } diff --git a/src/extension.ts b/src/extension.ts index 66e0200..e655e25 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -109,7 +109,7 @@ export function activate(context: vscode.ExtensionContext) { // Register completion provider disposable.push( vscode.languages.registerCompletionItemProvider( - ['json', 'yaml', 'yml'], + ['json', 'yaml', 'yml', 'terraform'], { async provideCompletionItems(document: vscode.TextDocument, position: vscode.Position) { if (await isBelowActionKey(document, position)) { @@ -123,7 +123,7 @@ export function activate(context: vscode.ExtensionContext) { item.detail = `IAM Action: ${action.split(':')[1]} (${actionData.access_level})`; item.documentation = new vscode.MarkdownString(`${actionData.description}\n\n${actionData.url}`); - if (document.languageId === 'json') { + if (document.languageId === 'json' || document.languageId === 'terraform') { const lineText = document.lineAt(position.line).text; const currentLineStripped = lineText.trim(); const nextLineText = @@ -177,7 +177,7 @@ export function activate(context: vscode.ExtensionContext) { // Register hover provider disposable.push( - vscode.languages.registerHoverProvider(['yaml', 'yml', 'json'], { + vscode.languages.registerHoverProvider(['yaml', 'yml', 'json', 'terraform'], { async provideHover(document: vscode.TextDocument, position: vscode.Position) { const actionRegex = /[a-zA-Z0-9]+:[a-zA-Z0-9*]+/; const range = document.getWordRangeAtPosition(position, actionRegex); @@ -260,12 +260,18 @@ async function isBelowActionKey(document: vscode.TextDocument, position: vscode. const startLine = Math.max(0, position.line - maxLinesUp); const text = document.getText(new vscode.Range(startLine, 0, position.line, position.character)); - if (document.languageId === 'json') { + if (document.languageId === 'json' || document.languageId === 'terraform') { const lines = text.split('\n').reverse(); for (const line of lines) { const trimmedLine = line.trim().toLowerCase(); - if (trimmedLine.includes('"action"') && trimmedLine.includes('[')) { - return true; + if (document.languageId === 'json') { + if (trimmedLine.includes('"action"') && trimmedLine.includes('[')) { + return true; + } + } else if (document.languageId === 'terraform') { + if (trimmedLine.includes('action') && trimmedLine.includes('=') && trimmedLine.includes('[')) { + return true; + } } if (trimmedLine.startsWith('}') || trimmedLine.startsWith(']')) { break;