Skip to content

Commit 9ec54ae

Browse files
author
alex lundberg
committed
feat: add project and group clusters
1 parent c937338 commit 9ec54ae

File tree

3 files changed

+145
-0
lines changed

3 files changed

+145
-0
lines changed

docs/api-objects.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ API examples
99
gl_objects/emojis
1010
gl_objects/badges
1111
gl_objects/branches
12+
gl_objects/clusters
1213
gl_objects/messages
1314
gl_objects/commits
1415
gl_objects/deploy_keys

gitlab/v4/objects.py

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -743,6 +743,57 @@ class GroupBoardManager(CRUDMixin, RESTManager):
743743
_create_attrs = (("name",), tuple())
744744

745745

746+
class GroupCluster(SaveMixin, ObjectDeleteMixin, RESTObject):
747+
pass
748+
749+
750+
class GroupClusterManager(CRUDMixin, RESTManager):
751+
_path = "/groups/%(group_id)s/clusters"
752+
_obj_cls = GroupCluster
753+
_from_parent_attrs = {"group_id": "id"}
754+
_create_attrs = (
755+
("name", "platform_kubernetes_attributes",),
756+
(
757+
"domain",
758+
"enabled",
759+
"managed",
760+
"platform_kubernetes_attributes",
761+
"environment_scope",
762+
),
763+
)
764+
_update_attrs = (
765+
tuple(),
766+
(
767+
"name",
768+
"domain",
769+
"management_project_id",
770+
"platform_kubernetes_attributes",
771+
"environment_scope",
772+
),
773+
)
774+
775+
@exc.on_http_error(exc.GitlabStopError)
776+
def create(self, data, **kwargs):
777+
"""Create a new object.
778+
779+
Args:
780+
data (dict): Parameters to send to the server to create the
781+
resource
782+
**kwargs: Extra options to send to the server (e.g. sudo or
783+
'ref_name', 'stage', 'name', 'all')
784+
785+
Raises:
786+
GitlabAuthenticationError: If authentication is not correct
787+
GitlabCreateError: If the server cannot perform the request
788+
789+
Returns:
790+
RESTObject: A new instance of the manage object class build with
791+
the data sent by the server
792+
"""
793+
path = "%s/user" % (self.path)
794+
return CreateMixin.create(self, data, path=path, **kwargs)
795+
796+
746797
class GroupCustomAttribute(ObjectDeleteMixin, RESTObject):
747798
_id_attr = "key"
748799

@@ -1150,6 +1201,7 @@ class Group(SaveMixin, ObjectDeleteMixin, RESTObject):
11501201
("projects", "GroupProjectManager"),
11511202
("subgroups", "GroupSubgroupManager"),
11521203
("variables", "GroupVariableManager"),
1204+
("clusters", "GroupClusterManager"),
11531205
)
11541206

11551207
@cli.register_custom_action("Group", ("to_project_id",))
@@ -1599,6 +1651,57 @@ class ProjectBranchManager(NoUpdateMixin, RESTManager):
15991651
_create_attrs = (("branch", "ref"), tuple())
16001652

16011653

1654+
class ProjectCluster(SaveMixin, ObjectDeleteMixin, RESTObject):
1655+
pass
1656+
1657+
1658+
class ProjectClusterManager(CRUDMixin, RESTManager):
1659+
_path = "/projects/%(project_id)s/clusters"
1660+
_obj_cls = ProjectCluster
1661+
_from_parent_attrs = {"project_id": "id"}
1662+
_create_attrs = (
1663+
("name", "platform_kubernetes_attributes",),
1664+
(
1665+
"domain",
1666+
"enabled",
1667+
"managed",
1668+
"platform_kubernetes_attributes",
1669+
"environment_scope",
1670+
),
1671+
)
1672+
_update_attrs = (
1673+
tuple(),
1674+
(
1675+
"name",
1676+
"domain",
1677+
"management_project_id",
1678+
"platform_kubernetes_attributes",
1679+
"environment_scope",
1680+
),
1681+
)
1682+
1683+
@exc.on_http_error(exc.GitlabStopError)
1684+
def create(self, data, **kwargs):
1685+
"""Create a new object.
1686+
1687+
Args:
1688+
data (dict): Parameters to send to the server to create the
1689+
resource
1690+
**kwargs: Extra options to send to the server (e.g. sudo or
1691+
'ref_name', 'stage', 'name', 'all')
1692+
1693+
Raises:
1694+
GitlabAuthenticationError: If authentication is not correct
1695+
GitlabCreateError: If the server cannot perform the request
1696+
1697+
Returns:
1698+
RESTObject: A new instance of the manage object class build with
1699+
the data sent by the server
1700+
"""
1701+
path = "%s/user" % (self.path)
1702+
return CreateMixin.create(self, data, path=path, **kwargs)
1703+
1704+
16021705
class ProjectCustomAttribute(ObjectDeleteMixin, RESTObject):
16031706
_id_attr = "key"
16041707

@@ -3943,6 +4046,7 @@ class Project(SaveMixin, ObjectDeleteMixin, RESTObject):
39434046
("triggers", "ProjectTriggerManager"),
39444047
("variables", "ProjectVariableManager"),
39454048
("wikis", "ProjectWikiManager"),
4049+
("clusters", "ProjectClusterManager"),
39464050
)
39474051

39484052
@cli.register_custom_action("Project", ("submodule", "branch", "commit_sha"))

tools/python_test_v4.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,46 @@
503503
env.delete()
504504
assert len(admin_project.environments.list()) == 0
505505

506+
# Project clusters
507+
admin_project.clusters.create(
508+
{
509+
"name": "cluster1",
510+
"platform_kubernetes_attributes": {
511+
"api_url": "http://url",
512+
"token": "tokenval",
513+
},
514+
}
515+
)
516+
clusters = admin_project.clusters.list()
517+
assert len(clusters) == 1
518+
cluster = clusters[0]
519+
cluster.platform_kubernetes_attributes = {"api_url": "http://newurl"}
520+
cluster.save()
521+
cluster = admin_project.clusters.list()[0]
522+
assert cluster.platform_kubernetes["api_url"] == "http://newurl"
523+
cluster.delete()
524+
assert len(admin_project.clusters.list()) == 0
525+
526+
# Group clusters
527+
group1.clusters.create(
528+
{
529+
"name": "cluster1",
530+
"platform_kubernetes_attributes": {
531+
"api_url": "http://url",
532+
"token": "tokenval",
533+
},
534+
}
535+
)
536+
clusters = group1.clusters.list()
537+
assert len(clusters) == 1
538+
cluster = clusters[0]
539+
cluster.platform_kubernetes_attributes = {"api_url": "http://newurl"}
540+
cluster.save()
541+
cluster = group1.clusters.list()[0]
542+
assert cluster.platform_kubernetes["api_url"] == "http://newurl"
543+
cluster.delete()
544+
assert len(group1.clusters.list()) == 0
545+
506546
# project events
507547
admin_project.events.list()
508548

0 commit comments

Comments
 (0)