project.py 13.7 KB
Newer Older
1 2 3 4
# -*- coding: utf-8 -*-

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

9 10 11 12 13 14 15 16 17 18 19
def is_member_exist_in_project(members, member_user_name, expected_member_role_id = None):
    result = False
    for member in members:
        if member.entity_name == member_user_name:
            if expected_member_role_id != None:
                if member.role_id == expected_member_role_id:
                    return True
            else:
                return True
    return result

20 21 22 23 24 25
def get_member_id_by_name(members, member_user_name):
    for member in members:
        if member.entity_name == member_user_name:
            return member.id
    return None

26
class Project(base.Base):
27 28 29 30 31 32
    def __init__(self, username=None, password=None):
        kwargs = dict(api_type="projectv2")
        if username and password:
            kwargs["credential"] = base.Credential('basic_auth', username, password)
        super(Project, self).__init__(**kwargs)

33
    def create_project(self, name=None, metadata=None, expect_status_code = 201, expect_response_body = None, **kwargs):
Yan's avatar
Yan committed
34 35
        if name is None:
            name = base._random_name("project")
danfengliu's avatar
danfengliu committed
36 37
        if metadata is None:
            metadata = {}
38
        client = self._get_client(**kwargs)
39 40

        try:
41
            _, status_code, header = client.create_project_with_http_info(v2_swagger_client.ProjectReq(project_name=name, metadata=metadata))
42
        except ApiException as e:
43 44 45 46
            base._assert_status_code(expect_status_code, e.status)
            if expect_response_body is not None:
                base._assert_status_body(expect_response_body, e.body)
            return
47
        base._assert_status_code(expect_status_code, status_code)
48
        base._assert_status_code(201, status_code)
49
        return base._get_id_from_header(header), name
danfengliu's avatar
danfengliu committed
50 51 52 53

    def get_projects(self, params, **kwargs):
        client = self._get_client(**kwargs)
        data = []
54
        data, status_code, _ = client.list_projects_with_http_info(**params)
danfengliu's avatar
danfengliu committed
55 56 57
        base._assert_status_code(200, status_code)
        return data

58 59 60 61 62 63 64 65
    def get_project_id(self, project_name, **kwargs):
        project_data = self.get_projects(dict(), **kwargs)
        actual_count = len(project_data)
        if actual_count == 1 and str(project_data[0].project_name) != str(project_name):
            return project_data[0].project_id
        else:
            return None

66 67 68 69 70 71 72 73
    def projects_should_exist(self, params, expected_count = None, expected_project_id = None, **kwargs):
        project_data = self.get_projects(params, **kwargs)
        actual_count = len(project_data)
        if expected_count is not None and actual_count!= expected_count:
            raise Exception(r"Private project count should be {}.".format(expected_count))
        if expected_project_id is not None and actual_count == 1 and str(project_data[0].project_id) != str(expected_project_id):
            raise Exception(r"Project-id check failed, expect {} but got {}, please check this test case.".format(str(expected_project_id), str(project_data[0].project_id)))

danfengliu's avatar
danfengliu committed
74 75
    def check_project_name_exist(self, name=None, **kwargs):
        client = self._get_client(**kwargs)
76
        try:
77
            _, status_code, _ = client.head_project_with_http_info(name)
78 79
        except ApiException as e:
            status_code = -1
danfengliu's avatar
danfengliu committed
80 81 82
        return {
            200: True,
            404: False,
83
        }.get(status_code,False)
danfengliu's avatar
danfengliu committed
84

85
    def get_project(self, project_id, expect_status_code = 200, expect_response_body = None, **kwargs):
danfengliu's avatar
danfengliu committed
86
        client = self._get_client(**kwargs)
87
        try:
88
            data, status_code, _ = client.get_project_with_http_info(project_id)
89
        except ApiException as e:
90 91 92 93 94
            base._assert_status_code(expect_status_code, e.status)
            if expect_response_body is not None:
                base._assert_status_body(expect_response_body, e.body)
            return

95
        base._assert_status_code(expect_status_code, status_code)
96
        base._assert_status_code(200, status_code)
danfengliu's avatar
danfengliu committed
97 98
        return data

99
    def update_project(self, project_id, expect_status_code=200, metadata=None, cve_allowlist=None, **kwargs):
danfengliu's avatar
danfengliu committed
100
        client = self._get_client(**kwargs)
101
        project = v2_swagger_client.ProjectReq(metadata=metadata, cve_allowlist=cve_allowlist)
102
        try:
103
            _, sc, _ = client.update_project_with_http_info(project_id, project)
104 105 106 107
        except ApiException as e:
            base._assert_status_code(expect_status_code, e.status)
        else:
            base._assert_status_code(expect_status_code, sc)
danfengliu's avatar
danfengliu committed
108

