From 09064e5b64a6e10be3552e8d96c326883d99ca4c Mon Sep 17 00:00:00 2001 From: Tycho Bokdam Date: Fri, 10 May 2024 13:17:31 +0200 Subject: [PATCH] feat(pulumi): Added `import` executor --- packages/pulumi/executors.json | 10 +++++ .../pulumi/src/executors/import/compat.ts | 5 +++ .../src/executors/import/import.impl.ts | 40 +++++++++++++++++++ .../pulumi/src/executors/import/schema.json | 39 ++++++++++++++++++ .../pulumi/src/generators/init/init.impl.ts | 4 ++ 5 files changed, 98 insertions(+) create mode 100644 packages/pulumi/src/executors/import/compat.ts create mode 100644 packages/pulumi/src/executors/import/import.impl.ts create mode 100644 packages/pulumi/src/executors/import/schema.json diff --git a/packages/pulumi/executors.json b/packages/pulumi/executors.json index 002a1d68..221e9983 100644 --- a/packages/pulumi/executors.json +++ b/packages/pulumi/executors.json @@ -14,6 +14,11 @@ "implementation": "./src/executors/refresh/refresh.impl", "schema": "./src/executors/refresh/schema.json", "description": "refresh executor" + }, + "import": { + "implementation": "./src/executors/import/import.impl", + "schema": "./src/executors/import/schema.json", + "description": "import executor" } }, "builders": { @@ -31,6 +36,11 @@ "implementation": "./src/executors/refresh/refresh.impl", "schema": "./src/executors/refresh/schema.json", "description": "refresh executor" + }, + "import": { + "implementation": "./src/executors/import/import.impl", + "schema": "./src/executors/import/schema.json", + "description": "import executor" } } } diff --git a/packages/pulumi/src/executors/import/compat.ts b/packages/pulumi/src/executors/import/compat.ts new file mode 100644 index 00000000..7136588c --- /dev/null +++ b/packages/pulumi/src/executors/import/compat.ts @@ -0,0 +1,5 @@ +import { convertNxExecutor } from '@nx/devkit' + +import importExecutor from './import.impl' + +export default convertNxExecutor(importExecutor) diff --git a/packages/pulumi/src/executors/import/import.impl.ts b/packages/pulumi/src/executors/import/import.impl.ts new file mode 100644 index 00000000..d3bc47f2 --- /dev/null +++ b/packages/pulumi/src/executors/import/import.impl.ts @@ -0,0 +1,40 @@ +import { ExecutorContext, workspaceRoot } from '@nx/devkit' +import { buildCommand } from '@nx-extend/core' +import { execSync } from 'child_process' +import { join } from 'path' +import { which } from 'shelljs' + +export interface PreviewOptions { + stack?: string + root?: string + target: string + name: string + id: string + parent?: string +} + +export default async function creatExecutor( + options: PreviewOptions, + context: ExecutorContext +): Promise<{ success: boolean }> { + if (!which('pulumi')) { + throw new Error('pulumi is not installed!') + } + + const { sourceRoot } = context.workspace.projects[context.projectName] + + execSync(buildCommand([ + 'PULUMI_EXPERIMENTAL=true', + 'pulumi import', + options.target, + options.name, + options.id, + options.parent && `--parent 'parent=${options.parent}'`, + options.stack && `--stack=${options.stack}` + ]), { + cwd: join(workspaceRoot, options.root ?? sourceRoot), + stdio: 'inherit' + }) + + return { success: true } +} diff --git a/packages/pulumi/src/executors/import/schema.json b/packages/pulumi/src/executors/import/schema.json new file mode 100644 index 00000000..37226be2 --- /dev/null +++ b/packages/pulumi/src/executors/import/schema.json @@ -0,0 +1,39 @@ +{ + "version": 2, + "outputCapture": "direct-nodejs", + "$schema": "http://json-schema.org/schema", + "type": "object", + "title": "Preview executor", + "description": "Preview", + "properties": { + "stack": { + "type": "string", + "description": "The target stack to use, if specified." + }, + "root": { + "type": "string", + "description": "The working directory to run Pulumi commands from, if specified." + }, + "target": { + "type": "string", + "$default": { + "$source": "argv", + "index": 0 + } + }, + "name": { + "type": "string", + "$default": { + "$source": "argv", + "index": 1 + } + }, + "id": { + "type": "string", + "$default": { + "$source": "argv", + "index": 2 + } + } + } +} diff --git a/packages/pulumi/src/generators/init/init.impl.ts b/packages/pulumi/src/generators/init/init.impl.ts index fbd0d785..4cf6c49f 100644 --- a/packages/pulumi/src/generators/init/init.impl.ts +++ b/packages/pulumi/src/generators/init/init.impl.ts @@ -138,6 +138,10 @@ export default async function (tree: Tree, rawOptions: InitOptions) { refresh: { executor: '@nx-extend/pulumi:refresh', options: {} + }, + import: { + executor: '@nx-extend/pulumi:import', + options: {} } }, tags: options.parsedTags