Skip to content

feat: add project and group clusters #946

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/api-objects.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ API examples
gl_objects/emojis
gl_objects/badges
gl_objects/branches
gl_objects/clusters
gl_objects/messages
gl_objects/commits
gl_objects/deploy_keys
Expand Down
92 changes: 92 additions & 0 deletions gitlab/v4/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,51 @@ class GroupBoardManager(CRUDMixin, RESTManager):
_create_attrs = (("name",), tuple())


class GroupCluster(SaveMixin, ObjectDeleteMixin, RESTObject):
pass


class GroupClusterManager(CRUDMixin, RESTManager):
_path = "/groups/%(group_id)s/clusters"
_obj_cls = GroupCluster
_from_parent_attrs = {"group_id": "id"}
_create_attrs = (
("name", "platform_kubernetes_attributes",),
("domain", "enabled", "managed", "environment_scope",),
)
_update_attrs = (
tuple(),
(
"name",
"domain",
"management_project_id",
"platform_kubernetes_attributes",
"environment_scope",
),
)

@exc.on_http_error(exc.GitlabStopError)
def create(self, data, **kwargs):
"""Create a new object.

Args:
data (dict): Parameters to send to the server to create the
resource
**kwargs: Extra options to send to the server (e.g. sudo or
'ref_name', 'stage', 'name', 'all')

Raises:
GitlabAuthenticationError: If authentication is not correct
GitlabCreateError: If the server cannot perform the request

Returns:
RESTObject: A new instance of the manage object class build with
the data sent by the server
"""
path = "%s/user" % (self.path)
return CreateMixin.create(self, data, path=path, **kwargs)


class GroupCustomAttribute(ObjectDeleteMixin, RESTObject):
_id_attr = "key"

Expand Down Expand Up @@ -1150,6 +1195,7 @@ class Group(SaveMixin, ObjectDeleteMixin, RESTObject):
("projects", "GroupProjectManager"),
("subgroups", "GroupSubgroupManager"),
("variables", "GroupVariableManager"),
("clusters", "GroupClusterManager"),
)

@cli.register_custom_action("Group", ("to_project_id",))
Expand Down Expand Up @@ -1599,6 +1645,51 @@ class ProjectBranchManager(NoUpdateMixin, RESTManager):
_create_attrs = (("branch", "ref"), tuple())


class ProjectCluster(SaveMixin, ObjectDeleteMixin, RESTObject):
pass


class ProjectClusterManager(CRUDMixin, RESTManager):
_path = "/projects/%(project_id)s/clusters"
_obj_cls = ProjectCluster
_from_parent_attrs = {"project_id": "id"}
_create_attrs = (
("name", "platform_kubernetes_attributes",),
("domain", "enabled", "managed", "environment_scope",),
)
_update_attrs = (
tuple(),
(
"name",
"domain",
"management_project_id",
"platform_kubernetes_attributes",
"environment_scope",
),
)

@exc.on_http_error(exc.GitlabStopError)
def create(self, data, **kwargs):
"""Create a new object.

Args:
data (dict): Parameters to send to the server to create the
resource
**kwargs: Extra options to send to the server (e.g. sudo or
'ref_name', 'stage', 'name', 'all')

Raises:
GitlabAuthenticationError: If authentication is not correct
GitlabCreateError: If the server cannot perform the request

Returns:
RESTObject: A new instance of the manage object class build with
the data sent by the server
"""
path = "%s/user" % (self.path)
return CreateMixin.create(self, data, path=path, **kwargs)


class ProjectCustomAttribute(ObjectDeleteMixin, RESTObject):
_id_attr = "key"

Expand Down Expand Up @@ -3943,6 +4034,7 @@ class Project(SaveMixin, ObjectDeleteMixin, RESTObject):
("triggers", "ProjectTriggerManager"),
("variables", "ProjectVariableManager"),
("wikis", "ProjectWikiManager"),
("clusters", "ProjectClusterManager"),
)

@cli.register_custom_action("Project", ("submodule", "branch", "commit_sha"))
Expand Down
40 changes: 40 additions & 0 deletions tools/python_test_v4.py
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,46 @@
env.delete()
assert len(admin_project.environments.list()) == 0

# Project clusters
admin_project.clusters.create(
{
"name": "cluster1",
"platform_kubernetes_attributes": {
"api_url": "http://url",
"token": "tokenval",
},
}
)
clusters = admin_project.clusters.list()
assert len(clusters) == 1
cluster = clusters[0]
cluster.platform_kubernetes_attributes = {"api_url": "http://newurl"}
cluster.save()
cluster = admin_project.clusters.list()[0]
assert cluster.platform_kubernetes["api_url"] == "http://newurl"
cluster.delete()
assert len(admin_project.clusters.list()) == 0

# Group clusters
group1.clusters.create(
{
"name": "cluster1",
"platform_kubernetes_attributes": {
"api_url": "http://url",
"token": "tokenval",
},
}
)
clusters = group1.clusters.list()
assert len(clusters) == 1
cluster = clusters[0]
cluster.platform_kubernetes_attributes = {"api_url": "http://newurl"}
cluster.save()
cluster = group1.clusters.list()[0]
assert cluster.platform_kubernetes["api_url"] == "http://newurl"
cluster.delete()
assert len(group1.clusters.list()) == 0

# project events
admin_project.events.list()

Expand Down