| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
"""Orchestration v1 Stack action 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 |
from osc_lib import utils |
| 22 |
from oslo_serialization import jsonutils |
| 23 |
from urllib import request |
| 24 |
|
| 25 |
from heatclient.common import format_utils |
| 26 |
from heatclient.common import utils as heat_utils |
| 27 |
from heatclient import exc as heat_exc |
| 28 |
|
| 29 |
|
| 30 |
class ResourceShow(command.ShowOne): |
| 31 |
"""Display stack resource.""" |
| 32 |
|
| 33 |
log = logging.getLogger(__name__ + '.ResourceShowStack') |
| 34 |
|
| 35 |
def get_parser(self, prog_name): |
| 36 |
parser = super().get_parser(prog_name) |
| 37 |
parser.add_argument( |
| 38 |
'stack', |
| 39 |
metavar='<stack>', |
| 40 |
help=_('Name or ID of stack to query') |
| 41 |
) |
| 42 |
parser.add_argument( |
| 43 |
'resource', |
| 44 |
metavar='<resource>', |
| 45 |
help=_('Name of resource') |
| 46 |
) |
| 47 |
parser.add_argument( |
| 48 |
'--with-attr', |
| 49 |
metavar='<attribute>', |
| 50 |
action='append', |
| 51 |
help=_('Attribute to show, can be specified multiple times') |
| 52 |
) |
| 53 |
return parser |
| 54 |
|
| 55 |
def take_action(self, parsed_args): |
| 56 |
self.log.debug('take_action(%s)', parsed_args) |
| 57 |
|
| 58 |
client = self.app.client_manager.orchestration |
| 59 |
|
| 60 |
try: |
| 61 |
resource = client.resources.get(parsed_args.stack, |
| 62 |
parsed_args.resource, |
| 63 |
with_attr=parsed_args.with_attr) |
| 64 |
except heat_exc.HTTPNotFound: |
| 65 |
msg = (_('Stack or resource not found: %(stack)s %(resource)s') % |
| 66 |
{'stack': parsed_args.stack, |
| 67 |
'resource': parsed_args.resource}) |
| 68 |
raise exc.CommandError(msg) |
| 69 |
|
| 70 |
return self.dict2columns(resource.to_dict()) |
| 71 |
|
| 72 |
|
| 73 |
class ResourceList(command.Lister): |
| 74 |
"""List stack resources.""" |
| 75 |
|
| 76 |
log = logging.getLogger(__name__ + '.ResourceListStack') |
| 77 |
|
| 78 |
@property |
| 79 |
def formatter_namespace(self): |
| 80 |
return 'heatclient.resource.formatter.list' |
| 81 |
|
| 82 |
def get_parser(self, prog_name): |
| 83 |
parser = super().get_parser(prog_name) |
| 84 |
parser.add_argument( |
| 85 |
'stack', |
| 86 |
metavar='<stack>', |
| 87 |
help=_('Name or ID of stack to query') |
| 88 |
) |
| 89 |
parser.add_argument( |
| 90 |
'--long', |
| 91 |
action='store_true', |
| 92 |
help=_('Enable detailed information presented for each resource ' |
| 93 |
'in resource list') |
| 94 |
) |
| 95 |
parser.add_argument( |
| 96 |
'-n', '--nested-depth', |
| 97 |
metavar='<nested-depth>', |
| 98 |
type=int, |
| 99 |
help=_('Depth of nested stacks from which to display resources') |
| 100 |
) |
| 101 |
|
| 102 |
parser.add_argument( |
| 103 |
'--filter', |
| 104 |
metavar='<key=value>', |
| 105 |
action='append', |
| 106 |
help=_('Filter parameters to apply on returned resources based on ' |
| 107 |
'their name, status, type, action, id and ' |
| 108 |
'physical_resource_id') |
| 109 |
) |
| 110 |
|
| 111 |
return parser |
| 112 |
|
| 113 |
def take_action(self, parsed_args): |
| 114 |
self.log.debug('take_action(%s)', parsed_args) |
| 115 |
|
| 116 |
client = self.app.client_manager.orchestration |
| 117 |
|
| 118 |
fields = { |
| 119 |
'nested_depth': parsed_args.nested_depth, |
| 120 |
'with_detail': parsed_args.long, |
| 121 |
'filters': heat_utils.format_parameters(parsed_args.filter), |
| 122 |
} |
| 123 |
|
| 124 |
try: |
| 125 |
resources = client.resources.list(parsed_args.stack, **fields) |
| 126 |
except heat_exc.HTTPNotFound: |
| 127 |
msg = _('Stack not found: %s') % parsed_args.stack |
| 128 |
raise exc.CommandError(msg) |
| 129 |
|
| 130 |
if parsed_args.formatter == 'dot': |
| 131 |
return [], resources |
| 132 |
|
| 133 |
columns = ['physical_resource_id', 'resource_type', 'resource_status', |
| 134 |
'updated_time'] |
| 135 |
|
| 136 |
if len(resources) >= 1 and not hasattr(resources[0], 'resource_name'): |
| 137 |
columns.insert(0, 'logical_resource_id') |
| 138 |
else: |
| 139 |
columns.insert(0, 'resource_name') |
| 140 |
|
| 141 |
if parsed_args.nested_depth or parsed_args.long: |
| 142 |
columns.append('stack_name') |
| 143 |
|
| 144 |
return ( |
| 145 |
columns, |
| 146 |
(utils.get_item_properties(r, columns) for r in resources) |
| 147 |
) |
| 148 |
|
| 149 |
|
| 150 |
class ResourceMetadata(format_utils.JsonFormat): |
| 151 |
"""Show resource metadata""" |
| 152 |
|
| 153 |
log = logging.getLogger(__name__ + ".ResourceMetadata") |
| 154 |
|
| 155 |
def get_parser(self, prog_name): |
| 156 |
parser = super().get_parser(prog_name) |
| 157 |
parser.add_argument( |
| 158 |
'stack', |
| 159 |
metavar='<stack>', |
| 160 |
help=_('Stack to display (name or ID)'), |
| 161 |
) |
| 162 |
parser.add_argument( |
| 163 |
'resource', |
| 164 |
metavar='<resource>', |
| 165 |
help=_('Name of the resource to show the metadata for')) |
| 166 |
return parser |
| 167 |
|
| 168 |
def take_action(self, parsed_args): |
| 169 |
self.log.debug("take_action(%s)", parsed_args) |
| 170 |
|
| 171 |
heat_client = self.app.client_manager.orchestration |
| 172 |
return _resource_metadata(heat_client, parsed_args) |
| 173 |
|
| 174 |
|
| 175 |
def _resource_metadata(heat_client, args): |
| 176 |
fields = {'stack_id': args.stack, |
| 177 |
'resource_name': args.resource} |
| 178 |
try: |
| 179 |
metadata = heat_client.resources.metadata(**fields) |
| 180 |
except heat_exc.HTTPNotFound: |
| 181 |
raise exc.CommandError(_('Stack %(stack)s or resource %(resource)s ' |
| 182 |
'not found.') % |
| 183 |
{'stack': args.stack, |
| 184 |
'resource': args.resource}) |
| 185 |
|
| 186 |
data = list(metadata.values()) |
| 187 |
columns = list(metadata.keys()) |
| 188 |
return columns, data |
| 189 |
|
| 190 |
|
| 191 |
class ResourceSignal(command.Command): |
| 192 |
"""Signal a resource with optional data.""" |
| 193 |
|
| 194 |
log = logging.getLogger(__name__ + ".ResourceSignal") |
| 195 |
|
| 196 |
def get_parser(self, prog_name): |
| 197 |
parser = super().get_parser(prog_name) |
| 198 |
parser.add_argument( |
| 199 |
'stack', |
| 200 |
metavar='<stack>', |
| 201 |
help=_('Name or ID of stack the resource belongs to'), |
| 202 |
) |
| 203 |
parser.add_argument( |
| 204 |
'resource', |
| 205 |
metavar='<resource>', |
| 206 |
help=_('Name of the resoure to signal'), |
| 207 |
) |
| 208 |
parser.add_argument( |
| 209 |
'--data', |
| 210 |
metavar='<data>', |
| 211 |
help=_('JSON Data to send to the signal handler') |
| 212 |
) |
| 213 |
parser.add_argument( |
| 214 |
'--data-file', |
| 215 |
metavar='<data-file>', |
| 216 |
help=_('File containing JSON data to send to the signal handler') |
| 217 |
) |
| 218 |
return parser |
| 219 |
|
| 220 |
def take_action(self, parsed_args): |
| 221 |
self.log.debug("take_action(%s)", parsed_args) |
| 222 |
|
| 223 |
heat_client = self.app.client_manager.orchestration |
| 224 |
return _resource_signal(heat_client, parsed_args) |
| 225 |
|
| 226 |
|
| 227 |
def _resource_signal(heat_client, args): |
| 228 |
fields = {'stack_id': args.stack, |
| 229 |
'resource_name': args.resource} |
| 230 |
data = args.data |
| 231 |
data_file = args.data_file |
| 232 |
if data and data_file: |
| 233 |
raise exc.CommandError(_('Should only specify one of data or ' |
| 234 |
'data-file')) |
| 235 |
|
| 236 |
if data_file: |
| 237 |
data_url = heat_utils.normalise_file_path_to_url(data_file) |
| 238 |
data = request.urlopen(data_url).read() |
| 239 |
|
| 240 |
if data: |
| 241 |
try: |
| 242 |
data = jsonutils.loads(data) |
| 243 |
except ValueError as ex: |
| 244 |
raise exc.CommandError(_('Data should be in JSON format: %s') % ex) |
| 245 |
if not isinstance(data, dict): |
| 246 |
raise exc.CommandError(_('Data should be a JSON dict')) |
| 247 |
|
| 248 |
fields['data'] = data |
| 249 |
try: |
| 250 |
heat_client.resources.signal(**fields) |
| 251 |
except heat_exc.HTTPNotFound: |
| 252 |
raise exc.CommandError(_('Stack %(stack)s or resource %(resource)s ' |
| 253 |
'not found.') % |
| 254 |
{'stack': args.stack, |
| 255 |
'resource': args.resource}) |
| 256 |
|
| 257 |
|
| 258 |
class ResourceMarkUnhealthy(command.Command): |
| 259 |
"""Set resource's health.""" |
| 260 |
|
| 261 |
log = logging.getLogger(__name__ + ".ResourceMarkUnhealthy") |
| 262 |
|
| 263 |
def get_parser(self, prog_name): |
| 264 |
parser = super().get_parser(prog_name) |
| 265 |
parser.add_argument( |
| 266 |
'stack', |
| 267 |
metavar='<stack>', |
| 268 |
help=_('Name or ID of stack the resource belongs to') |
| 269 |
) |
| 270 |
parser.add_argument( |
| 271 |
'resource', |
| 272 |
metavar='<resource>', |
| 273 |
help=_('Name of the resource') |
| 274 |
) |
| 275 |
parser.add_argument( |
| 276 |
'reason', |
| 277 |
default="", |
| 278 |
nargs='?', |
| 279 |
help=_('Reason for state change') |
| 280 |
) |
| 281 |
parser.add_argument( |
| 282 |
'--reset', |
| 283 |
default=False, |
| 284 |
action="store_true", |
| 285 |
help=_('Set the resource as healthy') |
| 286 |
) |
| 287 |
return parser |
| 288 |
|
| 289 |
def take_action(self, parsed_args): |
| 290 |
self.log.debug("take_action(%s)", parsed_args) |
| 291 |
|
| 292 |
heat_client = self.app.client_manager.orchestration |
| 293 |
fields = {'stack_id': parsed_args.stack, |
| 294 |
'resource_name': parsed_args.resource, |
| 295 |
'mark_unhealthy': not parsed_args.reset, |
| 296 |
'resource_status_reason': parsed_args.reason} |
| 297 |
try: |
| 298 |
heat_client.resources.mark_unhealthy(**fields) |
| 299 |
except heat_exc.HTTPNotFound: |
| 300 |
raise exc.CommandError(_('Stack or resource not found: ' |
| 301 |
'%(id)s %(resource)s') % |
| 302 |
{'id': parsed_args.stack, |
| 303 |
'resource': parsed_args.resource}) |