...

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

Documentation: github.com/xanzy/go-gitlab

     1  //
     2  // Copyright 2022, Timo Furrer <tuxtimo@gmail.com>
     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  // ClusterAgentsService handles communication with the cluster agents related
    26  // methods of the GitLab API.
    27  //
    28  // GitLab API docs: https://docs.gitlab.com/ee/api/cluster_agents.html
    29  type ClusterAgentsService struct {
    30  	client *Client
    31  }
    32  
    33  // Agent represents a GitLab agent for Kubernetes.
    34  //
    35  // GitLab API docs: https://docs.gitlab.com/ee/api/cluster_agents.html
    36  type Agent struct {
    37  	ID              int           `json:"id"`
    38  	Name            string        `json:"name"`
    39  	CreatedAt       *time.Time    `json:"created_at"`
    40  	CreatedByUserID int           `json:"created_by_user_id"`
    41  	ConfigProject   ConfigProject `json:"config_project"`
    42  }
    43  
    44  type ConfigProject struct {
    45  	ID                int        `json:"id"`
    46  	Description       string     `json:"description"`
    47  	Name              string     `json:"name"`
    48  	NameWithNamespace string     `json:"name_with_namespace"`
    49  	Path              string     `json:"path"`
    50  	PathWithNamespace string     `json:"path_with_namespace"`
    51  	CreatedAt         *time.Time `json:"created_at"`
    52  }
    53  
    54  func (a Agent) String() string {
    55  	return Stringify(a)
    56  }
    57  
    58  // AgentToken represents a GitLab agent token.
    59  //
    60  // GitLab API docs:
    61  // https://docs.gitlab.com/ee/api/cluster_agents.html#list-tokens-for-an-agent
    62  type AgentToken struct {
    63  	ID              int        `json:"id"`
    64  	Name            string     `json:"name"`
    65  	Description     string     `json:"description"`
    66  	AgentID         int        `json:"agent_id"`
    67  	Status          string     `json:"status"`
    68  	CreatedAt       *time.Time `json:"created_at"`
    69  	CreatedByUserID int        `json:"created_by_user_id"`
    70  	LastUsedAt      *time.Time `json:"last_used_at"`
    71  	Token           string     `json:"token"`
    72  }
    73  
    74  func (a AgentToken) String() string {
    75  	return Stringify(a)
    76  }
    77  
    78  // ListAgentsOptions represents the available ListAgents() options.
    79  //
    80  // GitLab API docs:
    81  // https://docs.gitlab.com/ee/api/cluster_agents.html#list-the-agents-for-a-project
    82  type ListAgentsOptions ListOptions
    83  
    84  // ListAgents returns a list of agents registered for the project.
    85  //
    86  // GitLab API docs:
    87  // https://docs.gitlab.com/ee/api/cluster_agents.html#list-the-agents-for-a-project
    88  func (s *ClusterAgentsService) ListAgents(pid interface{}, opt *ListAgentsOptions, options ...RequestOptionFunc) ([]*Agent, *Response, error) {
    89  	project, err := parseID(pid)
    90  	if err != nil {
    91  		return nil, nil, err
    92  	}
    93  	uri := fmt.Sprintf("projects/%s/cluster_agents", PathEscape(project))
    94  
    95  	req, err := s.client.NewRequest(http.MethodGet, uri, opt, options)
    96  	if err != nil {
    97  		return nil, nil, err
    98  	}
    99  
   100  	var as []*Agent
   101  	resp, err := s.client.Do(req, &as)
   102  	if err != nil {
   103  		return nil, resp, err
   104  	}
   105  
   106  	return as, resp, nil
   107  }
   108  
   109  // GetAgent gets a single agent details.
   110  //
   111  // GitLab API docs:
   112  // https://docs.gitlab.com/ee/api/cluster_agents.html#get-details-about-an-agent
   113  func (s *ClusterAgentsService) GetAgent(pid interface{}, id int, options ...RequestOptionFunc) (*Agent, *Response, error) {
   114  	project, err := parseID(pid)
   115  	if err != nil {
   116  		return nil, nil, err
   117  	}
   118  	uri := fmt.Sprintf("projects/%s/cluster_agents/%d", PathEscape(project), id)
   119  
   120  	req, err := s.client.NewRequest(http.MethodGet, uri, nil, options)
   121  	if err != nil {
   122  		return nil, nil, err
   123  	}
   124  
   125  	a := new(Agent)
   126  	resp, err := s.client.Do(req, a)
   127  	if err != nil {
   128  		return nil, resp, err
   129  	}
   130  
   131  	return a, resp, nil
   132  }
   133  
   134  // RegisterAgentOptions represents the available RegisterAgent()
   135  // options.
   136  //
   137  // GitLab API docs:
   138  // https://docs.gitlab.com/ee/api/cluster_agents.html#register-an-agent-with-a-project
   139  type RegisterAgentOptions struct {
   140  	Name *string `url:"name,omitempty" json:"name,omitempty"`
   141  }
   142  
   143  // RegisterAgent registers an agent to the project.
   144  //
   145  // GitLab API docs:
   146  // https://docs.gitlab.com/ee/api/cluster_agents.html#register-an-agent-with-a-project
   147  func (s *ClusterAgentsService) RegisterAgent(pid interface{}, opt *RegisterAgentOptions, options ...RequestOptionFunc) (*Agent, *Response, error) {
   148  	project, err := parseID(pid)
   149  	if err != nil {
   150  		return nil, nil, err
   151  	}
   152  	uri := fmt.Sprintf("projects/%s/cluster_agents", PathEscape(project))
   153  
   154  	req, err := s.client.NewRequest(http.MethodPost, uri, opt, options)
   155  	if err != nil {
   156  		return nil, nil, err
   157  	}
   158  
   159  	a := new(Agent)
   160  	resp, err := s.client.Do(req, a)
   161  	if err != nil {
   162  		return nil, resp, err
   163  	}
   164  
   165  	return a, resp, nil
   166  }
   167  
   168  // DeleteAgent deletes an existing agent registration.
   169  //
   170  // GitLab API docs:
   171  // https://docs.gitlab.com/ee/api/cluster_agents.html#delete-a-registered-agent
   172  func (s *ClusterAgentsService) DeleteAgent(pid interface{}, id int, options ...RequestOptionFunc) (*Response, error) {
   173  	project, err := parseID(pid)
   174  	if err != nil {
   175  		return nil, err
   176  	}
   177  	uri := fmt.Sprintf("projects/%s/cluster_agents/%d", PathEscape(project), id)
   178  
   179  	req, err := s.client.NewRequest(http.MethodDelete, uri, nil, options)
   180  	if err != nil {
   181  		return nil, err
   182  	}
   183  
   184  	return s.client.Do(req, nil)
   185  }
   186  
   187  // ListAgentTokensOptions represents the available ListAgentTokens() options.
   188  //
   189  // GitLab API docs:
   190  // https://docs.gitlab.com/ee/api/cluster_agents.html#list-tokens-for-an-agent
   191  type ListAgentTokensOptions ListOptions
   192  
   193  // ListAgentTokens returns a list of tokens for an agent.
   194  //
   195  // GitLab API docs:
   196  // https://docs.gitlab.com/ee/api/cluster_agents.html#list-tokens-for-an-agent
   197  func (s *ClusterAgentsService) ListAgentTokens(pid interface{}, aid int, opt *ListAgentTokensOptions, options ...RequestOptionFunc) ([]*AgentToken, *Response, error) {
   198  	project, err := parseID(pid)
   199  	if err != nil {
   200  		return nil, nil, err
   201  	}
   202  	uri := fmt.Sprintf("projects/%s/cluster_agents/%d/tokens", PathEscape(project), aid)
   203  
   204  	req, err := s.client.NewRequest(http.MethodGet, uri, opt, options)
   205  	if err != nil {
   206  		return nil, nil, err
   207  	}
   208  
   209  	var ats []*AgentToken
   210  	resp, err := s.client.Do(req, &ats)
   211  	if err != nil {
   212  		return nil, resp, err
   213  	}
   214  
   215  	return ats, resp, nil
   216  }
   217  
   218  // GetAgentToken gets a single agent token.
   219  //
   220  // GitLab API docs:
   221  // https://docs.gitlab.com/ee/api/cluster_agents.html#get-a-single-agent-token
   222  func (s *ClusterAgentsService) GetAgentToken(pid interface{}, aid int, id int, options ...RequestOptionFunc) (*AgentToken, *Response, error) {
   223  	project, err := parseID(pid)
   224  	if err != nil {
   225  		return nil, nil, err
   226  	}
   227  	uri := fmt.Sprintf("projects/%s/cluster_agents/%d/tokens/%d", PathEscape(project), aid, id)
   228  
   229  	req, err := s.client.NewRequest(http.MethodGet, uri, nil, options)
   230  	if err != nil {
   231  		return nil, nil, err
   232  	}
   233  
   234  	at := new(AgentToken)
   235  	resp, err := s.client.Do(req, at)
   236  	if err != nil {
   237  		return nil, resp, err
   238  	}
   239  
   240  	return at, resp, nil
   241  }
   242  
   243  // CreateAgentTokenOptions represents the available CreateAgentToken() options.
   244  //
   245  // GitLab API docs:
   246  // https://docs.gitlab.com/ee/api/cluster_agents.html#create-an-agent-token
   247  type CreateAgentTokenOptions struct {
   248  	Name        *string `url:"name,omitempty" json:"name,omitempty"`
   249  	Description *string `url:"description,omitempty" json:"description,omitempty"`
   250  }
   251  
   252  // CreateAgentToken creates a new token for an agent.
   253  //
   254  // GitLab API docs:
   255  // https://docs.gitlab.com/ee/api/cluster_agents.html#create-an-agent-token
   256  func (s *ClusterAgentsService) CreateAgentToken(pid interface{}, aid int, opt *CreateAgentTokenOptions, options ...RequestOptionFunc) (*AgentToken, *Response, error) {
   257  	project, err := parseID(pid)
   258  	if err != nil {
   259  		return nil, nil, err
   260  	}
   261  	uri := fmt.Sprintf("projects/%s/cluster_agents/%d/tokens", PathEscape(project), aid)
   262  
   263  	req, err := s.client.NewRequest(http.MethodPost, uri, opt, options)
   264  	if err != nil {
   265  		return nil, nil, err
   266  	}
   267  
   268  	at := new(AgentToken)
   269  	resp, err := s.client.Do(req, at)
   270  	if err != nil {
   271  		return nil, resp, err
   272  	}
   273  
   274  	return at, resp, nil
   275  }
   276  
   277  // RevokeAgentToken revokes an agent token.
   278  //
   279  // GitLab API docs:
   280  // https://docs.gitlab.com/ee/api/cluster_agents.html#revoke-an-agent-token
   281  func (s *ClusterAgentsService) RevokeAgentToken(pid interface{}, aid int, id int, options ...RequestOptionFunc) (*Response, error) {
   282  	project, err := parseID(pid)
   283  	if err != nil {
   284  		return nil, err
   285  	}
   286  	uri := fmt.Sprintf("projects/%s/cluster_agents/%d/tokens/%d", PathEscape(project), aid, id)
   287  
   288  	req, err := s.client.NewRequest(http.MethodDelete, uri, nil, options)
   289  	if err != nil {
   290  		return nil, err
   291  	}
   292  
   293  	return s.client.Do(req, nil)
   294  }
   295  

View as plain text