...

Source file src/github.com/xanzy/go-gitlab/license.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  	"time"
    23  )
    24  
    25  // LicenseService handles communication with the license
    26  // related methods of the GitLab API.
    27  //
    28  // GitLab API docs:
    29  // https://docs.gitlab.com/ee/api/license.html
    30  type LicenseService struct {
    31  	client *Client
    32  }
    33  
    34  // License represents a GitLab license.
    35  //
    36  // GitLab API docs:
    37  // https://docs.gitlab.com/ee/api/license.html
    38  type License struct {
    39  	ID               int        `json:"id"`
    40  	Plan             string     `json:"plan"`
    41  	CreatedAt        *time.Time `json:"created_at"`
    42  	StartsAt         *ISOTime   `json:"starts_at"`
    43  	ExpiresAt        *ISOTime   `json:"expires_at"`
    44  	HistoricalMax    int        `json:"historical_max"`
    45  	MaximumUserCount int        `json:"maximum_user_count"`
    46  	Expired          bool       `json:"expired"`
    47  	Overage          int        `json:"overage"`
    48  	UserLimit        int        `json:"user_limit"`
    49  	ActiveUsers      int        `json:"active_users"`
    50  	Licensee         struct {
    51  		Name    string `json:"Name"`
    52  		Company string `json:"Company"`
    53  		Email   string `json:"Email"`
    54  	} `json:"licensee"`
    55  	// Add on codes that may occur in legacy licenses that don't have a plan yet.
    56  	// https://gitlab.com/gitlab-org/gitlab/-/blob/master/ee/app/models/license.rb
    57  	AddOns struct {
    58  		GitLabAuditorUser int `json:"GitLab_Auditor_User"`
    59  		GitLabDeployBoard int `json:"GitLab_DeployBoard"`
    60  		GitLabFileLocks   int `json:"GitLab_FileLocks"`
    61  		GitLabGeo         int `json:"GitLab_Geo"`
    62  		GitLabServiceDesk int `json:"GitLab_ServiceDesk"`
    63  	} `json:"add_ons"`
    64  }
    65  
    66  func (l License) String() string {
    67  	return Stringify(l)
    68  }
    69  
    70  // GetLicense retrieves information about the current license.
    71  //
    72  // GitLab API docs:
    73  // https://docs.gitlab.com/ee/api/license.html#retrieve-information-about-the-current-license
    74  func (s *LicenseService) GetLicense(options ...RequestOptionFunc) (*License, *Response, error) {
    75  	req, err := s.client.NewRequest(http.MethodGet, "license", nil, options)
    76  	if err != nil {
    77  		return nil, nil, err
    78  	}
    79  
    80  	l := new(License)
    81  	resp, err := s.client.Do(req, l)
    82  	if err != nil {
    83  		return nil, resp, err
    84  	}
    85  
    86  	return l, resp, nil
    87  }
    88  
    89  // AddLicenseOptions represents the available AddLicense() options.
    90  //
    91  // https://docs.gitlab.com/ee/api/license.html#add-a-new-license
    92  type AddLicenseOptions struct {
    93  	License *string `url:"license" json:"license"`
    94  }
    95  
    96  // AddLicense adds a new license.
    97  //
    98  // GitLab API docs:
    99  // https://docs.gitlab.com/ee/api/license.html#add-a-new-license
   100  func (s *LicenseService) AddLicense(opt *AddLicenseOptions, options ...RequestOptionFunc) (*License, *Response, error) {
   101  	req, err := s.client.NewRequest(http.MethodPost, "license", opt, options)
   102  	if err != nil {
   103  		return nil, nil, err
   104  	}
   105  
   106  	l := new(License)
   107  	resp, err := s.client.Do(req, l)
   108  	if err != nil {
   109  		return nil, resp, err
   110  	}
   111  
   112  	return l, resp, nil
   113  }
   114  
   115  // DeleteLicense deletes an existing license.
   116  //
   117  // GitLab API docs:
   118  // https://docs.gitlab.com/ee/api/license.html#delete-a-license
   119  func (s *LicenseService) DeleteLicense(licenseID int, options ...RequestOptionFunc) (*Response, error) {
   120  	u := fmt.Sprintf("license/%d", licenseID)
   121  
   122  	req, err := s.client.NewRequest(http.MethodDelete, u, nil, options)
   123  	if err != nil {
   124  		return nil, err
   125  	}
   126  
   127  	return s.client.Do(req, nil)
   128  }
   129  

View as plain text