Enable strategy parameters

Strategy provides parameters to customize algorithm behavior. End user
could query then specify parameters for their requirements.

Change-Id: Id097db5f6e79c94b57674c8e5d55b06098abf18c
Implements-bp: optimization-threshold
This commit is contained in:
Edwin Zhai
2016-03-17 01:50:39 +00:00
parent ec64203bee
commit 0b29a8394b
16 changed files with 209 additions and 13 deletions

View File

@@ -60,6 +60,9 @@ class AuditPostType(wtypes.Base):
state = wsme.wsattr(wtypes.text, readonly=True,
default=objects.audit.State.PENDING)
parameters = wtypes.wsattr({wtypes.text: types.jsontype}, mandatory=False,
default={})
def as_audit(self):
audit_type_values = [val.value for val in objects.audit.AuditType]
if self.type not in audit_type_values:
@@ -68,7 +71,9 @@ class AuditPostType(wtypes.Base):
return Audit(
audit_template_id=self.audit_template_uuid,
type=self.type,
deadline=self.deadline)
deadline=self.deadline,
parameters=self.parameters,
)
class AuditPatchType(types.JsonPatchType):
@@ -148,6 +153,9 @@ class Audit(base.APIBase):
mandatory=False)
"""The name of the audit template this audit refers to"""
parameters = {wtypes.text: types.jsontype}
"""The strategy parameters for this audit"""
links = wsme.wsattr([link.Link], readonly=True)
"""A list containing a self link and associated audit links"""
@@ -364,6 +372,25 @@ class AuditsController(rest.RestController):
message=_('The audit template UUID or name specified is '
'invalid'))
audit_template = objects.AuditTemplate.get(pecan.request.context,
audit._audit_template_uuid)
strategy_id = audit_template.strategy_id
no_schema = True
if strategy_id is not None:
# validate parameter when predefined strategy in audit template
strategy = objects.Strategy.get(pecan.request.context, strategy_id)
schema = strategy.parameters_spec
if schema:
# validate input parameter with default value feedback
no_schema = False
utils.DefaultValidatingDraft4Validator(schema).validate(
audit.parameters)
if no_schema and audit.parameters:
raise exception.Invalid(_('Specify parameters but no predefined '
'strategy for audit template, or no '
'parameter spec in predefined strategy'))
audit_dict = audit.as_dict()
context = pecan.request.context
new_audit = objects.Audit(context, **audit_dict)

View File

@@ -112,6 +112,9 @@ class Strategy(base.APIBase):
mandatory=False)
"""The name of the goal this audit refers to"""
parameters_spec = {wtypes.text: types.jsontype}
""" Parameters spec dict"""
def __init__(self, **kwargs):
super(Strategy, self).__init__()
@@ -121,11 +124,14 @@ class Strategy(base.APIBase):
self.fields.append('display_name')
self.fields.append('goal_uuid')
self.fields.append('goal_name')
self.fields.append('parameters_spec')
setattr(self, 'uuid', kwargs.get('uuid', wtypes.Unset))
setattr(self, 'name', kwargs.get('name', wtypes.Unset))
setattr(self, 'display_name', kwargs.get('display_name', wtypes.Unset))
setattr(self, 'goal_uuid', kwargs.get('goal_id', wtypes.Unset))
setattr(self, 'goal_name', kwargs.get('goal_id', wtypes.Unset))
setattr(self, 'parameters_spec', kwargs.get('parameters_spec',
wtypes.Unset))
@staticmethod
def _convert_with_links(strategy, url, expand=True):