...

Source file src/github.com/xanzy/go-gitlab/group_clusters.go

Documentation: github.com/xanzy/go-gitlab

     1  //
     2  // Copyright 2021, 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  
    17  package gitlab
    18  
    19  import (
    20  	"fmt"
    21  	"net/http"
    22  	"time"
    23  )
    24  
    25  // GroupClustersService handles communication with the
    26  // group clusters related methods of the GitLab API.
    27  //
    28  // GitLab API docs:
    29  // https://docs.gitlab.com/ee/api/group_clusters.html
    30  type GroupClustersService struct {
    31  	client *Client
    32  }
    33  
    34  // GroupCluster represents a GitLab Group Cluster.
    35  //
    36  // GitLab API docs: https://docs.gitlab.com/ee/api/group_clusters.html
    37  type GroupCluster struct {
    38  	ID                 int                 `json:"id"`
    39  	Name               string              `json:"name"`
    40  	Domain             string              `json:"domain"`
    41  	CreatedAt          *time.Time          `json:"created_at"`
    42  	Managed            bool                `json:"managed"`
    43  	Enabled            bool                `json:"enabled"`
    44  	ProviderType       string              `json:"provider_type"`
    45  	PlatformType       string              `json:"platform_type"`
    46  	EnvironmentScope   string              `json:"environment_scope"`
    47  	ClusterType        string              `json:"cluster_type"`
    48  	User               *User               `json:"user"`
    49  	PlatformKubernetes *PlatformKubernetes `json:"platform_kubernetes"`
    50  	ManagementProject  *ManagementProject  `json:"management_project"`
    51  	Group              *Group              `json:"group"`
    52  }
    53  
    54  func (v GroupCluster) String() string {
    55  	return Stringify(v)
    56  }
    57  
    58  // ListClusters gets a list of all clusters in a group.
    59  //
    60  // GitLab API docs:
    61  // https://docs.gitlab.com/ee/api/group_clusters.html#list-group-clusters
    62  func (s *GroupClustersService) ListClusters(pid interface{}, options ...RequestOptionFunc) ([]*GroupCluster, *Response, error) {
    63  	group, err := parseID(pid)
    64  	if err != nil {
    65  		return nil, nil, err
    66  	}
    67  	u := fmt.Sprintf("groups/%s/clusters", PathEscape(group))
    68  
    69  	req, err := s.client.NewRequest(http.MethodGet, u, nil, options)
    70  	if err != nil {
    71  		return nil, nil, err
    72  	}
    73  
    74  	var pcs []*GroupCluster
    75  	resp, err := s.client.Do(req, &pcs)
    76  	if err != nil {
    77  		return nil, resp, err
    78  	}
    79  
    80  	return pcs, resp, nil
    81  }
    82  
    83  // GetCluster gets a cluster.
    84  //
    85  // GitLab API docs:
    86  // https://docs.gitlab.com/ee/api/group_clusters.html#get-a-single-group-cluster
    87  func (s *GroupClustersService) GetCluster(pid interface{}, cluster int, options ...RequestOptionFunc) (*GroupCluster, *Response, error) {
    88  	group, err := parseID(pid)
    89  	if err != nil {
    90  		return nil, nil, err
    91  	}
    92  	u := fmt.Sprintf("groups/%s/clusters/%d", PathEscape(group), cluster)
    93  
    94  	req, err := s.client.NewRequest(http.MethodGet, u, nil, options)
    95  	if err != nil {
    96  		return nil, nil, err
    97  	}
    98  
    99  	gc := new(GroupCluster)
   100  	resp, err := s.client.Do(req, &gc)
   101  	if err != nil {
   102  		return nil, resp, err
   103  	}
   104  
   105  	return gc, resp, nil
   106  }
   107  
   108  // AddGroupClusterOptions represents the available AddCluster() options.
   109  //
   110  // GitLab API docs:
   111  // https://docs.gitlab.com/ee/api/group_clusters.html#add-existing-cluster-to-group
   112  type AddGroupClusterOptions struct {
   113  	Name                *string                            `url:"name,omitempty" json:"name,omitempty"`
   114  	Domain              *string                            `url:"domain,omitempty" json:"domain,omitempty"`
   115  	ManagementProjectID *string                            `url:"management_project_id,omitempty" json:"management_project_id,omitempty"`
   116  	Enabled             *bool                              `url:"enabled,omitempty" json:"enabled,omitempty"`
   117  	Managed             *bool                              `url:"managed,omitempty" json:"managed,omitempty"`
   118  	EnvironmentScope    *string                            `url:"environment_scope,omitempty" json:"environment_scope,omitempty"`
   119  	PlatformKubernetes  *AddGroupPlatformKubernetesOptions `url:"platform_kubernetes_attributes,omitempty" json:"platform_kubernetes_attributes,omitempty"`
   120  }
   121  
   122  // AddGroupPlatformKubernetesOptions represents the available PlatformKubernetes options for adding.
   123  type AddGroupPlatformKubernetesOptions struct {
   124  	APIURL            *string `url:"api_url,omitempty" json:"api_url,omitempty"`
   125  	Token             *string `url:"token,omitempty" json:"token,omitempty"`
   126  	CaCert            *string `url:"ca_cert,omitempty" json:"ca_cert,omitempty"`
   127  	Namespace         *string `url:"namespace,omitempty" json:"namespace,omitempty"`
   128  	AuthorizationType *string `url:"authorization_type,omitempty" json:"authorization_type,omitempty"`
   129  }
   130  
   131  // AddCluster adds an existing cluster to the group.
   132  //
   133  // GitLab API docs:
   134  // https://docs.gitlab.com/ee/api/group_clusters.html#add-existing-cluster-to-group
   135  func (s *GroupClustersService) AddCluster(pid interface{}, opt *AddGroupClusterOptions, options ...RequestOptionFunc) (*GroupCluster, *Response, error) {
   136  	group, err := parseID(pid)
   137  	if err != nil {
   138  		return nil, nil, err
   139  	}
   140  	u := fmt.Sprintf("groups/%s/clusters/user", PathEscape(group))
   141  
   142  	req, err := s.client.NewRequest(http.MethodPost, u, opt, options)
   143  	if err != nil {
   144  		return nil, nil, err
   145  	}
   146  
   147  	gc := new(GroupCluster)
   148  	resp, err := s.client.Do(req, gc)
   149  	if err != nil {
   150  		return nil, resp, err
   151  	}
   152  
   153  	return gc, resp, nil
   154  }
   155  
   156  // EditGroupClusterOptions represents the available EditCluster() options.
   157  //
   158  // GitLab API docs:
   159  // https://docs.gitlab.com/ee/api/group_clusters.html#edit-group-cluster
   160  type EditGroupClusterOptions struct {
   161  	Name                *string                             `url:"name,omitempty" json:"name,omitempty"`
   162  	Domain              *string                             `url:"domain,omitempty" json:"domain,omitempty"`
   163  	EnvironmentScope    *string                             `url:"environment_scope,omitempty" json:"environment_scope,omitempty"`
   164  	PlatformKubernetes  *EditGroupPlatformKubernetesOptions `url:"platform_kubernetes_attributes,omitempty" json:"platform_kubernetes_attributes,omitempty"`
   165  	ManagementProjectID *string                             `url:"management_project_id,omitempty" json:"management_project_id,omitempty"`
   166  }
   167  
   168  // EditGroupPlatformKubernetesOptions represents the available PlatformKubernetes options for editing.
   169  type EditGroupPlatformKubernetesOptions struct {
   170  	APIURL *string `url:"api_url,omitempty" json:"api_url,omitempty"`
   171  	Token  *string `url:"token,omitempty" json:"token,omitempty"`
   172  	CaCert *string `url:"ca_cert,omitempty" json:"ca_cert,omitempty"`
   173  }
   174  
   175  // EditCluster updates an existing group cluster.
   176  //
   177  // GitLab API docs:
   178  // https://docs.gitlab.com/ee/api/group_clusters.html#edit-group-cluster
   179  func (s *GroupClustersService) EditCluster(pid interface{}, cluster int, opt *EditGroupClusterOptions, options ...RequestOptionFunc) (*GroupCluster, *Response, error) {
   180  	group, err := parseID(pid)
   181  	if err != nil {
   182  		return nil, nil, err
   183  	}
   184  	u := fmt.Sprintf("groups/%s/clusters/%d", PathEscape(group), cluster)
   185  
   186  	req, err := s.client.NewRequest(http.MethodPut, u, opt, options)
   187  	if err != nil {
   188  		return nil, nil, err
   189  	}
   190  
   191  	gc := new(GroupCluster)
   192  	resp, err := s.client.Do(req, gc)
   193  	if err != nil {
   194  		return nil, resp, err
   195  	}
   196  
   197  	return gc, resp, nil
   198  }
   199  
   200  // DeleteCluster deletes an existing group cluster.
   201  //
   202  // GitLab API docs:
   203  // https://docs.gitlab.com/ee/api/group_clusters.html#delete-group-cluster
   204  func (s *GroupClustersService) DeleteCluster(pid interface{}, cluster int, options ...RequestOptionFunc) (*Response, error) {
   205  	group, err := parseID(pid)
   206  	if err != nil {
   207  		return nil, err
   208  	}
   209  	u := fmt.Sprintf("groups/%s/clusters/%d", PathEscape(group), cluster)
   210  
   211  	req, err := s.client.NewRequest(http.MethodDelete, u, nil, options)
   212  	if err != nil {
   213  		return nil, err
   214  	}
   215  
   216  	return s.client.Do(req, nil)
   217  }
   218  

View as plain text