Allow YAML as a CLI import format

This changset allows the import of YAML formatted resources. The CLI
user can indicate which format to use with the `-f, --format` option.
The CLI help text has been amended to reflect the new feature.

The AWX CLI `export` subcommand offers the option of formatting the output
as YAML or JSON, so it makes sense that the `import` subcommand reflects
this.

A simple test is also provided. In order to ease the task of testing
commands that import resources by reading the stdin, the CLI has been
extended to allow specifying an alternative file descriptor for stdin,
similarly to stdout and stderr.
This commit is contained in:
Rigel Di Scala
2020-07-29 15:27:45 +02:00
parent 15c4ab3a9b
commit 579604d2c6
4 changed files with 39 additions and 5 deletions

View File

@@ -1,8 +1,9 @@
import yaml
import json
import os
import sys
from awxkit import api, config
from awxkit.exceptions import ImportExportError
from awxkit.utils import to_str
from awxkit.api.pages import Page
from awxkit.api.pages.api import EXPORTABLE_RESOURCES
@@ -135,7 +136,13 @@ class Import(CustomCommand):
parser.print_help()
raise SystemExit()
data = json.load(sys.stdin)
format = getattr(client.args, 'conf.format')
if format == 'json':
data = json.load(client.stdin)
elif format == 'yaml':
data = yaml.safe_load(client.stdin)
else:
raise ImportExportError("Unsupported format for Import: " + format)
client.authenticate()
client.v2.import_assets(data)