| 1 |
# |
| 2 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 3 |
# not use this file except in compliance with the License. You may obtain |
| 4 |
# a copy of the License at |
| 5 |
# |
| 6 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
# |
| 8 |
# Unless required by applicable law or agreed to in writing, software |
| 9 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 10 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 11 |
# License for the specific language governing permissions and limitations |
| 12 |
# under the License. |
| 13 |
|
| 14 |
import argparse |
| 15 |
|
| 16 |
from cliff import command |
| 17 |
from cliff import lister |
| 18 |
from cliff import show |
| 19 |
from oslo_serialization import jsonutils |
| 20 |
from oslo_utils import strutils |
| 21 |
from oslo_utils import uuidutils |
| 22 |
|
| 23 |
from aodhclient import exceptions |
| 24 |
from aodhclient.i18n import _ |
| 25 |
from aodhclient import utils |
| 26 |
|
| 27 |
ALARM_TYPES = ['prometheus', 'event', 'composite', 'threshold', |
| 28 |
'gnocchi_resources_threshold', |
| 29 |
'gnocchi_aggregation_by_metrics_threshold', |
| 30 |
'gnocchi_aggregation_by_resources_threshold', |
| 31 |
'loadbalancer_member_health'] |
| 32 |
ALARM_STATES = ['ok', 'alarm', 'insufficient data'] |
| 33 |
ALARM_SEVERITY = ['low', 'moderate', 'critical'] |
| 34 |
ALARM_OPERATORS = ['lt', 'le', 'eq', 'ne', 'ge', 'gt'] |
| 35 |
ALARM_OP_MAP = dict(zip(ALARM_OPERATORS, ('<', '<=', '=', '!=', '>=', '>'))) |
| 36 |
STATISTICS = ['max', 'min', 'avg', 'sum', 'count'] |
| 37 |
ALARM_LIST_COLS = ['alarm_id', 'type', 'name', 'state', 'severity', 'enabled'] |
| 38 |
|
| 39 |
|
| 40 |
class CliAlarmList(lister.Lister): |
| 41 |
"""List alarms""" |
| 42 |
|
| 43 |
@staticmethod |
| 44 |
def split_filter_param(param): |
| 45 |
key, eq_op, value = param.partition('=') |
| 46 |
if not eq_op: |
| 47 |
msg = 'Malformed parameter(%s). Use the key=value format.' % param |
| 48 |
raise ValueError(msg) |
| 49 |
return key, value |
| 50 |
|
| 51 |
def get_parser(self, prog_name): |
| 52 |
parser = super().get_parser(prog_name) |
| 53 |
exclusive_group = parser.add_mutually_exclusive_group() |
| 54 |
exclusive_group.add_argument("--query", |
| 55 |
help="Rich query supported by aodh, " |
| 56 |
"e.g. project_id!=my-id " |
| 57 |
"user_id=foo or user_id=bar") |
| 58 |
exclusive_group.add_argument('--filter', dest='filter', |
| 59 |
metavar='<KEY1=VALUE1;KEY2=VALUE2...>', |
| 60 |
type=self.split_filter_param, |
| 61 |
action='append', |
| 62 |
help='Filter parameters to apply on' |
| 63 |
' returned alarms.') |
| 64 |
parser.add_argument("--limit", type=int, metavar="<LIMIT>", |
| 65 |
help="Number of resources to return " |
| 66 |
"(Default is server default)") |
| 67 |
parser.add_argument("--marker", metavar="<MARKER>", |
| 68 |
help="Last item of the previous listing. " |
| 69 |
"Return the next results after this value," |
| 70 |
"the supported marker is alarm_id.") |
| 71 |
parser.add_argument("--sort", action="append", |
| 72 |
metavar="<SORT_KEY:SORT_DIR>", |
| 73 |
help="Sort of resource attribute, " |
| 74 |
"e.g. name:asc") |
| 75 |
return parser |
| 76 |
|
| 77 |
def take_action(self, parsed_args): |
| 78 |
if parsed_args.query: |
| 79 |
if any([parsed_args.limit, parsed_args.sort, parsed_args.marker]): |
| 80 |
raise exceptions.CommandError( |
| 81 |
"Query and pagination options are mutually " |
| 82 |
"exclusive.") |
| 83 |
query = jsonutils.dumps( |
| 84 |
utils.search_query_builder(parsed_args.query)) |
| 85 |
alarms = utils.get_client(self).alarm.query(query=query) |
| 86 |
else: |
| 87 |
filters = dict(parsed_args.filter) if parsed_args.filter else None |
| 88 |
alarms = utils.get_client(self).alarm.list( |
| 89 |
filters=filters, sorts=parsed_args.sort, |
| 90 |
limit=parsed_args.limit, marker=parsed_args.marker) |
| 91 |
return utils.list2cols(ALARM_LIST_COLS, alarms) |
| 92 |
|
| 93 |
|
| 94 |
def _format_alarm(alarm): |
| 95 |
if alarm.get('composite_rule'): |
| 96 |
composite_rule = jsonutils.dumps(alarm['composite_rule'], indent=2) |
| 97 |
alarm['composite_rule'] = composite_rule |
| 98 |
return alarm |
| 99 |
for alarm_type in ALARM_TYPES: |
| 100 |
if alarm.get('%s_rule' % alarm_type): |
| 101 |
alarm.update(alarm.pop('%s_rule' % alarm_type)) |
| 102 |
if alarm["time_constraints"]: |
| 103 |
alarm["time_constraints"] = jsonutils.dumps(alarm["time_constraints"], |
| 104 |
sort_keys=True, |
| 105 |
indent=2) |
| 106 |
# only works for threshold and event alarm |
| 107 |
if isinstance(alarm.get('query'), list): |
| 108 |
query_rows = [] |
| 109 |
for q in alarm['query']: |
| 110 |
op = ALARM_OP_MAP.get(q['op'], q['op']) |
| 111 |
query_rows.append('{} {} {}'.format(q['field'], op, q['value'])) |
| 112 |
alarm['query'] = ' AND\n'.join(query_rows) |
| 113 |
return alarm |
| 114 |
|
| 115 |
|
| 116 |
def _find_alarm_by_name(client, name): |
| 117 |
# then try to get entity as name |
| 118 |
query = jsonutils.dumps({"=": {"name": name}}) |
| 119 |
alarms = client.alarm.query(query) |
| 120 |
if len(alarms) > 1: |
| 121 |
msg = (_("Multiple alarms matches found for '%s', " |
| 122 |
"use an ID to be more specific.") % name) |
| 123 |
raise exceptions.NoUniqueMatch(msg) |
| 124 |
elif not alarms: |
| 125 |
msg = (_("Alarm %s not found") % name) |
| 126 |
raise exceptions.NotFound(msg) |
| 127 |
else: |
| 128 |
return alarms[0] |
| 129 |
|
| 130 |
|
| 131 |
def _find_alarm_id_by_name(client, name): |
| 132 |
alarm = _find_alarm_by_name(client, name) |
| 133 |
return alarm['alarm_id'] |
| 134 |
|
| 135 |
|
| 136 |
def _check_name_and_id_coexist(parsed_args, action): |
| 137 |
if parsed_args.id and parsed_args.name: |
| 138 |
raise exceptions.CommandError( |
| 139 |
"You should provide only one of " |
| 140 |
"alarm ID and alarm name(--name) " |
| 141 |
"to %s an alarm." % action) |
| 142 |
|
| 143 |
|
| 144 |
def _check_name_and_id_exist(parsed_args, action): |
| 145 |
if not parsed_args.id and not parsed_args.name: |
| 146 |
msg = (_("You need to specify one of " |
| 147 |
"alarm ID and alarm name(--name) " |
| 148 |
"to %s an alarm.") % action) |
| 149 |
raise exceptions.CommandError(msg) |
| 150 |
|
| 151 |
|
| 152 |
def _check_name_and_id(parsed_args, action): |
| 153 |
_check_name_and_id_coexist(parsed_args, action) |
| 154 |
_check_name_and_id_exist(parsed_args, action) |
| 155 |
|
| 156 |
|
| 157 |
def _add_name_to_parser(parser, required=False): |
| 158 |
parser.add_argument('--name', metavar='<NAME>', |
| 159 |
required=required, |
| 160 |
help='Name of the alarm') |
| 161 |
return parser |
| 162 |
|
| 163 |
|
| 164 |
def _add_id_to_parser(parser): |
| 165 |
parser.add_argument("id", nargs='?', |
| 166 |
metavar='<ALARM ID or NAME>', |
| 167 |
help="ID or name of an alarm.") |
| 168 |
return parser |
| 169 |
|
| 170 |
|
| 171 |
class CliAlarmShow(show.ShowOne): |
| 172 |
"""Show an alarm""" |
| 173 |
|
| 174 |
def get_parser(self, prog_name): |
| 175 |
return _add_name_to_parser( |
| 176 |
_add_id_to_parser( |
| 177 |
super().get_parser(prog_name))) |
| 178 |
|
| 179 |
def take_action(self, parsed_args): |
| 180 |
_check_name_and_id(parsed_args, 'query') |
| 181 |
c = utils.get_client(self) |
| 182 |
if parsed_args.name: |
| 183 |
alarm = _find_alarm_by_name(c, parsed_args.name) |
| 184 |
else: |
| 185 |
if uuidutils.is_uuid_like(parsed_args.id): |
| 186 |
try: |
| 187 |
alarm = c.alarm.get(alarm_id=parsed_args.id) |
| 188 |
except exceptions.NotFound: |
| 189 |
# Maybe it's a name |
| 190 |
alarm = _find_alarm_by_name(c, parsed_args.id) |
| 191 |
else: |
| 192 |
alarm = _find_alarm_by_name(c, parsed_args.id) |
| 193 |
|
| 194 |
return self.dict2columns(_format_alarm(alarm)) |
| 195 |
|
| 196 |
|
| 197 |
class CliAlarmCreate(show.ShowOne): |
| 198 |
"""Create an alarm""" |
| 199 |
|
| 200 |
create = True |
| 201 |
|
| 202 |
def get_parser(self, prog_name): |
| 203 |
parser = _add_name_to_parser( |
| 204 |
super().get_parser(prog_name), |
| 205 |
required=self.create) |
| 206 |
|
| 207 |
parser.add_argument('-t', '--type', metavar='<TYPE>', |
| 208 |
required=self.create, |
| 209 |
choices=ALARM_TYPES, |
| 210 |
help='Type of alarm, should be one of: ' |
| 211 |
'%s.' % ', '.join(ALARM_TYPES)) |
| 212 |
parser.add_argument('--project-id', metavar='<PROJECT_ID>', |
| 213 |
help='Project to associate with alarm ' |
| 214 |
'(configurable by admin users only)') |
| 215 |
parser.add_argument('--user-id', metavar='<USER_ID>', |
| 216 |
help='User to associate with alarm ' |
| 217 |
'(configurable by admin users only)') |
| 218 |
parser.add_argument('--description', metavar='<DESCRIPTION>', |
| 219 |
help='Free text description of the alarm') |
| 220 |
parser.add_argument('--state', metavar='<STATE>', |
| 221 |
choices=ALARM_STATES, |
| 222 |
help='State of the alarm, one of: ' |
| 223 |
+ str(ALARM_STATES)) |
| 224 |
parser.add_argument('--severity', metavar='<SEVERITY>', |
| 225 |
choices=ALARM_SEVERITY, |
| 226 |
help='Severity of the alarm, one of: ' |
| 227 |
+ str(ALARM_SEVERITY)) |
| 228 |
parser.add_argument('--enabled', type=strutils.bool_from_string, |
| 229 |
metavar='{True|False}', |
| 230 |
help=('True if alarm evaluation is enabled')) |
| 231 |
parser.add_argument('--alarm-action', dest='alarm_actions', |
| 232 |
metavar='<Webhook URL>', action='append', |
| 233 |
help=('URL to invoke when state transitions to ' |
| 234 |
'alarm. May be used multiple times')) |
| 235 |
parser.add_argument('--ok-action', dest='ok_actions', |
| 236 |
metavar='<Webhook URL>', action='append', |
| 237 |
help=('URL to invoke when state transitions to ' |
| 238 |
'OK. May be used multiple times')) |
| 239 |
parser.add_argument('--insufficient-data-action', |
| 240 |
dest='insufficient_data_actions', |
| 241 |
metavar='<Webhook URL>', action='append', |
| 242 |
help=('URL to invoke when state transitions to ' |
| 243 |
'insufficient data. May be used multiple ' |
| 244 |
'times')) |
| 245 |
parser.add_argument( |
| 246 |
'--time-constraint', dest='time_constraints', |
| 247 |
metavar='<Time Constraint>', action='append', |
| 248 |
type=self.validate_time_constraint, |
| 249 |
help=('Only evaluate the alarm if the time at evaluation ' |
| 250 |
'is within this time constraint. Start point(s) of ' |
| 251 |
'the constraint are specified with a cron expression' |
| 252 |
', whereas its duration is given in seconds. ' |
| 253 |
'Can be specified multiple times for multiple ' |
| 254 |
'time constraints, format is: ' |
| 255 |
'name=<CONSTRAINT_NAME>;start=<CRON>;' |
| 256 |
'duration=<SECONDS>;[description=<DESCRIPTION>;' |
| 257 |
'[timezone=<IANA Timezone>]]')) |
| 258 |
parser.add_argument('--repeat-actions', dest='repeat_actions', |
| 259 |
metavar='{True|False}', |
| 260 |
type=strutils.bool_from_string, |
| 261 |
help=('True if actions should be repeatedly ' |
| 262 |
'notified while alarm remains in target ' |
| 263 |
'state')) |
| 264 |
|
| 265 |
common_group = parser.add_argument_group('common alarm rules') |
| 266 |
common_group.add_argument( |
| 267 |
'--query', metavar='<QUERY>', dest='query', |
| 268 |
help="For alarms of type threshold or event: " |
| 269 |
"key[op]data_type::value; list. data_type is optional, " |
| 270 |
"but if supplied must be string, integer, float, or boolean. " |
| 271 |
'For alarms of ' |
| 272 |
'type gnocchi_aggregation_by_resources_threshold: ' |
| 273 |
'need to specify a complex query json string, like:' |
| 274 |
' {"and": [{"=": {"ended_at": null}}, ...]}. ' |
| 275 |
'For alarms of type prometheus this should be valid ' |
| 276 |
'PromQL query.') |
| 277 |
common_group.add_argument( |
| 278 |
'--comparison-operator', metavar='<OPERATOR>', |
| 279 |
dest='comparison_operator', choices=ALARM_OPERATORS, |
| 280 |
help='Operator to compare with, one of: ' + str(ALARM_OPERATORS)) |
| 281 |
common_group.add_argument( |
| 282 |
'--evaluation-periods', type=int, metavar='<EVAL_PERIODS>', |
| 283 |
dest='evaluation_periods', |
| 284 |
help='Number of periods to evaluate over') |
| 285 |
common_group.add_argument( |
| 286 |
'--threshold', type=float, metavar='<THRESHOLD>', |
| 287 |
dest='threshold', help='Threshold to evaluate against.') |
| 288 |
|
| 289 |
# For event type alarm |
| 290 |
event_group = parser.add_argument_group('event alarm') |
| 291 |
event_group.add_argument( |
| 292 |
'--event-type', metavar='<EVENT_TYPE>', |
| 293 |
dest='event_type', help='Event type to evaluate against') |
| 294 |
|
| 295 |
# For Ceilometer threshold type alarm |
| 296 |
threshold_group = parser.add_argument_group('threshold alarm') |
| 297 |
threshold_group.add_argument( |
| 298 |
'-m', '--meter-name', metavar='<METER NAME>', |
| 299 |
dest='meter_name', help='Meter to evaluate against') |
| 300 |
threshold_group.add_argument( |
| 301 |
'--period', type=int, metavar='<PERIOD>', dest='period', |
| 302 |
help='Length of each period (seconds) to evaluate over.') |
| 303 |
threshold_group.add_argument( |
| 304 |
'--statistic', metavar='<STATISTIC>', dest='statistic', |
| 305 |
choices=STATISTICS, |
| 306 |
help='Statistic to evaluate, one of: ' + str(STATISTICS)) |
| 307 |
|
| 308 |
# For common Gnocchi threshold type alarm |
| 309 |
gnocchi_common_group = parser.add_argument_group( |
| 310 |
'common gnocchi alarm rules') |
| 311 |
gnocchi_common_group.add_argument( |
| 312 |
'--granularity', metavar='<GRANULARITY>', |
| 313 |
dest='granularity', |
| 314 |
help='The time range in seconds over which to query.') |
| 315 |
gnocchi_common_group.add_argument( |
| 316 |
'--aggregation-method', metavar='<AGGR_METHOD>', |
| 317 |
dest='aggregation_method', |
| 318 |
help='The aggregation_method to compare to the threshold.') |
| 319 |
gnocchi_common_group.add_argument( |
| 320 |
'--metric', '--metrics', metavar='<METRIC>', action='append', |
| 321 |
dest='metrics', help='The metric id or name ' |
| 322 |
'depending of the alarm type') |
| 323 |
|
| 324 |
gnocchi_resource_threshold_group = parser.add_argument_group( |
| 325 |
'gnocchi resource threshold alarm') |
| 326 |
gnocchi_resource_threshold_group.add_argument( |
| 327 |
'--resource-type', metavar='<RESOURCE_TYPE>', |
| 328 |
dest='resource_type', help='The type of resource.') |
| 329 |
gnocchi_resource_threshold_group.add_argument( |
| 330 |
'--resource-id', metavar='<RESOURCE_ID>', |
| 331 |
dest='resource_id', help='The id of a resource.') |
| 332 |
|
| 333 |
# For composite type alarm |
| 334 |
composite_group = parser.add_argument_group('composite alarm') |
| 335 |
composite_group.add_argument( |
| 336 |
'--composite-rule', metavar='<COMPOSITE_RULE>', |
| 337 |
dest='composite_rule', |
| 338 |
type=jsonutils.loads, |
| 339 |
help='Composite threshold rule with JSON format, the form can ' |
| 340 |
'be a nested dict which combine threshold/gnocchi rules by ' |
| 341 |
'"and", "or". For example, the form is like: ' |
| 342 |
'{"or":[RULE1, RULE2, {"and": [RULE3, RULE4]}]}, The ' |
| 343 |
'RULEx can be basic threshold rules but must include a ' |
| 344 |
'"type" field, like this: {"threshold": 0.8,' |
| 345 |
'"meter_name":"cpu_util","type":"threshold"}' |
| 346 |
) |
| 347 |
|
| 348 |
loadbalancer_member_health_group = parser.add_argument_group( |
| 349 |
'loadbalancer member health alarm') |
| 350 |
loadbalancer_member_health_group.add_argument( |
| 351 |
'--stack-id', metavar='<STACK_NAME_OR_ID>', |
| 352 |
dest="stack_id", |
| 353 |
type=str, help=( |
| 354 |
"Name or ID of the root / top level Heat stack containing the " |
| 355 |
"loadbalancer pool and members. An update will be triggered " |
| 356 |
"on the root Stack if an unhealthy member is detected in the " |
| 357 |
"loadbalancer pool.") |
| 358 |
) |
| 359 |
loadbalancer_member_health_group.add_argument( |
| 360 |
'--pool-id', metavar='<LOADBALANCER_POOL_NAME_OR_ID>', |
| 361 |
dest="pool_id", |
| 362 |
type=str, help=( |
| 363 |
"Name or ID of the loadbalancer pool for which the health of " |
| 364 |
"each member will be evaluated.") |
| 365 |
) |
| 366 |
loadbalancer_member_health_group.add_argument( |
| 367 |
'--autoscaling-group-id', metavar='<AUTOSCALING_GROUP_NAME_OR_ID>', |
| 368 |
dest="autoscaling_group_id", |
| 369 |
type=str, help=( |
| 370 |
"ID of the Heat autoscaling group that contains the " |
| 371 |
"loadbalancer members. Unhealthy members will be marked " |
| 372 |
"as such before an update is triggered on the root stack.")) |
| 373 |
self.parser = parser |
| 374 |
return parser |
| 375 |
|
| 376 |
def validate_time_constraint(self, values_to_convert): |
| 377 |
"""Converts 'a=1;b=2' to {a:1,b:2}.""" |
| 378 |
|
| 379 |
try: |
| 380 |
return dict((item.strip(" \"'") |
| 381 |
for item in kv.split("=", 1)) |
| 382 |
for kv in values_to_convert.split(";")) |
| 383 |
except ValueError: |
| 384 |
msg = ('must be a list of ' |
| 385 |
'key1=value1;key2=value2;... not %s' |
| 386 |
% values_to_convert) |
| 387 |
raise argparse.ArgumentTypeError(msg) |
| 388 |
|
| 389 |
def _validate_args(self, parsed_args): |
| 390 |
if (parsed_args.type == 'threshold' and |
| 391 |
not (parsed_args.meter_name and parsed_args.threshold)): |
| 392 |
self.parser.error('Threshold alarm requires -m/--meter-name and ' |
| 393 |
'--threshold parameters. Meter name can be ' |
| 394 |
'found in Ceilometer') |
| 395 |
elif (parsed_args.type == 'gnocchi_resources_threshold' and |
| 396 |
not (parsed_args.metrics and parsed_args.threshold is not None |
| 397 |
and parsed_args.resource_id and parsed_args.resource_type |
| 398 |
and parsed_args.aggregation_method)): |
| 399 |
self.parser.error('gnocchi_resources_threshold requires --metric, ' |
| 400 |
'--threshold, --resource-id, --resource-type ' |
| 401 |
'and --aggregation-method') |
| 402 |
elif (parsed_args.type == 'gnocchi_aggregation_by_metrics_threshold' |
| 403 |
and not (parsed_args.metrics |
| 404 |
and parsed_args.threshold is not None |
| 405 |
and parsed_args.aggregation_method)): |
| 406 |
self.parser.error('gnocchi_aggregation_by_metrics_threshold ' |
| 407 |
'requires --metric, --threshold and ' |
| 408 |
'--aggregation-method') |
| 409 |
elif (parsed_args.type == 'gnocchi_aggregation_by_resources_threshold' |
| 410 |
and not (parsed_args.metrics |
| 411 |
and parsed_args.threshold is not None |
| 412 |
and parsed_args.query and parsed_args.resource_type and |
| 413 |
parsed_args.aggregation_method)): |
| 414 |
self.parser.error('gnocchi_aggregation_by_resources_threshold ' |
| 415 |
'requires --metric, --threshold, ' |
| 416 |
'--aggregation-method, --query and ' |
| 417 |
'--resource-type') |
| 418 |
elif (parsed_args.type == 'composite' and |
| 419 |
not parsed_args.composite_rule): |
| 420 |
self.parser.error('Composite alarm requires' |
| 421 |
' --composite-rule parameter') |
| 422 |
elif (parsed_args.type == 'loadbalancer_member_health') and \ |
| 423 |
(parsed_args.stack_id is None or |
| 424 |
parsed_args.pool_id is None or |
| 425 |
parsed_args.autoscaling_group_id is None): |
| 426 |
self.parser.error('Loadbalancer member health alarm requires' |
| 427 |
'--stack-id, --pool-id and' |
| 428 |
'--autoscaling-group-id') |
| 429 |
elif (parsed_args.type == 'prometheus' and |
| 430 |
not (parsed_args.query and parsed_args.threshold)): |
| 431 |
self.parser.error('Prometheus alarm requires --query and ' |
| 432 |
'--threshold parameters.') |
| 433 |
|
| 434 |
def _alarm_from_args(self, parsed_args): |
| 435 |
alarm = utils.dict_from_parsed_args( |
| 436 |
parsed_args, ['name', 'type', 'project_id', 'user_id', |
| 437 |
'description', 'state', 'severity', 'enabled', |
| 438 |
'alarm_actions', 'ok_actions', |
| 439 |
'insufficient_data_actions', |
| 440 |
'time_constraints', 'repeat_actions']) |
| 441 |
if parsed_args.type in ('threshold', 'event') and parsed_args.query: |
| 442 |
parsed_args.query = utils.cli_to_array(parsed_args.query) |
| 443 |
alarm['threshold_rule'] = utils.dict_from_parsed_args( |
| 444 |
parsed_args, ['meter_name', 'period', 'evaluation_periods', |
| 445 |
'statistic', 'comparison_operator', 'threshold', |
| 446 |
'query']) |
| 447 |
alarm['event_rule'] = utils.dict_from_parsed_args( |
| 448 |
parsed_args, ['event_type', 'query']) |
| 449 |
alarm['prometheus_rule'] = ( |
| 450 |
utils.dict_from_parsed_args(parsed_args, |
| 451 |
['comparison_operator', 'threshold', |
| 452 |
'query'])) |
| 453 |
alarm['gnocchi_resources_threshold_rule'] = ( |
| 454 |
utils.dict_from_parsed_args(parsed_args, |
| 455 |
['granularity', 'comparison_operator', |
| 456 |
'threshold', 'aggregation_method', |
| 457 |
'evaluation_periods', 'metric', |
| 458 |
'resource_id', 'resource_type'])) |
| 459 |
alarm['gnocchi_aggregation_by_metrics_threshold_rule'] = ( |
| 460 |
utils.dict_from_parsed_args(parsed_args, |
| 461 |
['granularity', 'comparison_operator', |
| 462 |
'threshold', 'aggregation_method', |
| 463 |
'evaluation_periods', 'metrics'])) |
| 464 |
alarm['gnocchi_aggregation_by_resources_threshold_rule'] = ( |
| 465 |
utils.dict_from_parsed_args(parsed_args, |
| 466 |
['granularity', 'comparison_operator', |
| 467 |
'threshold', 'aggregation_method', |
| 468 |
'evaluation_periods', 'metric', |
| 469 |
'query', 'resource_type'])) |
| 470 |
|
| 471 |
alarm['loadbalancer_member_health_rule'] = ( |
| 472 |
utils.dict_from_parsed_args(parsed_args, [ |
| 473 |
"stack_id", "pool_id", "autoscaling_group_id"])) |
| 474 |
|
| 475 |
alarm['composite_rule'] = parsed_args.composite_rule |
| 476 |
if self.create: |
| 477 |
alarm['type'] = parsed_args.type |
| 478 |
self._validate_args(parsed_args) |
| 479 |
return alarm |
| 480 |
|
| 481 |
def take_action(self, parsed_args): |
| 482 |
alarm = utils.get_client(self).alarm.create( |
| 483 |
alarm=self._alarm_from_args(parsed_args)) |
| 484 |
return self.dict2columns(_format_alarm(alarm)) |
| 485 |
|
| 486 |
|
| 487 |
class CliAlarmUpdate(CliAlarmCreate): |
| 488 |
"""Update an alarm""" |
| 489 |
|
| 490 |
create = False |
| 491 |
|
| 492 |
def get_parser(self, prog_name): |
| 493 |
return _add_id_to_parser( |
| 494 |
super().get_parser(prog_name)) |
| 495 |
|
| 496 |
def take_action(self, parsed_args): |
| 497 |
attributes = self._alarm_from_args(parsed_args) |
| 498 |
_check_name_and_id_exist(parsed_args, 'update') |
| 499 |
c = utils.get_client(self) |
| 500 |
|
| 501 |
if uuidutils.is_uuid_like(parsed_args.id): |
| 502 |
try: |
| 503 |
alarm = c.alarm.update(alarm_id=parsed_args.id, |
| 504 |
alarm_update=attributes) |
| 505 |
except exceptions.NotFound: |
| 506 |
# Maybe it was not an ID but a name, damn |
| 507 |
_id = _find_alarm_id_by_name(c, parsed_args.id) |
| 508 |
else: |
| 509 |
return self.dict2columns(_format_alarm(alarm)) |
| 510 |
elif parsed_args.id: |
| 511 |
_id = _find_alarm_id_by_name(c, parsed_args.id) |
| 512 |
else: |
| 513 |
_id = _find_alarm_id_by_name(c, parsed_args.name) |
| 514 |
|
| 515 |
alarm = c.alarm.update(alarm_id=_id, alarm_update=attributes) |
| 516 |
return self.dict2columns(_format_alarm(alarm)) |
| 517 |
|
| 518 |
|
| 519 |
class CliAlarmDelete(command.Command): |
| 520 |
"""Delete an alarm""" |
| 521 |
|
| 522 |
def get_parser(self, prog_name): |
| 523 |
return _add_name_to_parser( |
| 524 |
_add_id_to_parser( |
| 525 |
super().get_parser(prog_name))) |
| 526 |
|
| 527 |
def take_action(self, parsed_args): |
| 528 |
_check_name_and_id(parsed_args, 'delete') |
| 529 |
c = utils.get_client(self) |
| 530 |
|
| 531 |
if parsed_args.name: |
| 532 |
_id = _find_alarm_id_by_name(c, parsed_args.name) |
| 533 |
elif uuidutils.is_uuid_like(parsed_args.id): |
| 534 |
try: |
| 535 |
return c.alarm.delete(parsed_args.id) |
| 536 |
except exceptions.NotFound: |
| 537 |
# Maybe it was not an ID after all |
| 538 |
_id = _find_alarm_id_by_name(c, parsed_args.id) |
| 539 |
else: |
| 540 |
_id = _find_alarm_id_by_name(c, parsed_args.id) |
| 541 |
|
| 542 |
c.alarm.delete(_id) |
| 543 |
|
| 544 |
|
| 545 |
class CliAlarmStateGet(show.ShowOne): |
| 546 |
"""Get state of an alarm""" |
| 547 |
|
| 548 |
def get_parser(self, prog_name): |
| 549 |
return _add_name_to_parser( |
| 550 |
_add_id_to_parser( |
| 551 |
super().get_parser(prog_name))) |
| 552 |
|
| 553 |
def take_action(self, parsed_args): |
| 554 |
_check_name_and_id(parsed_args, 'get state of') |
| 555 |
c = utils.get_client(self) |
| 556 |
|
| 557 |
if parsed_args.name: |
| 558 |
_id = _find_alarm_id_by_name(c, parsed_args.name) |
| 559 |
elif uuidutils.is_uuid_like(parsed_args.id): |
| 560 |
try: |
| 561 |
state = c.alarm.get_state(parsed_args.id) |
| 562 |
except exceptions.NotFound: |
| 563 |
# Maybe it was not an ID after all |
| 564 |
_id = _find_alarm_id_by_name(c, parsed_args.id) |
| 565 |
else: |
| 566 |
return self.dict2columns({'state': state}) |
| 567 |
else: |
| 568 |
_id = _find_alarm_id_by_name(c, parsed_args.id) |
| 569 |
|
| 570 |
state = c.alarm.get_state(_id) |
| 571 |
return self.dict2columns({'state': state}) |
| 572 |
|
| 573 |
|
| 574 |
class CliAlarmStateSet(show.ShowOne): |
| 575 |
"""Set state of an alarm""" |
| 576 |
|
| 577 |
def get_parser(self, prog_name): |
| 578 |
parser = _add_name_to_parser( |
| 579 |
_add_id_to_parser( |
| 580 |
super().get_parser(prog_name))) |
| 581 |
parser.add_argument('--state', metavar='<STATE>', |
| 582 |
required=True, |
| 583 |
choices=ALARM_STATES, |
| 584 |
help='State of the alarm, one of: ' |
| 585 |
+ str(ALARM_STATES)) |
| 586 |
return parser |
| 587 |
|
| 588 |
def take_action(self, parsed_args): |
| 589 |
_check_name_and_id(parsed_args, 'set state of') |
| 590 |
c = utils.get_client(self) |
| 591 |
|
| 592 |
if parsed_args.name: |
| 593 |
_id = _find_alarm_id_by_name(c, parsed_args.name) |
| 594 |
elif uuidutils.is_uuid_like(parsed_args.id): |
| 595 |
try: |
| 596 |
state = c.alarm.set_state(parsed_args.id, parsed_args.state) |
| 597 |
except exceptions.NotFound: |
| 598 |
# Maybe it was not an ID after all |
| 599 |
_id = _find_alarm_id_by_name(c, parsed_args.id) |
| 600 |
else: |
| 601 |
return self.dict2columns({'state': state}) |
| 602 |
else: |
| 603 |
_id = _find_alarm_id_by_name(c, parsed_args.id) |
| 604 |
|
| 605 |
state = c.alarm.set_state(_id, parsed_args.state) |
| 606 |
return self.dict2columns({'state': state}) |