Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
# Copyright 2010 Jacob Kaplan-Moss # Copyright 2011 OpenStack LLC. # All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); you may # not use this file except in compliance with the License. You may obtain # a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations # under the License.
Base utilities to build API operation managers and objects on top of. """
# Python 2.4 compat except NameError: def all(iterable): return True not in (not x for x in iterable)
""" Abstracts the common pattern of allowing both an object or an object's ID (UUID) as a parameter when dealing with relationships. """
# Try to return the object's UUID first, if we have a UUID. return obj.uuid
""" Managers interact with a particular type of API (servers, flavors, images, etc.) and provide CRUD operations for them. """
resp, body = self.api.post(url, body=body) else:
# NOTE(ja): keystone returns values as list as {'values': [ ... ]} # unlike other services which just return the list...
return body[response_key]
management=True): "POST": self.api.post, "PATCH": self.api.patch} management=management) else: except KeyError: raise exceptions.ClientException("Invalid update method: %s" % method) # PUT requests may not return a body
""" Like a `Manager`, but with additional `find()`/`findall()` methods. """
def list(self): pass
""" Find a single item with attributes matching ``**kwargs``.
This isn't very efficient: it loads the entire list then filters on the Python side. """ rl = self.findall(**kwargs) num = len(rl)
if num == 0: msg = "No %s matching %s." % (self.resource_class.__name__, kwargs) raise exceptions.NotFound(404, msg) elif num > 1: raise exceptions.NoUniqueMatch else: return rl[0]
""" Find all items with attributes matching ``**kwargs``.
This isn't very efficient: it loads the entire list then filters on the Python side. """ found = [] searches = kwargs.items()
for obj in self.list(): try: if all(getattr(obj, attr) == value for (attr, value) in searches): found.append(obj) except AttributeError: continue
return found
"""Base manager class for manipulating Keystone entities.
Children of this class are expected to define a `collection_key` and `key`.
- `collection_key`: Usually a plural noun by convention (e.g. `entities`); used to refer collections in both URL's (e.g. `/v3/entities`) and JSON objects containing a list of member resources (e.g. `{'entities': [{}, {}, {}]}`). - `key`: Usually a singular noun by convention (e.g. `entity`); used to refer to an individual member of the collection.
"""
"""Builds a resource URL for the given kwargs.
Given an example collection where `collection_key = 'entities'` and `key = 'entity'`, the following URL's could be generated.
By default, the URL will represent a collection of entities, e.g.::
/entities
If kwargs contains an `entity_id`, then the URL will represent a specific member, e.g.::
/entities/{entity_id}
If a `base_url` is provided, the generated URL will be appended to it.
"""
# do we have a specific entity?
# drop null values else: kwargs.pop(key) kwargs['%s_id' % key] = id_value
self.build_url(**kwargs), {self.key: kwargs}, self.key)
self.build_url(**kwargs), self.key)
'%(base_url)s%(query)s' % { 'base_url': self.build_url(base_url=base_url, **kwargs), 'query': '?%s' % urllib.urlencode(kwargs) if kwargs else '', }, self.collection_key)
self.build_url(base_url=base_url, **kwargs), method='PUT')
self.build_url(**kwargs), {self.key: params}, self.key, method='PATCH')
self.build_url(**kwargs))
""" A resource represents a particular instance of an object (tenant, user, etc). This is pretty much just a bag for attributes.
:param manager: Manager object :param info: dictionary representing resource attributes :param loaded: prevent lazy-loading if set to True """
#NOTE(bcwaldon): disallow lazy-loading if already loaded once
else: return self.__dict__[k]
k != 'manager')
# set_loaded() first ... so if we have to bail, we know we tried.
return self.manager.delete(self)
return False
|