cc1  v2.1
CC1 source code docs
 All Classes Namespaces Files Functions Variables Pages
filters.py
Go to the documentation of this file.
1 # -*- coding: utf-8 -*-
2 # @COPYRIGHT_begin
3 #
4 # Copyright [2010-2014] Institute of Nuclear Physics PAN, Krakow, Poland
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 #
18 # @COPYRIGHT_end
19 
20 ##
21 # @package src.ec2.helpers.parse
22 #
23 # @copyright Copyright (c) 2012 Institute of Nuclear Physics PAS <http://www.ifj.edu.pl/>
24 # @author Łukasz Chrząszcz <l.chrzaszcz@gmail.com>
25 #
26 
27 from ec2.error import InvalidFilter
28 from entities import Entity
29 from fnmatch import fnmatch
30 
31 filtersTranslation = {
32  'key-name': 'name',
33  'fingerprint': 'fingerprint',
34  'state': 'state',
35  'description': 'description', # TODO ominac powtarzajace sie wartosci
36  'image-id': 'imageId',
37  'instance-id': 'instanceId',
38  'name': 'name',
39  'status': 'status',
40  'size': 'size'
41  }
42 
43 prefixes = {
44  'instance-id': Entity.instance,
45  'image-id': Entity.image,
46  'reservation-id': Entity.reservation,
47  'volume-id': Entity.volume
48  }
49 
50 
51 ##
52 #
53 # Validates EC2 filters by checking if there is no unsupported filter provided by user
54 # and translates keys to CC1 ones
55 # If there is an extra filter InvalidFilter exception is raised
56 #
57 # @raises{InvalidFilter,EC2Exception}
58 #
59 # @parameter{filters, dict} Dictionary of filters provided by user
60 # @parameter{available_filters, dict} List of filters supported by server
61 #
62 # @returns{boolean} Returns True if filters are valid
63 #
64 def validateEc2Filters(filters, available_filters):
65 
66  for ec2filter in filters.keys():
67  if ec2filter not in available_filters:
68  return False
69 # translatedFilters[ available_filters[ec2filter] ] = filters[ec2filter]
70 
71  return True
72 
73 
74 ##
75 #
76 # Applies EC2 Filters generated by src.restapi.ec2.helpers.parse.parseFilters
77 #
78 # @returns{list} Returns list of filtered objects
79 #
80 def applyEc2Filters(objects, filters):
81  try:
82  for filter_name in filters.keys():
83  # tu by się przydał elegantszy sposób
84  for filter_value in filters[filter_name]:
85  extra_prefix = ""
86  for prefix in prefixes.iteritems(): # TODO TO JEST MASAKRYCZNY SPOSOB...
87  if filter_name == prefix[0]:
88  extra_prefix = prefix[1] + '-'
89  break
90 
91  objects = [item for item in objects if fnmatch(extra_prefix + str(item[filter_name]), filter_value)]
92 # objects = [item for item in objects if str(item[ filtersTranslation[ filter_name ] ]) in filters[ filter_name] ]
93  except KeyError, error:
94  raise InvalidFilter
95 
96  return objects
97