/
requests.go
243 lines (209 loc) · 7.66 KB
/
requests.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
// Copyright 2022 Cloudbase Solutions SRL
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.
package params
import (
"encoding/json"
"fmt"
commonParams "github.com/cloudbase/garm-provider-common/params"
"github.com/cloudbase/garm-provider-common/errors"
)
const DefaultRunnerPrefix = "garm"
type InstanceRequest struct {
Name string `json:"name"`
OSType commonParams.OSType `json:"os_type"`
OSVersion string `json:"os_version"`
}
type CreateRepoParams struct {
Owner string `json:"owner"`
Name string `json:"name"`
CredentialsName string `json:"credentials_name"`
WebhookSecret string `json:"webhook_secret"`
}
func (c *CreateRepoParams) Validate() error {
if c.Owner == "" {
return errors.NewBadRequestError("missing owner")
}
if c.Name == "" {
return errors.NewBadRequestError("missing repo name")
}
if c.CredentialsName == "" {
return errors.NewBadRequestError("missing credentials name")
}
if c.WebhookSecret == "" {
return errors.NewMissingSecretError("missing secret")
}
return nil
}
type CreateOrgParams struct {
Name string `json:"name"`
CredentialsName string `json:"credentials_name"`
WebhookSecret string `json:"webhook_secret"`
}
func (c *CreateOrgParams) Validate() error {
if c.Name == "" {
return errors.NewBadRequestError("missing org name")
}
if c.CredentialsName == "" {
return errors.NewBadRequestError("missing credentials name")
}
if c.WebhookSecret == "" {
return errors.NewMissingSecretError("missing secret")
}
return nil
}
type CreateEnterpriseParams struct {
Name string `json:"name"`
CredentialsName string `json:"credentials_name"`
WebhookSecret string `json:"webhook_secret"`
}
func (c *CreateEnterpriseParams) Validate() error {
if c.Name == "" {
return errors.NewBadRequestError("missing enterprise name")
}
if c.CredentialsName == "" {
return errors.NewBadRequestError("missing credentials name")
}
if c.WebhookSecret == "" {
return errors.NewMissingSecretError("missing secret")
}
return nil
}
// NewUserParams holds the needed information to create
// a new user
type NewUserParams struct {
Email string `json:"email"`
Username string `json:"username"`
FullName string `json:"full_name"`
Password string `json:"password"`
IsAdmin bool `json:"-"`
Enabled bool `json:"-"`
}
type UpdatePoolParams struct {
RunnerPrefix
Tags []string `json:"tags,omitempty"`
Enabled *bool `json:"enabled,omitempty"`
MaxRunners *uint `json:"max_runners,omitempty"`
MinIdleRunners *uint `json:"min_idle_runners,omitempty"`
RunnerBootstrapTimeout *uint `json:"runner_bootstrap_timeout,omitempty"`
Image string `json:"image"`
Flavor string `json:"flavor"`
OSType commonParams.OSType `json:"os_type"`
OSArch commonParams.OSArch `json:"os_arch"`
ExtraSpecs json.RawMessage `json:"extra_specs,omitempty"`
// GithubRunnerGroup is the github runner group in which the runners of this
// pool will be added to.
// The runner group must be created by someone with access to the enterprise.
GitHubRunnerGroup *string `json:"github-runner-group,omitempty"`
}
type CreateInstanceParams struct {
Name string
OSType commonParams.OSType
OSArch commonParams.OSArch
Status commonParams.InstanceStatus
RunnerStatus RunnerStatus
CallbackURL string
MetadataURL string
// GithubRunnerGroup is the github runner group to which the runner belongs.
// The runner group must be created by someone with access to the enterprise.
GitHubRunnerGroup string
CreateAttempt int `json:"-"`
AgentID int64 `json:"-"`
AditionalLabels []string
JitConfiguration map[string]string
}
type CreatePoolParams struct {
RunnerPrefix
ProviderName string `json:"provider_name"`
MaxRunners uint `json:"max_runners"`
MinIdleRunners uint `json:"min_idle_runners"`
Image string `json:"image"`
Flavor string `json:"flavor"`
OSType commonParams.OSType `json:"os_type"`
OSArch commonParams.OSArch `json:"os_arch"`
Tags []string `json:"tags"`
Enabled bool `json:"enabled"`
RunnerBootstrapTimeout uint `json:"runner_bootstrap_timeout"`
ExtraSpecs json.RawMessage `json:"extra_specs,omitempty"`
// GithubRunnerGroup is the github runner group in which the runners of this
// pool will be added to.
// The runner group must be created by someone with access to the enterprise.
GitHubRunnerGroup string `json:"github-runner-group"`
}
func (p *CreatePoolParams) Validate() error {
if p.ProviderName == "" {
return fmt.Errorf("missing provider")
}
if p.MinIdleRunners > p.MaxRunners {
return fmt.Errorf("min_idle_runners cannot be larger than max_runners")
}
if p.MaxRunners == 0 {
return fmt.Errorf("max_runners cannot be 0")
}
if len(p.Tags) == 0 {
return fmt.Errorf("missing tags")
}
if p.Flavor == "" {
return fmt.Errorf("missing flavor")
}
if p.Image == "" {
return fmt.Errorf("missing image")
}
return nil
}
type UpdateInstanceParams struct {
ProviderID string `json:"provider_id,omitempty"`
// OSName is the name of the OS. Eg: ubuntu, centos, etc.
OSName string `json:"os_name,omitempty"`
// OSVersion is the version of the operating system.
OSVersion string `json:"os_version,omitempty"`
// Addresses is a list of IP addresses the provider reports
// for this instance.
Addresses []commonParams.Address `json:"addresses,omitempty"`
// Status is the status of the instance inside the provider (eg: running, stopped, etc)
Status commonParams.InstanceStatus `json:"status,omitempty"`
RunnerStatus RunnerStatus `json:"runner_status,omitempty"`
ProviderFault []byte `json:"provider_fault,omitempty"`
AgentID int64 `json:"-"`
CreateAttempt int `json:"-"`
TokenFetched *bool `json:"-"`
JitConfiguration map[string]string `json:"-"`
}
type UpdateUserParams struct {
FullName string `json:"full_name"`
Password string `json:"password"`
Enabled *bool `json:"enabled"`
}
// PasswordLoginParams holds information used during
// password authentication, that will be passed to a
// password login function
type PasswordLoginParams struct {
Username string `json:"username"`
Password string `json:"password"`
}
// Validate checks if the username and password are set
func (p PasswordLoginParams) Validate() error {
if p.Username == "" || p.Password == "" {
return errors.ErrUnauthorized
}
return nil
}
type UpdateEntityParams struct {
CredentialsName string `json:"credentials_name"`
WebhookSecret string `json:"webhook_secret"`
}
type InstanceUpdateMessage struct {
Status RunnerStatus `json:"status"`
Message string `json:"message"`
AgentID *int64 `json:"agent_id,omitempty"`
}