109
    def delete_project(self, project_id, expect_status_code = 200, **kwargs):
danfengliu's avatar
danfengliu committed
110
        client = self._get_client(**kwargs)
111
        _, status_code, _ = client.delete_project_with_http_info(project_id)
112
        base._assert_status_code(expect_status_code, status_code)
danfengliu's avatar
danfengliu committed
113

114
    def get_project_log(self, project_name, expect_status_code = 200, **kwargs):
115
        client = self._get_client(**kwargs)
116
        body, status_code, _ = client.get_logs_with_http_info(project_name)
117 118 119
        base._assert_status_code(expect_status_code, status_code)
        return body

120 121
    def filter_project_logs(self, project_name, operator, resource, resource_type, operation, **kwargs):
        access_logs = self.get_project_log(project_name, **kwargs)
122 123 124
        count = 0
        for each_access_log in list(access_logs):
            if each_access_log.username == operator and \
125 126 127
                    each_access_log.resource_type == resource_type and \
                    each_access_log.resource == resource and \
                    each_access_log.operation == operation:
128 129 130
                count = count + 1
        return count

danfengliu's avatar
danfengliu committed
131
    def get_project_members(self, project_id, **kwargs):
132
        kwargs['api_type'] = 'products'
133 134 135
        client = self._get_client(**kwargs)
        return client.projects_project_id_members_get(project_id)

136
    def get_project_member(self, project_id, member_id, expect_status_code = 200, expect_response_body = None, **kwargs):
137 138
        from swagger_client.rest import ApiException
        kwargs['api_type'] = 'products'
danfengliu's avatar
danfengliu committed
139 140
        client = self._get_client(**kwargs)
        data = []
141 142 143
        try:
            data, status_code, _ = client.projects_project_id_members_mid_get_with_http_info(project_id, member_id,)
        except ApiException as e:
144 145 146 147 148
            base._assert_status_code(expect_status_code, e.status)
            if expect_response_body is not None:
                base._assert_status_body(expect_response_body, e.body)
            return

149
        base._assert_status_code(expect_status_code, status_code)
150
        base._assert_status_code(200, status_code)
151 152
        return data

153
    def get_project_member_id(self, project_id, member_user_name, **kwargs):
154
        kwargs['api_type'] = 'products'
155 156 157 158 159 160 161
        members = self.get_project_members(project_id, **kwargs)
        result = get_member_id_by_name(list(members), member_user_name)
        if result == None:
            raise Exception(r"Failed to get member id of member {} in project {}.".format(member_user_name, project_id))
        else:
            return result

162
    def check_project_member_not_exist(self, project_id, member_user_name, **kwargs):
163
        kwargs['api_type'] = 'products'
164 165 166 167 168 169
        members = self.get_project_members(project_id, **kwargs)
        result = is_member_exist_in_project(list(members), member_user_name)
        if result == True:
            raise Exception(r"User {} should not be a member of project with ID {}.".format(member_user_name, project_id))

    def check_project_members_exist(self, project_id, member_user_name, expected_member_role_id = None, **kwargs):
170
        kwargs['api_type'] = 'products'
171 172 173 174 175 176
        members = self.get_project_members(project_id, **kwargs)
        result = is_member_exist_in_project(members, member_user_name, expected_member_role_id = expected_member_role_id)
        if result == False:
            raise Exception(r"User {} should be a member of project with ID {}.".format(member_user_name, project_id))

    def update_project_member_role(self, project_id, member_id, member_role_id, expect_status_code = 200, **kwargs):
177
        kwargs['api_type'] = 'products'
178 179 180 181
        client = self._get_client(**kwargs)
        role = swagger_client.Role(role_id = member_role_id)
        data, status_code, _ = client.projects_project_id_members_mid_put_with_http_info(project_id, member_id, role = role)
        base._assert_status_code(expect_status_code, status_code)
182
        base._assert_status_code(200, status_code)
danfengliu's avatar
danfengliu committed
183 184
        return data

185
    def delete_project_member(self, project_id, member_id, expect_status_code = 200, **kwargs):
186
        kwargs['api_type'] = 'products'
187 188 189
        client = self._get_client(**kwargs)
        _, status_code, _ = client.projects_project_id_members_mid_delete_with_http_info(project_id, member_id)
        base._assert_status_code(expect_status_code, status_code)
190
        base._assert_status_code(200, status_code)
191

192
    def add_project_members(self, project_id, user_id = None, member_role_id = None, _ldap_group_dn=None, expect_status_code = 201, **kwargs):
193
        kwargs['api_type'] = 'products'
danfengliu's avatar
danfengliu committed
194 195 196
        projectMember = swagger_client.ProjectMember()
        if user_id is not None:
           projectMember.member_user = {"user_id": int(user_id)}
danfengliu's avatar
danfengliu committed
197
        if member_role_id is None:
