Template Validate API - client side
Change-Id: Iba0ca35f78d33e271504797ad56e2f201983be9e
This commit is contained in:
parent
ed48658f2a
commit
bfc17530bd
22
vitrageclient/common/exc.py
Normal file
22
vitrageclient/common/exc.py
Normal file
@ -0,0 +1,22 @@
|
||||
# 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.
|
||||
|
||||
|
||||
class CommandException(Exception):
|
||||
"""An error occurred."""
|
||||
def __init__(self, message=None):
|
||||
self.message = message
|
||||
|
||||
def __str__(self):
|
||||
return self.message or self.__class__.__doc__
|
@ -1,3 +1,4 @@
|
||||
# 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
|
||||
|
33
vitrageclient/common/yaml_utils.py
Normal file
33
vitrageclient/common/yaml_utils.py
Normal file
@ -0,0 +1,33 @@
|
||||
# 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 yaml
|
||||
|
||||
|
||||
def load(stream):
|
||||
try:
|
||||
yaml_dict = yaml.load(stream, Loader=yaml.BaseLoader)
|
||||
except yaml.YAMLError as exc:
|
||||
msg = 'An error occurred during YAML parsing.'
|
||||
if hasattr(exc, 'problem_mark'):
|
||||
msg += ' Error position: (%s:%s)' % (exc.problem_mark.line + 1,
|
||||
exc.problem_mark.column + 1)
|
||||
raise ValueError(msg)
|
||||
|
||||
if not isinstance(yaml_dict, dict) and not isinstance(yaml_dict, list):
|
||||
raise ValueError('The source is not a YAML mapping or list.')
|
||||
|
||||
if isinstance(yaml_dict, dict) and len(yaml_dict) < 1:
|
||||
raise ValueError('Could not find any element in your YAML mapping.')
|
||||
|
||||
return yaml_dict
|
@ -1,3 +1,5 @@
|
||||
# Copyright 2016 Nokia
|
||||
#
|
||||
# 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
|
||||
@ -31,6 +33,7 @@ import client
|
||||
from v1.cli import alarms
|
||||
from v1.cli import rca
|
||||
from v1.cli import resource
|
||||
from v1.cli import template
|
||||
from v1.cli import topology
|
||||
from vitrageclient import __version__
|
||||
|
||||
@ -42,6 +45,7 @@ class VitrageCommandManager(commandmanager.CommandManager):
|
||||
'resource list': resource.ResourceList,
|
||||
'alarms list': alarms.AlarmsList,
|
||||
'rca show': rca.RcaShow,
|
||||
'template validate': template.TemplateValidate,
|
||||
}
|
||||
|
||||
def load_commands(self, namespace):
|
||||
|
42
vitrageclient/v1/cli/template.py
Normal file
42
vitrageclient/v1/cli/template.py
Normal file
@ -0,0 +1,42 @@
|
||||
# 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.
|
||||
|
||||
from cliff import show
|
||||
from oslo_log import log
|
||||
from vitrageclient.common.exc import CommandException
|
||||
|
||||
LOG = log.getLogger(__name__)
|
||||
|
||||
|
||||
# noinspection PyAbstractClass
|
||||
class TemplateValidate(show.ShowOne):
|
||||
|
||||
def get_parser(self, prog_name):
|
||||
parser = super(TemplateValidate, self).get_parser(prog_name)
|
||||
parser.add_argument('--path', help='full path for template file or '
|
||||
'templates dir)')
|
||||
return parser
|
||||
|
||||
def formatter_default(self):
|
||||
return 'json'
|
||||
|
||||
def take_action(self, parsed_args):
|
||||
|
||||
if not parsed_args.path:
|
||||
raise CommandException(message='No path requested, add --path')
|
||||
|
||||
if parsed_args.path:
|
||||
result = self.app.client.template.validate(path=parsed_args.path)
|
||||
|
||||
return self.dict2columns(result)
|
@ -15,6 +15,7 @@ from vitrageclient import client
|
||||
from vitrageclient.v1 import alarms
|
||||
from vitrageclient.v1 import rca
|
||||
from vitrageclient.v1 import resource
|
||||
from vitrageclient.v1 import template
|
||||
from vitrageclient.v1 import topology
|
||||
|
||||
|
||||
@ -27,3 +28,4 @@ class Client(object):
|
||||
self.resource = resource.Resource(self._api)
|
||||
self.alarms = alarms.Alarm(self._api)
|
||||
self.rca = rca.Rca(self._api)
|
||||
self.template = template.Template(self._api)
|
||||
|
49
vitrageclient/v1/template.py
Normal file
49
vitrageclient/v1/template.py
Normal file
@ -0,0 +1,49 @@
|
||||
# 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 os
|
||||
from vitrageclient.common.exc import CommandException
|
||||
from vitrageclient.common import yaml_utils
|
||||
|
||||
|
||||
class Template(object):
|
||||
|
||||
URL = 'v1/template/'
|
||||
|
||||
def __init__(self, api):
|
||||
self.api = api
|
||||
|
||||
def validate(self, path=None):
|
||||
"""Template validation
|
||||
|
||||
:param path: the YAML file path
|
||||
"""
|
||||
|
||||
if os.path.isdir(path):
|
||||
pass
|
||||
else:
|
||||
template_def = self.load_template_definition(path)
|
||||
|
||||
params = dict(template_def=template_def)
|
||||
|
||||
return self.api.post(self.URL, json=params).json()
|
||||
|
||||
@staticmethod
|
||||
def load_template_definition(path):
|
||||
|
||||
with open(path, 'r') as stream:
|
||||
try:
|
||||
return yaml_utils.load(stream)
|
||||
except ValueError as e:
|
||||
message = 'Could not load template. Reason: %s' % e.message
|
||||
raise CommandException(message=message)
|
Loading…
Reference in New Issue
Block a user