def test_add_or_update_single_tag_from_resource_default(self, update_resource_tags): """Verifies we can add a new tag to a VM from values on the VM when values do not exist with default-value """ action = self._get_action( { 'tag': { 'resource': 'doesnotexist', 'default-value': 'default_tag' }, 'value': { 'resource': 'doesnotexist', 'default-value': 'default_value' } }) resource = tools.get_resource(self.existing_tags) action.process([resource]) tags = tools.get_tags_parameter(update_resource_tags) expected_tags = self.existing_tags.copy() expected_tags.update({'default_tag': 'default_value'}) self.assertEqual(tags, expected_tags)
def test_add_or_update_single_tag_from_resource(self, update_resource_tags): """Verifies we can add a new tag to a VM from values on the VM """ action = self._get_action( { 'tag': { 'type': 'resource', 'key': 'name' }, 'value': { 'type': 'resource', 'key': 'type' } }) resource = tools.get_resource(self.existing_tags) action.process([resource]) tags = tools.get_tags_parameter(update_resource_tags) expected_tags = self.existing_tags.copy() expected_tags.update({resource['name']: resource['type']}) self.assertEqual(tags, expected_tags)
def test_add_tags(self, update_resource_tags): resource = tools.get_resource(self.existing_tags) TagHelper.add_tags(None, resource, {}) update_resource_tags.assert_not_called() TagHelper.add_tags(None, resource, {'tag3': 'value3'}) expected_tags = self.existing_tags.copy() expected_tags.update({'tag3': 'value3'}) self.assertEqual(tools.get_tags_parameter(update_resource_tags), expected_tags)
def _test_event(self, event, expected_tag_value, update_resource_tags): action = self._get_action({'tag': 'CreatorEmail', 'update': True}) resource = tools.get_resource(self.existing_tags) action.process(resources=[resource], event=event) tags = tools.get_tags_parameter(update_resource_tags) expected_tags = self.existing_tags.copy() expected_tags.update({'CreatorEmail': expected_tag_value}) self.assertEqual(tags, expected_tags)
def test_remove_tags(self, update_resource_tags): resource = tools.get_resource(self.existing_tags) TagHelper.remove_tags(None, resource, []) update_resource_tags.assert_not_called() TagHelper.remove_tags(None, resource, ['tag3']) update_resource_tags.assert_not_called() TagHelper.remove_tags(None, resource, ['tag2']) expected_tags = {'tag1': 'value1'} self.assertEqual(tools.get_tags_parameter(update_resource_tags), expected_tags)
def test_auto_tag_add_created_date_tag(self, update_resource_tags, _2): """Adds CreatorEmail to a resource group.""" action = self._get_action({'tag': 'CreatedDate', 'days': 10, 'update': True}) resource = tools.get_resource(self.existing_tags) action.process([resource]) tags = tools.get_tags_parameter(update_resource_tags) expected_tags = self.existing_tags.copy() expected_tags.update({'CreatedDate': '05.01.2019'}) self.assertEqual(tags, expected_tags)
def test_auto_tag_user_event_grid_event(self, update_resource_tags): event = {'eventTime': '2019-05-01T15:20:04.8336028Z'} action = self._get_action({'tag': 'CreatedDate', 'update': True}) resource = tools.get_resource(self.existing_tags) action.process(resources=[resource], event=event) tags = tools.get_tags_parameter(update_resource_tags) expected_tags = self.existing_tags.copy() expected_tags.update({'CreatedDate': '05.01.2019'}) self.assertEqual(tags, expected_tags)
def test_auto_tag_add_created_date_tag_custom_format(self, update_resource_tags, _2): """Adds CreatorEmail to a resource group.""" action = self._get_action({'tag': 'CreatedDate', 'format': '%m/%d/%Y'}) resource = tools.get_resource(self.existing_tags) action.process([resource]) tags = tools.get_tags_parameter(update_resource_tags) expected_tags = self.existing_tags.copy() expected_tags.update({'CreatedDate': '05/01/2019'}) self.assertEqual(tags, expected_tags)
def test_remove_single_tag(self, update_resource_tags): """Verifies we can delete a tag without modifying an existing tag on that resource """ action = self._get_action({'tags': ['tag-to-delete']}) tags = self.existing_tags.copy() tags.update({'tag-to-delete': 'value'}) resource = tools.get_resource(tags) action.process([resource]) tags = tools.get_tags_parameter(update_resource_tags) self.assertEqual(tags, self.existing_tags)
def test_add_or_update_single_tag(self, update_resource_tags): """Verifies we can add a new tag to a VM and not modify an existing tag on that resource """ action = self._get_action({'tag': 'tag1', 'value': 'value1'}) resource = tools.get_resource(self.existing_tags) action.process([resource]) tags = tools.get_tags_parameter(update_resource_tags) expected_tags = self.existing_tags.copy() expected_tags.update({'tag1': 'value1'}) self.assertEqual(tags, expected_tags)
def test_add_or_update_tags(self, update_resource_tags): """Adds tags to an empty resource group, then updates one tag and adds a new tag """ action = self._get_action({'tags': {'tag1': 'value1', 'pre-existing-1': 'modified'}}) resource = tools.get_resource(self.existing_tags) action.process([resource]) tags = tools.get_tags_parameter(update_resource_tags) expected_tags = self.existing_tags.copy() expected_tags.update({'tag1': 'value1', 'pre-existing-1': 'modified'}) self.assertEqual(tags, expected_tags)
def test_mark_for_op(self, update_resource_tags): action = self._get_action({'op': 'stop', 'days': self.DAYS}) resource = tools.get_resource(self.existing_tags) action.process([resource]) tags = tools.get_tags_parameter(update_resource_tags) date = (self.get_test_date() + datetime.timedelta(days=self.DAYS)).strftime('%Y/%m/%d') expected_value = TagDelayedAction.default_template.format( op='stop', action_date=date) expected_tags = self.existing_tags.copy() expected_tags.update({'custodian_status': expected_value}) self.assertEqual(tags, expected_tags)
def test_remove_tags(self, update_resource_tags): """Verifies we can delete multiple tags without modifying existing tags. """ action = self._get_action( {'tags': ['tag-to-delete-1', 'tag-to-delete-2']}) tags = self.existing_tags.copy() tags.update({'tag-to-delete-1': 'value1', 'tag-to-delete-2': 'value2'}) resource = tools.get_resource(tags) action.process([resource]) tags = tools.get_tags_parameter(update_resource_tags) self.assertEqual(tags, self.existing_tags)
def test_tag_trim_space_0_removes_all_tags_but_preserve(self, update_resource_tags): """Verifies tag trim removes all other tags but tags listed in preserve """ action = self._get_action({'space': 0, 'preserve': [k for k in self.existing_tags.keys()]}) tags = self.existing_tags.copy() tags.update({'tag-to-trim1': 'value1', 'tag-to-trim2': 'value2', 'tag-to-trim-3': 'value3'}) resource = tools.get_resource(tags) action.process([resource]) tags = tools.get_tags_parameter(update_resource_tags) expected_tags = self.existing_tags.copy() self.assertEqual(tags, expected_tags)
def test_tag_trim_removes_tags_for_space(self, update_resource_tags): """Verifies tag trim removes tags when the space value and number of tags on the resource are greater than the max tag value (15) """ action = self._get_action({'space': 15 - len(self.existing_tags), 'preserve': [k for k in self.existing_tags.keys()]}) tags = self.existing_tags.copy() tags.update({'tag-to-trim1': 'value1', 'tag-to-trim2': 'value2'}) resource = tools.get_resource(tags) action.process([resource]) tags = tools.get_tags_parameter(update_resource_tags) expected_tags = self.existing_tags.copy() self.assertEqual(tags, expected_tags)