Package pulp :: Package server :: Package webservices :: Package controllers :: Module consumers
[hide private]
[frames] | no frames]

Source Code for Module pulp.server.webservices.controllers.consumers

  1  #!/usr/bin/env python 
  2  # -*- coding: utf-8 -*- 
  3  # 
  4  # Copyright © 2010 Red Hat, Inc. 
  5  # 
  6  # This software is licensed to you under the GNU General Public License, 
  7  # version 2 (GPLv2). There is NO WARRANTY for this software, express or 
  8  # implied, including the implied warranties of MERCHANTABILITY or FITNESS 
  9  # FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2 
 10  # along with this software; if not, see 
 11  # http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. 
 12  # 
 13  # Red Hat trademarks are not licensed under GPLv2. No permission is 
 14  # granted to use or replicate Red Hat trademarks that are incorporated 
 15  # in this software or its documentation. 
 16   
 17  import datetime 
 18  import itertools 
 19  import logging 
 20   
 21  import web 
 22   
 23  from pulp.server.api.consumer import ConsumerApi 
 24  from pulp.server.api.consumer_history import ConsumerHistoryApi, SORT_DESCENDING 
 25  from pulp.server.webservices import http 
 26  from pulp.server.webservices import mongo 
 27  from pulp.server.webservices.controllers.base import JSONController 
 28  from pulp.server.webservices.role_check import RoleCheck 
 29   
 30  # globals --------------------------------------------------------------------- 
 31   
 32  consumer_api = ConsumerApi() 
 33  history_api = ConsumerHistoryApi() 
 34  log = logging.getLogger('pulp') 
 35   
 36  # default fields for consumers being sent to a client 
 37  default_fields = ['id', 'description'] 
