Make use of parameter_type and constraints

provided by Tuskar API, to display different
input fields in Sevice configuration.

Change-Id: Ie232b29b13567540d5b2a74bd6093f88c23dea6d
This commit is contained in:
Jiri Tomasek 2015-05-05 15:26:12 +02:00
parent 9ba23e1c3d
commit 327c13e7fd
4 changed files with 98 additions and 34 deletions

View File

@ -488,25 +488,12 @@ class Role(base.APIResourceWrapper):
class Parameter(base.APIDictWrapper):
_attrs = ['name', 'value', 'default', 'description', 'hidden', 'label',
'type', 'constraints']
'parameter_type', 'constraints']
def __init__(self, apidict, plan=None):
super(Parameter, self).__init__(apidict)
self._plan = plan
# TODO(jtomasek) Remove when API supports parameter type
@property
def type(self):
return 'string'
# TODO(jtomasek) Remove when API supports parameter constraints
@property
def constraints(self):
return {'length': {'definition': None, 'description': None},
'range': {'definition': None, 'description': None},
'allowed_values': {'definition': [], 'description': None},
'allowed_pattern': {'definition': None, 'description': None}}
@property
def stripped_name(self):
return strip_prefix(self.name)
@ -526,6 +513,20 @@ class Parameter(base.APIDictWrapper):
"""Boolean: True if parameter is required, False otherwise."""
return self.default is None
def get_constraint_by_type(self, constraint_type):
"""Returns parameter constraint by it's type.
For available constraint types see HOT Spec:
http://docs.openstack.org/developer/heat/template_guide/hot_spec.html
"""
constraints_of_type = [c for c in self.constraints
if c['constraint_type'] == constraint_type]
if constraints_of_type:
return constraints_of_type[0]
else:
return None
@staticmethod
def required_parameters(parameters):
"""Yields parameters which are required."""

View File

