Implementation of triggers in policies
Added triggers to policies in topology template and added testcases to validate the same. Change-Id: I6e7b08ab043a031426e5b0f19e6cfaae2e4c1760 Implements: blueprint tosca-policies
This commit is contained in:
parent
fe5086c20c
commit
c8ccc383a9
toscaparser
@ -16,10 +16,13 @@ import logging
|
||||
from toscaparser.common.exception import ExceptionCollector
|
||||
from toscaparser.common.exception import UnknownFieldError
|
||||
from toscaparser.entity_template import EntityTemplate
|
||||
from toscaparser.triggers import Triggers
|
||||
from toscaparser.utils import validateutils
|
||||
|
||||
SECTIONS = (TYPE, METADATA, DESCRIPTION, PROPERTIES, TARGETS) = \
|
||||
('type', 'metadata', 'description', 'properties', 'targets')
|
||||
|
||||
SECTIONS = (TYPE, METADATA, DESCRIPTION, PROPERTIES, TARGETS, TRIGGERS) = \
|
||||
('type', 'metadata', 'description',
|
||||
'properties', 'targets', 'triggers')
|
||||
|
||||
log = logging.getLogger('tosca')
|
||||
|
||||
@ -37,6 +40,7 @@ class Policy(EntityTemplate):
|
||||
validateutils.validate_map(self.meta_data)
|
||||
self.targets_list = targets
|
||||
self.targets_type = targets_type
|
||||
self.triggers = self._triggers(policy.get(TRIGGERS))
|
||||
self._validate_keys()
|
||||
|
||||
@property
|
||||
@ -57,6 +61,14 @@ class Policy(EntityTemplate):
|
||||
def get_targets_list(self):
|
||||
return self.targets_list
|
||||
|
||||
def _triggers(self, triggers):
|
||||
triggerObjs = []
|
||||
if triggers:
|
||||
for name, trigger_tpl in triggers.items():
|
||||
triggersObj = Triggers(name, trigger_tpl)
|
||||
triggerObjs.append(triggersObj)
|
||||
return triggerObjs
|
||||
|
||||
def _validate_keys(self):
|
||||
for key in self.entity_tpl.keys():
|
||||
if key not in SECTIONS:
|
||||
|
@ -54,6 +54,27 @@ topology_template:
|
||||
description: Apply placement policy to servers
|
||||
metadata: { user1: 1001, user2: 1002 }
|
||||
targets: [ my_server_1, my_server_2 ]
|
||||
triggers:
|
||||
resize_compute:
|
||||
description: trigger
|
||||
event_type: tosca.events.resource.utilization
|
||||
schedule:
|
||||
start_time: "2015-05-07T07:00:00Z"
|
||||
end_time: "2015-06-07T07:00:00Z"
|
||||
target_filter:
|
||||
node: master-container
|
||||
requirement: host
|
||||
capability: Container
|
||||
condition:
|
||||
constraint: utilization greater_than 50%
|
||||
period: 60
|
||||
evaluations: 1
|
||||
method: average
|
||||
action:
|
||||
resize: # Operation name
|
||||
inputs:
|
||||
strategy: LEAST_USED
|
||||
implementation: Senlin.webhook()
|
||||
- my_groups_placement:
|
||||
type: mycompany.mytypes.myScalingPolicy
|
||||
targets: [ webserver_group ]
|
||||
|
@ -23,6 +23,7 @@ from toscaparser.relationship_template import RelationshipTemplate
|
||||
from toscaparser.tests.base import TestCase
|
||||
from toscaparser.topology_template import TopologyTemplate
|
||||
from toscaparser.tosca_template import ToscaTemplate
|
||||
from toscaparser.triggers import Triggers
|
||||
from toscaparser.utils.gettextutils import _
|
||||
|
||||
import toscaparser.utils.yamlparser
|
||||
@ -1300,6 +1301,71 @@ custom_types/wordpress.yaml
|
||||
lambda: Policy(name, policies[name], None, None))
|
||||
self.assertEqual(expectedmessage, err.__str__())
|
||||
|
||||
def test_policy_trigger_valid_keyname(self):
|
||||
tpl_snippet = '''
|
||||
triggers:
|
||||
- resize_compute:
|
||||
description: trigger
|
||||
event_type: tosca.events.resource.utilization
|
||||
schedule:
|
||||
start_time: "2015-05-07T07:00:00Z"
|
||||
end_time: "2015-06-07T07:00:00Z"
|
||||
target_filter:
|
||||
node: master-container
|
||||
requirement: host
|
||||
capability: Container
|
||||
condition:
|
||||
constraint: utilization greater_than 50%
|
||||
period: 60
|
||||
evaluations: 1
|
||||
method : average
|
||||
action:
|
||||
resize: # Operation name
|
||||
inputs:
|
||||
strategy: LEAST_USED
|
||||
implementation: Senlin.webhook()
|
||||
'''
|
||||
triggers = (toscaparser.utils.yamlparser.
|
||||
simple_parse(tpl_snippet))['triggers'][0]
|
||||
name = list(triggers.keys())[0]
|
||||
Triggers(name, triggers[name])
|
||||
|
||||
def test_policy_trigger_invalid_keyname(self):
|
||||
tpl_snippet = '''
|
||||
triggers:
|
||||
- resize_compute:
|
||||
description: trigger
|
||||
event_type: tosca.events.resource.utilization
|
||||
schedule:
|
||||
start_time: "2015-05-07T07:00:00Z"
|
||||
end_time: "2015-06-07T07:00:00Z"
|
||||
target_filter1:
|
||||
node: master-container
|
||||
requirement: host
|
||||
capability: Container
|
||||
condition:
|
||||
constraint: utilization greater_than 50%
|
||||
period1: 60
|
||||
evaluations: 1
|
||||
method: average
|
||||
action:
|
||||
resize: # Operation name
|
||||
inputs:
|
||||
strategy: LEAST_USED
|
||||
implementation: Senlin.webhook()
|
||||
'''
|
||||
triggers = (toscaparser.utils.yamlparser.
|
||||
simple_parse(tpl_snippet))['triggers'][0]
|
||||
name = list(triggers.keys())[0]
|
||||
expectedmessage = _(
|
||||
'Triggers "resize_compute" contains unknown field '
|
||||
'"target_filter1". Refer to the definition '
|
||||
'to verify valid values.')
|
||||
err = self.assertRaises(
|
||||
exception.UnknownFieldError,
|
||||
lambda: Triggers(name, triggers[name]))
|
||||
self.assertEqual(expectedmessage, err.__str__())
|
||||
|
||||
def test_policy_missing_required_keyname(self):
|
||||
tpl_snippet = '''
|
||||
policies:
|
||||
|
68
toscaparser/triggers.py
Normal file
68
toscaparser/triggers.py
Normal file
@ -0,0 +1,68 @@
|
||||
# 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 logging
|
||||
|
||||
from toscaparser.common.exception import ExceptionCollector
|
||||
from toscaparser.common.exception import UnknownFieldError
|
||||
from toscaparser.entity_template import EntityTemplate
|
||||
|
||||
SECTIONS = (DESCRIPTION, EVENT, SCHEDULE, TARGET_FILTER, CONDITION, ACTION) = \
|
||||
('description', 'event_type', 'schedule',
|
||||
'target_filter', 'condition', 'action')
|
||||
CONDITION_KEYNAMES = (CONTRAINT, PERIOD, EVALUATIONS, METHOD) = \
|
||||
('constraint', 'period', 'evaluations', 'method')
|
||||
log = logging.getLogger('tosca')
|
||||
|
||||
|
||||
class Triggers(EntityTemplate):
|
||||
|
||||
'''Triggers defined in policies of topology template'''
|
||||
|
||||
def __init__(self, name, trigger_tpl):
|
||||
self.name = name
|
||||
self.trigger_tpl = trigger_tpl
|
||||
self._validate_keys()
|
||||
self._validate_condition()
|
||||
|
||||
def get_description(self):
|
||||
return self.trigger_tpl['description']
|
||||
|
||||
def get_event(self):
|
||||
return self.trigger_tpl['event_type']
|
||||
|
||||
def get_schedule(self):
|
||||
return self.trigger_tpl['schedule']
|
||||
|
||||
def get_target_filter(self):
|
||||
return self.trigger_tpl['target_filter']
|
||||
|
||||
def get_condition(self):
|
||||
return self.trigger_tpl['condition']
|
||||
|
||||
def get_action(self):
|
||||
return self.trigger_tpl['action']
|
||||
|
||||
def _validate_keys(self):
|
||||
for key in self.trigger_tpl.keys():
|
||||
if key not in SECTIONS:
|
||||
ExceptionCollector.appendException(
|
||||
UnknownFieldError(what='Triggers "%s"' % self.name,
|
||||
field=key))
|
||||
|
||||
def _validate_condition(self):
|
||||
for key in self.get_condition():
|
||||
if key not in CONDITION_KEYNAMES:
|
||||
ExceptionCollector.appendException(
|
||||
UnknownFieldError(what='Triggers "%s"' % self.name,
|
||||
field=key))
|
Loading…
x
Reference in New Issue
Block a user