def test_success_upload_when_unlocked(self):
     container_name = 'test_fail_upload_when_locked'
     admin_url, admin_token = self.admin.get_auth()
     user_url, user_token = self.user.get_auth()
     swiftclient.post_account(user_url, admin_token, {CONF['header']: "0"})
     self.assertNotRaises(swiftclient.ClientException,
                          self.user.put_container,
                          container_name)
Example #2
0
class Accounts(object):
    """Process Keystone Accounts."""
    def __init__(self):
        self.keystone_cnx = None
        self.container_cls = swsync.containers.Containers()

    def get_swift_auth(self, auth_url, tenant, user, password):
        """Get swift connexion from args."""
        return swiftclient.client.Connection(auth_url,
                                             '%s:%s' % (tenant, user),
                                             password,
                                             auth_version=2).get_auth()

    def get_ks_auth_orig(self):
        """Get keystone cnx from config."""
        orig_auth_url = get_config('auth', 'keystone_origin')
        cfg = get_config('auth', 'keystone_origin_admin_credentials')
        (tenant_name, username, password) = cfg.split(':')

        return keystoneclient.v2_0.client.Client(auth_url=orig_auth_url,
                                                 username=username,
                                                 password=password,
                                                 tenant_name=tenant_name)

    def get_target_tenant_filter(self):
        """Returns a set of target tenants from the tenant_list_file.

        tenant_list_file is defined in the config file or given as a command
        line argument.

        If tenant_list_file is not defined, returns None (an empty filter).

        """
        try:
            tenant_filter_filename = get_config('sync', 'tenant_filter_file')

            with open(tenant_filter_filename) as tenantsfile:
                return {name.strip() for name in tenantsfile.readlines()}
        except ConfigurationError:
            return None

    def account_headers_clean(self, account_headers, to_null=False):
        ret = {}
        for key, value in account_headers.iteritems():
            if key.startswith('x-account-meta'):
                if to_null:
                    value = ''
                ret[key] = value
        return ret

    def sync_account(self, orig_storage_url, orig_token, dest_storage_url,
                     dest_token):
        """Sync a single account with url/tok to dest_url/dest_tok."""
        orig_storage_cnx = swiftclient.http_connection(orig_storage_url)
        dest_storage_cnx = swiftclient.http_connection(dest_storage_url)
        account_id = os.path.basename(orig_storage_url.replace("AUTH_", ''))

        try:
            orig_account_headers, orig_containers = (swiftclient.get_account(
                None,
                orig_token,
                http_conn=orig_storage_cnx,
                full_listing=True))

            dest_account_headers, dest_containers = (swiftclient.get_account(
                None,
                dest_token,
                http_conn=dest_storage_cnx,
                full_listing=True))
        except (swiftclient.client.ClientException), e:
            logging.info("error getting account: %s, %s" %
                         (account_id, e.http_reason))
            return

        self.container_cls.delete_container(dest_storage_cnx, dest_token,
                                            orig_containers, dest_containers)

        do_headers = False
        if len(dest_account_headers) != len(orig_account_headers):
            do_headers = True
        else:
            for k, v in orig_account_headers.iteritems():
                if not k.startswith('x-account-meta'):
                    continue
                if k not in dest_account_headers:
                    do_headers = True
                elif dest_account_headers[k] != v:
                    do_headers = True

        if do_headers:
            orig_metadata_headers = self.account_headers_clean(
                orig_account_headers)
            dest_metadata_headers = self.account_headers_clean(
                dest_account_headers, to_null=True)

            new_headers = dict(dest_metadata_headers.items() +
                               orig_metadata_headers.items())
            try:
                swiftclient.post_account(
                    "",
                    dest_token,
                    new_headers,
                    http_conn=dest_storage_cnx,
                )
                logging.info("HEADER: sync headers: %s" % (account_id))
            except (swiftclient.client.ClientException), e:
                logging.info("ERROR: updating container metadata: %s, %s" %
                             (account_id, e.http_reason))
                # We don't pass on because since the server was busy
                # let's pass it on for the next pass
                return
 def test_success_update_user_locked_header_with_admin_token(self):
     admin_url, admin_token = self.admin.get_auth()
     user_url, user_token = self.user.get_auth()
     swiftclient.post_account(user_url, admin_token, {CONF['header']: "1"})