...

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

Documentation: github.com/xanzy/go-gitlab

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

View as plain text