Merge "Error handling for vnfd-create with empty vnfd-file"

This commit is contained in:
Jenkins 2017-07-05 09:33:49 +00:00 committed by Gerrit Code Review
commit bfed79e1b0
7 changed files with 85 additions and 20 deletions

View File

@ -203,6 +203,10 @@ class InvalidContentType(TackerClientException):
message = _("Invalid content type %(content_type)s.")
class InvalidInput(TackerClientException):
message = _("Invalid input: %(reason)s")
# Command line exceptions
class TackerCLIError(TackerException):

View File

@ -12,6 +12,7 @@
import yaml
from tackerclient.common import exceptions
from tackerclient.i18n import _
from tackerclient.tacker import v1_0 as tackerV10
@ -91,8 +92,11 @@ class CreateNS(tackerV10.CreateCommand):
if parsed_args.param_file:
with open(parsed_args.param_file) as f:
param_yaml = f.read()
args['attributes']['param_values'] = yaml.load(
param_yaml, Loader=yaml.SafeLoader)
try:
args['attributes']['param_values'] = yaml.load(
param_yaml, Loader=yaml.SafeLoader)
except yaml.YAMLError as e:
raise exceptions.InvalidInput(e)
tackerV10.update_dict(parsed_args, body[self.resource],
['tenant_id', 'name', 'description',
'nsd_id', 'vim_id'])

View File

@ -67,7 +67,11 @@ class CreateVIM(tackerV10.CreateCommand):
if parsed_args.config_file:
with open(parsed_args.config_file) as f:
vim_config = f.read()
config_param = yaml.load(vim_config, Loader=yaml.SafeLoader)
try:
config_param = yaml.load(vim_config,
Loader=yaml.SafeLoader)
except yaml.YAMLError as e:
raise exceptions.InvalidInput(e)
vim_obj = body[self.resource]
try:
auth_url = config_param.pop('auth_url')
@ -113,7 +117,10 @@ class UpdateVIM(tackerV10.UpdateCommand):
if parsed_args.config_file:
with open(parsed_args.config_file) as f:
config_yaml = f.read()
config_param = yaml.load(config_yaml)
try:
config_param = yaml.load(config_yaml)
except yaml.YAMLError as e:
raise exceptions.InvalidInput(e)
vim_obj = body[self.resource]
if config_param is not None:
vim_utils.args2body_vim(config_param, vim_obj)

View File

