support 'tag-key' instance type
This commit is contained in:
		
							parent
							
								
									8095f31772
								
							
						
					
					
						commit
						53ec30e3ba
					
				| @ -310,7 +310,9 @@ def get_object_value(obj, attr): | |||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| def is_tag_filter(filter_name): | def is_tag_filter(filter_name): | ||||||
|     return filter_name.startswith('tag:') |     return (filter_name.startswith('tag:') or  | ||||||
|  |             filter_name.startswith('tag-value') or | ||||||
|  |             filter_name.startswith('tag-key')) | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| def get_obj_tag(obj, filter_name): | def get_obj_tag(obj, filter_name): | ||||||
| @ -318,10 +320,24 @@ def get_obj_tag(obj, filter_name): | |||||||
|     tags = dict((tag['key'], tag['value']) for tag in obj.get_tags()) |     tags = dict((tag['key'], tag['value']) for tag in obj.get_tags()) | ||||||
|     return tags.get(tag_name) |     return tags.get(tag_name) | ||||||
| 
 | 
 | ||||||
|  | def get_obj_tag_names(obj): | ||||||
|  |     tags = set((tag['key'] for tag in obj.get_tags())) | ||||||
|  |     return tags | ||||||
|  | 
 | ||||||
|  | def get_obj_tag_values(obj): | ||||||
|  |     tags = set((tag['value'] for tag in obj.get_tags())) | ||||||
|  |     return tags | ||||||
| 
 | 
 | ||||||
| def tag_filter_matches(obj, filter_name, filter_values): | def tag_filter_matches(obj, filter_name, filter_values): | ||||||
|     tag_value = get_obj_tag(obj, filter_name) |     if filter_name == 'tag-key': | ||||||
|     return tag_value in filter_values |         tag_names = get_obj_tag_names(obj) | ||||||
|  |         return len(set(filter_values).intersection(tag_names)) > 0 | ||||||
|  |     elif filter_name == 'tag-value': | ||||||
|  |         tag_values = get_obj_tag_values(obj) | ||||||
|  |         return len(set(filter_values).intersection(tag_values)) > 0 | ||||||
|  |     else: | ||||||
|  |         tag_value = get_obj_tag(obj, filter_name) | ||||||
|  |         return tag_value in filter_values | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| filter_dict_attribute_mapping = { | filter_dict_attribute_mapping = { | ||||||
| @ -331,7 +347,7 @@ filter_dict_attribute_mapping = { | |||||||
|     'source-dest-check': 'source_dest_check', |     'source-dest-check': 'source_dest_check', | ||||||
|     'vpc-id': 'vpc_id', |     'vpc-id': 'vpc_id', | ||||||
|     'group-id': 'security_groups', |     'group-id': 'security_groups', | ||||||
|     'instance.group-id': 'security_groups' |     'instance.group-id': 'security_groups', | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -240,6 +240,42 @@ def test_get_instances_filtering_by_tag(): | |||||||
|     reservations[0].instances[0].id.should.equal(instance1.id) |     reservations[0].instances[0].id.should.equal(instance1.id) | ||||||
|     reservations[0].instances[1].id.should.equal(instance3.id) |     reservations[0].instances[1].id.should.equal(instance3.id) | ||||||
| 
 | 
 | ||||||
|  | @mock_ec2 | ||||||
|  | def test_get_instances_filtering_by_tag_name(): | ||||||
|  |     conn = boto.connect_ec2() | ||||||
|  |     reservation = conn.run_instances('ami-1234abcd', min_count=3) | ||||||
|  |     instance1, instance2, instance3 = reservation.instances | ||||||
|  |     instance1.add_tag('tag1') | ||||||
|  |     instance1.add_tag('tag2') | ||||||
|  |     instance2.add_tag('tag1') | ||||||
|  |     instance2.add_tag('tag2X') | ||||||
|  |     instance3.add_tag('tag3') | ||||||
|  | 
 | ||||||
|  |     reservations = conn.get_all_instances(filters={'tag-key' : 'tagX'}) | ||||||
|  |     # get_all_instances should return no instances | ||||||
|  |     reservations.should.have.length_of(0) | ||||||
|  | 
 | ||||||
|  |     reservations = conn.get_all_instances(filters={'tag-key' : 'tag1'}) | ||||||
|  |     # get_all_instances should return both instances with this tag value | ||||||
|  |     reservations.should.have.length_of(1) | ||||||
|  |     reservations[0].instances.should.have.length_of(2) | ||||||
|  |     reservations[0].instances[0].id.should.equal(instance1.id) | ||||||
|  |     reservations[0].instances[1].id.should.equal(instance2.id) | ||||||
|  | 
 | ||||||
|  |     reservations = conn.get_all_instances(filters={'tag-key' : 'tag1', 'tag-key' : 'tag2'}) | ||||||
|  |     # get_all_instances should return the instance with both tag values | ||||||
|  |     reservations.should.have.length_of(1) | ||||||
|  |     reservations[0].instances.should.have.length_of(1) | ||||||
|  |     reservations[0].instances[0].id.should.equal(instance1.id) | ||||||
|  | 
 | ||||||
|  |     reservations = conn.get_all_instances(filters={'tag-key' : ['tag1', 'tag3']}) | ||||||
|  |     # get_all_instances should return both instances with one of the acceptable tag values | ||||||
|  |     reservations.should.have.length_of(1) | ||||||
|  |     reservations[0].instances.should.have.length_of(3) | ||||||
|  |     reservations[0].instances[0].id.should.equal(instance1.id) | ||||||
|  |     reservations[0].instances[1].id.should.equal(instance2.id) | ||||||
|  |     reservations[0].instances[2].id.should.equal(instance3.id) | ||||||
|  | 
 | ||||||
| @mock_ec2 | @mock_ec2 | ||||||
| def test_instance_start_and_stop(): | def test_instance_start_and_stop(): | ||||||
|     conn = boto.connect_ec2('the_key', 'the_secret') |     conn = boto.connect_ec2('the_key', 'the_secret') | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user