|
| 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 | + UpdateMethod, |
| 15 | + UpdateMixin, |
| 16 | +) |
| 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 | +) |
4 | 50 |
|
5 | | -__all__ = ["GroupServiceAccount", "GroupServiceAccountManager"] |
6 | 51 |
|
| 52 | +# --------------------------------------------------------------------------- |
| 53 | +# Instance-level service accounts |
| 54 | +# --------------------------------------------------------------------------- |
7 | 55 |
|
8 | | -class GroupServiceAccount(ObjectDeleteMixin, RESTObject): |
| 56 | + |
| 57 | +class ServiceAccount(SaveMixin, RESTObject): |
9 | 58 | pass |
10 | 59 |
|
11 | 60 |
|
| 61 | +class ServiceAccountManager( |
| 62 | + CreateMixin[ServiceAccount], ListMixin[ServiceAccount], UpdateMixin[ServiceAccount] |
| 63 | +): |
| 64 | + _path = "/service_accounts" |
| 65 | + _obj_cls = ServiceAccount |
| 66 | + _create_attrs = _SA_ACCOUNT_ATTRS |
| 67 | + _update_attrs = _SA_ACCOUNT_ATTRS |
| 68 | + _update_method = UpdateMethod.PATCH |
| 69 | + _list_filters = ("order_by", "sort") |
| 70 | + |
| 71 | + |
| 72 | +# --------------------------------------------------------------------------- |
| 73 | +# Group-level service accounts |
| 74 | +# --------------------------------------------------------------------------- |
| 75 | + |
| 76 | + |
| 77 | +class GroupServiceAccountAccessToken(ObjectDeleteMixin, ObjectRotateMixin, RESTObject): |
| 78 | + pass |
| 79 | + |
| 80 | + |
| 81 | +class GroupServiceAccountAccessTokenManager( |
| 82 | + CreateMixin[GroupServiceAccountAccessToken], |
| 83 | + DeleteMixin[GroupServiceAccountAccessToken], |
| 84 | + ListMixin[GroupServiceAccountAccessToken], |
| 85 | + RotateMixin[GroupServiceAccountAccessToken], |
| 86 | +): |
| 87 | + _path = "/groups/{group_id}/service_accounts/{user_id}/personal_access_tokens" |
| 88 | + _obj_cls = GroupServiceAccountAccessToken |
| 89 | + _from_parent_attrs = {"group_id": "group_id", "user_id": "id"} |
| 90 | + _create_attrs = _SA_TOKEN_CREATE_ATTRS |
| 91 | + _types = {"scopes": ArrayAttribute} |
| 92 | + _list_filters = _SA_TOKEN_LIST_FILTERS |
| 93 | + |
| 94 | + |
| 95 | +class GroupServiceAccount(SaveMixin, ObjectDeleteMixin, RESTObject): |
| 96 | + access_tokens: GroupServiceAccountAccessTokenManager |
| 97 | + |
| 98 | + |
12 | 99 | class GroupServiceAccountManager( |
13 | 100 | CreateMixin[GroupServiceAccount], |
14 | 101 | DeleteMixin[GroupServiceAccount], |
15 | 102 | ListMixin[GroupServiceAccount], |
| 103 | + UpdateMixin[GroupServiceAccount], |
16 | 104 | ): |
17 | 105 | _path = "/groups/{group_id}/service_accounts" |
18 | 106 | _obj_cls = GroupServiceAccount |
19 | 107 | _from_parent_attrs = {"group_id": "id"} |
20 | | - _create_attrs = RequiredOptional(optional=("name", "username")) |
| 108 | + _create_attrs = _SA_ACCOUNT_ATTRS |
| 109 | + _update_attrs = _SA_ACCOUNT_ATTRS |
| 110 | + _update_method = UpdateMethod.PATCH |
| 111 | + _list_filters = ("order_by", "sort") |
| 112 | + |
| 113 | + |
| 114 | +# --------------------------------------------------------------------------- |
| 115 | +# Project-level service accounts |
| 116 | +# --------------------------------------------------------------------------- |
| 117 | + |
| 118 | + |
| 119 | +class ProjectServiceAccountAccessToken( |
| 120 | + ObjectDeleteMixin, ObjectRotateMixin, RESTObject |
| 121 | +): |
| 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