2018-10-31 06:13:31 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
import base
|
2021-04-09 13:40:29 +02:00
|
|
|
import v2_swagger_client
|
|
|
|
from v2_swagger_client.rest import ApiException
|
2018-10-31 06:13:31 +01:00
|
|
|
|
2021-04-09 13:40:29 +02:00
|
|
|
|
|
|
|
class User(base.Base, object):
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
super(User, self).__init__(api_type = "users")
|
2018-10-31 06:13:31 +01:00
|
|
|
|
|
|
|
def create_user(self, name=None,
|
2021-04-09 13:40:29 +02:00
|
|
|
email=None, user_password=None, realname=None, expect_status_code=201, **kwargs):
|
2018-10-31 06:13:31 +01:00
|
|
|
if name is None:
|
|
|
|
name = base._random_name("user")
|
|
|
|
if realname is None:
|
|
|
|
realname = base._random_name("realname")
|
|
|
|
if email is None:
|
2021-04-09 13:40:29 +02:00
|
|
|
email = '%s@%s.com' % (realname, "vmware")
|
2018-10-31 06:13:31 +01:00
|
|
|
if user_password is None:
|
|
|
|
user_password = "Harbor12345678"
|
2021-04-09 13:40:29 +02:00
|
|
|
user_req = v2_swagger_client.UserCreationReq(username=name, email=email, password=user_password, realname=realname)
|
2020-09-07 05:33:27 +02:00
|
|
|
try:
|
2021-04-09 13:40:29 +02:00
|
|
|
_, status_code, header = self._get_client(**kwargs).create_user_with_http_info(user_req)
|
2020-09-07 05:33:27 +02:00
|
|
|
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
|
2018-10-31 06:13:31 +01:00
|
|
|
|
2020-09-07 05:33:27 +02:00
|
|
|
def get_users(self, user_name=None, email=None, page=None, page_size=None, expect_status_code=200, **kwargs):
|
2021-04-09 13:40:29 +02:00
|
|
|
query = []
|
2020-08-17 08:51:18 +02:00
|
|
|
if user_name is not None:
|
2021-04-09 13:40:29 +02:00
|
|
|
query.append("username=" + user_name)
|
2018-10-31 06:13:31 +01:00
|
|
|
if email is not None:
|
2021-04-09 13:40:29 +02:00
|
|
|
query.append("email=" + email)
|
|
|
|
|
|
|
|
params = {}
|
|
|
|
if len(query) > 0:
|
|
|
|
params["q"] = ",".join(query)
|
2018-10-31 06:13:31 +01:00
|
|
|
if page is not None:
|
|
|
|
params["page"] = page
|
|
|
|
if page_size is not None:
|
|
|
|
params["page_size"] = page_size
|
2020-09-07 05:33:27 +02:00
|
|
|
try:
|
2021-04-09 13:40:29 +02:00
|
|
|
data, status_code, _ = self._get_client(**kwargs).list_users_with_http_info(**params)
|
2020-09-07 05:33:27 +02:00
|
|
|
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
|
2018-10-31 06:13:31 +01:00
|
|
|
|
2020-08-17 08:51:18 +02:00
|
|
|
def get_user_by_id(self, user_id, **kwargs):
|
2021-04-09 13:40:29 +02:00
|
|
|
data, status_code, _ = self._get_client(**kwargs).get_user_with_http_info(user_id)
|
2018-10-31 06:13:31 +01:00
|
|
|
base._assert_status_code(200, status_code)
|
|
|
|
return data
|
|
|
|
|
2020-09-07 05:33:27 +02:00
|
|
|
def get_user_by_name(self, name, expect_status_code=200, **kwargs):
|
2021-04-09 13:40:29 +02:00
|
|
|
users = self.get_users(user_name=name, expect_status_code=expect_status_code, **kwargs)
|
2020-08-17 08:51:18 +02:00
|
|
|
for user in users:
|
|
|
|
if user.username == name:
|
|
|
|
return user
|
|
|
|
return None
|
|
|
|
|
2018-10-31 06:13:31 +01:00
|
|
|
def get_user_current(self, **kwargs):
|
2021-04-09 13:40:29 +02:00
|
|
|
data, status_code, _ = self._get_client(**kwargs).get_current_user_info_with_http_info()
|
2018-10-31 06:13:31 +01:00
|
|
|
base._assert_status_code(200, status_code)
|
|
|
|
return data
|
|
|
|
|
2021-04-09 13:40:29 +02:00
|
|
|
def delete_user(self, user_id, expect_status_code=200, **kwargs):
|
|
|
|
_, status_code, _ = self._get_client(**kwargs).delete_user_with_http_info(user_id)
|
2018-11-01 11:26:04 +01:00
|
|
|
base._assert_status_code(expect_status_code, status_code)
|
2018-10-31 06:13:31 +01:00
|
|
|
return user_id
|
|
|
|
|
|
|
|
def update_user_pwd(self, user_id, new_password=None, old_password=None, **kwargs):
|
|
|
|
if old_password is None:
|
2021-04-09 13:40:29 +02:00
|
|
|
old_password = ""
|
|
|
|
password = v2_swagger_client.PasswordReq(old_password=old_password, new_password=new_password)
|
|
|
|
_, status_code, _ = self._get_client(**kwargs).update_user_password_with_http_info(user_id, password)
|
2018-10-31 06:13:31 +01:00
|
|
|
base._assert_status_code(200, status_code)
|
|
|
|
return user_id
|
|
|
|
|
2019-08-13 08:02:00 +02:00
|
|
|
def update_user_profile(self, user_id, email=None, realname=None, comment=None, **kwargs):
|
2021-04-09 13:40:29 +02:00
|
|
|
user_profile = v2_swagger_client.UserProfile(email=email, realname=realname, comment=comment)
|
|
|
|
_, status_code, _ = self._get_client(**kwargs).update_user_profile_with_http_info(user_id, user_profile)
|
2018-10-31 06:13:31 +01:00
|
|
|
base._assert_status_code(200, status_code)
|
|
|
|
return user_id
|
|
|
|
|
2019-08-13 08:02:00 +02:00
|
|
|
def update_user_role_as_sysadmin(self, user_id, IsAdmin, **kwargs):
|
2021-04-09 13:40:29 +02:00
|
|
|
sysadmin_flag = v2_swagger_client.UserSysAdminFlag(sysadmin_flag=IsAdmin)
|
|
|
|
_, status_code, _ = self._get_client(**kwargs).set_user_sys_admin_with_http_info(user_id, sysadmin_flag)
|
2018-10-31 06:13:31 +01:00
|
|
|
base._assert_status_code(200, status_code)
|
|
|
|
return user_id
|