10000 feat(api): add support for project cluster agents · python-gitlab/python-gitlab@32dbc6f · GitHub
[go: up one dir, main page]

Skip to content

Commit 32dbc6f

Browse files
nejchmax-wittig
authored andcommitted
feat(api): add support for project cluster agents
1 parent ee393a1 commit 32dbc6f

File tree

6 files changed

+168
-0
lines changed

6 files changed

+168
-0
lines changed

docs/api-objects.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ API examples
1414
gl_objects/bulk_imports
1515
gl_objects/messages
1616
gl_objects/ci_lint
17+
gl_objects/cluster_agents
1718
gl_objects/commits
1819
gl_objects/deploy_keys
1920
gl_objects/deploy_tokens

docs/gl_objects/cluster_agents.rst

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
##############
2+
Cluster agents
3+
##############
4+
5+
You can list and manage project cluster agents with the GitLab agent for Kubernetes.
6+
7+
.. warning::
8+
Check the GitLab API documentation linked below for project permissions
9+
required to access specific cluster agent endpoints.
10+
11+
Reference
12+
---------
13+
14+
* v4 API:
15+
16+
+ :class:`gitlab.v4.objects.ProjectClusterAgent`
17+
+ :class:`gitlab.v4.objects.ProjectClusterAgentManager`
18+
+ :attr:`gitlab.v4.objects.Project.cluster_agents`
19+
20+
* GitLab API: https://docs.gitlab.com/ee/api/cluster_agents.html
21+
22+
Examples
23+
--------
24+
25+
List cluster agents for a project::
26+
27+
cluster_agents = project.cluster_agents.list()
28+
29+
Register a cluster agent with a project::
30+
31+
cluster_agent = project.cluster_agents.create({"name": "Agent 1"})
32+
33+
Retrieve a specific cluster agent for a project::
34+
35+
cluster_agent = project.cluster_agents.get(cluster_agent.id)
36+
37+
Delete a registered cluster agent from a project::
38+
39+
cluster_agent = project.cluster_agents.delete(cluster_agent.id)
40+
# or
41+
cluster.delete()

gitlab/v4/objects/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from .broadcast_messages import *
1111
from .bulk_imports import *
1212
from .ci_lint import *
13+
from .cluster_agents import *
1314
from .clusters import *
1415
from .commits import *
1516
from .container_registry import *

gitlab/v4/objects/cluster_agents.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from typing import Any, cast, Union
2+
3+
from gitlab.base import RESTManager, RESTObject
4+
from gitlab.mixins import NoUpdateMixin, ObjectDeleteMixin, SaveMixin
5+
from gitlab.types import RequiredOptional
6+
7+
__all__ = [
8+
"ProjectClusterAgent",
9+
"ProjectClusterAgentManager",
10+
]
11+
12+
13+
class ProjectClusterAgent(SaveMixin, ObjectDeleteMixin, RESTObject):
14+
_repr_attr = "name"
15+
16+
17+
class ProjectClusterAgentManager(NoUpdateMixin, RESTManager):
18+
_path = "/projects/{project_id}/cluster_agents"
19+
_obj_cls = ProjectClusterAgent
20+
_from_parent_attrs = {"project_id": "id"}
21+
_create_attrs = RequiredOptional(required=("name",))
22+
23+
def get(
24+
self, id: Union[str, int], lazy: bool = False, **kwargs: Any
25+
) -> ProjectClusterAgent:
26+
return cast(ProjectClusterAgent, super().get(id=id, lazy=lazy, **kwargs))

