|
| 1 | +# (C) Copyright [2020] Hewlett Packard Enterprise Development LP |
| 2 | +# |
| 3 | +# Permission is hereby granted, free of charge, to any person obtaining a |
| 4 | +# copy of this software and associated documentation files (the "Software"), |
| 5 | +# to deal in the Software without restriction, including without limitation |
| 6 | +# the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 7 | +# and/or sell copies of the Software, and to permit persons to whom the |
| 8 | +# Software is furnished to do so, subject to the following conditions: |
| 9 | +# |
| 10 | +# The above copyright notice and this permission notice shall be included |
| 11 | +# in all copies or substantial portions of the Software. |
| 12 | +# |
| 13 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 16 | +# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 17 | +# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 18 | +# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 19 | +# OTHER DEALINGS IN THE SOFTWARE. |
| 20 | + |
| 21 | +"""HPE Container Platform CLI.""" |
| 22 | + |
| 23 | +from __future__ import print_function |
| 24 | + |
| 25 | +import json |
| 26 | +import sys |
| 27 | +import yaml |
| 28 | + |
| 29 | +from hpecp.cli import base |
| 30 | + |
| 31 | + |
| 32 | +class ConfigProxy(object): |
| 33 | + """Proxy object to :py:attr:`<hpecp.client.config>`.""" |
| 34 | + |
| 35 | + def __dir__(self): |
| 36 | + """Return the CLI method names.""" |
| 37 | + return [ |
| 38 | + "get", |
| 39 | + ] |
| 40 | + |
| 41 | + @base.intercept_exception |
| 42 | + def get(self, output="yaml", query=None): |
| 43 | + """Get the system config. |
| 44 | +
|
| 45 | + :param output: how to display the output '[yaml|json|json-pp]' |
| 46 | + """ |
| 47 | + if output not in ["yaml", "json", "json-pp", "text"]: |
| 48 | + print( |
| 49 | + "'output' parameter must be 'yaml', 'json', 'json-pp' or 'text'.", |
| 50 | + file=sys.stderr, |
| 51 | + ) |
| 52 | + sys.exit(1) |
| 53 | + |
| 54 | + if output == "yaml" and query is not None: |
| 55 | + print( |
| 56 | + "output=yaml is not supported when providing the query parameter.", |
| 57 | + file=sys.stderr, |
| 58 | + ) |
| 59 | + sys.exit(1) |
| 60 | + |
| 61 | + response = base.get_client().config.get() |
| 62 | + |
| 63 | + if output == "yaml": |
| 64 | + print( |
| 65 | + yaml.dump( |
| 66 | + yaml.load( |
| 67 | + json.dumps(response), |
| 68 | + Loader=yaml.FullLoader, |
| 69 | + ) |
| 70 | + ) |
| 71 | + ) |
| 72 | + else: |
| 73 | + if query is None: |
| 74 | + data = response |
| 75 | + else: |
| 76 | + data = jmespath.search(str(query), response) |
| 77 | + |
| 78 | + if output == "json-pp": |
| 79 | + print(json.dumps(data, indent=4, sort_keys=True)) |
| 80 | + elif output == "text": |
| 81 | + print(TextOutput.dump(data)) |
| 82 | + else: |
| 83 | + print(json.dumps(data)) |
0 commit comments