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