-
Notifications
You must be signed in to change notification settings - Fork 50
/
label.go
170 lines (132 loc) · 3.78 KB
/
label.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
package label
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/label"
"github.com/mittwald/goharbor-client/v5/apiv2/model"
"github.com/mittwald/goharbor-client/v5/apiv2/pkg/config"
"github.com/mittwald/goharbor-client/v5/apiv2/pkg/util"
)
// RESTClient is a subclient for handling label 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 {
CreateLabel(ctx context.Context, l *model.Label) error
GetLabelByID(ctx context.Context, id int64) (*model.Label, error)
ListLabels(ctx context.Context, name string, projectID *int64, scope Scope) ([]*model.Label, error)
DeleteLabel(ctx context.Context, id int64) error
UpdateLabel(ctx context.Context, id int64, l *model.Label) error
}
type Scope string
const (
ScopeGlobal Scope = "g"
ScopeProject Scope = "p"
)
func (in Scope) String() string {
return string(in)
}
func (c *RESTClient) CreateLabel(ctx context.Context, l *model.Label) error {
params := &label.CreateLabelParams{
Label: l,
Context: ctx,
}
params.WithTimeout(c.Options.Timeout)
_, err := c.V2Client.Label.CreateLabel(params, c.AuthInfo)
if err != nil {
return handleSwaggerLabelErrors(err)
}
return nil
}
func (c *RESTClient) GetLabelByID(ctx context.Context, id int64) (*model.Label, error) {
params := &label.GetLabelByIDParams{
LabelID: id,
Context: ctx,
}
params.WithTimeout(c.Options.Timeout)
resp, err := c.V2Client.Label.GetLabelByID(params, c.AuthInfo)
if err != nil {
return nil, handleSwaggerLabelErrors(err)
}
return resp.Payload, nil
}
func (c *RESTClient) ListLabels(ctx context.Context, name string, projectID *int64) ([]*model.Label, error) {
var labels []*model.Label
page := c.Options.Page
var scope Scope
if projectID == nil {
scope = ScopeGlobal
} else {
scope = ScopeProject
}
params := &label.ListLabelsParams{
Name: &name,
Page: &page,
PageSize: &c.Options.PageSize,
ProjectID: projectID,
Q: &c.Options.Query,
Scope: util.StringPtr(scope.String()),
Sort: &c.Options.Sort,
Context: ctx,
}
params.WithTimeout(c.Options.Timeout)
for {
resp, err := c.V2Client.Label.ListLabels(params, c.AuthInfo)
if err != nil {
return nil, handleSwaggerLabelErrors(err)
}
if len(resp.Payload) == 0 {
break
}
totalCount := resp.XTotalCount
labels = append(labels, resp.Payload...)
if int64(len(labels)) >= totalCount {
break
}
page++
}
return labels, nil
}
func (c *RESTClient) DeleteLabel(ctx context.Context, id int64) error {
params := &label.DeleteLabelParams{
LabelID: id,
Context: ctx,
}
params.WithTimeout(c.Options.Timeout)
_, err := c.V2Client.Label.DeleteLabel(params, c.AuthInfo)
if err != nil {
return handleSwaggerLabelErrors(err)
}
return nil
}
func (c *RESTClient) UpdateLabel(ctx context.Context, id int64, l *model.Label) error {
// Name is the only required field for a label update
if l.Name == "" {
return fmt.Errorf("label name must be set")
}
params := &label.UpdateLabelParams{
Label: l,
LabelID: id,
Context: ctx,
}
params.WithTimeout(c.Options.Timeout)
_, err := c.V2Client.Label.UpdateLabel(params, c.AuthInfo)
if err != nil {
return handleSwaggerLabelErrors(err)
}
return nil
}