@ -10,9 +10,10 @@
# License for the specific language governing permissions and limitations
# under the License.
from tackerclient.i18n import _
import yaml
from tackerclient.common import exceptions
from tackerclient.i18n import _
from tackerclient.tacker import v1_0 as tackerV10
@ -131,8 +132,11 @@ class CreateVNFFG(tackerV10.CreateCommand):
if parsed_args.param_file:
with open(parsed_args.param_file) as f:
param_yaml = f.read()
args['attributes']['param_values'] = yaml.load(
param_yaml, Loader=yaml.SafeLoader)
try:
args['attributes']['param_values'] = yaml.load(
param_yaml, Loader=yaml.SafeLoader)
except yaml.YAMLError as e:
raise exceptions.InvalidInput(e)
tackerV10.update_dict(parsed_args, body[self.resource],
['tenant_id', 'name', 'vnffgd_id',

View File

@ -17,6 +17,7 @@
import yaml
from tackerclient.common import exceptions
from tackerclient.i18n import _
from tackerclient.tacker import v1_0 as tackerV10
@ -87,8 +88,11 @@ class CreateVNF(tackerV10.CreateCommand):
if parsed_args.config_file:
with open(parsed_args.config_file) as f:
config_yaml = f.read()
config = yaml.load(
config_yaml, Loader=yaml.SafeLoader)
try:
config = yaml.load(
config_yaml, Loader=yaml.SafeLoader)
except yaml.YAMLError as e:
raise exceptions.InvalidInput(e)
if config:
args['attributes']['config'] = config
@ -113,14 +117,20 @@ class CreateVNF(tackerV10.CreateCommand):
elif parsed_args.vnfd_template:
with open(parsed_args.vnfd_template) as f:
template = f.read()
args['vnfd_template'] = yaml.load(
template, Loader=yaml.SafeLoader)
try:
args['vnfd_template'] = yaml.load(
template, Loader=yaml.SafeLoader)
except yaml.YAMLError as e:
raise exceptions.InvalidInput(e)
if parsed_args.param_file:
with open(parsed_args.param_file) as f:
param_yaml = f.read()
args['attributes']['param_values'] = yaml.load(
param_yaml, Loader=yaml.SafeLoader)
try:
args['attributes']['param_values'] = yaml.load(
param_yaml, Loader=yaml.SafeLoader)
except yaml.YAMLError as e:
raise exceptions.InvalidInput(e)
tackerV10.update_dict(parsed_args, body[self.resource],
['tenant_id', 'name', 'description',
'vnfd_id', 'vim_id'])
@ -147,12 +157,18 @@ class UpdateVNF(tackerV10.UpdateCommand):
if parsed_args.config_file:
with open(parsed_args.config_file) as f:
config_yaml = f.read()
config = yaml.load(config_yaml, Loader=yaml.SafeLoader)
try:
config = yaml.load(config_yaml, Loader=yaml.SafeLoader)
except yaml.YAMLError as e:
raise exceptions.InvalidInput(e)
if parsed_args.config:
config = parsed_args.config
if isinstance(config, str) or isinstance(config, unicode):
config_str = parsed_args.config.decode('unicode_escape')
config = yaml.load(config_str, Loader=yaml.SafeLoader)
try:
config = yaml.load(config_str, Loader=yaml.SafeLoader)
except yaml.YAMLError as e:
raise exceptions.InvalidInput(e)
if config:
body[self.resource]['attributes'] = {'config': config}
tackerV10.update_dict(parsed_args, body[self.resource], ['tenant_id'])

View File

@ -20,6 +20,7 @@ from __future__ import print_function
from oslo_serialization import jsonutils
import yaml
from tackerclient.common import exceptions
from tackerclient.i18n import _
from tackerclient.tacker import v1_0 as tackerV10
@ -75,13 +76,17 @@ class CreateVNFD(tackerV10.CreateCommand):
def args2body(self, parsed_args):
body = {self.resource: {}}
vnfd = None
if parsed_args.vnfd_file:
with open(parsed_args.vnfd_file) as f:
vnfd = f.read()
if not parsed_args.vnfd_file:
raise exceptions.InvalidInput("Invalid input for vnfd file")
with open(parsed_args.vnfd_file) as f:
vnfd = f.read()
try:
vnfd = yaml.load(vnfd, Loader=yaml.SafeLoader)
if vnfd:
except yaml.YAMLError as e:
raise exceptions.InvalidInput(e)
if not vnfd:
raise exceptions.InvalidInput("vnfd file is empty")
body[self.resource]['attributes'] = {'vnfd': vnfd}
tackerV10.update_dict(parsed_args, body[self.resource],
['tenant_id', 'name', 'description'])
return body

View File

@ -18,6 +18,7 @@ from mock import mock_open
from mock import patch
import sys
from tackerclient.common.exceptions import InvalidInput
from tackerclient.tacker.v1_0.vnfm import vnfd
from tackerclient.tests.unit import test_cli10
@ -73,6 +74,30 @@ class CLITestV10VmVNFDJSON(test_cli10.CLITestV10Base):
args, position_names, position_values,
extra_body=extra_body)
@patch("tackerclient.tacker.v1_0.vnfm.vnfd.open",
side_effect=mock_open(read_data=""),
create=True)
def test_create_vnfd_with_empty_file(self, mo):
cmd = vnfd.CreateVNFD(
test_cli10.MyApp(sys.stdout), None)
name = 'my_name'
my_id = 'my-id'
args = [name, '--vnfd-file', 'vnfd-file', ]
position_names = ['name']
position_values = [name]
extra_body = {
'service_types': [{'service_type': 'vnfd'}],
'attributes': {'vnfd': 'vnfd'}
}
err = None
try:
self._test_create_resource(self._RESOURCE, cmd, name, my_id,
args, position_names, position_values,
extra_body=extra_body)
except InvalidInput:
err = True
self.assertEqual(True, err)
def test_list_vnfds(self):
cmd = vnfd.ListVNFD(test_cli10.MyApp(sys.stdout), None)
self._test_list_resources(self._RESOURCES, cmd, True,