-
Notifications
You must be signed in to change notification settings - Fork 50
/
artifact.go
324 lines (263 loc) · 9.59 KB
/
artifact.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
package artifact
import (
"context"
"fmt"
"github.com/go-openapi/runtime"
v2client "github.com/mittwald/goharbor-client/v5/apiv2/internal/api/client"
"github.com/mittwald/goharbor-client/v5/apiv2/internal/api/client/artifact"
"github.com/mittwald/goharbor-client/v5/apiv2/model"
"github.com/mittwald/goharbor-client/v5/apiv2/pkg/config"
)
// RESTClient is a subclient for handling artifact related actions.
type RESTClient struct {
// Options contains optional configuration when making API calls.
Options *config.Options
// The new client of the harbor v2 API
V2Client *v2client.Harbor
// AuthInfo contains the auth information that is provided on API calls.
AuthInfo runtime.ClientAuthInfoWriter
}
func NewClient(v2Client *v2client.Harbor, opts *config.Options, authInfo runtime.ClientAuthInfoWriter) *RESTClient {
return &RESTClient{
Options: opts,
V2Client: v2Client,
AuthInfo: authInfo,
}
}
type Client interface {
AddArtifactLabel(ctx context.Context, projectName, repositoryName, reference string, label *model.Label) error
CopyArtifact(ctx context.Context, from *CopyReference, projectName, repositoryName string) error
CreateTag(ctx context.Context, projectName, repositoryName, reference string, tag *model.Tag) error
DeleteTag(ctx context.Context, projectName, repositoryName, reference, tagName string) error
GetArtifact(ctx context.Context, projectName, repositoryName, reference string) (*model.Artifact, error)
DeleteArtifact(ctx context.Context, projectName, repositoryName, reference string) error
ListArtifacts(ctx context.Context, projectName, repositoryName string) ([]*model.Artifact, error)
ListTags(ctx context.Context, projectName, repositoryName, reference string) ([]*model.Tag, error)
RemoveLabel(ctx context.Context, projectName, repositoryName, reference string, id int64) error
// TODO: Introduce this, once https://github.com/goharbor/harbor/issues/13468 is resolved.
// GetAddition(ctx context.Context, projectName, repositoryName, reference string, addition Addition) (string, error)
// GetVulnerabilitiesAddition(ctx context.Context, projectName, repositoryName, reference string) (string, error)
}
// ToString returns a string representation of a CopyReference.
// Possible formats are "project/repository:tag" or "project/repository@digest".
// Returns an error if neither tag nor digest is set.
func (in CopyReference) toString() (string, error) {
var suffix string
if in.Digest == "" && in.Tag == "" {
return "", fmt.Errorf("no tag or digest specified")
}
if in.Digest != "" {
suffix = "@" + in.Digest
}
if in.Tag != "" {
suffix = ":" + in.Tag
}
return in.ProjectName + "/" + in.RepositoryName + suffix, nil
}
// CopyReference defines the parameters needed for an artifact copy.
type CopyReference struct {
ProjectName string
RepositoryName string
Tag string
Digest string
}
type Addition string
const (
AdditionBuildHistory Addition = "build_history"
AdditionValuesYAML Addition = "values.yaml"
AdditionReadme Addition = "readme.md"
AdditionDependencies Addition = "dependencies"
)
// TODO: Introduce this, once https://github.com/goharbor/harbor/issues/13468 is resolved.
//func (in Addition) string() string {
// return string(in)
//}
func (c *RESTClient) AddArtifactLabel(ctx context.Context, projectName, repositoryName, reference string, label *model.Label) error {
params := &artifact.AddLabelParams{
Label: label,
ProjectName: projectName,
Reference: reference,
RepositoryName: repositoryName,
Context: ctx,
}
params.WithTimeout(c.Options.Timeout)
_, err := c.V2Client.Artifact.AddLabel(params, c.AuthInfo)
return handleSwaggerArtifactErrors(err)
}
func (c *RESTClient) CopyArtifact(ctx context.Context, from *CopyReference, projectName, repositoryName string) error {
f, err := from.toString()
if err != nil {
return err
}
params := &artifact.CopyArtifactParams{
From: f,
ProjectName: projectName,
RepositoryName: repositoryName,
Context: ctx,
}
params.WithTimeout(c.Options.Timeout)
_, err = c.V2Client.Artifact.CopyArtifact(params, c.AuthInfo)
return handleSwaggerArtifactErrors(err)
}
func (c *RESTClient) CreateTag(ctx context.Context, projectName, repositoryName, reference string, tag *model.Tag) error {
params := &artifact.CreateTagParams{
ProjectName: projectName,
Reference: reference,
RepositoryName: repositoryName,
Tag: tag,
Context: ctx,
}
params.WithTimeout(c.Options.Timeout)
_, err := c.V2Client.Artifact.CreateTag(params, c.AuthInfo)
return handleSwaggerArtifactErrors(err)
}
func (c *RESTClient) DeleteTag(ctx context.Context, projectName, repositoryName, reference, tagName string) error {
params := &artifact.DeleteTagParams{
ProjectName: projectName,
Reference: reference,
RepositoryName: repositoryName,
TagName: tagName,
Context: ctx,
}
params.WithTimeout(c.Options.Timeout)
_, err := c.V2Client.Artifact.DeleteTag(params, c.AuthInfo)
return handleSwaggerArtifactErrors(err)
}
func (c *RESTClient) GetArtifact(ctx context.Context, projectName, repositoryName, reference string) (*model.Artifact, error) {
params := artifact.NewGetArtifactParams()
params.WithTimeout(c.Options.Timeout)
params.WithPage(&c.Options.Page)
params.WithPageSize(&c.Options.PageSize)
params.WithProjectName(projectName)
params.WithRepositoryName(repositoryName)
params.WithReference(reference)
params.WithContext(ctx)
resp, err := c.V2Client.Artifact.GetArtifact(params, c.AuthInfo)
if err != nil {
return nil, handleSwaggerArtifactErrors(err)
}
return resp.Payload, nil
}
func (c *RESTClient) DeleteArtifact(ctx context.Context, projectName, repositoryName, reference string) error {
params := artifact.NewDeleteArtifactParams()
params.WithTimeout(c.Options.Timeout)
params.WithProjectName(projectName)
params.WithRepositoryName(repositoryName)
params.WithReference(reference)
params.WithContext(ctx)
_, err := c.V2Client.Artifact.DeleteArtifact(params, c.AuthInfo)
if err != nil {
return handleSwaggerArtifactErrors(err)
}
return nil
}
func (c *RESTClient) ListArtifacts(ctx context.Context, projectName, repositoryName string) ([]*model.Artifact, error) {
var artifacts []*model.Artifact
page := c.Options.Page
params := artifact.NewListArtifactsParams()
params.WithContext(ctx)
params.WithTimeout(c.Options.Timeout)
params.Page = &page
params.PageSize = &c.Options.PageSize
params.Q = &c.Options.Query
params.Sort = &c.Options.Sort
params.WithProjectName(projectName)
params.WithRepositoryName(repositoryName)
for {
resp, err := c.V2Client.Artifact.ListArtifacts(params, c.AuthInfo)
if err != nil {
return nil, handleSwaggerArtifactErrors(err)
}
if len(resp.Payload) == 0 {
break
}
totalCount := resp.XTotalCount
artifacts = append(artifacts, resp.Payload...)
if int64(len(artifacts)) >= totalCount {
break
}
page++
}
return artifacts, nil
}
func (c *RESTClient) ListTags(ctx context.Context, projectName, repositoryName, reference string) ([]*model.Tag, error) {
var tags []*model.Tag
page := c.Options.Page
params := artifact.NewListTagsParams()
params.Page = &page
params.PageSize = &c.Options.PageSize
params.WithProjectName(projectName)
params.WithRepositoryName(repositoryName)
params.WithReference(reference)
params.Q = &c.Options.Query
params.Sort = &c.Options.Sort
params.WithContext(ctx)
params.WithTimeout(c.Options.Timeout)
for {
resp, err := c.V2Client.Artifact.ListTags(params, c.AuthInfo)
if err != nil {
return nil, handleSwaggerArtifactErrors(err)
}
if len(resp.Payload) == 0 {
break
}
totalCount := resp.XTotalCount
tags = append(tags, resp.Payload...)
if int64(len(tags)) >= totalCount {
break
}
page++
}
return tags, nil
}
func (c *RESTClient) RemoveLabel(ctx context.Context, projectName, repositoryName, reference string, id int64) error {
params := &artifact.RemoveLabelParams{
LabelID: id,
ProjectName: projectName,
RepositoryName: repositoryName,
Reference: reference,
Context: ctx,
}
params.WithTimeout(c.Options.Timeout)
_, err := c.V2Client.Artifact.RemoveLabel(params, c.AuthInfo)
if err != nil {
return handleSwaggerArtifactErrors(err)
}
return nil
}
// TODO: Introduce this, once https://github.com/goharbor/harbor/issues/13468 is resolved.
//func (c *RESTClient) GetAddition(ctx context.Context, projectName, repositoryName, reference string, addition Addition) (interface{}, error) {
// params := &artifact.GetAdditionParams{
// ProjectName: projectName,
// RepositoryName: repositoryName,
// Reference: reference,
// Addition: addition.String(),
// Context: ctx,
// }
//
// params.WithTimeout(c.Options.Timeout)
//
// resp, err := c.V2Client.Artifact.GetAddition(params, c.AuthInfo)
// if err != nil {
// return nil, err
// }
//
// return resp.Payload, nil
//}
//func (c *RESTClient) GetVulnerabilitiesAddition(ctx context.Context, projectName, repositoryName, reference string) (interface{}, error) {
// params := artifact.NewGetVulnerabilitiesAdditionParams()
// params.ProjectName = projectName
// params.RepositoryName = repositoryName
// params.Reference = reference
// params.Context = ctx
// params.WithTimeout(c.Options.Timeout)
// xAcceptVulnerabilities := "application/vnd.security.vulnerability.report; version=1.1, application/vnd.scanner.adapter.vuln.report.harbor+json; version=1.0"
// params.WithXAcceptVulnerabilities(&xAcceptVulnerabilities)
//
// resp, err := c.V2Client.Artifact.GetVulnerabilitiesAddition(params, c.AuthInfo)
// if err != nil {
// return nil, handleSwaggerArtifactErrors(err)
// }
//
// return resp.Payload, nil
//}