-
Notifications
You must be signed in to change notification settings - Fork 50
/
retention.go
241 lines (186 loc) · 6.93 KB
/
retention.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
package retention
import (
"context"
"errors"
"fmt"
"strconv"
"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/retention"
modelv2 "github.com/mittwald/goharbor-client/v5/apiv2/model"
"github.com/mittwald/goharbor-client/v5/apiv2/pkg/clients/projectmeta"
"github.com/mittwald/goharbor-client/v5/apiv2/pkg/common"
"github.com/mittwald/goharbor-client/v5/apiv2/pkg/config"
)
const (
// AlgorithmOr is the default algorithm when operating on harbor retention rules
AlgorithmOr string = "or"
// Key for defining matching repositories
ScopeSelectorRepoMatches ScopeSelector = "repoMatches"
// Key for defining excluded repositories
ScopeSelectorRepoExcludes ScopeSelector = "repoExcludes"
// Key for defining matching tag expressions
TagSelectorMatches TagSelector = "matches"
// Key for defining excluded tag expressions
TagSelectorExcludes TagSelector = "excludes"
// The kind of the retention selector, _always_ defaults to 'doublestar'
SelectorTypeDefault string = "doublestar"
// Retain the most recently pushed n artifacts - count
PolicyTemplateLatestPushedArtifacts PolicyTemplate = "latestPushedK"
// Retain the most recently pulled n artifacts - count
PolicyTemplateLatestPulledArtifacts PolicyTemplate = "latestPulledN"
// Retain the artifacts pushed within the last n days
PolicyTemplateDaysSinceLastPush PolicyTemplate = "nDaysSinceLastPush"
// Retain the artifacts pulled within the last n days
PolicyTemplateDaysSinceLastPull PolicyTemplate = "nDaysSinceLastPull"
// Retain always
PolicyTemplateRetainAlways PolicyTemplate = "always"
)
type Client interface {
NewRetentionPolicy(ctx context.Context, ret *modelv2.RetentionPolicy) error
GetRetentionPolicyByProject(ctx context.Context, projectNameOrID string) (*modelv2.RetentionPolicy, error)
GetRetentionPolicyByID(ctx context.Context, id int64) (*modelv2.RetentionPolicy, error)
DeleteRetentionPolicyByID(ctx context.Context, id int64) error
UpdateRetentionPolicy(ctx context.Context, ret *modelv2.RetentionPolicy) error
}
// RESTClient is a subclient for handling retention related actions.
type RESTClient struct {
// Options contains optional configuration when making API calls.
Options *config.Options
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,
}
}
// ScopeSelector is the retention selector decoration used for operations on retention objects.
type ScopeSelector string
func (r ScopeSelector) String() string {
return string(r)
}
// PolicyTemplate defines the possible values used for the policy matching mechanism.
type PolicyTemplate string
func (p PolicyTemplate) String() string {
return string(p)
}
// TagSelector defines the possible values used for the tag matching mechanism. Valid values are: "matches, excludes".
type TagSelector string
// String returns the string value of a TagSelector.
func (t TagSelector) String() string {
return string(t)
}
// NewRetentionPolicy creates a new tag retention policy for a project.
func (c *RESTClient) NewRetentionPolicy(ctx context.Context, ret *modelv2.RetentionPolicy) error {
if ret == nil {
return &ErrRetentionNotProvided{}
}
params := &retention.CreateRetentionParams{
Policy: ret,
Context: ctx,
}
params.WithTimeout(c.Options.Timeout)
_, err := c.V2Client.Retention.CreateRetention(params, c.AuthInfo)
if err != nil {
return handleSwaggerRetentionErrors(err)
}
return nil
}
// GetRetentionPolicyByProject returns the retention policy associated to a project.
func (c *RESTClient) GetRetentionPolicyByProject(ctx context.Context, projectNameOrID string) (*modelv2.RetentionPolicy, error) {
pm := projectmeta.NewClient(c.V2Client, c.Options, c.AuthInfo)
val, err := pm.GetProjectMetadataValue(ctx, projectNameOrID, common.ProjectMetadataKeyRetentionID)
if err != nil {
return nil, err
}
id, err := strconv.ParseInt(val, 10, 64)
if err != nil {
return nil, fmt.Errorf("could not convert retention id %q to int64, project: %s", val, projectNameOrID)
}
return c.GetRetentionPolicyByID(ctx, id)
}
// GetRetentionPolicyByID returns a retention policy identified by its id.
func (c *RESTClient) GetRetentionPolicyByID(ctx context.Context, id int64) (*modelv2.RetentionPolicy, error) {
params := &retention.GetRetentionParams{
ID: id,
Context: ctx,
}
params.WithTimeout(c.Options.Timeout)
resp, err := c.V2Client.Retention.GetRetention(params, c.AuthInfo)
if err != nil {
return nil, handleSwaggerRetentionErrors(err)
}
return resp.Payload, nil
}
func (c *RESTClient) DeleteRetentionPolicyByID(ctx context.Context, id int64) error {
params := &retention.DeleteRetentionParams{
ID: id,
Context: ctx,
}
params.WithTimeout(c.Options.Timeout)
_, err := c.V2Client.Retention.DeleteRetention(params, c.AuthInfo)
if err != nil {
return handleSwaggerRetentionErrors(err)
}
return nil
}
// UpdateRetentionPolicy updates the specified retention policy ret.
func (c *RESTClient) UpdateRetentionPolicy(ctx context.Context, ret *modelv2.RetentionPolicy) error {
if ret == nil {
return &ErrRetentionNotProvided{}
}
params := &retention.UpdateRetentionParams{
ID: ret.ID,
Policy: ret,
Context: ctx,
}
params.WithTimeout(c.Options.Timeout)
resp, err := c.V2Client.Retention.UpdateRetention(params, c.AuthInfo)
if resp == nil {
return &ErrRetentionDoesNotExist{}
}
if err != nil {
return handleSwaggerRetentionErrors(err)
}
return nil
}
// ToTagSelectorExtras converts a boolean to the representative string value used by Harbor.
// Represents the functionality of the 'untagged artifacts' checkbox when editing tag retention rules in the Harbor UI.
func ToTagSelectorExtras(untagged bool) string {
if untagged {
return `{"untagged":"true"}`
}
return `{"untagged":"false"}`
}
// evaluateRetentionRuleParams evaluates the provided map of PolicyTemplate by comparing the keys to the pre-defined PolicyTemplates.
// Returns an error if the provided or resulting map is empty.
func evaluateRetentionRuleParams(params map[PolicyTemplate]interface{}) (map[string]interface{}, error) {
res := make(map[string]interface{})
if len(params) == 0 {
return nil, errors.New("no retention rule params provided")
}
for k, v := range params {
if _, ok := params[k]; ok {
switch k {
case PolicyTemplateDaysSinceLastPull:
res[k.String()] = v
case PolicyTemplateDaysSinceLastPush:
res[k.String()] = v
case PolicyTemplateLatestPulledArtifacts:
res[k.String()] = v
case PolicyTemplateLatestPushedArtifacts:
res[k.String()] = v
default:
continue
}
}
}
if len(res) == 0 {
return nil, errors.New("invalid params provided")
}
return res, nil
}