-
Notifications
You must be signed in to change notification settings - Fork 50
/
gc.go
140 lines (115 loc) · 3.98 KB
/
gc.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
package gc
import (
"context"
"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/gc"
"github.com/mittwald/goharbor-client/v5/apiv2/pkg/config"
"github.com/mittwald/goharbor-client/v5/apiv2/pkg/errors"
modelv2 "github.com/mittwald/goharbor-client/v5/apiv2/model"
)
// RESTClient is a subclient for handling system 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 {
NewGarbageCollection(ctx context.Context, gcSchedule *modelv2.Schedule) error
UpdateGarbageCollection(ctx context.Context,
newGCSchedule *modelv2.Schedule) error
GetGarbageCollectionExecution(ctx context.Context, id int64) (*modelv2.GCHistory, error)
GetGarbageCollectionSchedule(ctx context.Context) (*modelv2.GCHistory, error)
ResetGarbageCollection(ctx context.Context) error
}
// NewGarbageCollection creates a new garbage collection schedule.
func (c *RESTClient) NewGarbageCollection(ctx context.Context, gcSchedule *modelv2.Schedule) error {
if gcSchedule == nil {
return &errors.ErrSystemGcScheduleNotProvided{}
}
if gcSchedule.Parameters == nil {
gcSchedule.Parameters = map[string]interface{}{
"delete_untagged": false,
}
}
_, err := c.V2Client.GC.CreateGCSchedule(&gc.CreateGCScheduleParams{
Schedule: gcSchedule,
Context: ctx,
}, c.AuthInfo)
err = handleSwaggerSystemErrors(err)
if err != nil {
return err
}
return nil
}
// UpdateGarbageCollection updates the system GC schedule.
func (c *RESTClient) UpdateGarbageCollection(ctx context.Context,
newGCSchedule *modelv2.Schedule) error {
if newGCSchedule == nil {
return &errors.ErrSystemGcScheduleNotProvided{}
}
if newGCSchedule.Parameters == nil {
newGCSchedule.Parameters = map[string]interface{}{
"delete_untagged": false,
}
}
_, err := c.V2Client.GC.UpdateGCSchedule(&gc.UpdateGCScheduleParams{
Schedule: newGCSchedule,
Context: ctx,
}, c.AuthInfo)
return handleSwaggerSystemErrors(err)
}
// GetGarbageCollectionExecution Returns a garbage collection execution identified by its id.
func (c *RESTClient) GetGarbageCollectionExecution(ctx context.Context, id int64) (*modelv2.GCHistory, error) {
resp, err := c.V2Client.GC.GetGC(&gc.GetGCParams{
Context: ctx,
GCID: id,
}, c.AuthInfo)
if err != nil {
return nil, handleSwaggerSystemErrors(err)
}
if resp.Payload.Schedule == nil {
return nil, &errors.ErrSystemGcUndefined{}
}
return resp.Payload, nil
}
// GetGarbageCollectionSchedule returns the system GC schedule.
func (c *RESTClient) GetGarbageCollectionSchedule(ctx context.Context) (*modelv2.GCHistory, error) {
resp, err := c.V2Client.GC.GetGCSchedule(&gc.GetGCScheduleParams{
Context: ctx,
}, c.AuthInfo)
if err != nil {
return nil, handleSwaggerSystemErrors(err)
}
if resp.Payload.Schedule != nil {
return resp.Payload, nil
}
return nil, &errors.ErrSystemGcScheduleUndefined{}
}
// ResetGarbageCollection resets the system GC schedule to it's default values
// containing "None" as the Schedule Type, which effectively deactivates the schedule.
// For this to work correctly, a GC schedule must exist beforehand.
func (c *RESTClient) ResetGarbageCollection(ctx context.Context) error {
_, err := c.V2Client.GC.UpdateGCSchedule(&gc.UpdateGCScheduleParams{
Schedule: &modelv2.Schedule{
Parameters: map[string]interface{}{
"delete_untagged": false,
},
Schedule: &modelv2.ScheduleObj{
Type: "None",
},
},
Context: ctx,
}, c.AuthInfo)
return handleSwaggerSystemErrors(err)
}