@ -69,16 +69,17 @@ def parameter_fields(request, prefix=None, read_only=False):
else:
if p.hidden:
widget = django.forms.PasswordInput(render_value=True)
elif p.type == 'number':
elif p.parameter_type == 'number':
Field = django.forms.IntegerField
elif p.type == 'boolean':
elif p.parameter_type == 'boolean':
Field = django.forms.BooleanField
elif (p.type == 'string' and
p.constraints['allowed_values']['definition']):
elif (p.parameter_type == 'string' and
p.get_constraint_by_type('allowed_values')):
Field = django.forms.ChoiceField
field_kwargs['choices'] = (
p.constraints['allowed_values']['definition'])
elif (p.type in ['json', 'comma_delimited_list'] or
field_kwargs['choices'] = [
(choice, choice) for choice in
p.get_constraint_by_type('allowed_values')['definition']]
elif (p.parameter_type in ['json', 'comma_delimited_list'] or
'Certificate' in p.name):
widget = django.forms.Textarea

View File

@ -146,35 +146,45 @@ class TuskarAPITests(test.APITestCase):
'name': 'Controller-1::KeystoneCACertificate',
'value': 'unset',
'default': '',
'label': 'Keystone CA CertificateAdmin'},
'label': 'Keystone CA CertificateAdmin',
'parameter_type': 'string',
'constraints': []},
'Controller-1::SnmpdReadonlyUserPassword': {
'description': 'Snmpd password',
'hidden': True,
'name': 'Controller-1::SnmpdReadonlyUserPassword',
'value': '',
'default': '',
'label': 'Snmpd password'},
'label': 'Snmpd password',
'parameter_type': 'string',
'constraints': []},
'Controller-1::AdminPassword': {
'description': 'Admin password',
'hidden': True,
'name': 'Controller-1::AdminPassword',
'value': 'unset',
'default': '',
'label': 'Admin Password'},
'label': 'Admin Password',
'parameter_type': 'string',
'constraints': []},
'Controller-1::AdminToken': {
'description': 'Admin Token',
'hidden': True,
'name': 'Controller-1::AdminToken',
'value': '',
'default': '',
'label': 'Admin Token'},
'label': 'Admin Token',
'parameter_type': 'string',
'constraints': []},
'Compute-1::SnmpdReadonlyUserPassword': {
'description': 'Snmpd password',
'hidden': True,
'name': 'Compute-1::SnmpdReadonlyUserPassword',
'value': 'unset',
'default': '',
'label': 'Snmpd password'}})
'label': 'Snmpd password',
'parameter_type': 'string',
'constraints': []}})
mock_parameter_list.assert_called_once_with()
@ -194,28 +204,36 @@ class TuskarAPITests(test.APITestCase):
'name': 'Compute-1::SnmpdReadonlyUserPassword',
'value': 'unset',
'default': '',
'label': 'Snmpd password'},
'label': 'Snmpd password',
'parameter_type': 'string',
'constraints': []},
'KeystoneCACertificate': {
'description': 'Keystone CA CertificateAdmin',
'hidden': True,
'name': 'Controller-1::KeystoneCACertificate',
'value': 'unset',
'default': '',
'label': 'Keystone CA CertificateAdmin'},
'label': 'Keystone CA CertificateAdmin',
'parameter_type': 'string',
'constraints': []},
'AdminToken': {
'description': 'Admin Token',
'hidden': True,
'name': 'Controller-1::AdminToken',
'value': '',
'default': '',
'label': 'Admin Token'},
'label': 'Admin Token',
'parameter_type': 'string',
'constraints': []},
'AdminPassword': {
'description': 'Admin password',
'hidden': True,
'name': 'Controller-1::AdminPassword',
'value': 'unset',
'default': '',
'label': 'Admin Password'}})
'label': 'Admin Password',
'parameter_type': 'string',
'constraints': []}})
mock_parameter_list.assert_called_once_with()
@ -271,25 +289,33 @@ class TuskarAPITests(test.APITestCase):
'name': 'Compute-1::SnmpdReadonlyUserPassword',
'value': 'unset',
'default': '',
'label': 'Snmpd password'},
'label': 'Snmpd password',
'parameter_type': 'string',
'constraints': []},
'KeystoneCACertificate': {
'description': 'Keystone CA CertificateAdmin',
'hidden': True, 'name':
'Controller-1::KeystoneCACertificate',
'value': 'unset',
'default': '',
'label': 'Keystone CA CertificateAdmin'},
'label': 'Keystone CA CertificateAdmin',
'parameter_type': 'string',
'constraints': []},
'AdminToken': {
'description': 'Admin Token',
'hidden': True,
'name': 'Controller-1::AdminToken',
'value': '',
'default': '',
'label': 'Admin Token'},
'label': 'Admin Token',
'parameter_type': 'string',
'constraints': []},
'AdminPassword': {
'description': 'Admin password',
'hidden': True,
'name': 'Controller-1::AdminPassword',
'value': 'unset',
'default': '',
'label': 'Admin Password'}})
'label': 'Admin Password',
'parameter_type': 'string',
'constraints': []}})

View File

