|
| 1 | +// |
| 2 | +// Copyright 2020, Serena Fang |
| 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 | + |
| 17 | +package gitlab |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "time" |
| 22 | +) |
| 23 | + |
| 24 | +// InstanceClustersService handles communication with the |
| 25 | +// instance clusters related methods of the GitLab API. |
| 26 | +// |
| 27 | +// GitLab API docs: |
| 28 | +// https://docs.gitlab.com/ee/api/instance_clusters.html |
| 29 | +type InstanceClustersService struct { |
| 30 | + client *Client |
| 31 | +} |
| 32 | + |
| 33 | +// InstanceCluster represents a GitLab Instance Cluster. |
| 34 | +// |
| 35 | +// GitLab API docs: https://docs.gitlab.com/ee/api/instance_clusters.html |
| 36 | +type InstanceCluster 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 | + ManagementProject *ManagementProject `json:"management_project"` |
| 48 | +} |
| 49 | + |
| 50 | +func (v InstanceCluster) String() string { |
| 51 | + return Stringify(v) |
| 52 | +} |
| 53 | + |
| 54 | +// ListClusters gets a list of all instance clusters. |
| 55 | +// |
| 56 | +// GitLab API docs: |
| 57 | +// https://docs.gitlab.com/ee/api/instance_clusters.html#list-instance-clusters |
| 58 | +func (s *InstanceClustersService) ListClusters(options ...RequestOptionFunc) ([]*InstanceCluster, *Response, error) { |
| 59 | + u := fmt.Sprintf("admin/clusters") |
| 60 | + |
| 61 | + req, err := s.client.NewRequest("GET", u, nil, options) |
| 62 | + if err != nil { |
| 63 | + return nil, nil, err |
| 64 | + } |
| 65 | + |
| 66 | + var ics []*InstanceCluster |
| 67 | + resp, err := s.client.Do(req, &ics) |
| 68 | + if err != nil { |
| 69 | + return nil, resp, err |
| 70 | + } |
| 71 | + |
| 72 | + return ics, resp, err |
| 73 | +} |
| 74 | + |
| 75 | +// GetCluster gets an instance cluster. |
| 76 | +// |
| 77 | +// GitLab API docs: |
| 78 | +// https://docs.gitlab.com/ee/api/instance_clusters.html#get-a-single-instance-cluster |
| 79 | +func (s *InstanceClustersService) GetCluster(cluster int, options ...RequestOptionFunc) (*InstanceCluster, *Response, error) { |
| 80 | + u := fmt.Sprintf("admin/clusters/%d", cluster) |
| 81 | + |
| 82 | + req, err := s.client.NewRequest("GET", u, nil, options) |
| 83 | + if err != nil { |
| 84 | + return nil, nil, err |
| 85 | + } |
| 86 | + |
| 87 | + ic := new(InstanceCluster) |
| 88 | + resp, err := s.client.Do(req, &ic) |
| 89 | + if err != nil { |
| 90 | + return nil, resp, err |
| 91 | + } |
| 92 | + |
| 93 | + return ic, resp, err |
| 94 | +} |
| 95 | + |
| 96 | +// AddCluster adds an existing cluster to the instance. |
| 97 | +// |
| 98 | +// GitLab API docs: |
| 99 | +// https://docs.gitlab.com/ee/api/instance_clusters.html#add-existing-instance-cluster |
| 100 | +func (s *InstanceClustersService) AddCluster(opt *AddClusterOptions, options ...RequestOptionFunc) (*InstanceCluster, *Response, error) { |
| 101 | + u := fmt.Sprintf("admin/clusters/add") |
| 102 | + |
| 103 | + req, err := s.client.NewRequest("POST", u, opt, options) |
| 104 | + if err != nil { |
| 105 | + return nil, nil, err |
| 106 | + } |
| 107 | + |
| 108 | + ic := new(InstanceCluster) |
| 109 | + resp, err := s.client.Do(req, ic) |
| 110 | + if err != nil { |
| 111 | + return nil, resp, err |
| 112 | + } |
| 113 | + |
| 114 | + return ic, resp, err |
| 115 | +} |
| 116 | + |
| 117 | +// EditCluster updates an existing instance cluster. |
| 118 | +// |
| 119 | +// GitLab API docs: |
| 120 | +// https://docs.gitlab.com/ee/api/instance_clusters.html#edit-instance-cluster |
| 121 | +func (s *InstanceClustersService) EditCluster(cluster int, opt *EditClusterOptions, options ...RequestOptionFunc) (*InstanceCluster, *Response, error) { |
| 122 | + u := fmt.Sprintf("admin/clusters/%d", cluster) |
| 123 | + |
| 124 | + req, err := s.client.NewRequest("PUT", u, opt, options) |
| 125 | + if err != nil { |
| 126 | + return nil, nil, err |
| 127 | + } |
| 128 | + |
| 129 | + ic := new(InstanceCluster) |
| 130 | + resp, err := s.client.Do(req, ic) |
| 131 | + if err != nil { |
| 132 | + return nil, resp, err |
| 133 | + } |
| 134 | + |
| 135 | + return ic, resp, err |
| 136 | +} |
| 137 | + |
| 138 | +// DeleteCluster deletes an existing instance cluster. |
| 139 | +// |
| 140 | +// GitLab API docs: |
| 141 | +// https://docs.gitlab.com/ee/api/instance_clusters.html#delete-instance-cluster |
| 142 | +func (s *InstanceClustersService) DeleteCluster(cluster int, options ...RequestOptionFunc) (*Response, error) { |
| 143 | + u := fmt.Sprintf("admin/clusters/%d", cluster) |
| 144 | + |
| 145 | + req, err := s.client.NewRequest("DELETE", u, nil, options) |
| 146 | + if err != nil { |
| 147 | + return nil, err |
| 148 | + } |
| 149 | + |
| 150 | + return s.client.Do(req, nil) |
| 151 | +} |
0 commit comments