instance_definition = self.stack.t['Resources'][conf]
# honour the Tags property in the InstanceGroup and AutoScalingGroup
- tags = self.properties.data.get('Tags', [])
- instance_definition['Properties']['Tags'] = tags
-
+ instance_definition['Properties']['Tags'] = self._tags()
return GroupedInstance(name, instance_definition, self.stack)
+ def _tags(self):
+ """
+ Make sure that we add a tag that Ceilometer can pick up.
+ These need to be prepended with 'metering.'.
+ """
+ tags = self.properties.get('Tags') or []
+ for t in tags:
+ if t['Key'].startswith('metering.'):
+ # the user has added one, don't add another.
+ return tags
+ return tags + [{'Key': 'metering.groupname',
+ 'Value': self.FnGetRefId()}]
+
def _instances(self):
'''
Convert the stored instance list into a list of GroupedInstance objects
'Cooldown', 'DesiredCapacity',)
def handle_create(self):
-
if self.properties['DesiredCapacity']:
num_to_create = int(self.properties['DesiredCapacity'])
else:
return result
+ def _tags(self):
+ """Add Identifing Tags to all servers in the group.
+
+ This is so the Dimensions received from cfn-push-stats all include
+ the groupname and stack id.
+ Note: the group name must match what is returned from FnGetRefId
+ """
+ autoscaling_tag = [{'Key': 'AutoScalingGroupName',
+ 'Value': self.FnGetRefId()}]
+ return super(AutoScalingGroup, self)._tags() + autoscaling_tag
+
def FnGetRefId(self):
return unicode(self.name)
dbwr = db_api.watch_rule_get_by_name(self.ctx, 'create_data_test')
self.assertEqual(dbwr.watch_data, [])
+ @utils.wr_delete_after
+ def test_create_watch_data_match(self):
+ rule = {u'EvaluationPeriods': u'1',
+ u'AlarmDescription': u'test alarm',
+ u'Period': u'300',
+ u'ComparisonOperator': u'GreaterThanThreshold',
+ u'Statistic': u'SampleCount',
+ u'Threshold': u'2',
+ u'Dimensions': [{u'Name': 'AutoScalingGroupName',
+ u'Value': 'group_x'}],
+ u'MetricName': u'CreateDataMetric'}
+ self.wr = watchrule.WatchRule(context=self.ctx,
+ watch_name='create_data_test',
+ stack_id=self.stack_id, rule=rule)
+ self.wr.store()
+
+ data = {u'CreateDataMetric': {"Unit": "Counter",
+ "Value": "1",
+ "Dimensions": [{u'AutoScalingGroupName':
+ u'group_x'}]}}
+ self.assertTrue(watchrule.rule_can_use_sample(self.wr, data))
+
+ @utils.wr_delete_after
+ def test_create_watch_data_match_2(self):
+ rule = {u'EvaluationPeriods': u'1',
+ u'AlarmDescription': u'test alarm',
+ u'Period': u'300',
+ u'ComparisonOperator': u'GreaterThanThreshold',
+ u'Statistic': u'SampleCount',
+ u'Threshold': u'2',
+ u'Dimensions': [{u'Name': 'AutoScalingGroupName',
+ u'Value': 'group_x'}],
+ u'MetricName': u'CreateDataMetric'}
+ self.wr = watchrule.WatchRule(context=self.ctx,
+ watch_name='create_data_test',
+ stack_id=self.stack_id, rule=rule)
+ self.wr.store()
+
+ data = {u'not_interesting': {"Unit": "Counter",
+ "Value": "1",
+ "Dimensions": [
+ {u'AutoScalingGroupName':
+ u'group_x'}]},
+ u'CreateDataMetric': {"Unit": "Counter",
+ "Value": "1",
+ "Dimensions": [
+ {u'AutoScalingGroupName':
+ u'group_x'}]}}
+ self.assertTrue(watchrule.rule_can_use_sample(self.wr, data))
+
+ def test_create_watch_data_match_3(self):
+ rule = {u'EvaluationPeriods': u'1',
+ u'AlarmDescription': u'test alarm',
+ u'Period': u'300',
+ u'ComparisonOperator': u'GreaterThanThreshold',
+ u'Statistic': u'SampleCount',
+ u'Threshold': u'2',
+ u'Dimensions': [{u'Name': 'AutoScalingGroupName',
+ u'Value': 'group_x'}],
+ u'MetricName': u'CreateDataMetric'}
+ self.wr = watchrule.WatchRule(context=self.ctx,
+ watch_name='create_data_test',
+ stack_id=self.stack_id, rule=rule)
+ self.wr.store()
+
+ data = {u'CreateDataMetric': {"Unit": "Counter",
+ "Value": "1",
+ "Dimensions": [
+ {u'AutoScalingGroupName':
+ u'not_this'}]},
+ u'CreateDataMetric': {"Unit": "Counter",
+ "Value": "1",
+ "Dimensions": [
+ {u'AutoScalingGroupName':
+ u'group_x'}]}}
+ self.assertTrue(watchrule.rule_can_use_sample(self.wr, data))
+
+ def test_create_watch_data_not_match_metric(self):
+ rule = {u'EvaluationPeriods': u'1',
+ u'AlarmDescription': u'test alarm',
+ u'Period': u'300',
+ u'ComparisonOperator': u'GreaterThanThreshold',
+ u'Statistic': u'SampleCount',
+ u'Threshold': u'2',
+ u'Dimensions': [{u'Name': 'AutoScalingGroupName',
+ u'Value': 'group_x'}],
+ u'MetricName': u'CreateDataMetric'}
+ self.wr = watchrule.WatchRule(context=self.ctx,
+ watch_name='create_data_test',
+ stack_id=self.stack_id, rule=rule)
+ self.wr.store()
+
+ data = {u'not_this': {"Unit": "Counter",
+ "Value": "1",
+ "Dimensions": [
+ {u'AutoScalingGroupName':
+ u'group_x'}]},
+ u'nor_this': {"Unit": "Counter",
+ "Value": "1",
+ "Dimensions": [
+ {u'AutoScalingGroupName':
+ u'group_x'}]}}
+ self.assertFalse(watchrule.rule_can_use_sample(self.wr, data))
+
+ def test_create_watch_data_not_match_dimensions(self):
+ rule = {u'EvaluationPeriods': u'1',
+ u'AlarmDescription': u'test alarm',
+ u'Period': u'300',
+ u'ComparisonOperator': u'GreaterThanThreshold',
+ u'Statistic': u'SampleCount',
+ u'Threshold': u'2',
+ u'Dimensions': [{u'Name': 'AutoScalingGroupName',
+ u'Value': 'group_x'}],
+ u'MetricName': u'CreateDataMetric'}
+ self.wr = watchrule.WatchRule(context=self.ctx,
+ watch_name='create_data_test',
+ stack_id=self.stack_id, rule=rule)
+ self.wr.store()
+
+ data = {u'CreateDataMetric': {"Unit": "Counter",
+ "Value": "1",
+ "Dimensions": [
+ {u'AutoScalingGroupName':
+ u'not_this'}]},
+ u'CreateDataMetric': {"Unit": "Counter",
+ "Value": "1",
+ "Dimensions": [
+ {u'wrong_key':
+ u'group_x'}]}}
+ self.assertFalse(watchrule.rule_can_use_sample(self.wr, data))
+
def test_destroy(self):
rule = {'EvaluationPeriods': '1',
'MetricName': 'test_metric',