@ -55,6 +55,8 @@ def data(TEST):
'hidden': False,
'default': '',
'value': 1,
'parameter_type': 'string',
'constraints': [],
}, {
'name': 'Compute-1::count',
'label': 'Compute Node Count',
@ -62,6 +64,8 @@ def data(TEST):
'hidden': False,
'default': '',
'value': 42,
'parameter_type': 'string',
'constraints': [],
}, {
'name': 'Block Storage-1::count',
'label': 'Block Sorage Node Count',
@ -69,6 +73,8 @@ def data(TEST):
'hidden': False,
'default': '',
'value': 5,
'parameter_type': 'string',
'constraints': [],
}, {
'name': 'Controller-1::Flavor',
'label': 'Controller Flavor',
@ -76,6 +82,8 @@ def data(TEST):
'hidden': False,
'default': '',
'value': 'flavor-1',
'parameter_type': 'string',
'constraints': [],
}, {
'name': 'Compute-1::Flavor',
'label': 'Compute Flavor',
@ -83,6 +91,8 @@ def data(TEST):
'hidden': False,
'default': '',
'value': 'flavor-1',
'parameter_type': 'string',
'constraints': [],
}, {
'name': 'Block Storage-1::Flavor',
'label': 'Block Storage Flavor',
@ -90,6 +100,8 @@ def data(TEST):
'hidden': False,
'default': '',
'value': 'flavor-2',
'parameter_type': 'string',
'constraints': [],
}, {
'name': 'Controller-1::Image',
'label': 'Controller Image ID',
@ -97,6 +109,8 @@ def data(TEST):
'hidden': False,
'default': '',
'value': '2',
'parameter_type': 'string',
'constraints': [],
}, {
'name': 'Compute-1::Image',
'label': 'Compute Image ID',
@ -104,6 +118,8 @@ def data(TEST):
'hidden': False,
'default': '',
'value': '1',
'parameter_type': 'string',
'constraints': [],
}, {
'name': 'Block Storage-1::Image',
'label': 'Block Storage Image ID',
@ -111,6 +127,8 @@ def data(TEST):
'hidden': False,
'default': '',
'value': '4',
'parameter_type': 'string',
'constraints': [],
}, {
'name': 'Controller-1::KeystoneCACertificate',
'label': 'Keystone CA CertificateAdmin',
@ -118,6 +136,8 @@ def data(TEST):
'hidden': True,
'default': '',
'value': 'unset',
'parameter_type': 'string',
'constraints': [],
}, {
'name': 'Controller-1::AdminPassword',
'label': 'Admin Password',
@ -125,6 +145,8 @@ def data(TEST):
'hidden': True,
'default': '',
'value': 'unset',
'parameter_type': 'string',
'constraints': [],
}, {
'name': 'Controller-1::AdminToken',
'label': 'Admin Token',
@ -132,6 +154,8 @@ def data(TEST):
'hidden': True,
'default': '',
'value': '',
'parameter_type': 'string',
'constraints': [],
}, {
'name': 'Controller-1::SnmpdReadonlyUserPassword',
'label': 'Snmpd password',
@ -139,6 +163,8 @@ def data(TEST):
'hidden': True,
'default': '',
'value': '',
'parameter_type': 'string',
'constraints': [],
}, {
'name': 'Compute-1::SnmpdReadonlyUserPassword',
'label': 'Snmpd password',
@ -146,6 +172,8 @@ def data(TEST):
'hidden': True,
'default': '',
'value': 'unset',
'parameter_type': 'string',
'constraints': [],
}, {
'name': 'Controller-1::ExtraConfig',
'label': 'Extra Config',
@ -153,6 +181,8 @@ def data(TEST):
'hidden': False,
'default': '',
'value': '{}',
'parameter_type': 'string',
'constraints': [],
}, {
'name': 'Compute-1::ExtraConfig',
'label': 'Extra Config',
@ -160,6 +190,8 @@ def data(TEST):
'hidden': False,
'default': '',
'value': '{}',
'parameter_type': 'string',
'constraints': [],
}, {
'name': 'Block Storage-1::ExtraConfig',
'label': 'Extra Config',
@ -167,6 +199,8 @@ def data(TEST):
'hidden': False,
'default': '',
'value': '{}',
'parameter_type': 'string',
'constraints': [],
}, {
'name': 'Object Storage-1::ExtraConfig',
'label': 'Extra Config',
@ -174,6 +208,8 @@ def data(TEST):
'hidden': False,
'default': '',
'value': '{}',
'parameter_type': 'string',
'constraints': [],
}],
})
TEST.tuskarclient_plans.add(plan_1)