...

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

Documentation: github.com/xanzy/go-gitlab

     1  //
     2  // Copyright 2023, James Hong
     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  )
    23  
    24  // GroupServiceAccount represents a GitLab service account user.
    25  //
    26  // GitLab API docs:
    27  // https://docs.gitlab.com/ee/api/groups.html#create-service-account-user
    28  type GroupServiceAccount struct {
    29  	ID       int    `json:"id"`
    30  	Name     string `json:"name"`
    31  	UserName string `json:"username"`
    32  }
    33  
    34  // CreateServiceAccount create a new service account user for a group.
    35  //
    36  // GitLab API docs:
    37  // https://docs.gitlab.com/ee/api/groups.html#create-service-account-user
    38  func (s *GroupsService) CreateServiceAccount(gid interface{}, options ...RequestOptionFunc) (*GroupServiceAccount, *Response, error) {
    39  	group, err := parseID(gid)
    40  	if err != nil {
    41  		return nil, nil, err
    42  	}
    43  	u := fmt.Sprintf("groups/%s/service_accounts", PathEscape(group))
    44  
    45  	req, err := s.client.NewRequest(http.MethodPost, u, nil, options)
    46  	if err != nil {
    47  		return nil, nil, err
    48  	}
    49  
    50  	sa := new(GroupServiceAccount)
    51  	resp, err := s.client.Do(req, sa)
    52  	if err != nil {
    53  		return nil, resp, err
    54  	}
    55  
    56  	return sa, resp, nil
    57  }
    58  
    59  // CreateServiceAccountPersonalAccessTokenOptions represents the available
    60  // CreateServiceAccountPersonalAccessToken() options.
    61  //
    62  // GitLab API docs:
    63  // https://docs.gitlab.com/ee/api/groups.html#create-personal-access-token-for-service-account-user
    64  type CreateServiceAccountPersonalAccessTokenOptions struct {
    65  	Scopes    *[]string `url:"scopes,omitempty" json:"scopes,omitempty"`
    66  	Name      *string   `url:"name,omitempty" json:"name,omitempty"`
    67  	ExpiresAt *ISOTime  `url:"expires_at,omitempty" json:"expires_at,omitempty"`
    68  }
    69  
    70  // CreateServiceAccountPersonalAccessToken add a new Personal Access Token for a
    71  // service account user for a group.
    72  //
    73  // GitLab API docs:
    74  // https://docs.gitlab.com/ee/api/groups.html#create-personal-access-token-for-service-account-user
    75  func (s *GroupsService) CreateServiceAccountPersonalAccessToken(gid interface{}, serviceAccount int, opt *CreateServiceAccountPersonalAccessTokenOptions, options ...RequestOptionFunc) (*PersonalAccessToken, *Response, error) {
    76  	group, err := parseID(gid)
    77  	if err != nil {
    78  		return nil, nil, err
    79  	}
    80  	u := fmt.Sprintf("groups/%s/service_accounts/%d/personal_access_tokens", PathEscape(group), serviceAccount)
    81  
    82  	req, err := s.client.NewRequest(http.MethodPost, u, opt, options)
    83  	if err != nil {
    84  		return nil, nil, err
    85  	}
    86  
    87  	pat := new(PersonalAccessToken)
    88  	resp, err := s.client.Do(req, pat)
    89  	if err != nil {
    90  		return nil, resp, err
    91  	}
    92  
    93  	return pat, resp, nil
    94  }
    95  
    96  // RotateServiceAccountPersonalAccessToken rotates a Personal Access Token for a
    97  // service account user for a group.
    98  //
    99  // GitLab API docs: https://docs.gitlab.com/ee/api/groups.html#create-personal-access-token-for-service-account-user
   100  func (s *GroupsService) RotateServiceAccountPersonalAccessToken(gid interface{}, serviceAccount, token int, options ...RequestOptionFunc) (*PersonalAccessToken, *Response, error) {
   101  	group, err := parseID(gid)
   102  	if err != nil {
   103  		return nil, nil, err
   104  	}
   105  	u := fmt.Sprintf("groups/%s/service_accounts/%d/personal_access_tokens/%d/rotate", PathEscape(group), serviceAccount, token)
   106  
   107  	req, err := s.client.NewRequest(http.MethodPost, u, nil, options)
   108  	if err != nil {
   109  		return nil, nil, err
   110  	}
   111  
   112  	pat := new(PersonalAccessToken)
   113  	resp, err := s.client.Do(req, pat)
   114  	if err != nil {
   115  		return nil, resp, err
   116  	}
   117  
   118  	return pat, resp, nil
   119  }
   120  

View as plain text