1//
2// Copyright 2019, Matej Velikonja
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// ProjectClustersService handles communication with the
25// project clusters related methods of the GitLab API.
26//
27// GitLab API docs:
28// https://docs.gitlab.com/ee/api/project_clusters.html
29type ProjectClustersService struct {
30 client *Client
31}
32
33// ProjectCluster represents a GitLab Project Cluster.
34//
35// GitLab API docs: https://docs.gitlab.com/ee/api/project_clusters.html
36type ProjectCluster 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 Project *Project `json:"project"`
48}
49
50func (v ProjectCluster) String() string {
51 return Stringify(v)
52}
53
54// PlatformKubernetes represents a GitLab Project Cluster PlatformKubernetes.
55type PlatformKubernetes struct {
56 APIURL string `json:"api_url"`
57 Token string `json:"token"`
58 CaCert string `json:"ca_cert"`
59 Namespace string `json:"namespace"`
60 AuthorizationType string `json:"authorization_type"`
61}
62
63// ListClusters gets a list of all clusters in a project.
64//
65// GitLab API docs:
66// https://docs.gitlab.com/ee/api/project_clusters.html#list-project-clusters
67func (s *ProjectClustersService) ListClusters(pid interface{}, options ...OptionFunc) ([]*ProjectCluster, *Response, error) {
68 project, err := parseID(pid)
69 if err != nil {
70 return nil, nil, err
71 }
72 u := fmt.Sprintf("projects/%s/clusters", pathEscape(project))
73
74 req, err := s.client.NewRequest("GET", u, nil, options)
75 if err != nil {
76 return nil, nil, err
77 }
78
79 var pcs []*ProjectCluster
80 resp, err := s.client.Do(req, &pcs)
81 if err != nil {
82 return nil, resp, err
83 }
84
85 return pcs, resp, err
86}
87
88// GetCluster gets a cluster.
89//
90// GitLab API docs:
91// https://docs.gitlab.com/ee/api/project_clusters.html#get-a-single-project-cluster
92func (s *ProjectClustersService) GetCluster(pid interface{}, cluster int, options ...OptionFunc) (*ProjectCluster, *Response, error) {
93 project, err := parseID(pid)
94 if err != nil {
95 return nil, nil, err
96 }
97 u := fmt.Sprintf("projects/%s/clusters/%d", pathEscape(project), cluster)
98
99 req, err := s.client.NewRequest("GET", u, nil, options)
100 if err != nil {
101 return nil, nil, err
102 }
103
104 pc := new(ProjectCluster)
105 resp, err := s.client.Do(req, &pc)
106 if err != nil {
107 return nil, resp, err
108 }
109
110 return pc, resp, err
111}
112
113// AddClusterOptions represents the available AddCluster() options.
114//
115// GitLab API docs:
116// https://docs.gitlab.com/ee/api/project_clusters.html#add-existing-cluster-to-project
117type AddClusterOptions struct {
118 Name *string `url:"name,omitempty" json:"name,omitempty"`
119 Domain *string `url:"domain,omitempty" json:"domain,omitempty"`
120 Enabled *bool `url:"enabled,omitempty" json:"enabled,omitempty"`
121 Managed *bool `url:"managed,omitempty" json:"managed,omitempty"`
122 EnvironmentScope *string `url:"environment_scope,omitempty" json:"environment_scope,omitempty"`
123 PlatformKubernetes *AddPlatformKubernetesOptions `url:"platform_kubernetes_attributes,omitempty" json:"platform_kubernetes_attributes,omitempty"`
124}
125
126// AddPlatformKubernetesOptions represents the available PlatformKubernetes options for adding.
127type AddPlatformKubernetesOptions struct {
128 APIURL *string `url:"api_url,omitempty" json:"api_url,omitempty"`
129 Token *string `url:"token,omitempty" json:"token,omitempty"`
130 CaCert *string `url:"ca_cert,omitempty" json:"ca_cert,omitempty"`
131 Namespace *string `url:"namespace,omitempty" json:"namespace,omitempty"`
132 AuthorizationType *string `url:"authorization_type,omitempty" json:"authorization_type,omitempty"`
133}
134
135// AddCluster adds an existing cluster to the project.
136//
137// GitLab API docs:
138// https://docs.gitlab.com/ee/api/project_clusters.html#add-existing-cluster-to-project
139func (s *ProjectClustersService) AddCluster(pid interface{}, opt *AddClusterOptions, options ...OptionFunc) (*ProjectCluster, *Response, error) {
140 project, err := parseID(pid)
141 if err != nil {
142 return nil, nil, err
143 }
144 u := fmt.Sprintf("projects/%s/clusters/user", pathEscape(project))
145
146 req, err := s.client.NewRequest("POST", u, opt, options)
147 if err != nil {
148 return nil, nil, err
149 }
150
151 pc := new(ProjectCluster)
152 resp, err := s.client.Do(req, pc)
153 if err != nil {
154 return nil, resp, err
155 }
156
157 return pc, resp, err
158}
159
160// EditClusterOptions represents the available EditCluster() options.
161//
162// GitLab API docs:
163// https://docs.gitlab.com/ee/api/project_clusters.html#edit-project-cluster
164type EditClusterOptions struct {
165 Name *string `url:"name,omitempty" json:"name,omitempty"`
166 Domain *string `url:"domain,omitempty" json:"domain,omitempty"`
167 EnvironmentScope *string `url:"environment_scope,omitempty" json:"environment_scope,omitempty"`
168 PlatformKubernetes *EditPlatformKubernetesOptions `url:"platform_kubernetes_attributes,omitempty" json:"platform_kubernetes_attributes,omitempty"`
169}
170
171// EditPlatformKubernetesOptions represents the available PlatformKubernetes options for editing.
172type EditPlatformKubernetesOptions struct {
173 APIURL *string `url:"api_url,omitempty" json:"api_url,omitempty"`
174 Token *string `url:"token,omitempty" json:"token,omitempty"`
175 CaCert *string `url:"ca_cert,omitempty" json:"ca_cert,omitempty"`
176 Namespace *string `url:"namespace,omitempty" json:"namespace,omitempty"`
177}
178
179// EditCluster updates an existing project cluster.
180//
181// GitLab API docs:
182// https://docs.gitlab.com/ee/api/project_clusters.html#edit-project-cluster
183func (s *ProjectClustersService) EditCluster(pid interface{}, cluster int, opt *EditClusterOptions, options ...OptionFunc) (*ProjectCluster, *Response, error) {
184 project, err := parseID(pid)
185 if err != nil {
186 return nil, nil, err
187 }
188 u := fmt.Sprintf("projects/%s/clusters/%d", pathEscape(project), cluster)
189
190 req, err := s.client.NewRequest("PUT", u, opt, options)
191 if err != nil {
192 return nil, nil, err
193 }
194
195 pc := new(ProjectCluster)
196 resp, err := s.client.Do(req, pc)
197 if err != nil {
198 return nil, resp, err
199 }
200
201 return pc, resp, err
202}
203
204// DeleteCluster deletes an existing project cluster.
205//
206// GitLab API docs:
207// https://docs.gitlab.com/ee/api/project_clusters.html#delete-project-cluster
208func (s *ProjectClustersService) DeleteCluster(pid interface{}, cluster int, options ...OptionFunc) (*Response, error) {
209 project, err := parseID(pid)
210 if err != nil {
211 return nil, err
212 }
213 u := fmt.Sprintf("projects/%s/clusters/%d", pathEscape(project), cluster)
214
215 req, err := s.client.NewRequest("DELETE", u, nil, options)
216 if err != nil {
217 return nil, err
218 }
219
220 return s.client.Do(req, nil)
221}