def _create_body(self, name, img=None, cont=None, img_format=None, img_name=None): """ Used to create a new task. Since tasks don't have names, the required 'name' parameter is used for the type of task: 'import' or 'export'. """ img = utils.get_id(img) cont = utils.get_name(cont) body = {"type": name} if name == "export": body["input"] = { "image_uuid": img, "receiving_swift_container": cont } else: nm = "%s/%s" % (cont, utils.get_name(img)) body["input"] = { "image_properties": { "name": img_name or img }, "import_from": nm, "import_from_format": img_format or DEFAULT_FORMAT } return body
def test_get_name(self): nm = utils.random_name() class ObjWithName(object): name = nm obj = ObjWithName() self.assertEqual(utils.get_name(obj), nm) self.assertEqual(utils.get_name(obj.name), nm) self.assertRaises(exc.MissingName, utils.get_name, object())
def delete_user(self, user): """ Deletes the specified user. If no user by that name exists, no exception will be raised; instead, nothing at all is done. """ name = utils.get_name(user) self._user_manager.delete(name)
def delete_database(self, name_or_obj): """ Deletes the specified database. If no database by that name exists, no exception will be raised; instead, nothing at all is done. """ name = utils.get_name(name_or_obj) self._database_manager.delete(name)
def _get_db_names(self, dbs, strict=True): """ Accepts a single db (name or object) or a list of dbs, and returns a list of database names. If any of the supplied dbs do not exist, a NoSuchDatabase exception will be raised, unless you pass strict=False. """ dbs = utils.coerce_string_to_list(dbs) db_names = [utils.get_name(db) for db in dbs] if strict: good_dbs = self.instance.list_databases() good_names = [utils.get_name(good_db) for good_db in good_dbs] bad_names = [db_name for db_name in db_names if db_name not in good_names] if bad_names: bad = ", ".join(bad_names) raise exc.NoSuchDatabase("The following database(s) were not " "found: %s" % bad) return db_names
def _create_body(self, name, img=None, cont=None, img_format=None, img_name=None): """ Used to create a new task. Since tasks don't have names, the required 'name' parameter is used for the type of task: 'import' or 'export'. """ img = utils.get_id(img) cont = utils.get_name(cont) body = {"type": name} if name == "export": body["input"] = {"image_uuid": img, "receiving_swift_container": cont} else: nm = "%s/%s" % (cont, utils.get_name(img)) body["input"] = { "image_properties": {"name": img_name or img}, "import_from": nm, "import_from_format": img_format or DEFAULT_FORMAT, } return body
def revoke_user_access(self, user, db_names, strict=True): """ Revokes access to the databases listed in `db_names` for the user. If any of the databases do not exist, a NoSuchDatabase exception will be raised, unless you specify `strict=False` in the call. """ user = utils.get_name(user) db_names = self._get_db_names(db_names, strict=strict) bad_names = [] for db_name in db_names: uri = "/%s/%s/databases/%s" % (self.uri_base, user, db_name) resp, resp_body = self.api.method_delete(uri)
def list_user_access(self, user): """ Returns a list of all database names for which the specified user has access rights. """ user = utils.get_name(user) uri = "/%s/%s/databases" % (self.uri_base, user) try: resp, resp_body = self.api.method_get(uri) except exc.NotFound as e: raise exc.NoSuchDatabaseUser("User '%s' does not exist." % user) dbs = resp_body.get("databases", {}) return [CloudDatabaseDatabase(self, db) for db in dbs]
def change_user_password(self, user, new_pass): """ Changes the password for the user to the supplied value. Returns None upon success; raises PasswordChangeFailed if the call does not complete successfully. """ user = utils.get_name(user) body = {"users": [{"name": user, "password": new_pass}]} uri = "/%s" % self.uri_base resp, resp_body = self.api.method_put(uri, body=body) if resp.status > 299: raise exc.PasswordChangeFailed( "Password for '%s' was not changed" % user) return None
def change_user_password(self, user, new_pass): """ Changes the password for the user to the supplied value. Returns None upon success; raises PasswordChangeFailed if the call does not complete successfully. """ user = utils.get_name(user) body = {"users": [{"name": user, "password": new_pass}]} uri = "/%s" % self.uri_base resp, resp_body = self.api.method_put(uri, body=body) if resp.status > 299: raise exc.PasswordChangeFailed("Password for '%s' was not changed" % user) return None
def grant_user_access(self, user, db_names, strict=True): """ Gives access to the databases listed in `db_names` to the user. You may pass in either a single db or a list of dbs. If any of the databases do not exist, a NoSuchDatabase exception will be raised, unless you specify `strict=False` in the call. """ user = utils.get_name(user) uri = "/%s/%s/databases" % (self.uri_base, user) db_names = self._get_db_names(db_names, strict=strict) dbs = [{"name": db_name} for db_name in db_names] body = {"databases": dbs} try: resp, resp_body = self.api.method_put(uri, body=body) except exc.NotFound as e: raise exc.NoSuchDatabaseUser("User '%s' does not exist." % user)