license.go

 1//
 2// Copyright 2018, 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
17package gitlab
18
19// LicenseService handles communication with the license
20// related methods of the GitLab API.
21//
22// GitLab API docs:
23// https://docs.gitlab.com/ee/api/license.html
24type LicenseService struct {
25	client *Client
26}
27
28// License represents a GitLab license.
29//
30// GitLab API docs:
31// https://docs.gitlab.com/ee/api/license.html
32type License struct {
33	StartsAt  *ISOTime `json:"starts_at"`
34	ExpiresAt *ISOTime `json:"expires_at"`
35	Licensee  struct {
36		Name    string `json:"Name"`
37		Company string `json:"Company"`
38		Email   string `json:"Email"`
39	} `json:"licensee"`
40	UserLimit   int `json:"user_limit"`
41	ActiveUsers int `json:"active_users"`
42	AddOns      struct {
43		GitLabFileLocks int `json:"GitLabFileLocks"`
44	} `json:"add_ons"`
45}
46
47func (l License) String() string {
48	return Stringify(l)
49}
50
51// GetLicense retrieves information about the current license.
52//
53// GitLab API docs:
54// https://docs.gitlab.com/ee/api/license.html#retrieve-information-about-the-current-license
55func (s *LicenseService) GetLicense() (*License, *Response, error) {
56	req, err := s.client.NewRequest("GET", "license", nil, nil)
57	if err != nil {
58		return nil, nil, err
59	}
60
61	l := new(License)
62	resp, err := s.client.Do(req, l)
63	if err != nil {
64		return nil, resp, err
65	}
66
67	return l, resp, err
68}
69
70// AddLicenseOptions represents the available AddLicense() options.
71//
72// https://docs.gitlab.com/ee/api/license.html#add-a-new-license
73type AddLicenseOptions struct {
74	License *string `url:"license" json:"license"`
75}
76
77// AddLicense adds a new license.
78//
79// GitLab API docs:
80// https://docs.gitlab.com/ee/api/license.html#add-a-new-license
81func (s *LicenseService) AddLicense(opt *AddLicenseOptions, options ...OptionFunc) (*License, *Response, error) {
82	req, err := s.client.NewRequest("POST", "license", opt, options)
83	if err != nil {
84		return nil, nil, err
85	}
86
87	l := new(License)
88	resp, err := s.client.Do(req, l)
89	if err != nil {
90		return nil, resp, err
91	}
92
93	return l, resp, err
94}