danfengliu's avatar
danfengliu committed
198 199 200 201 202 203
            projectMember.role_id = 1
        else:
            projectMember.role_id = member_role_id
        if _ldap_group_dn is not None:
            projectMember.member_group = swagger_client.UserGroup(ldap_group_dn=_ldap_group_dn)

danfengliu's avatar
danfengliu committed
204 205
        client = self._get_client(**kwargs)
        data = []
206 207 208 209 210 211 212
        try:
            data, status_code, header = client.projects_project_id_members_post_with_http_info(project_id, project_member = projectMember)
        except swagger_client.rest.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)
213

danfengliu's avatar
danfengliu committed
214
    def add_project_robot_account(self, project_id, project_name, expires_at, robot_name = None, robot_desc = None, has_pull_right = True,  has_push_right = True, has_chart_read_right = True,  has_chart_create_right = True, expect_status_code = 201, **kwargs):
215
        kwargs['api_type'] = 'products'
216 217 218 219 220 221 222 223
        if robot_name is None:
            robot_name = base._random_name("robot")
        if robot_desc is None:
            robot_desc = base._random_name("robot_desc")
        if has_pull_right is False and has_push_right is False:
            has_pull_right = True
        access_list = []
        resource_by_project_id = "/project/"+str(project_id)+"/repository"
danfengliu's avatar
danfengliu committed
224
        resource_helm_by_project_id = "/project/"+str(project_id)+"/helm-chart"
225
        resource_helm_create_by_project_id = "/project/"+str(project_id)+"/helm-chart-version"
226 227
        action_pull = "pull"
        action_push = "push"
danfengliu's avatar
danfengliu committed
228 229
        action_read = "read"
        action_create = "create"
230 231 232 233 234 235
        if has_pull_right is True:
            robotAccountAccess = swagger_client.RobotAccountAccess(resource = resource_by_project_id, action = action_pull)
            access_list.append(robotAccountAccess)
        if has_push_right is True:
            robotAccountAccess = swagger_client.RobotAccountAccess(resource = resource_by_project_id, action = action_push)
            access_list.append(robotAccountAccess)
danfengliu's avatar
danfengliu committed
236 237 238 239
        if has_chart_read_right is True:
            robotAccountAccess = swagger_client.RobotAccountAccess(resource = resource_helm_by_project_id, action = action_read)
            access_list.append(robotAccountAccess)
        if has_chart_create_right is True:
240
            robotAccountAccess = swagger_client.RobotAccountAccess(resource = resource_helm_create_by_project_id, action = action_create)
danfengliu's avatar
danfengliu committed
241 242
            access_list.append(robotAccountAccess)

243
        robotAccountCreate = swagger_client.RobotAccountCreate(robot_name, robot_desc, expires_at, access_list)
244 245 246 247 248 249 250 251
        client = self._get_client(**kwargs)
        data = []
        data, status_code, header = client.projects_project_id_robots_post_with_http_info(project_id, robotAccountCreate)
        base._assert_status_code(expect_status_code, status_code)
        base._assert_status_code(201, status_code)
        return base._get_id_from_header(header), data

    def get_project_robot_account_by_id(self, project_id, robot_id, **kwargs):
252
        kwargs['api_type'] = 'products'
253 254 255 256 257
        client = self._get_client(**kwargs)
        data, status_code, _ = client.projects_project_id_robots_robot_id_get_with_http_info(project_id, robot_id)
        return data

    def disable_project_robot_account(self, project_id, robot_id, disable,  expect_status_code = 200, **kwargs):
258
        kwargs['api_type'] = 'products'
259 260 261 262 263 264 265
        client = self._get_client(**kwargs)
        robotAccountUpdate = swagger_client.RobotAccountUpdate(disable)
        _, status_code, _ = client.projects_project_id_robots_robot_id_put_with_http_info(project_id, robot_id, robotAccountUpdate)
        base._assert_status_code(expect_status_code, status_code)
        base._assert_status_code(200, status_code)

    def delete_project_robot_account(self, project_id, robot_id, expect_status_code = 200, **kwargs):
266
        kwargs['api_type'] = 'products'
267 268 269
        client = self._get_client(**kwargs)
        _, status_code, _ = client.projects_project_id_robots_robot_id_delete_with_http_info(project_id, robot_id)
        base._assert_status_code(expect_status_code, status_code)
270
        base._assert_status_code(200, status_code)
danfengliu's avatar
danfengliu committed
271 272 273 274 275 276 277 278 279 280 281

    def query_user_logs(self, project_name, status_code=200, **kwargs):
        try:
            logs = self.get_project_log(project_name, expect_status_code=status_code, **kwargs)
            count = 0
            for log in list(logs):
                count = count + 1
            return count
        except ApiException as e:
            _assert_status_code(status_code, e.status)
            return 0