From 24c0346864d0639aa4fe0390fbb96e26330e801e Mon Sep 17 00:00:00 2001 From: Stefan Reimer Date: Mon, 2 Dec 2024 12:48:30 +0000 Subject: [PATCH] feat: add import task to pulumi stacks --- cloudbender/cli.py | 16 ++++++++++++++++ cloudbender/stack.py | 13 +++++++++++++ 2 files changed, 29 insertions(+) diff --git a/cloudbender/cli.py b/cloudbender/cli.py index 68401ec..10e4f41 100644 --- a/cloudbender/cli.py +++ b/cloudbender/cli.py @@ -212,6 +212,21 @@ def execute(cb, stack_name, function, args): ) +@click.command('import') +@click.argument("stack_name") +@click.argument("pulumi_state_file") +@click.pass_obj +def _import(cb, stack_name, pulumi_state_file): + """Imports a Pulumi state file as stack""" + stacks = _find_stacks(cb, [stack_name]) + + for s in stacks: + if s.mode == "pulumi": + s._import(pulumi_state_file) + else: + logger.info("{} uses Cloudformation, export skipped.".format(s.stackname)) + + @click.command() @click.argument("stack_name") @click.option( @@ -482,6 +497,7 @@ cli.add_command(refresh) cli.add_command(preview) cli.add_command(set_config) cli.add_command(get_config) +cli.add_command(_import) cli.add_command(export) cli.add_command(assimilate) cli.add_command(execute) diff --git a/cloudbender/stack.py b/cloudbender/stack.py index 4e65b0b..55467aa 100644 --- a/cloudbender/stack.py +++ b/cloudbender/stack.py @@ -1024,6 +1024,19 @@ class Stack(object): return + @pulumi_ws + def _import(self, pulumi_state_file): + """Imports a Pulumi stack""" + + pulumi_stack = self._get_pulumi_stack() + + with open(pulumi_state_file, "r") as file: + state = json.loads(file.read()) + deployment = pulumi.automation.Deployment(version=3, deployment=state) + pulumi_stack.import_stack(deployment) + + return + @pulumi_ws def set_config(self, key, value, secret): """Set a config or secret"""