Skip to content

Commit

Permalink
feat: add support for terraform
Browse files Browse the repository at this point in the history
  • Loading branch information
dannysteenman committed Nov 14, 2024
1 parent 970767c commit 87a2bce
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 10 deletions.
22 changes: 22 additions & 0 deletions .vscode-test/tf-template.tf
Original file line number Diff line number Diff line change
@@ -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 = "*"
}
]
})
}
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
16 changes: 14 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@
],
"activationEvents": [
"onLanguage:yaml",
"onLanguage:json"
"onLanguage:json",
"onLanguage:terraform"
],
"main": "./out/extension.js",
"scripts": {
Expand Down Expand Up @@ -78,13 +79,24 @@
"id": "yaml",
"aliases": [
"YAML",
"json"
"yaml"
],
"extensions": [
".yaml",
".yml",
".template"
]
},
{
"id": "terraform",
"aliases": [
"Terraform",
"terraform"
],
"extensions": [
".tf",
".tfvars"
]
}
]
}
Expand Down
18 changes: 12 additions & 6 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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 =
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 87a2bce

Please sign in to comment.