Merge pull request #233 from pigmej/vr_lists
List input value support in VR
This commit is contained in:
commit
bca74e8a92
@ -1,4 +1,4 @@
|
|||||||
# -*- coding: UTF-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
# Copyright 2015 Mirantis, Inc.
|
# Copyright 2015 Mirantis, Inc.
|
||||||
#
|
#
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||||
@ -59,7 +59,12 @@ def create_resource(name, base_path, args=None, tags=None, virtual_resource=None
|
|||||||
base_path = base_path.directory
|
base_path = base_path.directory
|
||||||
|
|
||||||
# List args init with empty list. Elements will be added later
|
# List args init with empty list. Elements will be added later
|
||||||
args = {key: (value if not isinstance(value, list) else []) for key, value in args.items()}
|
def _filter(value):
|
||||||
|
if not isinstance(value, list):
|
||||||
|
return value
|
||||||
|
return filter(lambda res: not is_connection(res), value)
|
||||||
|
|
||||||
|
args = {key: _filter(value) for key, value in args.items()}
|
||||||
r = Resource(
|
r = Resource(
|
||||||
name, base_path, args=args, tags=tags, virtual_resource=virtual_resource
|
name, base_path, args=args, tags=tags, virtual_resource=virtual_resource
|
||||||
)
|
)
|
||||||
@ -212,6 +217,8 @@ def parse_events(template_events):
|
|||||||
return parsed_events
|
return parsed_events
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def parse_inputs(args):
|
def parse_inputs(args):
|
||||||
connections = []
|
connections = []
|
||||||
assignments = {}
|
assignments = {}
|
||||||
@ -221,7 +228,7 @@ def parse_inputs(args):
|
|||||||
connections.extend(c)
|
connections.extend(c)
|
||||||
assignments.update(a)
|
assignments.update(a)
|
||||||
else:
|
else:
|
||||||
if isinstance(arg, basestring) and '::' in arg:
|
if is_connection(arg):
|
||||||
c = parse_connection(r_input, arg)
|
c = parse_connection(r_input, arg)
|
||||||
connections.append(c)
|
connections.append(c)
|
||||||
else:
|
else:
|
||||||
@ -233,15 +240,23 @@ def parse_list_input(r_input, args):
|
|||||||
connections = []
|
connections = []
|
||||||
assignments = {}
|
assignments = {}
|
||||||
for arg in args:
|
for arg in args:
|
||||||
if isinstance(arg, basestring) and '::' in arg:
|
if is_connection(arg):
|
||||||
c = parse_connection(r_input, arg)
|
c = parse_connection(r_input, arg)
|
||||||
connections.append(c)
|
connections.append(c)
|
||||||
else:
|
else:
|
||||||
# Not supported yet
|
try:
|
||||||
raise Exception('Only connections are supported in lists')
|
assignments[r_input].append(arg)
|
||||||
|
except KeyError:
|
||||||
|
assignments[r_input] = [arg]
|
||||||
return connections, assignments
|
return connections, assignments
|
||||||
|
|
||||||
|
|
||||||
|
def is_connection(arg):
|
||||||
|
if isinstance(arg, basestring) and '::' in arg:
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
def parse_connection(child_input, element):
|
def parse_connection(child_input, element):
|
||||||
parent, parent_input = element.split('::', 1)
|
parent, parent_input = element.split('::', 1)
|
||||||
try:
|
try:
|
||||||
|
@ -0,0 +1,9 @@
|
|||||||
|
id: simple_resource_with_list
|
||||||
|
resources:
|
||||||
|
- id: res1
|
||||||
|
from: {resource_path}
|
||||||
|
values:
|
||||||
|
ip: '10.0.0.3'
|
||||||
|
servers:
|
||||||
|
- 1
|
||||||
|
- 2
|
@ -73,6 +73,22 @@ def test_create_virtual_resource(tmpdir):
|
|||||||
resources = vr.create('nodes', str(vr_file))
|
resources = vr.create('nodes', str(vr_file))
|
||||||
assert len(resources) == 2
|
assert len(resources) == 2
|
||||||
|
|
||||||
|
def test_create_virtual_resource_with_list(tmpdir):
|
||||||
|
base_path = os.path.join(
|
||||||
|
os.path.dirname(os.path.realpath(__file__)),
|
||||||
|
'resource_fixtures')
|
||||||
|
vr_tmpl_path = os.path.join(base_path, 'resource_with_list.yaml.tmpl')
|
||||||
|
base_resource_path = os.path.join(base_path, 'base_service')
|
||||||
|
with open(vr_tmpl_path) as f:
|
||||||
|
vr_data = f.read().format(resource_path=base_resource_path)
|
||||||
|
vr_file = tmpdir.join('base.yaml')
|
||||||
|
vr_file.write(vr_data)
|
||||||
|
resources = vr.create('base', str(vr_file))
|
||||||
|
assert len(resources) == 1
|
||||||
|
res = resources[0]
|
||||||
|
assert res.args['servers'] == [1, 2]
|
||||||
|
|
||||||
|
|
||||||
def test_update(tmpdir):
|
def test_update(tmpdir):
|
||||||
# XXX: make helper for it
|
# XXX: make helper for it
|
||||||
base_path = os.path.join(
|
base_path = os.path.join(
|
||||||
@ -120,6 +136,17 @@ def test_add_connections(mocker, resources):
|
|||||||
vr.update_inputs('service1', args)
|
vr.update_inputs('service1', args)
|
||||||
assert mocked_signals.connect.call_count == 3
|
assert mocked_signals.connect.call_count == 3
|
||||||
|
|
||||||
|
|
||||||
|
def test_add_list_values(mocker, resources):
|
||||||
|
mocked_signals = mocker.patch('solar.core.resource.resource.signals')
|
||||||
|
args = {'ip': 'node1::ip',
|
||||||
|
'servers': ['server1', 'server2'],
|
||||||
|
'alias': 'ser1'
|
||||||
|
}
|
||||||
|
vr.update_inputs('service1', args)
|
||||||
|
assert mocked_signals.connect.call_count == 1
|
||||||
|
|
||||||
|
|
||||||
def test_parse_connection():
|
def test_parse_connection():
|
||||||
correct_connection = {'child_input': 'ip',
|
correct_connection = {'child_input': 'ip',
|
||||||
'parent' : 'node1',
|
'parent' : 'node1',
|
||||||
|
Loading…
x
Reference in New Issue
Block a user