user.py 4.13 KB
Newer Older
danfengliu's avatar
danfengliu committed
1 2 3 4
# -*- coding: utf-8 -*-

import base
import swagger_client
5
from swagger_client.rest import ApiException
danfengliu's avatar
danfengliu committed
6 7 8 9

class User(base.Base):

    def create_user(self, name=None,
10
        email = None, user_password=None, realname = None, role_id = None, expect_status_code=201, **kwargs):
danfengliu's avatar
danfengliu committed
11 12 13 14 15 16 17 18
        if name is None:
            name = base._random_name("user")
        if realname is None:
            realname = base._random_name("realname")
        if email is None:
            email = '%s@%s.com' % (realname,"vmware")
        if user_password is None:
            user_password = "Harbor12345678"
19 20
        if role_id is None:
            role_id = 0
danfengliu's avatar
danfengliu committed
21 22

        client = self._get_client(**kwargs)
23
        user = swagger_client.User(username = name, email = email, password = user_password, realname = realname, role_id = role_id)
danfengliu's avatar
danfengliu committed
24

25 26 27 28 29 30 31
        try:
            _, status_code, header = client.users_post_with_http_info(user)
        except ApiException as e:
            base._assert_status_code(expect_status_code, e.status)
        else:
            base._assert_status_code(expect_status_code, status_code)
            return base._get_id_from_header(header), name
danfengliu's avatar
danfengliu committed
32

33
    def get_users(self, user_name=None, email=None, page=None, page_size=None, expect_status_code=200, **kwargs):
danfengliu's avatar
danfengliu committed
34 35
        client = self._get_client(**kwargs)
        params={}
danfengliu's avatar
danfengliu committed
36 37
        if user_name is not None:
            params["username"] = user_name
danfengliu's avatar
danfengliu committed
38 39 40 41 42 43
        if email is not None:
            params["email"] = email
        if page is not None:
            params["page"] = page
        if page_size is not None:
            params["page_size"] = page_size
44 45 46 47 48 49 50
        try:
            data, status_code, _ = client.users_get_with_http_info(**params)
        except ApiException as e:
            base._assert_status_code(expect_status_code, e.status)
        else:
            base._assert_status_code(expect_status_code, status_code)
            return data
danfengliu's avatar
danfengliu committed
51

danfengliu's avatar
danfengliu committed
52
    def get_user_by_id(self, user_id, **kwargs):
danfengliu's avatar
danfengliu committed
53 54 55 56 57
        client = self._get_client(**kwargs)
        data, status_code, _ = client.users_user_id_get_with_http_info(user_id)
        base._assert_status_code(200, status_code)
        return data

58 59
    def get_user_by_name(self, name, expect_status_code=200, **kwargs):
        users = self.get_users(user_name=name, expect_status_code=expect_status_code , **kwargs)
danfengliu's avatar
danfengliu committed
60 61 62 63 64
        for user in users:
            if user.username == name:
                return user
        return None

danfengliu's avatar
danfengliu committed
65 66 67 68 69 70 71

    def get_user_current(self, **kwargs):
        client = self._get_client(**kwargs)
        data, status_code, _ = client.users_current_get_with_http_info()
        base._assert_status_code(200, status_code)
        return data

72
    def delete_user(self, user_id, expect_status_code = 200, **kwargs):
danfengliu's avatar
danfengliu committed
73 74
        client = self._get_client(**kwargs)
        _, status_code, _ = client.users_user_id_delete_with_http_info(user_id)
75
        base._assert_status_code(expect_status_code, status_code)
danfengliu's avatar
danfengliu committed
76 77 78 79 80 81 82 83 84 85 86
        return user_id

    def update_user_pwd(self, user_id, new_password=None, old_password=None, **kwargs):
        if old_password is None:
            old_password  = ""
        password = swagger_client.Password(old_password, new_password)
        client = self._get_client(**kwargs)
        _, status_code, _ = client.users_user_id_password_put_with_http_info(user_id, password)
        base._assert_status_code(200, status_code)
        return user_id

87
    def update_user_profile(self, user_id, email=None, realname=None, comment=None, **kwargs):
danfengliu's avatar
danfengliu committed
88 89 90 91 92 93
        client = self._get_client(**kwargs)
        user_rofile = swagger_client.UserProfile(email, realname, comment)
        _, status_code, _ = client.users_user_id_put_with_http_info(user_id, user_rofile)
        base._assert_status_code(200, status_code)
        return user_id

94
    def update_user_role_as_sysadmin(self, user_id, IsAdmin, **kwargs):
danfengliu's avatar
danfengliu committed
95
        client = self._get_client(**kwargs)
96 97
        sysadmin_flag = swagger_client.SysAdminFlag(IsAdmin)
        _, status_code, _ = client.users_user_id_sysadmin_put_with_http_info(user_id, sysadmin_flag)
danfengliu's avatar
danfengliu committed
98 99
        base._assert_status_code(200, status_code)
        return user_id