|
17 | 17 | """ |
18 | 18 | import argparse |
19 | 19 | import json |
| 20 | +import sys |
20 | 21 |
|
21 | 22 | from cliff import command |
22 | 23 | from cliff import lister |
23 | 24 | from cliff import show |
24 | 25 |
|
25 | 26 | from coriolisclient.cli import formatter |
| 27 | +from coriolisclient.cli import utils as cli_utils |
26 | 28 | from coriolisclient import exceptions |
27 | 29 |
|
28 | 30 |
|
@@ -249,6 +251,36 @@ def take_action(self, args): |
249 | 251 | return EndpointFormatter().list_objects(obj_list) |
250 | 252 |
|
251 | 253 |
|
| 254 | +class ExportEndpointInventory(command.Command): |
| 255 | + """Export the VM inventory of an endpoint in CSV format""" |
| 256 | + |
| 257 | + def get_parser(self, prog_name): |
| 258 | + parser = super(ExportEndpointInventory, self).get_parser(prog_name) |
| 259 | + parser.add_argument('id', help='The endpoint\'s id or name') |
| 260 | + cli_utils.add_args_for_json_option_to_parser(parser, 'environment') |
| 261 | + parser.add_argument( |
| 262 | + '--output-file', |
| 263 | + help='Path to write the CSV to. Defaults to stdout.') |
| 264 | + return parser |
| 265 | + |
| 266 | + def take_action(self, args): |
| 267 | + endpoints = self.app.client_manager.coriolis.endpoints |
| 268 | + endpoint_id = endpoints.get_endpoint_id_for_name(args.id) |
| 269 | + source_environment = cli_utils.get_option_value_from_args( |
| 270 | + args, 'environment', error_on_no_value=False) |
| 271 | + |
| 272 | + csv_content = endpoints.get_inventory_csv( |
| 273 | + endpoint_id, source_environment=source_environment) |
| 274 | + |
| 275 | + if args.output_file: |
| 276 | + with open(args.output_file, 'w') as fout: |
| 277 | + fout.write(csv_content) |
| 278 | + self.app.stdout.write( |
| 279 | + 'Inventory written to %s\n' % args.output_file) |
| 280 | + else: |
| 281 | + sys.stdout.write(csv_content) |
| 282 | + |
| 283 | + |
252 | 284 | class EndpointValidateConnection(command.Command): |
253 | 285 | """validates an edpoint's connection""" |
254 | 286 |
|
|
0 commit comments