Permalink
Browse files

re-org from_database_args, "full" documentation on Connection.create_…

…instance class
  • Loading branch information...
adregner committed Jul 22, 2012
1 parent b007aa8 commit c7f29cae9119ca1646e48a8557ccb19ee98db34f
Showing with 58 additions and 32 deletions.
  1. +29 −7 clouddb/connection.py
  2. +22 −0 clouddb/helpers.py
  3. +3 −0 clouddb/models/base.py
  4. +4 −25 clouddb/models/instance.py
View
@@ -77,8 +77,32 @@ def get_instance(self, instance_id):
"""
return helpers.get_from_id(self, Instance, instance_id)
- def create_instance(self, name=None, flavor=None, size=None, database=None, user=None, wait=False, instance={}):
- """
+ def create_instance(self, name=None, flavor=None, size=None, databases=None,
+ character_set=None, collate=None, user=None, wait=False, instance={}):
+ """Creates a new instance, optionally with a new database and user pre-made.
+
+ Required parameters:
+ name :: Arbitrary string representation for the new instance.
+
+ flavor :: Flavor (int id, str href or Flavor object) of the instance to be made.
+
+ size :: Size (in GB) of the database data storage volume.
+
+ Optional parameters:
+ databases :: Name of a new database to be created within the new instance.
+
+ user :: User name of a user to be granted access to the new database above.
+
+ wait :: Poll at regular intervals and return only once the instance
+ is finished building on the backend.
+
+ instance :: Dictionary object that will be encoded and sent directaly
+ in the request to the API. See the README file that came with this
+ package for information on its format.
+
+ The API does support creating multiple databases and users at once along
+ with a new instance, however this function does not yet have support for
+ that. Future versions will.
"""
# name
if name is not None:
@@ -96,11 +120,9 @@ def create_instance(self, name=None, flavor=None, size=None, database=None, user
if size is not None and (type(size) == int or size.isdigit()):
instance['volume'] = { 'size': int(size) }
- # initial database
- if type(database) in (str, unicode):
- instance['databases'] = [{"name": database}]
- elif type(database) == dict:
- instance['databases'] = [database]
+ # initial database(s)
+ if databases is not None:
+ instance['databases'] = helpers.form_database_args(databases, character_set, collate)
# initial user
if type(user) == dict:
View
@@ -60,3 +60,25 @@ def poll_for_result(self, path, status, timeout, first_poll, poll_interval):
apiresult = self.client.get(path).values()[0]
return apiresult
+
+def form_database_args(self, name, character_set, collate):
+ """Takes many different forms of arguments and creates a list that the API
+ will like to represent a database or databases.
+ """
+ if type(name) == dict:
+ databases = [databases]
+ elif type(name) in (list, tuple) and len(name) >= 1 and type(name[0]) == dict:
+ databases = list(name)
+ elif type(name) in (list, tuple) and len(name) >= 1 and type(name[0]) in (str, unicode):
+ databases = [{'name': dbname, 'character_set': character_set, 'collate': collate} for dbname in name]
+ elif type(name) in (str, unicode):
+ databases = [{'name': str(name), 'character_set': character_set, 'collate': collate}]
+
+ for n in xrange(len(databases)):
+ self._sanatize_database_name(databases[n]['name'])
+ if 'collate' in databases[n] and databases[n]['collate'] is None:
+ del databases[n]['collate']
+ elif 'character_set' in databases[n] and databases[n]['character_set'] is None:
+ del databases[n]['character_set']
+
+ return databases
View
@@ -48,6 +48,9 @@ def _load_into_self(self, new_data, keys):
if l['rel'] == k:
d[link_key] = l['href']
+ if 'volume' in d:
+ d['size'] = d['volume']['size']
+
self._data.update(d)
@property
View
@@ -61,7 +61,7 @@ def get_user(self, user_id):
def create_database(self, name, character_set=None, collate=None):
"""
"""
- databases = self._form_database_args(name, character_set, collate)
+ databases = helpers.form_database_args(name, character_set, collate)
self.client.post(self.path+'/databases', {'databases': databases})
return True
@@ -71,33 +71,11 @@ def create_user(self, name, password, databases=None, character_set=None, collat
data = {'users': [{'name': name, 'password': password}]}
if databases is not None:
- data['databases'] = self._form_database_args(databases, character_set, collate)
+ data['databases'] = helpers.form_database_args(databases, character_set, collate)
self.client.post(self.path+'/users', data)
return True
- def _form_database_args(self, name, character_set, collate):
- """Takes many different forms of arguments and creates a list that the API
- will like to represent a database or databases.
- """
- if type(name) == dict:
- databases = [databases]
- elif type(name) in (list, tuple) and len(name) >= 1 and type(name[0]) == dict:
- databases = list(name)
- elif type(name) in (list, tuple) and len(name) >= 1 and type(name[0]) in (str, unicode):
- databases = [{'name': dbname, 'character_set': character_set, 'collate': collate} for dbname in name]
- elif type(name) in (str, unicode):
- databases = [{'name': str(name), 'character_set': character_set, 'collate': collate}]
-
- for n in xrange(len(databases)):
- self._sanatize_database_name(databases[n]['name'])
- if 'collate' in databases[n] and databases[n]['collate'] is None:
- del databases[n]['collate']
- elif 'character_set' in databases[n] and databases[n]['character_set'] is None:
- del databases[n]['character_set']
-
- return databases
-
def enable_root(self):
"""Enable root access and return the root user's password
"""
@@ -148,7 +126,8 @@ def grow(self, size):
if self.size > size['size']:
# TODO : proper error
- raise Exception("This instance has a data storage volume of %d GB and cannot be shrunk. (Tried to specify %d GB as new size.)" % (self.size, size['size']))
+ raise Exception("This instance has a data storage volume of %d GB and cannot " + \
+ "be shrunk. (Tried to specify %d GB as new size.)" % (self.size, size['size']))
self.client.post(self.path+'/action', { 'resize': {'volume': size} })
return True

0 comments on commit c7f29ca

Please sign in to comment.