From 9f7fecf8da117bf91e819a6a330711ad3ffcf995 Mon Sep 17 00:00:00 2001 From: Jeff Bradberry Date: Wed, 29 Jan 2020 14:31:38 -0500 Subject: [PATCH] Add basic import command --- awxkit/awxkit/cli/resource.py | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/awxkit/awxkit/cli/resource.py b/awxkit/awxkit/cli/resource.py index c899126f96..47b8c83a39 100644 --- a/awxkit/awxkit/cli/resource.py +++ b/awxkit/awxkit/cli/resource.py @@ -1,4 +1,6 @@ +import json import os +import sys from awxkit import api, config from awxkit.utils import to_str @@ -123,9 +125,34 @@ class Config(CustomCommand): } +class Import(CustomCommand): + name = 'import' + help_text = 'import resources into Tower' + + def create_resource(self, client, resource, asset): + api_resource = getattr(client.v2, resource) + if resource == 'users' and 'password' not in asset: + asset['password'] = 'password' + api_resource.post(asset) + + def handle(self, client, parser): + if client.help: + parser.print_help() + raise SystemExit() + + data = json.load(sys.stdin) + client.authenticate() + + for resource, assets in data.items(): + for asset in assets: + self.create_resource(client, resource, asset) + + return {} + + class Export(CustomCommand): name = 'export' - help_text = 'export resources from Tower as yaml' + help_text = 'export resources from Tower' def extend_parser(self, parser): resources = parser.add_argument_group('resources')