Exemple #1
0
class RGWSubuserManager(object):
    
    def __init__(self, connection):
        self.connection = connection
        self.formatter = JsonFormatter()
        
    def create_subuser(self, user_info):
        d = {k: v for k, v in user_info}
        d['uid'] = user_info.uid
        # find a key
        key = None
        key_type = None
        if user_info.keys:
            key = user_info.keys[0]
            key_type = 's3'
        elif user_info.swift_keys:
            key = user_info.swift_keys[0]
            key_type = 'swift'
        if key:
            d['secret-key'] = key.secret_key
            d['key-type'] = key_type
        text = self.connection.create_subuser(**d)
        l = self.formatter.deserialize(text)
        ul = [RGWSubuserInfo(**x) for x in l or []]
        return ul
    
    def modify_subuser(self, user_info):
        d = {k: v for k, v in user_info}
        d['uid'] = user_info.uid
        # find a key
        key = None
        key_type = None
        if user_info.keys:
            key = user_info.keys[0]
            key_type = 's3'
        elif user_info.swift_keys:
            key = user_info.swift_keys[0]
            key_type = 'swift'
        text = self.connection.modify_subuser(**d)
        l = self.formatter.deserialize(text)
        ul = [RGWSubuserInfo(**x) for x in l or []]
        return ul
    
    def remove_subuser(self, user_info):
        try:
            text = self.connection.remove_subuser(**user_info)
            return True
        except:
            return False
Exemple #2
0
class RGWMetadataManager(object):
    def __init__(self, connection):
        self.connection = connection
        self.formatter = JsonFormatter()

    def get_info(self):
        text = self.connection.get_metadata_info()
        return RGWMetadataInfo.loads(text, self.formatter)

    def get_key(self):
        text = self.connection.get_metadata_keys()
        l = self.formatter.deserialize(text)
        ul = [RGWMetadataKeys(**x) for x in l or []]
        return ul

    def update(self, metadata_info):
        d = {k: v for k, v in bucket_info}
        text = self.connection.metadata_update(**d)
        return RGWMetadataInfo.loads(text, self.formatter)

    def remove(self):
        text = self.connection.remove_metadata()
        return text

    def lock(self, metadata_info):
        d = {k: v for k, v in bucket_info}
        text = self.connection.metadata_lock(**d)
        return text

    def unlock(self, metadata_info):
        d = {k: v for k, v in bucket_info}
        text = self.connection.metadata_unlock(**d)
        return text
Exemple #3
0
 def __init__(self, connection):
     self.connection = connection
     self.formatter = JsonFormatter()
Exemple #4
0
class RGWUserManager(object):
    def __init__(self, connection):
        self.connection = connection
        self.formatter = JsonFormatter()

    def create(self, user_info):
        # NOTE: Do no use dict(user_info), attribute 'keys' in RGWUserInfo causes dict() works incorrectly
        d = {k: v for k, v in user_info}
        d["uid"] = user_info.user_id
        # find a key
        key = None
        key_type = None
        if user_info.keys:
            key = user_info.keys[0]
            key_type = "s3"
        elif user_info.swift_keys:
            key = user_info.swift_keys[0]
            key_type = "swift"
        if key:
            d["access-key"] = key.access_key
            d["secret-key"] = key.secret_key
            d["key-type"] = key_type
        gen_key = False if key else True
        d["gen-key"] = gen_key
        # caps
        user_caps = ";".join(["%s=%s" % (c.type, c.perms) for c in user_info.caps or []])
        d["user-caps"] = user_caps
        # exclusive ?
        d["exclusive"] = False
        text = self.connection.create_user(**d)
        return RGWUserInfo.loads(text, self.formatter)

    def list(self):
        text = self.connection.get_users()
        l = self.formatter.deserialize(text)
        ul = [lazyinit(RGWUserInfo)(user_id=u, manager=self) for u in l or []]
        return ul

    def get(self, user_info):  # pre-version: uid  (liuyb)
        d = {k: v for k, v in user_info}
        d["uid"] = user_info.user_id
        text = self.connection.get_user_info(**d)
        return RGWUserInfo.loads(text, self.formatter)

    def modify(self, user_info):
        # NOTE: Do no use dict(user_info),
        # attribute 'keys' in RGWUserInfo causes dict() works incorrectly
        d = {k: v for k, v in user_info}
        d["uid"] = user_info.user_id
        # find a key
        key = None
        key_type = None
        if user_info.keys:
            key = user_info.keys[0]
            key_type = "s3"
        elif user_info.swift_keys:
            key = user_info.swift_keys[0]
            key_type = "swift"
        if key:
            d["access-key"] = key.access_key
            d["secret-key"] = key.secret_key
            d["key-type"] = key_type
        gen_key = False if key else True
        d["gen-key"] = gen_key
        # caps
        user_caps = ";".join(["%s=%s" % (c.type, c.perms) for c in user_info.caps or []])
        d["user-caps"] = user_caps
        # exclusive ?
        d["exclusive"] = False
        text = self.connection.modify_user(**d)
        return RGWUserInfo.loads(text, self.formatter)

    def remove(self, uid, purge_data=True):
        try:
            text = self.connection.remove_user(uid=uid, purge_data=purge_data)
            return True
        except:
            return False

    def create_key(self, uid, subuser=None, key_type=None, access_key=None, secret_key=None, generate_key=None):
        """
        key_info:
        uid, subuser, key_type, access_key, secret_key, generate_key
        """
        text = self.connection.create_key(
            uid,
            subuser=subuser,
            key_type=key_type,
            access_key=access_key,
            secret_key=secret_key,
            generate_key=generate_key,
        )
        # l = self.formatter.deserialize(text)
        # ul = [RGWAccessKey(**x) for x in l or []]
        return text

    def remove_key(self, access_key, uid=None, subuser=None, key_type=None):
        try:
            text = self.connection.remove_key(access_key, uid=uid, subuser=subuser, key_type=key_type)
            return True
        except:
            return False

    def get_user_quota(self, uid):
        text = self.connection.get_user_quota(uid)
        return text

    def set_user_quota(self, uid, max_size_kb, max_objects=None, enabled=True):
        """
        quota_info = {'uid':ID, 'max_size_kb':SIZE, 'max_objects':SIZE, 'enabled':true}
        """
        d = {}
        d["uid"] = uid
        d["max_size_kb"] = max_size_kb
        d["max_objects"] = max_objects
        d["enabled"] = enabled
        try:
            text = self.connection.set_user_quota(**d)
            return 0
        except:
            return -1

    def get_bucket_quota(self, user_info):
        d = {k: v for k, v in user_info}
        d["uid"] = user_info.user_id
        text = self.connection.get_bucket_quota(**d)
        return RGWQuotaInfo(text)

    def set_bucket_quota(self, user_info):
        d = {k: v for k, v in user_info}
        d["uid"] = user_info.user_id
        quota = user_info.user_quota
        d["max_size_kb"] = quota.max_size_kb
        d["max_objects"] = quota.max_objects
        d["enabled"] = quota.enabled
        try:
            text = self.connection.set_bucket_quota(**d)
            return True
        except:
            return False