| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
"""Orchestration v1 resource type implementations""" |
| 15 |
|
| 16 |
import logging |
| 17 |
|
| 18 |
from osc_lib.command import command |
| 19 |
from osc_lib import exceptions as exc |
| 20 |
from osc_lib.i18n import _ |
| 21 |
|
| 22 |
from heatclient.common import format_utils |
| 23 |
from heatclient.common import utils as heat_utils |
| 24 |
from heatclient import exc as heat_exc |
| 25 |
|
| 26 |
|
| 27 |
class ResourceTypeShow(format_utils.YamlFormat): |
| 28 |
"""Show details and optionally generate a template for a resource type.""" |
| 29 |
|
| 30 |
log = logging.getLogger(__name__ + ".ResourceTypeShow") |
| 31 |
|
| 32 |
def get_parser(self, prog_name): |
| 33 |
parser = super().get_parser(prog_name) |
| 34 |
parser.add_argument( |
| 35 |
'resource_type', |
| 36 |
metavar='<resource-type>', |
| 37 |
help=_('Resource type to show details for'), |
| 38 |
) |
| 39 |
parser.add_argument( |
| 40 |
'--template-type', |
| 41 |
metavar='<template-type>', |
| 42 |
help=_('Optional template type to generate, hot or cfn') |
| 43 |
) |
| 44 |
parser.add_argument( |
| 45 |
'--long', |
| 46 |
default=False, |
| 47 |
action='store_true', |
| 48 |
help=_('Show resource type with corresponding description.') |
| 49 |
) |
| 50 |
return parser |
| 51 |
|
| 52 |
def take_action(self, parsed_args): |
| 53 |
self.log.debug("take_action(%s)", parsed_args) |
| 54 |
|
| 55 |
if parsed_args.template_type is not None and parsed_args.long: |
| 56 |
msg = _('Cannot use --template-type and --long in one time.') |
| 57 |
raise exc.CommandError(msg) |
| 58 |
|
| 59 |
heat_client = self.app.client_manager.orchestration |
| 60 |
return _show_resourcetype(heat_client, parsed_args) |
| 61 |
|
| 62 |
|
| 63 |
def _show_resourcetype(heat_client, parsed_args): |
| 64 |
try: |
| 65 |
if parsed_args.template_type: |
| 66 |
template_type = parsed_args.template_type.lower() |
| 67 |
if template_type not in ('hot', 'cfn'): |
| 68 |
raise exc.CommandError( |
| 69 |
_('Template type invalid: %s') % parsed_args.template_type) |
| 70 |
|
| 71 |
fields = {'resource_type': parsed_args.resource_type, |
| 72 |
'template_type': template_type} |
| 73 |
data = heat_client.resource_types.generate_template(**fields) |
| 74 |
else: |
| 75 |
data = heat_client.resource_types.get(parsed_args.resource_type, |
| 76 |
parsed_args.long) |
| 77 |
except heat_exc.HTTPNotFound: |
| 78 |
raise exc.CommandError( |
| 79 |
_('Resource type not found: %s') % parsed_args.resource_type) |
| 80 |
|
| 81 |
rows = list(data.values()) |
| 82 |
columns = list(data.keys()) |
| 83 |
return columns, rows |
| 84 |
|
| 85 |
|
| 86 |
class ResourceTypeList(command.Lister): |
| 87 |
"""List resource types.""" |
| 88 |
|
| 89 |
log = logging.getLogger(__name__ + '.ResourceTypeList') |
| 90 |
|
| 91 |
def get_parser(self, prog_name): |
| 92 |
parser = super().get_parser(prog_name) |
| 93 |
parser.add_argument( |
| 94 |
'--filter', |
| 95 |
dest='filter', |
| 96 |
metavar='<key=value>', |
| 97 |
help=_('Filter parameters to apply on returned resource types. ' |
| 98 |
'This can be specified multiple times. It can be any of ' |
| 99 |
'name, version or support_status'), |
| 100 |
action='append' |
| 101 |
) |
| 102 |
parser.add_argument( |
| 103 |
'--long', |
| 104 |
default=False, |
| 105 |
action='store_true', |
| 106 |
help=_('Show resource types with corresponding description of ' |
| 107 |
'each resource type.') |
| 108 |
) |
| 109 |
return parser |
| 110 |
|
| 111 |
def take_action(self, parsed_args): |
| 112 |
self.log.debug("take_action(%s)", parsed_args) |
| 113 |
|
| 114 |
heat_client = self.app.client_manager.orchestration |
| 115 |
return _list_resourcetypes(heat_client, parsed_args) |
| 116 |
|
| 117 |
|
| 118 |
def _list_resourcetypes(heat_client, parsed_args): |
| 119 |
resource_types = heat_client.resource_types.list( |
| 120 |
filters=heat_utils.format_parameters(parsed_args.filter), |
| 121 |
with_description=parsed_args.long |
| 122 |
) |
| 123 |
if parsed_args.long: |
| 124 |
columns = ['Resource Type', 'Description'] |
| 125 |
rows = sorted([r.resource_type, r.description] for r in resource_types) |
| 126 |
else: |
| 127 |
columns = ['Resource Type'] |
| 128 |
rows = sorted([r.resource_type] for r in resource_types) |
| 129 |
return columns, rows |