Debian Codegraph @

glanceclient/v2/tasks.py

raw symbols SCIP
1 # Copyright 2013 OpenStack Foundation
2 # Copyright 2013 IBM Corp.
3 # All Rights Reserved.
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License"); you may
6 # not use this file except in compliance with the License. You may obtain
7 # a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 # License for the specific language governing permissions and limitations
15 # under the License.
16
17 import urllib.parse
18
19 from oslo_utils import encodeutils
20 import warlock
21
22 from glanceclient.common import utils
23 from glanceclient.v2 import schemas
24
25 DEFAULT_PAGE_SIZE = 20
26
27 SORT_DIR_VALUES = ('asc', 'desc')
28 SORT_KEY_VALUES = ('id', 'type', 'status')
29
30
31 class Controller(object):
32 def __init__(self, http_client, schema_client):
33 self.http_client = http_client
34 self.schema_client = schema_client
35
36 @utils.memoized_property
37 def model(self):
38 schema = self.schema_client.get('task')
39 return warlock.model_factory(schema.raw(),
40 base_class=schemas.SchemaBasedModel)
41
42 @utils.add_req_id_to_generator()
43 def list(self, **kwargs):
44 """Retrieve a listing of Task objects.
45
46 :param page_size: Number of tasks to request in each paginated request
47 :returns: generator over list of Tasks
48
49 """
50 def paginate(url):
51 resp, body = self.http_client.get(url)
52 for task in body['tasks']:
53 yield task, resp
54 try:
55 next_url = body['next']
56 except KeyError:
57 return
58 else:
59 for task, resp in paginate(next_url):
60 yield task, resp
61
62 filters = kwargs.get('filters', {})
63
64 if not kwargs.get('page_size'):
65 filters['limit'] = DEFAULT_PAGE_SIZE
66 else:
67 filters['limit'] = kwargs['page_size']
68
69 if 'marker' in kwargs:
70 filters['marker'] = kwargs['marker']
71
72 sort_key = kwargs.get('sort_key')
73 if sort_key is not None:
74 if sort_key in SORT_KEY_VALUES:
75 filters['sort_key'] = sort_key
76 else:
77 raise ValueError('sort_key must be one of the following: %s.'
78 % ', '.join(SORT_KEY_VALUES))
79
80 sort_dir = kwargs.get('sort_dir')
81 if sort_dir is not None:
82 if sort_dir in SORT_DIR_VALUES:
83 filters['sort_dir'] = sort_dir
84 else:
85 raise ValueError('sort_dir must be one of the following: %s.'
86 % ', '.join(SORT_DIR_VALUES))
87
88 for param, value in filters.items():
89 if isinstance(value, str):
90 filters[param] = encodeutils.safe_encode(value)
91
92 url = '/v2/tasks?%s' % urllib.parse.urlencode(filters)
93 for task, resp in paginate(url):
94 # NOTE(flwang): remove 'self' for now until we have an elegant
95 # way to pass it into the model constructor without conflict
96 task.pop('self', None)
97 yield self.model(**task), resp
98
99 @utils.add_req_id_to_object()
100 def get(self, task_id):
101 """Get a task based on given task id."""
102 url = '/v2/tasks/%s' % task_id
103 resp, body = self.http_client.get(url)
104 # NOTE(flwang): remove 'self' for now until we have an elegant
105 # way to pass it into the model constructor without conflict
106 body.pop('self', None)
107 return self.model(**body), resp
108
109 @utils.add_req_id_to_object()
110 def create(self, **kwargs):
111 """Create a new task."""
112 url = '/v2/tasks'
113 task = self.model()
114
115 for (key, value) in kwargs.items():
116 try:
117 setattr(task, key, value)
118 except warlock.InvalidOperation as e:
119 raise TypeError(str(e))
120
121 resp, body = self.http_client.post(url, data=task)
122 # NOTE(flwang): remove 'self' for now until we have an elegant
123 # way to pass it into the model constructor without conflict
124 body.pop('self', None)
125 return self.model(**body), resp