gitlab/v4/objects/projects.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
from .boards import ProjectBoardManager # noqa: F401
4444
from .branches import ProjectBranchManager, ProjectProtectedBranchManager # noqa: F401
4545
from .ci_lint import ProjectCiLintManager # noqa: F401
46+
from .cluster_agents import ProjectClusterAgentManager # noqa: F401
4647
from .clusters import ProjectClusterManager # noqa: F401
4748
from .commits import ProjectCommitManager # noqa: F401
4849
from .container_registry import ProjectRegistryRepositoryManager # noqa: F401
@@ -183,6 +184,7 @@ class Project(
183184
branches: ProjectBranchManager
184185
ci_lint: ProjectCiLintManager
185186
clusters: ProjectClusterManager
187+
cluster_agents: ProjectClusterAgentManager
186188
commits: ProjectCommitManager
187189
customattributes: ProjectCustomAttributeManager
188190
deployments: ProjectDeploymentManager
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
"""
2+
GitLab API: https://docs.gitlab.com/ee/api/cluster_agents.html
3+
"""
4+
5+
import pytest
6+
import responses
7+
8+
from gitlab.v4.objects import ProjectClusterAgent
9+
10+
agent_content = {
11+
"id": 1,
12+
"name": "agent-1",
13+
"config_project": {
14+
"id": 20,
15+
"description": "",
16+
"name": "test",
17+
"name_with_namespace": "Administrator / test",
18+
"path": "test",
19+
"path_with_namespace": "root/test",
20+
"created_at": "2022-03-20T20:42:40.221Z",
21+
},
22+
"created_at": "2022-04-20T20:42:40.221Z",
23+
"created_by_user_id": 42,
24+
}
25+
26+
27+
@pytest.fixture
28+
def resp_list_project_cluster_agents():
29+
with responses.RequestsMock() as rsps:
30+
rsps.add(
31+
method=responses.GET,
32+
url="http://localhost/api/v4/projects/1/cluster_agents",
33+
json=[agent_content],
34+
content_type="application/json",
35+
status=200,
36+
)
37+
yield rsps
38+
39+
40+
@pytest.fixture
41+
def resp_get_project_cluster_agent():
42+
with responses.RequestsMock() as rsps:
43+
rsps.add(
44+
method=responses.GET,
45+
url="http://localhost/api/v4/projects/1/cluster_agents/1",
46+
json=agent_content,
47+
content_type="application/json",
48+
status=200,
49+
)
50+
yield rsps
51+
52+
53+
@pytest.fixture
54+
def resp_create_project_cluster_agent():
55+
with responses.RequestsMock() as rsps:
56+
rsps.add(
57+
method=responses.POST,
58+
url="http://localhost/api/v4/projects/1/cluster_agents",
59+
json=agent_content,
60+
content_type="application/json",
61+
status=201,
62+
)
63+
yield rsps
64+
65+
66+
@pytest.fixture
67+
def resp_delete_project_cluster_agent():
68+
with responses.RequestsMock() as rsps:
69+
rsps.add(
70+
method=responses.DELETE,
71+
url="http://localhost/api/v4/projects/1/cluster_agents/1",
72+
status=204,
73+
)
74+
yield rsps
75+
76+
77+
def test_list_project_cluster_agents(project, resp_list_project_cluster_agents):
78+
agent = project.cluster_agents.list()[0]
79+
assert isinstance(agent, ProjectClusterAgent)
80+
assert agent.name == "agent-1"
81+
82+
83+
def test_get_project_cluster_agent(project, resp_get_project_cluster_agent):
84+
agent = project.cluster_agents.get(1)
85+
assert isinstance(agent, ProjectClusterAgent)
86+
assert agent.name == "agent-1"
87+
88+
89+
def test_create_project_cluster_agent(project, resp_create_project_cluster_agent):
90+
agent = project.cluster_agents.create({"name": "agent-1"})
91+
assert isinstance(agent, ProjectClusterAgent)
92+
assert agent.name == "agent-1"
93+
94+
95+
def test_delete_project_cluster_agent(project, resp_delete_project_cluster_agent):
96+
agent = project.cluster_agents.get(1, lazy=True)
97+
agent.delete()

0 commit comments

Comments
 (0)
0