...

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

Documentation: github.com/xanzy/go-gitlab

     1  //
     2  // Copyright 2021, Patrick Webster
     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  	"net/url"
    23  )
    24  
    25  // InstanceVariablesService handles communication with the
    26  // instance level CI variables related methods of the GitLab API.
    27  //
    28  // GitLab API docs:
    29  // https://docs.gitlab.com/ee/api/instance_level_ci_variables.html
    30  type InstanceVariablesService struct {
    31  	client *Client
    32  }
    33  
    34  // InstanceVariable represents a GitLab instance level CI Variable.
    35  //
    36  // GitLab API docs:
    37  // https://docs.gitlab.com/ee/api/instance_level_ci_variables.html
    38  type InstanceVariable struct {
    39  	Key          string            `json:"key"`
    40  	Value        string            `json:"value"`
    41  	VariableType VariableTypeValue `json:"variable_type"`
    42  	Protected    bool              `json:"protected"`
    43  	Masked       bool              `json:"masked"`
    44  	Raw          bool              `json:"raw"`
    45  	Description  string            `json:"description"`
    46  }
    47  
    48  func (v InstanceVariable) String() string {
    49  	return Stringify(v)
    50  }
    51  
    52  // ListInstanceVariablesOptions represents the available options for listing variables
    53  // for an instance.
    54  //
    55  // GitLab API docs:
    56  // https://docs.gitlab.com/ee/api/instance_level_ci_variables.html#list-all-instance-variables
    57  type ListInstanceVariablesOptions ListOptions
    58  
    59  // ListVariables gets a list of all variables for an instance.
    60  //
    61  // GitLab API docs:
    62  // https://docs.gitlab.com/ee/api/instance_level_ci_variables.html#list-all-instance-variables
    63  func (s *InstanceVariablesService) ListVariables(opt *ListInstanceVariablesOptions, options ...RequestOptionFunc) ([]*InstanceVariable, *Response, error) {
    64  	u := "admin/ci/variables"
    65  
    66  	req, err := s.client.NewRequest(http.MethodGet, u, opt, options)
    67  	if err != nil {
    68  		return nil, nil, err
    69  	}
    70  
    71  	var vs []*InstanceVariable
    72  	resp, err := s.client.Do(req, &vs)
    73  	if err != nil {
    74  		return nil, resp, err
    75  	}
    76  
    77  	return vs, resp, nil
    78  }
    79  
    80  // GetVariable gets a variable.
    81  //
    82  // GitLab API docs:
    83  // https://docs.gitlab.com/ee/api/instance_level_ci_variables.html#show-instance-variable-details
    84  func (s *InstanceVariablesService) GetVariable(key string, options ...RequestOptionFunc) (*InstanceVariable, *Response, error) {
    85  	u := fmt.Sprintf("admin/ci/variables/%s", url.PathEscape(key))
    86  
    87  	req, err := s.client.NewRequest(http.MethodGet, u, nil, options)
    88  	if err != nil {
    89  		return nil, nil, err
    90  	}
    91  
    92  	v := new(InstanceVariable)
    93  	resp, err := s.client.Do(req, v)
    94  	if err != nil {
    95  		return nil, resp, err
    96  	}
    97  
    98  	return v, resp, nil
    99  }
   100  
   101  // CreateInstanceVariableOptions represents the available CreateVariable()
   102  // options.
   103  //
   104  // GitLab API docs:
   105  // https://docs.gitlab.com/ee/api/instance_level_ci_variables.html#create-instance-variable
   106  type CreateInstanceVariableOptions struct {
   107  	Key          *string            `url:"key,omitempty" json:"key,omitempty"`
   108  	Value        *string            `url:"value,omitempty" json:"value,omitempty"`
   109  	Description  *string            `url:"description,omitempty" json:"description,omitempty"`
   110  	Masked       *bool              `url:"masked,omitempty" json:"masked,omitempty"`
   111  	Protected    *bool              `url:"protected,omitempty" json:"protected,omitempty"`
   112  	Raw          *bool              `url:"raw,omitempty" json:"raw,omitempty"`
   113  	VariableType *VariableTypeValue `url:"variable_type,omitempty" json:"variable_type,omitempty"`
   114  }
   115  
   116  // CreateVariable creates a new instance level CI variable.
   117  //
   118  // GitLab API docs:
   119  // https://docs.gitlab.com/ee/api/instance_level_ci_variables.html#create-instance-variable
   120  func (s *InstanceVariablesService) CreateVariable(opt *CreateInstanceVariableOptions, options ...RequestOptionFunc) (*InstanceVariable, *Response, error) {
   121  	u := "admin/ci/variables"
   122  
   123  	req, err := s.client.NewRequest(http.MethodPost, u, opt, options)
   124  	if err != nil {
   125  		return nil, nil, err
   126  	}
   127  
   128  	v := new(InstanceVariable)
   129  	resp, err := s.client.Do(req, v)
   130  	if err != nil {
   131  		return nil, resp, err
   132  	}
   133  
   134  	return v, resp, nil
   135  }
   136  
   137  // UpdateInstanceVariableOptions represents the available UpdateVariable()
   138  // options.
   139  //
   140  // GitLab API docs:
   141  // https://docs.gitlab.com/ee/api/instance_level_ci_variables.html#update-instance-variable
   142  type UpdateInstanceVariableOptions struct {
   143  	Value        *string            `url:"value,omitempty" json:"value,omitempty"`
   144  	Description  *string            `url:"description,omitempty" json:"description,omitempty"`
   145  	Masked       *bool              `url:"masked,omitempty" json:"masked,omitempty"`
   146  	Protected    *bool              `url:"protected,omitempty" json:"protected,omitempty"`
   147  	Raw          *bool              `url:"raw,omitempty" json:"raw,omitempty"`
   148  	VariableType *VariableTypeValue `url:"variable_type,omitempty" json:"variable_type,omitempty"`
   149  }
   150  
   151  // UpdateVariable updates the position of an existing
   152  // instance level CI variable.
   153  //
   154  // GitLab API docs:
   155  // https://docs.gitlab.com/ee/api/instance_level_ci_variables.html#update-instance-variable
   156  func (s *InstanceVariablesService) UpdateVariable(key string, opt *UpdateInstanceVariableOptions, options ...RequestOptionFunc) (*InstanceVariable, *Response, error) {
   157  	u := fmt.Sprintf("admin/ci/variables/%s", url.PathEscape(key))
   158  
   159  	req, err := s.client.NewRequest(http.MethodPut, u, opt, options)
   160  	if err != nil {
   161  		return nil, nil, err
   162  	}
   163  
   164  	v := new(InstanceVariable)
   165  	resp, err := s.client.Do(req, v)
   166  	if err != nil {
   167  		return nil, resp, err
   168  	}
   169  
   170  	return v, resp, nil
   171  }
   172  
   173  // RemoveVariable removes an instance level CI variable.
   174  //
   175  // GitLab API docs:
   176  // https://docs.gitlab.com/ee/api/instance_level_ci_variables.html#remove-instance-variable
   177  func (s *InstanceVariablesService) RemoveVariable(key string, options ...RequestOptionFunc) (*Response, error) {
   178  	u := fmt.Sprintf("admin/ci/variables/%s", url.PathEscape(key))
   179  
   180  	req, err := s.client.NewRequest(http.MethodDelete, u, nil, options)
   181  	if err != nil {
   182  		return nil, err
   183  	}
   184  
   185  	return s.client.Do(req, nil)
   186  }
   187  

View as plain text