Skip to content

Instantly share code, notes, and snippets.

@adamhjk
Created July 21, 2010 21:19
Show Gist options
  • Save adamhjk/485158 to your computer and use it in GitHub Desktop.
Save adamhjk/485158 to your computer and use it in GitHub Desktop.
def create_account(self, new_account, new_user, new_password):
"""
Handles the create_account call for developers, used to request
an account be created both on a Swift cluster and in the auth server
database.
This will make ReST requests to the Swift cluster's account servers
to have an account created on its side. The resulting account hash
along with the URL to use to access the account, the account name, the
user name, and the password is recorded in the auth server's database.
The url is constructed now and stored separately to support changing
the configuration file's default_cluster_url for directing new accounts
to a different Swift cluster while still supporting old accounts going
to the Swift clusters they were created on.
:param new_account: The name for the new account
:param new_user: The name for the new user
:param new_password: The password for the new account
:returns: False if the create fails or the user exists, storage url if successful
"""
begin = time()
if not all((new_account, new_user, new_password)):
return False
account_hash = False
with self.get_conn() as conn:
row = conn.execute('''SELECT cfaccount FROM account WHERE
account = ? AND user = ? AND password = ?''',
(new_account, new_user, new_password)).fetchone()
if row:
account_hash = row[0]
if not account_hash:
account_hash = self.add_storage_account()
else:
account_hash = self.add_storage_account(self, account_hash)
if not account_hash:
self.logger.info(
'FAILED create_account(%s, %s, _,) [%.02f]' %
(repr(new_account), repr(new_user), time() - begin))
return False
url = self.default_cluster_url.rstrip('/') + '/' + account_hash
with self.get_conn() as conn:
conn.execute('''INSERT OR REPLACE INTO account
(account, url, cfaccount, user, password)
VALUES (?, ?, ?, ?, ?)''',
(new_account, url, account_hash, new_user, new_password))
conn.commit()
self.logger.info(
'SUCCESS create_account(%s, %s, _) = %s [%.02f]' %
(repr(new_account), repr(new_user), repr(url), time() - begin))
return url
======================================================================
FAIL: test_create_account_already_exists (test.unit.auth.test_server.TestAuthServer)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/adam/swift/test/unit/auth/test_server.py", line 190, in test_create_account_already_exists
self.assertEquals(first_url, second_url)
AssertionError: 'http://127.0.0.1:9000/v1/5323190e-fba6-4dee-a98e-32906b5e359b' != False
-------------------- >> begin captured logging << --------------------
root: INFO: auth SUCCESS create_account('test', 'tester', _) = 'http://127.0.0.1:9000/v1/5323190e-fba6-4dee-a98e-32906b5e359b' [0.00]
root: ERROR: auth ERROR With account server 10.0.0.0:1000/sda (will retry later): :
Traceback (most recent call last):
File "/home/adam/swift/swift/auth/server.py", line 143, in add_storage_account
partition, 'PUT', '/'+account_name, headers)
File "/home/adam/swift/test/unit/auth/test_server.py", line 61, in connect
return FakeConn(code_iter.next())
StopIteration
root: ERROR: auth ERROR With account server 10.0.0.1:1001/sda (will retry later): :
Traceback (most recent call last):
File "/home/adam/swift/swift/auth/server.py", line 143, in add_storage_account
partition, 'PUT', '/'+account_name, headers)
File "/home/adam/swift/test/unit/auth/test_server.py", line 61, in connect
return FakeConn(code_iter.next())
StopIteration
root: ERROR: auth ERROR With account server 10.0.0.2:1002/sda (will retry later): :
Traceback (most recent call last):
File "/home/adam/swift/swift/auth/server.py", line 143, in add_storage_account
partition, 'PUT', '/'+account_name, headers)
File "/home/adam/swift/test/unit/auth/test_server.py", line 61, in connect
return FakeConn(code_iter.next())
StopIteration
root: INFO: auth FAILED create_account('test', 'tester', _,) [0.00]
def test_create_account_already_exists(self):
auth_server.http_connect = fake_http_connect(201, 201, 201)
first_url = self.controller.create_account('test', 'tester', 'testing')
second_url = self.controller.create_account('test', 'tester', 'testily')
self.assertEquals(first_url, second_url)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment