本文整理汇总了Python中pyrax.manager.BaseManager.find方法的典型用法代码示例。如果您正苦于以下问题:Python BaseManager.find方法的具体用法?Python BaseManager.find怎么用?Python BaseManager.find使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pyrax.manager.BaseManager
的用法示例。
在下文中一共展示了BaseManager.find方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: CloudDatabaseInstance
# 需要导入模块: from pyrax.manager import BaseManager [as 别名]
# 或者: from pyrax.manager.BaseManager import find [as 别名]
class CloudDatabaseInstance(BaseResource):
"""
This class represents a MySQL instance in the cloud.
"""
def __init__(self, *args, **kwargs):
super(CloudDatabaseInstance, self).__init__(*args, **kwargs)
self._database_manager = BaseManager(self.manager.api,
resource_class=CloudDatabaseDatabase, response_key="database",
uri_base="instances/%s/databases" % self.id)
self._user_manager = BaseManager(self.manager.api,
resource_class=CloudDatabaseUser, response_key="user",
uri_base="instances/%s/users" % self.id)
def list_databases(self):
"""Returns a list of the names of all databases for this instance."""
return self._database_manager.list()
def list_users(self):
"""Returns a list of the names of all users for this instance."""
return self._user_manager.list()
def get_database(self, name):
"""
Finds the database in this instance with the specified name, and
returns a CloudDatabaseDatabase object. If no match is found, a
NoSuchDatabase exception is raised.
"""
try:
return [db for db in self.list_databases()
if db.name == name][0]
except IndexError:
raise exc.NoSuchDatabase("No database by the name '%s' exists." % name)
def get_user(self, name):
"""
Finds the user in this instance with the specified name, and
returns a CloudDatabaseUser object. If no match is found, a
NoSuchDatabaseUser exception is raised.
"""
try:
return [user for user in self.list_users()
if user.name == name][0]
except IndexError:
raise exc.NoSuchDatabaseUser("No user by the name '%s' exists." % name)
def create_database(self, name, character_set=None, collate=None):
"""
Creates a database with the specified name. If a database with
that name already exists, a BadRequest (400) exception will
be raised.
"""
if character_set is None:
character_set = "utf8"
if collate is None:
collate = "utf8_general_ci"
# Note that passing in non-None values is required for the _create_body
# method to distinguish between this and the request to create and instance.
self._database_manager.create(name=name, character_set=character_set,
collate=collate, return_none=True)
# Since the API doesn't return the info for creating the database object, we
# have to do it manually.
return self._database_manager.find(name=name)
def create_user(self, name, password, database_names):
"""
Creates a user with the specified name and password, and gives that
user access to the specified database(s).
If a user with
that name already exists, a BadRequest (400) exception will
be raised.
"""
if not isinstance(database_names, list):
database_names = [database_names]
# The API only accepts names, not DB objects
database_names = [db if isinstance(db, basestring) else db.name
for db in database_names]
# Note that passing in non-None values is required for the create_body
# method to distinguish between this and the request to create and instance.
self._user_manager.create(name=name, password=password,
database_names=database_names, return_none=True)
# Since the API doesn't return the info for creating the user object, we
# have to do it manually.
return self._user_manager.find(name=name)
def _get_name(self, name_or_obj):
"""
For convenience, many methods accept either an object or the name
of the object as a parameter, but need the name to send to the
API. This method handles that conversion.
"""
if isinstance(name_or_obj, basestring):
return name_or_obj
#.........这里部分代码省略.........