1//
2// Copyright 2017, Sander van Harmelen
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	"fmt"
21	"net/url"
22	"time"
23)
24
25// DeployKeysService handles communication with the keys related methods
26// of the GitLab API.
27//
28// GitLab API docs: https://docs.gitlab.com/ce/api/deploy_keys.html
29type DeployKeysService struct {
30	client *Client
31}
32
33// DeployKey represents a GitLab deploy key.
34type DeployKey struct {
35	ID        int        `json:"id"`
36	Title     string     `json:"title"`
37	Key       string     `json:"key"`
38	CanPush   *bool      `json:"can_push"`
39	CreatedAt *time.Time `json:"created_at"`
40}
41
42func (k DeployKey) String() string {
43	return Stringify(k)
44}
45
46// ListAllDeployKeys gets a list of all deploy keys
47//
48// GitLab API docs:
49// https://docs.gitlab.com/ce/api/deploy_keys.html#list-all-deploy-keys
50func (s *DeployKeysService) ListAllDeployKeys(options ...OptionFunc) ([]*DeployKey, *Response, error) {
51	req, err := s.client.NewRequest("GET", "deploy_keys", nil, options)
52	if err != nil {
53		return nil, nil, err
54	}
55
56	var ks []*DeployKey
57	resp, err := s.client.Do(req, &ks)
58	if err != nil {
59		return nil, resp, err
60	}
61
62	return ks, resp, err
63}
64
65// ListProjectDeployKeysOptions represents the available ListProjectDeployKeys()
66// options.
67//
68// GitLab API docs:
69// https://docs.gitlab.com/ce/api/deploy_keys.html#list-project-deploy-keys
70type ListProjectDeployKeysOptions ListOptions
71
72// ListProjectDeployKeys gets a list of a project's deploy keys
73//
74// GitLab API docs:
75// https://docs.gitlab.com/ce/api/deploy_keys.html#list-project-deploy-keys
76func (s *DeployKeysService) ListProjectDeployKeys(pid interface{}, opt *ListProjectDeployKeysOptions, options ...OptionFunc) ([]*DeployKey, *Response, error) {
77	project, err := parseID(pid)
78	if err != nil {
79		return nil, nil, err
80	}
81	u := fmt.Sprintf("projects/%s/deploy_keys", url.QueryEscape(project))
82
83	req, err := s.client.NewRequest("GET", u, opt, options)
84	if err != nil {
85		return nil, nil, err
86	}
87
88	var ks []*DeployKey
89	resp, err := s.client.Do(req, &ks)
90	if err != nil {
91		return nil, resp, err
92	}
93
94	return ks, resp, err
95}
96
97// GetDeployKey gets a single deploy key.
98//
99// GitLab API docs:
100// https://docs.gitlab.com/ce/api/deploy_keys.html#single-deploy-key
101func (s *DeployKeysService) GetDeployKey(pid interface{}, deployKey int, options ...OptionFunc) (*DeployKey, *Response, error) {
102	project, err := parseID(pid)
103	if err != nil {
104		return nil, nil, err
105	}
106	u := fmt.Sprintf("projects/%s/deploy_keys/%d", url.QueryEscape(project), deployKey)
107
108	req, err := s.client.NewRequest("GET", u, nil, options)
109	if err != nil {
110		return nil, nil, err
111	}
112
113	k := new(DeployKey)
114	resp, err := s.client.Do(req, k)
115	if err != nil {
116		return nil, resp, err
117	}
118
119	return k, resp, err
120}
121
122// AddDeployKeyOptions represents the available ADDDeployKey() options.
123//
124// GitLab API docs:
125// https://docs.gitlab.com/ce/api/deploy_keys.html#add-deploy-key
126type AddDeployKeyOptions struct {
127	Title   *string `url:"title,omitempty" json:"title,omitempty"`
128	Key     *string `url:"key,omitempty" json:"key,omitempty"`
129	CanPush *bool   `url:"can_push,omitempty" json:"can_push,omitempty"`
130}
131
132// AddDeployKey creates a new deploy key for a project. If deploy key already
133// exists in another project - it will be joined to project but only if
134// original one was is accessible by same user.
135//
136// GitLab API docs:
137// https://docs.gitlab.com/ce/api/deploy_keys.html#add-deploy-key
138func (s *DeployKeysService) AddDeployKey(pid interface{}, opt *AddDeployKeyOptions, options ...OptionFunc) (*DeployKey, *Response, error) {
139	project, err := parseID(pid)
140	if err != nil {
141		return nil, nil, err
142	}
143	u := fmt.Sprintf("projects/%s/deploy_keys", url.QueryEscape(project))
144
145	req, err := s.client.NewRequest("POST", u, opt, options)
146	if err != nil {
147		return nil, nil, err
148	}
149
150	k := new(DeployKey)
151	resp, err := s.client.Do(req, k)
152	if err != nil {
153		return nil, resp, err
154	}
155
156	return k, resp, err
157}
158
159// DeleteDeployKey deletes a deploy key from a project.
160//
161// GitLab API docs:
162// https://docs.gitlab.com/ce/api/deploy_keys.html#delete-deploy-key
163func (s *DeployKeysService) DeleteDeployKey(pid interface{}, deployKey int, options ...OptionFunc) (*Response, error) {
164	project, err := parseID(pid)
165	if err != nil {
166		return nil, err
167	}
168	u := fmt.Sprintf("projects/%s/deploy_keys/%d", url.QueryEscape(project), deployKey)
169
170	req, err := s.client.NewRequest("DELETE", u, nil, options)
171	if err != nil {
172		return nil, err
173	}
174
175	return s.client.Do(req, nil)
176}
177
178// EnableDeployKey enables a deploy key.
179//
180// GitLab API docs:
181// https://docs.gitlab.com/ce/api/deploy_keys.html#enable-deploy-key
182func (s *DeployKeysService) EnableDeployKey(pid interface{}, deployKey int, options ...OptionFunc) (*DeployKey, *Response, error) {
183	project, err := parseID(pid)
184	if err != nil {
185		return nil, nil, err
186	}
187	u := fmt.Sprintf("projects/%s/deploy_keys/%d/enable", url.QueryEscape(project), deployKey)
188
189	req, err := s.client.NewRequest("POST", u, nil, options)
190	if err != nil {
191		return nil, nil, err
192	}
193
194	k := new(DeployKey)
195	resp, err := s.client.Do(req, k)
196	if err != nil {
197		return nil, resp, err
198	}
199
200	return k, resp, err
201}
202