38 39 # controllers ----------------------------------------------------------------- 40 41 -class Consumers(JSONController):
42 43 @JSONController.error_handler 44 @RoleCheck(admin=True)
45 - def GET(self):
46 """ 47 List all available consumers. 48 @return: a list of all consumers 49 """ 50 valid_filters = ('id', 'package_name') 51 filters = self.filters(valid_filters) 52 package_names = filters.pop('package_name', None) 53 if package_names is not None: 54 result = consumer_api.consumers_with_package_names(package_names, default_fields) 55 consumers = self.filter_results(result, filters) 56 else: 57 spec = mongo.filters_to_re_spec(filters) 58 consumers = consumer_api.consumers(spec, default_fields) 59 # add the uri ref and deferred fields 60 for c in consumers: 61 c['uri_ref'] = http.extend_uri_path(c['id']) 62 for f in ConsumerDeferredFields.exposed_fields: 63 c[f] = http.extend_uri_path('/'.join((c['id'], f))) 64 return self.ok(consumers)
65 66 @JSONController.error_handler 67 @RoleCheck(admin=True)
68 - def PUT(self):
69 """ 70 Create a new consumer. 71 @return: consumer meta data on successful creation of consumer 72 """ 73 consumer_data = self.params() 74 id = consumer_data['id'] 75 consumer = consumer_api.consumer(id) 76 if consumer is not None: 77 return self.conflict('Consumer with id: %s, already exists' % id) 78 consumer = consumer_api.create(id, consumer_data['description']) 79 path = http.extend_uri_path(consumer.id) 80 return self.created(path, consumer)
81 82 @JSONController.error_handler 83 @RoleCheck(admin=True)
84 - def DELETE(self):
85 """ 86 Delete all consumers. 87 @return: True on successful deletion of all consumers 88 """ 89 consumer_api.clean() 90 return self.ok(True)
91
92 93 -class Bulk(JSONController):
94 # XXX this class breaks the restful practices.... (need a better solution) 95 @JSONController.error_handler 96 @RoleCheck(admin=True)
97 - def POST(self):
98 consumer_api.bulkcreate(self.params()) 99 return self.ok(True)
100
101 102 -class Consumer(JSONController):
103 104 @JSONController.error_handler 105 @RoleCheck(consumer_id=True, admin=True)
106 - def GET(self, id):
107 """ 108 Get a consumer's meta data. 109 @param id: consumer id 110 @return: consumer meta data 111 """ 112 consumer = consumer_api.consumer(id, fields=default_fields) 113 if consumer is None: 114 return self.not_found('No consumer %s' % id) 115 consumer['uri_ref'] = http.uri_path() 116 for field in ConsumerDeferredFields.exposed_fields: 117 consumer[field] = http.extend_uri_path(field) 118 return self.ok(consumer)
119 120 @JSONController.error_handler 121 @RoleCheck(admin=True)
122 - def PUT(self, id):
123 """ 124 Update consumer 125 @param id: The consumer id 126 @type id: str 127 """ 128 log.debug("PUT called.") 129 consumer_data = self.params() 130 if id != consumer_data['id']: 131 return self.bad_request('Cannot change the consumer id') 132 # remove the deferred fields as they are not manipulated via this method 133 for field in itertools.chain(['uri_ref'], # web services only field 134 ConsumerDeferredFields.exposed_fields): 135 consumer_data.pop(field, None) 136 consumer_api.update(consumer_data) 137 return self.ok(True)
138 139 @JSONController.error_handler 140 @RoleCheck(consumer_id=True, admin=True)
141 - def DELETE(self, id):
142 """ 143 Delete a consumer. 144 @param id: consumer id 145 @return: True on successful deletion of consumer 146 """ 147 consumer_api.delete(id=id) 148 return self.ok(True)
149
150 151 -class ConsumerDeferredFields(JSONController):
152 153 # NOTE the intersection of exposed_fields and exposed_actions must be empty 154 exposed_fields = ( 155 'package_profile', 156 'repoids', 157 'certificate' 158 ) 159 @RoleCheck(consumer_id=True, admin=True)
160 - def package_profile(self, id):
161 """ 162 Get a consumer's set of packages 163 @param id: consumer id 164 @return: consumer's installed packages 165 """ 166 valid_filters = ('name', 'arch') 167 filters = self.filters(valid_filters) 168 packages = consumer_api.packages(id) 169 packages = self.filter_results(packages, filters) 170 return self.ok(packages)
171 172 @RoleCheck(consumer_id=True, admin=True)
173 - def repoids(self, id):
174 """ 175 Get the ids of the repositories the consumer is bound to. 176 @type id: str 177 @param id: consumer id 178 @return: dict of repository id: uri reference 179 """ 180 valid_filters = ('id') 181 filters = self.filters(valid_filters) 182 consumer = consumer_api.consumer(id, fields=['repoids']) 183 repoids = self.filter_results(consumer['repoids'], filters) 184 repo_data = dict((id, '/repositories/%s/' % id) for id in repoids) 185 return self.ok(repo_data)
186 187 @RoleCheck(admin=True)
188 - def certificate(self, id):
189 """ 190 Get a X509 Certificate for this Consumer. Useful for uniquely and securely 191 identifying this Consumer later. 192 @type id: str ID of the Consumer 193 @param id: consumer id 194 @return: X509 PEM Certificate 195 """ 196 valid_filters = ('id') 197 filters = self.filters(valid_filters) 198 private_key, certificate = consumer_api.certificate(id) 199 certificate = {'certificate': certificate, 'private_key': private_key} 200 return self.ok(certificate)
201 202 @JSONController.error_handler
203 - def GET(self, id, field_name):
204 """ 205 Deferred field dispatcher. 206 """ 207 field = getattr(self, field_name, None) 208 if field is None: 209 return self.internal_server_error('No implementation for %s found' % field_name) 210 return field(id)
211
212 213 -class ConsumerActions(JSONController):
214 215 # See pulp.webservices.repositories.RepositoryActions for design 216 217 # NOTE the intersection of exposed_actions and exposed_fields must be empty 218 exposed_actions = ( 219 'bind', 220 'unbind', 221 'profile', 222 'installpackages', 223 'installpackagegroups', 224 'listerrata', 225 'installerrata', 226 'history', 227 ) 228 229 @RoleCheck(consumer_id=True, admin=True)
230 - def bind(self, id):
231 """ 232 Bind (subscribe) a user to a repository. 233 @param id: consumer id 234 """ 235 data = self.params() 236 consumer_api.bind(id, data) 237 return self.ok(True)
238 239 @RoleCheck(consumer_id=True, admin=True)
240 - def unbind(self, id):
241 """ 242 Unbind (unsubscribe) a user to a repository. 243 @param id: consumer id 244 """ 245 data = self.params() 246 consumer_api.unbind(id, data) 247 return self.ok(None)
248 249 @RoleCheck(consumer_id=True, admin=True)
250 - def profile(self, id):
251 """ 252 update/add Consumer profile information. eg:package, hardware etc 253 """ 254 log.debug("consumers.py profile() with id: %s" % id) 255 consumer_api.profile_update(id, self.params()) 256 return self.ok(True)
257 258 @RoleCheck(consumer_id=True, admin=True)
259 - def installpackages(self, id):
260 """ 261 Install packages. 262 Body contains a list of package names. 263 """ 264 data = self.params() 265 names = data.get('packagenames', []) 266 return self.ok(consumer_api.installpackages(id, names))
267 268 @RoleCheck(consumer_id=True, admin=True)
269 - def installpackagegroups(self, id):
270 """ 271 Install package groups. 272 Body contains a list of package ids. 273 """ 274 data = self.params() 275 ids = data.get('packageids', []) 276 return self.ok(consumer_api.installpackagegroups(id, ids))
277 278 @RoleCheck(consumer_id=True, admin=True)
279 - def installerrata(self, id):
280 """ 281 Install errata 282 Body contains list of errata ids and/or type 283 """ 284 data = self.params() 285 eids = data.get('errataids', []) 286 types = data.get('types', []) 287 return self.ok(consumer_api.installerrata(id, eids, types))
288 289 @JSONController.error_handler 290 @RoleCheck(consumer_id=True, admin=True)
291 - def listerrata(self, id):
292 """ 293 list applicable errata for a given repo. 294 filter by errata type if any 295 """ 296 data = self.params() 297 return self.ok(consumer_api.listerrata(id, data['types']))
298 299 @JSONController.error_handler 300 @RoleCheck(consumer_id=True, admin=True)
301 - def history(self, id):
302 data = self.params() 303 304 event_type = data.get('event_type', None) 305 limit = data.get('limit', None) 306 sort = data.get('sort', None) 307 start_date = data.get('start_date', None) 308 end_date = data.get('end_date', None) 309 310 if sort is None: 311 sort = SORT_DESCENDING 312 313 if limit: 314 limit = int(limit) 315 316 if start_date: 317 start_date = datetime.datetime.strptime(start_date, '%Y-%m-%d') 318 319 if end_date: 320 end_date = datetime.datetime.strptime(end_date, '%Y-%m-%d') 321 322 results = history_api.query(consumer_id=id, event_type=event_type, limit=limit, 323 sort=sort, start_date=start_date, end_date=end_date) 324 return self.ok(results)
325 326 @JSONController.error_handler
327 - def POST(self, id, action_name):
328 """ 329 Consumer action dispatcher 330 @type id: str 331 @param id: controller id 332 @type action_name: str 333 @param action_name: action name 334 """ 335 336 action = getattr(self, action_name, None) 337 log.debug("consumers.py POST. Action: %s" % action_name) 338 if action is None: 339 return self.internal_server_error('No implementation for %s found' % action_name) 340 return action(id)
341 342 # web.py application ---------------------------------------------------------- 343 344 URLS = ( 345 '/$', 'Consumers', 346 '/bulk/$', 'Bulk', 347 '/([^/]+)/$', 'Consumer', 348 349 '/([^/]+)/(%s)/$' % '|'.join(ConsumerDeferredFields.exposed_fields), 350 'ConsumerDeferredFields', 351 352 '/([^/]+)/(%s)/$' % '|'.join(ConsumerActions.exposed_actions), 353 'ConsumerActions', 354 ) 355 356 application = web.application(URLS, globals()) 357