|
| 1 | +""" |
| 2 | +GitLab API: https://docs.gitlab.com/api/service_accounts/ |
| 3 | +""" |
| 4 | + |
1 | 5 | from gitlab.base import RESTObject |
2 | | -from gitlab.mixins import CreateMixin, DeleteMixin, ListMixin, ObjectDeleteMixin |
3 | | -from gitlab.types import RequiredOptional |
| 6 | +from gitlab.mixins import ( |
| 7 | + CreateMixin, |
| 8 | + DeleteMixin, |
| 9 | + ListMixin, |
| 10 | + ObjectDeleteMixin, |
| 11 | + ObjectRotateMixin, |
| 12 | + RotateMixin, |
| 13 | + SaveMixin, |
| 14 | + UpdateMixin, |
| 15 | +) |
| 16 | +from gitlab.mixins import UpdateMethod |
| 17 | +from gitlab.types import ArrayAttribute, RequiredOptional |
| 18 | + |
| 19 | +__all__ = [ |
| 20 | + "ServiceAccount", |
| 21 | + "ServiceAccountManager", |
| 22 | + "GroupServiceAccount", |
| 23 | + "GroupServiceAccountManager", |
| 24 | + "GroupServiceAccountAccessToken", |
| 25 | + "GroupServiceAccountAccessTokenManager", |
| 26 | + "ProjectServiceAccount", |
| 27 | + "ProjectServiceAccountManager", |
| 28 | + "ProjectServiceAccountAccessToken", |
| 29 | + "ProjectServiceAccountAccessTokenManager", |
| 30 | +] |
| 31 | + |
| 32 | +_SA_ACCOUNT_ATTRS = RequiredOptional(optional=("name", "username", "email")) |
| 33 | + |
| 34 | +_SA_TOKEN_CREATE_ATTRS = RequiredOptional( |
| 35 | + required=("name", "scopes"), optional=("description", "expires_at") |
| 36 | +) |
| 37 | + |
| 38 | +_SA_TOKEN_LIST_FILTERS = ( |
| 39 | + "created_after", |
| 40 | + "created_before", |
| 41 | + "expires_after", |
| 42 | + "expires_before", |
| 43 | + "last_used_after", |
| 44 | + "last_used_before", |
| 45 | + "revoked", |
| 46 | + "search", |
| 47 | + "sort", |
| 48 | + "state", |
| 49 | +) |
| 50 | + |
| 51 | + |
| 52 | +# --------------------------------------------------------------------------- |
| 53 | +# Instance-level service accounts |
| 54 | +# --------------------------------------------------------------------------- |
| 55 | + |
4 | 56 |
|
5 | | -__all__ = ["GroupServiceAccount", "GroupServiceAccountManager"] |
| 57 | +class ServiceAccount(SaveMixin, RESTObject): |
| 58 | + pass |
| 59 | + |
| 60 | + |
| 61 | +class ServiceAccountManager( |
| 62 | + CreateMixin[ServiceAccount], |
| 63 | + ListMixin[ServiceAccount], |
| 64 | + UpdateMixin[ServiceAccount], |
| 65 | +): |
| 66 | + _path = "/service_accounts" |
| 67 | + _obj_cls = ServiceAccount |
| 68 | + _create_attrs = _SA_ACCOUNT_ATTRS |
| 69 | + _update_attrs = _SA_ACCOUNT_ATTRS |
| 70 | + _update_method = UpdateMethod.PATCH |
| 71 | + _list_filters = ("order_by", "sort") |
6 | 72 |
|
7 | 73 |
|
8 | | -class GroupServiceAccount(ObjectDeleteMixin, RESTObject): |
| 74 | +# --------------------------------------------------------------------------- |
| 75 | +# Group-level service accounts |
| 76 | +# --------------------------------------------------------------------------- |
| 77 | + |
| 78 | + |
| 79 | +class GroupServiceAccountAccessToken(ObjectDeleteMixin, ObjectRotateMixin, RESTObject): |
9 | 80 | pass |
10 | 81 |
|
11 | 82 |
|
| 83 | +class GroupServiceAccountAccessTokenManager( |
| 84 | + CreateMixin[GroupServiceAccountAccessToken], |
| 85 | + DeleteMixin[GroupServiceAccountAccessToken], |
| 86 | + ListMixin[GroupServiceAccountAccessToken], |
| 87 | + RotateMixin[GroupServiceAccountAccessToken], |
| 88 | +): |
| 89 | + _path = "/groups/{group_id}/service_accounts/{user_id}/personal_access_tokens" |
| 90 | + _obj_cls = GroupServiceAccountAccessToken |
| 91 | + _from_parent_attrs = {"group_id": "group_id", "user_id": "id"} |
| 92 | + _create_attrs = _SA_TOKEN_CREATE_ATTRS |
| 93 | + _types = {"scopes": ArrayAttribute} |
| 94 | + _list_filters = _SA_TOKEN_LIST_FILTERS |
| 95 | + |
| 96 | + |
| 97 | +class GroupServiceAccount(SaveMixin, ObjectDeleteMixin, RESTObject): |
| 98 | + access_tokens: GroupServiceAccountAccessTokenManager |
| 99 | + |
| 100 | + |
12 | 101 | class GroupServiceAccountManager( |
13 | 102 | CreateMixin[GroupServiceAccount], |
14 | 103 | DeleteMixin[GroupServiceAccount], |
15 | 104 | ListMixin[GroupServiceAccount], |
| 105 | + UpdateMixin[GroupServiceAccount], |
16 | 106 | ): |
17 | 107 | _path = "/groups/{group_id}/service_accounts" |
18 | 108 | _obj_cls = GroupServiceAccount |
19 | 109 | _from_parent_attrs = {"group_id": "id"} |
20 | | - _create_attrs = RequiredOptional(optional=("name", "username")) |
| 110 | + _create_attrs = _SA_ACCOUNT_ATTRS |
| 111 | + _update_attrs = _SA_ACCOUNT_ATTRS |
| 112 | + _update_method = UpdateMethod.PATCH |
| 113 | + _list_filters = ("order_by", "sort") |
| 114 | + |
| 115 | + |
| 116 | +# --------------------------------------------------------------------------- |
| 117 | +# Project-level service accounts |
| 118 | +# --------------------------------------------------------------------------- |
| 119 | + |
| 120 | + |
| 121 | +class ProjectServiceAccountAccessToken(ObjectDeleteMixin, ObjectRotateMixin, RESTObject): |
| 122 | + pass |
| 123 | + |
| 124 | + |
| 125 | +class ProjectServiceAccountAccessTokenManager( |
| 126 | + CreateMixin[ProjectServiceAccountAccessToken], |
| 127 | + DeleteMixin[ProjectServiceAccountAccessToken], |
| 128 | + ListMixin[ProjectServiceAccountAccessToken], |
| 129 | + RotateMixin[ProjectServiceAccountAccessToken], |
| 130 | +): |
| 131 | + _path = "/projects/{project_id}/service_accounts/{user_id}/personal_access_tokens" |
| 132 | + _obj_cls = ProjectServiceAccountAccessToken |
| 133 | + _from_parent_attrs = {"project_id": "project_id", "user_id": "id"} |
| 134 | + _create_attrs = _SA_TOKEN_CREATE_ATTRS |
| 135 | + _types = {"scopes": ArrayAttribute} |
| 136 | + _list_filters = _SA_TOKEN_LIST_FILTERS |
| 137 | + |
| 138 | + |
| 139 | +class ProjectServiceAccount(SaveMixin, ObjectDeleteMixin, RESTObject): |
| 140 | + access_tokens: ProjectServiceAccountAccessTokenManager |
| 141 | + |
| 142 | + |
| 143 | +class ProjectServiceAccountManager( |
| 144 | + CreateMixin[ProjectServiceAccount], |
| 145 | + DeleteMixin[ProjectServiceAccount], |
| 146 | + ListMixin[ProjectServiceAccount], |
| 147 | + UpdateMixin[ProjectServiceAccount], |
| 148 | +): |
| 149 | + _path = "/projects/{project_id}/service_accounts" |
| 150 | + _obj_cls = ProjectServiceAccount |
| 151 | + _from_parent_attrs = {"project_id": "id"} |
| 152 | + _create_attrs = _SA_ACCOUNT_ATTRS |
| 153 | + _update_attrs = _SA_ACCOUNT_ATTRS |
| 154 | + _update_method = UpdateMethod.PATCH |
| 155 | + _list_filters = ("order_by", "sort") |
0 commit comments