-
Notifications
You must be signed in to change notification settings - Fork 50
/
projectmeta.go
186 lines (153 loc) · 6.21 KB
/
projectmeta.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
package projectmeta
import (
"context"
"github.com/go-openapi/runtime"
v2client "github.com/mittwald/goharbor-client/v5/apiv2/internal/api/client"
projectmeta "github.com/mittwald/goharbor-client/v5/apiv2/internal/api/client/project_metadata"
"github.com/mittwald/goharbor-client/v5/apiv2/pkg/common"
"github.com/mittwald/goharbor-client/v5/apiv2/pkg/config"
"github.com/mittwald/goharbor-client/v5/apiv2/pkg/errors"
)
// RESTClient is a subclient for handling webhook 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 {
AddProjectMetadata(ctx context.Context, projectNameOrID string, key common.MetadataKey, value string) error
GetProjectMetadataValue(ctx context.Context, projectNameOrID string, key common.MetadataKey) (string, error)
ListProjectMetadata(ctx context.Context, projectNameOrID string) (map[string]string, error)
UpdateProjectMetadata(ctx context.Context, projectNameOrID string, key common.MetadataKey, value string) error
DeleteProjectMetadataValue(ctx context.Context, projectNameOrID string, key common.MetadataKey) error
}
// AddProjectMetadata AddMetadata adds a metadata value using a specific key to the specified project.
func (c *RESTClient) AddProjectMetadata(ctx context.Context, projectNameOrID string, key common.MetadataKey, value string) error {
params := &projectmeta.AddProjectMetadatasParams{
Metadata: map[string]string{
string(key): value,
},
ProjectNameOrID: projectNameOrID,
Context: ctx,
}
params.WithTimeout(c.Options.Timeout)
_, err := c.V2Client.ProjectMetadata.AddProjectMetadatas(params, c.AuthInfo)
return handleSwaggerProjectMetaErrors(err)
}
// GetProjectMetadataValue retrieves the corresponding metadata value to the key of the specified project.
func (c *RESTClient) GetProjectMetadataValue(ctx context.Context, projectNameOrID string, key common.MetadataKey) (string, error) {
if key == "" {
return "", &errors.ErrProjectMetadataKeyUndefined{}
}
params := &projectmeta.GetProjectMetadataParams{
MetaName: key.String(),
ProjectNameOrID: projectNameOrID,
Context: ctx,
}
params.WithTimeout(c.Options.Timeout)
meta, err := c.V2Client.ProjectMetadata.GetProjectMetadata(params, c.AuthInfo)
if err != nil {
return "", handleSwaggerProjectMetaErrors(err)
}
if meta == nil {
return "", &errors.ErrProjectMetadataUndefined{}
}
return retrieveMetadataValue(key, meta.Payload)
}
// ListProjectMetadata lists the metadata of project.
func (c *RESTClient) ListProjectMetadata(ctx context.Context, projectNameOrID string) (map[string]string, error) {
params := &projectmeta.ListProjectMetadatasParams{
ProjectNameOrID: projectNameOrID,
Context: ctx,
}
params.WithTimeout(c.Options.Timeout)
meta, err := c.V2Client.ProjectMetadata.ListProjectMetadatas(params, c.AuthInfo)
if err != nil {
return nil, handleSwaggerProjectMetaErrors(err)
}
if meta.Payload != nil {
return meta.Payload, nil
}
return nil, &errors.ErrProjectMetadataUndefined{}
}
// UpdateProjectMetadata UpdateMetadata deletes the specified metadata key, if it exists and re-adds this metadata key with the given value.
// This function works around the faulty behaviour of the corresponding 'Update' endpoint of the Harbor API.
func (c *RESTClient) UpdateProjectMetadata(ctx context.Context, projectNameOrID string, key common.MetadataKey, value string) error {
params := &projectmeta.UpdateProjectMetadataParams{
MetaName: key.String(),
Metadata: map[string]string{
key.String(): value,
},
ProjectNameOrID: projectNameOrID,
Context: ctx,
}
params.WithTimeout(c.Options.Timeout)
_, err := c.V2Client.ProjectMetadata.UpdateProjectMetadata(params, c.AuthInfo)
return handleSwaggerProjectMetaErrors(err)
}
// DeleteProjectMetadataValue DeleteMetadataValue deletes metadata of project p given by key.
func (c *RESTClient) DeleteProjectMetadataValue(ctx context.Context, projectNameOrID string, key common.MetadataKey) error {
params := &projectmeta.DeleteProjectMetadataParams{
MetaName: key.String(),
ProjectNameOrID: projectNameOrID,
Context: ctx,
}
params.WithTimeout(c.Options.Timeout)
_, err := c.V2Client.ProjectMetadata.DeleteProjectMetadata(params, c.AuthInfo)
return handleSwaggerProjectMetaErrors(err)
}
// // retrieveMetadataValue returns the value of the metadata k that is contained in the project metadata m.
// // Returns an empty string plus an error when encountering a nil pointer, or if the requested key k is invalid.
func retrieveMetadataValue(k common.MetadataKey, m map[string]string) (string, error) {
var r string
switch k {
case common.ProjectMetadataKeyEnableContentTrust:
if m[k.String()] == "" {
return "", &errors.ErrProjectMetadataValueEnableContentTrustUndefined{}
}
r = m[k.String()]
case common.ProjectMetadataKeyAutoScan:
if m[k.String()] == "" {
return "", &errors.ErrProjectMetadataValueAutoScanUndefined{}
}
r = m[k.String()]
case common.ProjectMetadataKeySeverity:
if m[k.String()] == "" {
return "", &errors.ErrProjectMetadataValueSeverityUndefined{}
}
r = m[k.String()]
case common.ProjectMetadataKeyReuseSysCVEAllowlist:
if m[k.String()] == "" {
return "", &errors.ErrProjectMetadataValueReuseSysCveAllowlistUndefined{}
}
r = m[k.String()]
case common.ProjectMetadataKeyPublic:
if m[k.String()] == "" {
return "", &errors.ErrProjectMetadataValuePublicUndefined{}
}
r = m[k.String()]
case common.ProjectMetadataKeyPreventVul:
if m[k.String()] == "" {
return "", &errors.ErrProjectMetadataValuePreventVulUndefined{}
}
r = m[k.String()]
case common.ProjectMetadataKeyRetentionID:
if m[k.String()] == "" {
return "", &errors.ErrProjectMetadataValueRetentionIDUndefined{}
}
r = m[k.String()]
default:
return "", &errors.ErrProjectInvalidRequest{}
}
return r, nil
}