def _unbind(instance_name, app_host): """ Removes app-host from the container CORS headers """ ip = socket.gethostbyname(socket.gethostname()) logger.info('Starting unbind to instance <{}> at unit {}'.format( instance_name, ip)) db_cli = SwiftsuruDBClient() instance = db_cli.get_instance(instance_name) if not instance: return "Instance not found", 404 container = instance.get("container") plan = instance.get("plan") db_plan = db_cli.get_plan(plan) tenant = db_plan.get("tenant") keystone = KeystoneClient(tenant=tenant) try: cors_url = utils.format_cors_url(app_host) client = SwiftClient(keystone) client.unset_cors(container, cors_url) except Exception, err: # TODO: remove user created on Keystone err_msg = 'Fail to set CORS to container on Swift: {}'.format(err) logger.error(err_msg) return "Internal error: Failed to unbind app", 500
def _unbind(instance_name, app_host): """ Removes app-host from the container CORS headers """ ip = socket.gethostbyname(socket.gethostname()) logger.info('Starting unbind to instance <{}> at unit {}'.format(instance_name, ip)) db_cli = SwiftsuruDBClient() instance = db_cli.get_instance(instance_name) if not instance: return "Instance not found", 404 container = instance.get("container") plan = instance.get("plan") db_plan = db_cli.get_plan(plan) tenant = db_plan.get("tenant") keystone = KeystoneClient(tenant=tenant) try: cors_url = utils.format_cors_url(app_host) client = SwiftClient(keystone) client.unset_cors(container, cors_url) except Exception, err: # TODO: remove user created on Keystone err_msg = 'Fail to set CORS to container on Swift: {}'.format(err) logger.error(err_msg) return "Internal error: Failed to unbind app", 500
def test_set_cors_http_request(self, get_auth_mock): b = Bogus() url = b.serve() get_auth_mock.return_value = ("{}/v1/AUTH_user".format(url), "AUTH_t0k3n") with patch("swiftsuru.swift_client.AUTH_URL", new_callable=lambda: url): cli = SwiftClient() cli.set_cors("my_container", "http://localhost") self.assertIn("/v1/AUTH_user/my_container", b.called_paths)
def test_remove_container(self, get_auth_mock): b = Bogus() url = b.serve() get_auth_mock.return_value = ("{}/v1/AUTH_user".format(url), "AUTH_t0k3n") with patch("swiftsuru.swift_client.AUTH_URL", new_callable=lambda: url): cli = SwiftClient() cli.remove_container("my_container", {"X-Container-Meta-something": "some metadata"}) self.assertIn("/v1/AUTH_user/my_container", b.called_paths)
def test_remove_account(self, get_auth_mock): b = Bogus() url = b.serve() get_auth_mock.return_value = ("{}/v1/AUTH_user".format(url), "AUTH_t0k3n") with patch("swiftsuru.swift_client.AUTH_URL", new_callable=lambda: url): cli = SwiftClient() cli.remove_account("MobyDick") self.assertIn("/v1/AUTH_user", Bogus.called_paths)
def test_create_account(self, get_auth_mock): b = Bogus() url = b.serve() get_auth_mock.return_value = ("{}/v1/AUTH_user".format(url), "AUTH_t0k3n") with patch("swiftsuru.swift_client.AUTH_URL", new_callable=lambda: url): cli = SwiftClient() cli.create_account({"X-Account-Meta-Book": "MobyDick", "X-Account-Meta-Subject": "Literature"}) self.assertIn("/v1/AUTH_user", Bogus.called_paths)
def test_set_cors_should_set_url(self, post_container_mock, head_container_mock, get_auth_mock): get_auth_mock.return_value = ("http://somehost/v1/AUTH_user", "AUTH_t0k3n") head_container_mock.return_value = {} cli = SwiftClient() cli.set_cors('mycontainer', 'http://myhost') expected_header = {'X-Container-Meta-Access-Control-Allow-Origin': 'http://myhost'} post_container_mock.assert_called_once_with('mycontainer', expected_header)
def test_get_cors_of_a_container(self, head_container_mock, get_auth_mock): get_auth_mock.return_value = ("http://somehost/v1/AUTH_user", "AUTH_t0k3n") head_container_mock.return_value = { 'x-container-meta-access-control-allow-origin': 'http://somehost' } cli = SwiftClient() computed_cors = cli.get_cors('mycontainer') self.assertEqual('http://somehost', computed_cors)
def test_unset_cors_of_a_container_with_more_than_one_host_on_cors_headers(self, post_container_mock, head_container_mock, get_auth_mock): get_auth_mock.return_value = ("http://somehost/v1/AUTH_user", "AUTH_t0k3n") head_container_mock.return_value = { 'x-container-meta-access-control-allow-origin': 'http://somehost https://otherhost http://thirdhost' } cli = SwiftClient() cli.unset_cors('mycontainer', 'http://somehost') expected_header = {'X-Container-Meta-Access-Control-Allow-Origin': 'https://otherhost http://thirdhost'} post_container_mock.assert_called_once_with('mycontainer', expected_header)
def test_set_cors_appending_url_to_a_pre_existent_cors_header(self, post_container_mock, head_container_mock, get_auth_mock): get_auth_mock.return_value = ("http://somehost/v1/AUTH_user", "AUTH_t0k3n") head_container_mock.return_value = { 'x-container-meta-access-control-allow-origin': 'http://somehost' } cli = SwiftClient() cli.set_cors('mycontainer', 'http://myhost') expected_header = {'X-Container-Meta-Access-Control-Allow-Origin': 'http://somehost http://myhost'} post_container_mock.assert_called_once_with('mycontainer', expected_header)
def test_account_containers(self, get_auth_mock): b = Bogus() handler = ("/v1/AUTH_user?format=json", lambda: ('[{"count": 0, "bytes": 0, "name": "mycontainer"}]', 200)) b.register(handler, method="GET") url = b.serve() get_auth_mock.return_value = ("{}/v1/AUTH_user?format=json".format(url), "AUTH_t0k3n") with patch("swiftsuru.swift_client.AUTH_URL", new_callable=lambda: url): cli = SwiftClient() containers = cli.account_containers() expected = [{"count": 0, "bytes": 0, "name": "mycontainer"}] self.assertListEqual(expected, containers)
def _bind(instance_name, app_host=None): ip = socket.gethostbyname(socket.gethostname()) logger.info('Starting bind to instance <{}> at unit {}'.format( instance_name, ip)) db_cli = SwiftsuruDBClient() instance = db_cli.get_instance(instance_name) if not instance: logger.info('Instance <{}> not found on MongoDB'.format(instance_name)) return "Instance not found", 500 container = instance.get("container") plan = instance.get("plan") db_plan = db_cli.get_plan(plan) tenant = db_plan.get("tenant") log_msg = 'Instance found: container={}, plan={}, tenant={}' logger.info(log_msg.format(container, plan, tenant)) keystone = KeystoneClient(tenant=tenant) endpoints = keystone.get_storage_endpoints() if app_host: try: cors_url = utils.format_cors_url(app_host) client = SwiftClient(keystone) client.set_cors(container, cors_url) log_msg = 'CORS set on <{}> to <{}>' logger.debug(log_msg.format(container, app_host)) except Exception, err: # TODO: remove user created on Keystone err_msg = 'Fail to set CORS to container on Swift: {}'.format(err) logger.error(err_msg) return "Internal error: Failed to bind instance", 500
def _bind(instance_name, app_host=None): ip = socket.gethostbyname(socket.gethostname()) logger.info('Starting bind to instance <{}> at unit {}'.format(instance_name, ip)) db_cli = SwiftsuruDBClient() instance = db_cli.get_instance(instance_name) if not instance: logger.info('Instance <{}> not found on MongoDB'.format(instance_name)) return "Instance not found", 500 container = instance.get("container") plan = instance.get("plan") db_plan = db_cli.get_plan(plan) tenant = db_plan.get("tenant") log_msg = 'Instance found: container={}, plan={}, tenant={}' logger.info(log_msg.format(container, plan, tenant)) keystone = KeystoneClient(tenant=tenant) endpoints = keystone.get_storage_endpoints() if app_host: try: cors_url = utils.format_cors_url(app_host) client = SwiftClient(keystone) client.set_cors(container, cors_url) log_msg = 'CORS set on <{}> to <{}>' logger.debug(log_msg.format(container, app_host)) except Exception, err: # TODO: remove user created on Keystone err_msg = 'Fail to set CORS to container on Swift: {}'.format(err) logger.error(err_msg)
enabled=True) except Exception, err: err_msg = 'Fail to create user on Keystone: {}'.format(err) logger.error(err_msg) return "Internal error: Failed to create instance", 500 container_name = utils.generate_container_name() tenant_user = "******".format(tenant, username) headers = { "X-Container-Write": "{}".format(tenant_user), "X-Container-Read": ".r:*,{}".format(tenant_user) } try: client = SwiftClient(keystone) client.create_container(container_name, headers) # Container created to allow the use of undelete middleware client.create_container('.trash-{}'.format(container_name), headers) except Exception, err: # TODO: remove user created on Keystone err_msg = 'Fail to create container on Swift: {}'.format(err) logger.error(err_msg) return "Internal error: Failed to create instance", 500 try: db_cli.add_instance(name, team, container_name, plan, username, password) except Exception, err: # TODO: remove user created on Keystone
def test_init_should_call_get_token(self, swiftclient): swiftclient.client.Connection.return_value.get_auth.return_value = (self.url, self.token) cli = SwiftClient() swiftclient.client.Connection.return_value.get_token.assert_called_once()
def test_init_should_obtain_token(self, swiftclient): swiftclient.client.Connection.return_value.get_auth.return_value = (self.url, self.token) cli = SwiftClient() calls = [call(authurl='http://127.0.0.1:8080/auth/v1', user='******', key='testing')] swiftclient.client.Connection.assert_has_calls(calls)
def test_init_should_get_client_with_url_and_token(self, swiftclient): swiftclient.client.Connection.return_value.get_auth.return_value = (self.url, self.token) cli = SwiftClient() swiftclient.client.Connection.assert_called_with(preauthurl=self.url, preauthtoken=self.token)