def test_get_tags(self): item1_id = fakes.random_ec2_id('fake') item2_id = fakes.random_ec2_id('fake') item3_id = fakes.random_ec2_id('fake1') tag1 = {'item_id': item1_id, 'key': 'key1', 'value': 'val1'} tag2 = {'item_id': item2_id, 'key': 'key2', 'value': 'val2'} tag3 = {'item_id': item3_id, 'key': 'key3', 'value': 'val3'} db_api.add_tags(self.context, [tag1, tag2, tag3]) self.assertThat( db_api.get_tags(self.context), matchers.ListMatches([tag1, tag2, tag3], orderless_lists=True)) self.assertThat( db_api.get_tags(self.context, ('fake', )), matchers.ListMatches([tag1, tag2], orderless_lists=True)) self.assertThat( db_api.get_tags(self.context, ('fake', ), [item1_id, item2_id]), matchers.ListMatches([tag1, tag2], orderless_lists=True)) self.assertThat( db_api.get_tags(self.context, ('fake', ), (item1_id, )), matchers.ListMatches([tag1], orderless_lists=True)) self.assertThat( db_api.get_tags(self.context, ('fake', ), (item3_id, )), matchers.ListMatches([])) self.assertThat( db_api.get_tags(self.context, item_ids=(item1_id, item3_id)), matchers.ListMatches([tag1, tag3], orderless_lists=True)) self.assertThat( db_api.get_tags(self.context, ('fake', 'fake1'), (item2_id, item3_id)), matchers.ListMatches([tag2, tag3], orderless_lists=True))
def test_delete_tags_isolation(self): item_id = fakes.random_ec2_id('fake') tag1 = {'item_id': item_id, 'key': 'key', 'value': 'val1'} db_api.add_tags(self.context, [tag1]) tag2 = {'item_id': item_id, 'key': 'key', 'value': 'val2'} db_api.add_tags(self.other_context, [tag2]) db_api.delete_tags(self.context, item_id) self.assertThat(db_api.get_tags(self.other_context), matchers.ListMatches([tag2]))
def test_delete_tags(self): item1_id = fakes.random_ec2_id('fake') item2_id = fakes.random_ec2_id('fake') item3_id = fakes.random_ec2_id('fake1') tag1_1 = {'item_id': item1_id, 'key': 'key1', 'value': 'val_a'} tag1_2 = {'item_id': item1_id, 'key': 'key2', 'value': 'val_b'} tag2_1 = {'item_id': item2_id, 'key': 'key1', 'value': 'val_c'} tag2_2 = {'item_id': item2_id, 'key': 'key2', 'value': 'val_a'} tag3_1 = {'item_id': item3_id, 'key': 'key1', 'value': 'val_b'} tag3_2 = {'item_id': item3_id, 'key': 'key2', 'value': 'val_d'} db_api.add_tags(self.context, [tag1_1, tag2_1, tag3_1, tag1_2, tag2_2, tag3_2]) def do_check(*tag_list): self.assertThat(db_api.get_tags(self.context), matchers.ListMatches(tag_list, orderless_lists=True)) db_api.add_tags(self.context, [tag1_1, tag2_1, tag3_1, tag1_2, tag2_2, tag3_2]) db_api.delete_tags(self.context, []) do_check(tag1_1, tag1_2, tag2_1, tag2_2, tag3_1, tag3_2) db_api.delete_tags(self.context, [item1_id]) do_check(tag2_1, tag2_2, tag3_1, tag3_2) db_api.delete_tags(self.context, [item1_id, item3_id]) do_check(tag2_1, tag2_2) db_api.delete_tags(self.context, [item1_id, item2_id, item3_id], [{'key': 'key1'}, {'value': 'val_d'}, {'key': 'key2', 'value': 'val_b'}]) do_check(tag2_2)
def test_add_tags_isolation(self): item_id = fakes.random_ec2_id('fake') tag1 = {'item_id': item_id, 'key': 'key1', 'value': 'val1'} tag2 = {'item_id': item_id, 'key': 'key2', 'value': 'val2'} db_api.add_tags(self.context, [tag1, tag2]) db_api.add_tags(self.other_context, [{ 'item_id': item_id, 'key': 'key1', 'value': 'val1_1' }, { 'item_id': item_id, 'key': 'key3', 'value': 'val3' }]) tags = db_api.get_tags(self.context) self.assertThat( tags, matchers.ListMatches([tag1, tag2], orderless_lists=True))
def test_add_tags_isolation(self): item_id = fakes.random_ec2_id('fake') tag1 = {'item_id': item_id, 'key': 'key1', 'value': 'val1'} tag2 = {'item_id': item_id, 'key': 'key2', 'value': 'val2'} db_api.add_tags(self.context, [tag1, tag2]) db_api.add_tags(self.other_context, [{'item_id': item_id, 'key': 'key1', 'value': 'val1_1'}, {'item_id': item_id, 'key': 'key3', 'value': 'val3'}]) tags = db_api.get_tags(self.context) self.assertThat(tags, matchers.ListMatches([tag1, tag2], orderless_lists=True))
def test_add_tags(self): item1_id = fakes.random_ec2_id('fake') item2_id = fakes.random_ec2_id('fake') item3_id = fakes.random_ec2_id('fake') tag1_01 = {'item_id': item1_id, 'key': 'key1', 'value': None} tag1_1 = {'item_id': item1_id, 'key': 'key1', 'value': 'val'} tag1_2 = {'item_id': item1_id, 'key': 'key2', 'value': 'val'} tag1_3 = {'item_id': item1_id, 'key': 'key3', 'value': 'val'} tag2_1 = {'item_id': item2_id, 'key': 'key1', 'value': None} tag2_2 = {'item_id': item2_id, 'key': 'key2', 'value': 'val'} tag3_1 = {'item_id': item3_id, 'key': 'key1', 'value': 'val'} tag3_3 = {'item_id': item3_id, 'key': 'key3', 'value': 'val'} db_api.add_tags(self.context, [tag1_01, tag2_1, tag1_2, tag2_2]) db_api.add_tags(self.context, [tag1_1, tag3_1, tag1_3, tag3_3]) tags = db_api.get_tags(self.context) self.assertThat( tags, matchers.ListMatches( [tag1_1, tag1_2, tag1_3, tag2_1, tag2_2, tag3_1, tag3_3], orderless_lists=True))
def test_delete_tags(self): item1_id = fakes.random_ec2_id('fake') item2_id = fakes.random_ec2_id('fake') item3_id = fakes.random_ec2_id('fake1') tag1_1 = {'item_id': item1_id, 'key': 'key1', 'value': 'val_a'} tag1_2 = {'item_id': item1_id, 'key': 'key2', 'value': 'val_b'} tag2_1 = {'item_id': item2_id, 'key': 'key1', 'value': 'val_c'} tag2_2 = {'item_id': item2_id, 'key': 'key2', 'value': 'val_a'} tag3_1 = {'item_id': item3_id, 'key': 'key1', 'value': 'val_b'} tag3_2 = {'item_id': item3_id, 'key': 'key2', 'value': 'val_d'} db_api.add_tags(self.context, [tag1_1, tag2_1, tag3_1, tag1_2, tag2_2, tag3_2]) def do_check(*tag_list): self.assertThat( db_api.get_tags(self.context), matchers.ListMatches(tag_list, orderless_lists=True)) db_api.add_tags(self.context, [tag1_1, tag2_1, tag3_1, tag1_2, tag2_2, tag3_2]) db_api.delete_tags(self.context, []) do_check(tag1_1, tag1_2, tag2_1, tag2_2, tag3_1, tag3_2) db_api.delete_tags(self.context, [item1_id]) do_check(tag2_1, tag2_2, tag3_1, tag3_2) db_api.delete_tags(self.context, [item1_id, item3_id]) do_check(tag2_1, tag2_2) db_api.delete_tags(self.context, [item1_id, item2_id, item3_id], [{ 'key': 'key1' }, { 'value': 'val_d' }, { 'key': 'key2', 'value': 'val_b' }]) do_check(tag2_2)
def test_get_tags(self): item1_id = fakes.random_ec2_id('fake') item2_id = fakes.random_ec2_id('fake') item3_id = fakes.random_ec2_id('fake1') tag1 = {'item_id': item1_id, 'key': 'key1', 'value': 'val1'} tag2 = {'item_id': item2_id, 'key': 'key2', 'value': 'val2'} tag3 = {'item_id': item3_id, 'key': 'key3', 'value': 'val3'} db_api.add_tags(self.context, [tag1, tag2, tag3]) self.assertThat(db_api.get_tags(self.context), matchers.ListMatches([tag1, tag2, tag3], orderless_lists=True)) self.assertThat(db_api.get_tags(self.context, ('fake',)), matchers.ListMatches([tag1, tag2], orderless_lists=True)) self.assertThat(db_api.get_tags(self.context, ('fake',), [item1_id, item2_id]), matchers.ListMatches([tag1, tag2], orderless_lists=True)) self.assertThat(db_api.get_tags(self.context, ('fake',), (item1_id,)), matchers.ListMatches([tag1], orderless_lists=True)) self.assertThat(db_api.get_tags(self.context, ('fake',), (item3_id,)), matchers.ListMatches([])) self.assertThat(db_api.get_tags(self.context, item_ids=(item1_id, item3_id)), matchers.ListMatches([tag1, tag3], orderless_lists=True)) self.assertThat(db_api.get_tags(self.context, ('fake', 'fake1'), (item2_id, item3_id)), matchers.ListMatches([tag2, tag3], orderless_lists=True))
def test_add_tags(self): item1_id = fakes.random_ec2_id('fake') item2_id = fakes.random_ec2_id('fake') item3_id = fakes.random_ec2_id('fake') tag1_01 = {'item_id': item1_id, 'key': 'key1', 'value': None} tag1_1 = {'item_id': item1_id, 'key': 'key1', 'value': 'val'} tag1_2 = {'item_id': item1_id, 'key': 'key2', 'value': 'val'} tag1_3 = {'item_id': item1_id, 'key': 'key3', 'value': 'val'} tag2_1 = {'item_id': item2_id, 'key': 'key1', 'value': None} tag2_2 = {'item_id': item2_id, 'key': 'key2', 'value': 'val'} tag3_1 = {'item_id': item3_id, 'key': 'key1', 'value': 'val'} tag3_3 = {'item_id': item3_id, 'key': 'key3', 'value': 'val'} db_api.add_tags(self.context, [tag1_01, tag2_1, tag1_2, tag2_2]) db_api.add_tags(self.context, [tag1_1, tag3_1, tag1_3, tag3_3]) tags = db_api.get_tags(self.context) self.assertThat(tags, matchers.ListMatches([tag1_1, tag1_2, tag1_3, tag2_1, tag2_2, tag3_1, tag3_3], orderless_lists=True))
def create_tags(context, resource_id, tag): reason = None for tag_pair in tag: if not tag_pair.get('key'): reason = _('Not empty key must be present') elif len(tag_pair['key']) > 127: reason = _('Tag key exceeds the maximum length of 127 characters') elif tag_pair['key'].startswith('aws:'): reason = _("Tag keys starting with 'aws:' are reserved for " "internal use") elif 'value' not in tag_pair: reason = _('Value must be present') elif len(tag_pair['value']) > 255: reason = _('Tag value exceeds the maximum length of 255 ' 'characters') if reason: raise exception.InvalidParameterValue(parameter='Tag', value=str(tag_pair), reason=reason) for item_id in resource_id: kind = ec2utils.get_ec2_id_kind(item_id) if kind not in RESOURCE_TYPES: raise exception.InvalidID(id=item_id) # NOTE(ft): check items exist (excluding images because AWS allows to # create a tag with any image id) if kind not in ('jmi', 'jri', 'jki'): ec2utils.get_db_item(context, item_id) tags = [ dict(item_id=item_id, key=tag_pair['key'], value=tag_pair['value']) for item_id in resource_id for tag_pair in tag ] db_api.add_tags(context, tags) return True
def create_tags(context, resource_id, tag): reason = None for tag_pair in tag: if not tag_pair.get('key'): reason = _('Not empty key must be present') elif len(tag_pair['key']) > 127: reason = _('Tag key exceeds the maximum length of 127 characters') elif tag_pair['key'].startswith('aws:'): reason = _("Tag keys starting with 'aws:' are reserved for " "internal use") elif 'value' not in tag_pair: reason = _('Value must be present') elif len(tag_pair['value']) > 255: reason = _('Tag value exceeds the maximum length of 255 ' 'characters') if reason: raise exception.InvalidParameterValue( parameter='Tag', value=str(tag_pair), reason=reason) for item_id in resource_id: kind = ec2utils.get_ec2_id_kind(item_id) if kind not in RESOURCE_TYPES: raise exception.InvalidID(id=item_id) # NOTE(ft): check items exist (excluding images because AWS allows to # create a tag with any image id) if kind not in ('ami', 'ari', 'aki'): ec2utils.get_db_item(context, item_id) tags = [dict(item_id=item_id, key=tag_pair['key'], value=tag_pair['value']) for item_id in resource_id for tag_pair in tag] db_api.add_tags(context, tags) return True
def do_check(*tag_list): self.assertThat( db_api.get_tags(self.context), matchers.ListMatches(tag_list, orderless_lists=True)) db_api.add_tags(self.context, [tag1_1, tag2_1, tag3_1, tag1_2, tag2_2, tag3_2])
def do_check(*tag_list): self.assertThat(db_api.get_tags(self.context), matchers.ListMatches(tag_list, orderless_lists=True)) db_api.add_tags(self.context, [tag1_1, tag2_1, tag3_1, tag1_2, tag2_2, tag3_2])