1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
|
# Copyright 2016 - Nokia Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import sys
from cliff import command
from cliff import lister
from cliff import show
from oslo_log import log
from vitrageclient.common import utils
from vitrageclient.common.utils import find_template_with_uuid
LOG = log.getLogger(__name__)
def _parse_template_params(cli_param_list):
return dict(cli_param.split('=', 1) for cli_param in cli_param_list) \
if cli_param_list else {}
# noinspection PyAbstractClass
class TemplateValidate(show.ShowOne):
"""Validate a template file"""
def get_parser(self, prog_name):
parser = super(TemplateValidate, self).get_parser(prog_name)
parser.add_argument('--path',
required=True,
help='full path for template file or templates dir'
)
parser.add_argument('--type',
choices=['standard', 'definition', 'equivalence'],
help='Template type. Valid types:'
'[standard, definition, equivalence]')
parser.add_argument('--params', nargs='+',
help='Actual values for parameters of the '
'template. Several key=value pairs may be '
'used, for example: --params '
'template_name=cpu_problem '
'alarm_name=\'High CPU Load\'')
return parser
@property
def formatter_default(self):
return 'json'
def take_action(self, parsed_args):
cli_param_list = parsed_args.params
params = _parse_template_params(cli_param_list)
result = utils.get_client(self).template.validate(
path=parsed_args.path,
template_type=parsed_args.type,
params=params)
return self.dict2columns(result)
class TemplateVersions(lister.Lister):
"""List all template versions"""
def get_parser(self, prog_name):
parser = super(TemplateVersions, self).get_parser(prog_name)
return parser
def take_action(self, parsed_args):
templates = utils.get_client(self).template.versions()
return utils.list2cols_with_rename(
(
('Version', 'version'),
('Status', 'status'),
), templates)
class TemplateList(lister.Lister):
"""List all templates"""
def get_parser(self, prog_name):
parser = super(TemplateList, self).get_parser(prog_name)
return parser
def take_action(self, parsed_args):
templates = utils.get_client(self).template.list()
return utils.list2cols_with_rename(
(
('UUID', 'uuid'),
('Name', 'name'),
('Status', 'status'),
('Status details', 'status details'),
('Date', 'date'),
('Type', 'type'),
), templates)
class TemplateShow(show.ShowOne):
"""Show a template"""
def get_parser(self, prog_name):
parser = super(TemplateShow, self).get_parser(prog_name)
parser.add_argument('id', help='Template UUID or Name')
return parser
@property
def formatter_default(self):
return 'yaml'
def take_action(self, parsed_args):
_id = parsed_args.id
template = utils.get_client(self).template.show(_id=_id)
return self.dict2columns(template)
class TemplateAdd(lister.Lister):
"""Add a template
support 3 types of templates:
standard, definition, equivalence
"""
def get_parser(self, prog_name):
parser = super(TemplateAdd, self).get_parser(prog_name)
parser.add_argument('--path',
required=True,
help='full path for template file or templates dir'
)
parser.add_argument('--type',
choices=['standard', 'definition', 'equivalence'],
help='Template type. Valid types:'
'[standard, definition, equivalence]')
parser.add_argument('--params', nargs='+',
help='Actual values for parameters of the '
'template. Several key=value pairs may be '
'used, for example: --params '
'template_name=cpu_problem '
'alarm_name=\'High CPU Load\'')
parser.add_argument('--wait',
type=int,
default=None,
nargs='?',
const=sys.maxsize,
help='Wait until template is ACTIVE or in ERROR'
' default is to wait forever '
'else number of seconds'
)
parser.add_argument('--overwrite',
default=False,
action='store_true',
help='If template exists by name overwrite it'
)
return parser
def take_action(self, parsed_args):
path = parsed_args.path
template_type = parsed_args.type
cli_param_list = parsed_args.params
params = _parse_template_params(cli_param_list)
wait = parsed_args.wait
overwrite = parsed_args.overwrite
templates = utils.get_client(self).template.add(
path=path, template_type=template_type, params=params,
overwrite=overwrite)
if wait:
utils.wait_for_action_to_end(wait,
self._check_finished_loading,
templates=templates)
return utils.list2cols_with_rename(
(
('UUID', 'uuid'),
('Name', 'name'),
('Status', 'status'),
('Status details', 'status details'),
('Date', 'date'),
('Type', 'type'),
), templates)
def _check_finished_loading(self, templates):
if all(
(
template['status'] == 'ERROR'
for template in templates
)
):
return True
try:
api_templates = utils.get_client(self).template.list()
self._update_templates_status(api_templates, templates)
if any(
(
template['status'] == 'LOADING'
for template in templates
)
):
return False
return True
except Exception:
return True
@staticmethod
def _update_templates_status(api_templates, templates):
for template in templates:
uuid = template.get('uuid')
if uuid:
api_template = find_template_with_uuid(uuid, api_templates)
if api_template:
template['status'] = api_template['status']
class TemplateDelete(command.Command):
"""Delete a template"""
def get_parser(self, prog_name):
parser = super(TemplateDelete, self).get_parser(prog_name)
parser.add_argument('id',
help='<ID or Name> of a template',
nargs='+')
parser.add_argument('--wait',
type=int,
default=None,
nargs='?',
const=sys.maxsize,
help='Wait until template is DELETED or in ERROR'
' default is to wait forever else number of '
'seconds'
)
return parser
@property
def formatter_default(self):
return 'json'
def take_action(self, parsed_args):
ids = parsed_args.id
wait = parsed_args.wait
utils.get_client(self).template.delete(ids=ids)
if wait:
utils.wait_for_action_to_end(wait,
self._check_deleted,
ids=ids)
def _check_deleted(self, ids):
for _id in ids:
try:
utils.get_client(self).template.show(_id)
except Exception: # if deleted we get exception
pass
else:
return False
return True
|