1//
2// Copyright 2019, Paul Shoemaker
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
16
17package gitlab
18
19import (
20 "fmt"
21 "time"
22)
23
24// GroupClustersService handles communication with the
25// group clusters related methods of the GitLab API.
26//
27// GitLab API docs:
28// https://docs.gitlab.com/ee/api/group_clusters.html
29type GroupClustersService struct {
30 client *Client
31}
32
33// GroupCluster represents a GitLab Group Cluster.
34//
35// GitLab API docs: https://docs.gitlab.com/ee/api/group_clusters.html
36type GroupCluster struct {
37 ID int `json:"id"`
38 Name string `json:"name"`
39 Domain string `json:"domain"`
40 CreatedAt *time.Time `json:"created_at"`
41 ProviderType string `json:"provider_type"`
42 PlatformType string `json:"platform_type"`
43 EnvironmentScope string `json:"environment_scope"`
44 ClusterType string `json:"cluster_type"`
45 User *User `json:"user"`
46 PlatformKubernetes *PlatformKubernetes `json:"platform_kubernetes"`
47 Group *Group `json:"group"`
48}
49
50func (v GroupCluster) String() string {
51 return Stringify(v)
52}
53
54// ListClusters gets a list of all clusters in a group.
55//
56// GitLab API docs:
57// https://docs.gitlab.com/ee/api/group_clusters.html#list-group-clusters
58func (s *GroupClustersService) ListClusters(pid interface{}, options ...OptionFunc) ([]*GroupCluster, *Response, error) {
59 group, err := parseID(pid)
60 if err != nil {
61 return nil, nil, err
62 }
63 u := fmt.Sprintf("groups/%s/clusters", pathEscape(group))
64
65 req, err := s.client.NewRequest("GET", u, nil, options)
66 if err != nil {
67 return nil, nil, err
68 }
69
70 var pcs []*GroupCluster
71 resp, err := s.client.Do(req, &pcs)
72 if err != nil {
73 return nil, resp, err
74 }
75
76 return pcs, resp, err
77}
78
79// GetCluster gets a cluster.
80//
81// GitLab API docs:
82// https://docs.gitlab.com/ee/api/group_clusters.html#get-a-single-group-cluster
83func (s *GroupClustersService) GetCluster(pid interface{}, cluster int, options ...OptionFunc) (*GroupCluster, *Response, error) {
84 group, err := parseID(pid)
85 if err != nil {
86 return nil, nil, err
87 }
88 u := fmt.Sprintf("groups/%s/clusters/%d", pathEscape(group), cluster)
89
90 req, err := s.client.NewRequest("GET", u, nil, options)
91 if err != nil {
92 return nil, nil, err
93 }
94
95 pc := new(GroupCluster)
96 resp, err := s.client.Do(req, &pc)
97 if err != nil {
98 return nil, resp, err
99 }
100
101 return pc, resp, err
102}
103
104// AddGroupClusterOptions represents the available AddCluster() options.
105//
106// GitLab API docs:
107// https://docs.gitlab.com/ee/api/group_clusters.html#add-existing-cluster-to-group
108type AddGroupClusterOptions struct {
109 Name *string `url:"name,omitempty" json:"name,omitempty"`
110 Domain *string `url:"domain,omitempty" json:"domain,omitempty"`
111 Enabled *bool `url:"enabled,omitempty" json:"enabled,omitempty"`
112 Managed *bool `url:"managed,omitempty" json:"managed,omitempty"`
113 EnvironmentScope *string `url:"environment_scope,omitempty" json:"environment_scope,omitempty"`
114 PlatformKubernetes *AddGroupPlatformKubernetesOptions `url:"platform_kubernetes_attributes,omitempty" json:"platform_kubernetes_attributes,omitempty"`
115}
116
117// AddGroupPlatformKubernetesOptions represents the available PlatformKubernetes options for adding.
118type AddGroupPlatformKubernetesOptions struct {
119 APIURL *string `url:"api_url,omitempty" json:"api_url,omitempty"`
120 Token *string `url:"token,omitempty" json:"token,omitempty"`
121 CaCert *string `url:"ca_cert,omitempty" json:"ca_cert,omitempty"`
122 Namespace *string `url:"namespace,omitempty" json:"namespace,omitempty"`
123 AuthorizationType *string `url:"authorization_type,omitempty" json:"authorization_type,omitempty"`
124}
125
126// AddCluster adds an existing cluster to the group.
127//
128// GitLab API docs:
129// https://docs.gitlab.com/ee/api/group_clusters.html#add-existing-cluster-to-group
130func (s *GroupClustersService) AddCluster(pid interface{}, opt *AddGroupClusterOptions, options ...OptionFunc) (*GroupCluster, *Response, error) {
131 group, err := parseID(pid)
132 if err != nil {
133 return nil, nil, err
134 }
135 u := fmt.Sprintf("groups/%s/clusters/user", pathEscape(group))
136
137 req, err := s.client.NewRequest("POST", u, opt, options)
138 if err != nil {
139 return nil, nil, err
140 }
141
142 pc := new(GroupCluster)
143 resp, err := s.client.Do(req, pc)
144 if err != nil {
145 return nil, resp, err
146 }
147
148 return pc, resp, err
149}
150
151// EditGroupClusterOptions represents the available EditCluster() options.
152//
153// GitLab API docs:
154// https://docs.gitlab.com/ee/api/group_clusters.html#edit-group-cluster
155type EditGroupClusterOptions struct {
156 Name *string `url:"name,omitempty" json:"name,omitempty"`
157 Domain *string `url:"domain,omitempty" json:"domain,omitempty"`
158 EnvironmentScope *string `url:"environment_scope,omitempty" json:"environment_scope,omitempty"`
159 PlatformKubernetes *EditGroupPlatformKubernetesOptions `url:"platform_kubernetes_attributes,omitempty" json:"platform_kubernetes_attributes,omitempty"`
160}
161
162// EditGroupPlatformKubernetesOptions represents the available PlatformKubernetes options for editing.
163type EditGroupPlatformKubernetesOptions struct {
164 APIURL *string `url:"api_url,omitempty" json:"api_url,omitempty"`
165 Token *string `url:"token,omitempty" json:"token,omitempty"`
166 CaCert *string `url:"ca_cert,omitempty" json:"ca_cert,omitempty"`
167}
168
169// EditCluster updates an existing group cluster.
170//
171// GitLab API docs:
172// https://docs.gitlab.com/ee/api/group_clusters.html#edit-group-cluster
173func (s *GroupClustersService) EditCluster(pid interface{}, cluster int, opt *EditGroupClusterOptions, options ...OptionFunc) (*GroupCluster, *Response, error) {
174 group, err := parseID(pid)
175 if err != nil {
176 return nil, nil, err
177 }
178 u := fmt.Sprintf("groups/%s/clusters/%d", pathEscape(group), cluster)
179
180 req, err := s.client.NewRequest("PUT", u, opt, options)
181 if err != nil {
182 return nil, nil, err
183 }
184
185 pc := new(GroupCluster)
186 resp, err := s.client.Do(req, pc)
187 if err != nil {
188 return nil, resp, err
189 }
190
191 return pc, resp, err
192}
193
194// DeleteCluster deletes an existing group cluster.
195//
196// GitLab API docs:
197// https://docs.gitlab.com/ee/api/group_clusters.html#delete-group-cluster
198func (s *GroupClustersService) DeleteCluster(pid interface{}, cluster int, options ...OptionFunc) (*Response, error) {
199 group, err := parseID(pid)
200 if err != nil {
201 return nil, err
202 }
203 u := fmt.Sprintf("groups/%s/clusters/%d", pathEscape(group), cluster)
204
205 req, err := s.client.NewRequest("DELETE", u, nil, options)
206 if err != nil {
207 return nil, err
208 }
209
210 return s.client.Do(req, nil)
211}