This repository has been archived by the owner on Dec 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
resource_client.go
279 lines (244 loc) · 9.92 KB
/
resource_client.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
package keptn
import (
"context"
"errors"
"fmt"
"strings"
keptnmodels "github.com/keptn/go-utils/pkg/api/models"
v2 "github.com/keptn/go-utils/pkg/api/utils/v2"
log "github.com/sirupsen/logrus"
)
const projectFieldName = "project"
const stageFieldName = "stage"
const serviceFieldName = "service"
const resourceURIFieldName = "resourceURI"
// ResourceClientInterface defines the methods for interacting with resources of Keptn's configuration service.
type ResourceClientInterface interface {
// GetResource tries to find the first instance of a given resource on service, stage or project level.
GetResource(ctx context.Context, project string, stage string, service string, resourceURI string) (string, error)
// GetProjectResource tries to retrieve a resource on project level.
GetProjectResource(ctx context.Context, project string, resourceURI string) (string, error)
// GetStageResource tries to retrieve a resource on stage level.
GetStageResource(ctx context.Context, project string, stage string, resourceURI string) (string, error)
// GetServiceResource tries to retrieve a resource on service level.
GetServiceResource(ctx context.Context, project string, stage string, service string, resourceURI string) (string, error)
// UploadResource tries to upload a resource.
UploadResource(ctx context.Context, contentToUpload []byte, remoteResourceURI string, project string, stage string, service string) error
}
// ResourceError represents an error for a resource that was not found.
type ResourceError struct {
uri string
project string
stage string
service string
}
// ResourceNotFoundError represents an error for a resource that was not found.
type ResourceNotFoundError ResourceError
// Error returns a string representation of this error
func (e *ResourceNotFoundError) Error() string {
return fmt.Sprintf("could not find resource: '%s' %s", e.uri, getLocation(e.service, e.stage, e.project))
}
// ResourceEmptyError represents an error for a resource that was found, but is empty.
type ResourceEmptyError ResourceError
// Error returns a string representation of this error
func (e *ResourceEmptyError) Error() string {
return fmt.Sprintf("found resource: '%s' %s, but it is empty", e.uri, getLocation(e.service, e.stage, e.project))
}
// ResourceUploadFailedError represents an error for a resource that could not be uploaded.
type ResourceUploadFailedError struct {
ResourceError
message string
}
// Error returns a string representation of this error.
func (e *ResourceUploadFailedError) Error() string {
return fmt.Sprintf("could not upload resource: '%s' %s: %s", e.uri, getLocation(e.service, e.stage, e.project), e.message)
}
// ResourceRetrievalFailedError represents an error for a resource that could not be retrieved because of an error.
type ResourceRetrievalFailedError struct {
ResourceError
cause error
}
// Error returns a string representation of this error.
func (e *ResourceRetrievalFailedError) Error() string {
return fmt.Sprintf("could not retrieve resource: '%s' %s: %v", e.uri, getLocation(e.service, e.stage, e.project), e.cause)
}
// Unwrap returns the cause of the ResourceRetrievalFailedError.
func (e *ResourceRetrievalFailedError) Unwrap() error {
return e.cause
}
func getLocation(service string, stage string, project string) string {
var location string
if service != "" {
location += fmt.Sprintf(" for service '%s'", service)
}
if stage != "" {
location += fmt.Sprintf(" at stage '%s'", stage)
}
if project != "" {
location += fmt.Sprintf(" of project '%s'", project)
}
return strings.TrimLeft(location, " ")
}
// ResourceHelper is the default implementation for the ResourceClientInterface using a Keptn api.ResourcesV1Interface.
type ResourceHelper struct {
client v2.ResourcesInterface
}
// NewResourceHelper creates a new ResourceHelper using a api.ResourcesV1Interface.
func NewResourceHelper(client v2.ResourcesInterface) *ResourceHelper {
return &ResourceHelper{
client: client,
}
}
// GetResource tries to find the first instance of a given resource on service, stage or project level.
func (rc *ResourceHelper) GetResource(ctx context.Context, project string, stage string, service string, resourceURI string) (string, error) {
var rnfErrorType *ResourceNotFoundError
if project != "" && stage != "" && service != "" {
keptnResourceContent, err := rc.GetServiceResource(ctx, project, stage, service, resourceURI)
if errors.As(err, &rnfErrorType) {
log.WithFields(
log.Fields{
projectFieldName: project,
stageFieldName: stage,
serviceFieldName: service,
resourceURIFieldName: resourceURI,
}).Debug("Resource not available for service")
} else if err != nil {
return "", err
} else {
log.WithFields(
log.Fields{
projectFieldName: project,
stageFieldName: stage,
serviceFieldName: service,
resourceURIFieldName: resourceURI,
}).Info("Found resource for service")
return keptnResourceContent, nil
}
}
if project != "" && stage != "" {
keptnResourceContent, err := rc.GetStageResource(ctx, project, stage, resourceURI)
if errors.As(err, &rnfErrorType) {
log.WithFields(
log.Fields{
projectFieldName: project,
stageFieldName: stage,
resourceURIFieldName: resourceURI,
}).Debug("Resource not available for stage")
} else if err != nil {
return "", err
} else {
log.WithFields(
log.Fields{
projectFieldName: project,
stageFieldName: stage,
resourceURIFieldName: resourceURI,
}).Info("Found resource for stage")
return keptnResourceContent, nil
}
}
if project != "" {
keptnResourceContent, err := rc.GetProjectResource(ctx, project, resourceURI)
if errors.As(err, &rnfErrorType) {
log.WithFields(
log.Fields{
projectFieldName: project,
resourceURIFieldName: resourceURI,
}).Debug("Resource not available for project")
} else if err != nil {
return "", err
} else {
log.WithFields(
log.Fields{projectFieldName: project,
resourceURIFieldName: resourceURI,
}).Info("Found resource for project")
return keptnResourceContent, nil
}
}
log.WithField(resourceURIFieldName, resourceURI).Info("Resource not found")
return "", &ResourceNotFoundError{uri: resourceURI, project: project, stage: stage, service: service}
}
// GetServiceResource tries to retrieve a resource on service level.
func (rc *ResourceHelper) GetServiceResource(ctx context.Context, project string, stage string, service string, resourceURI string) (string, error) {
return getResourceByFunc(
func() (*keptnmodels.Resource, error) {
return rc.client.GetResource(ctx,
*v2.NewResourceScope().Project(project).Stage(stage).Service(service).Resource(resourceURI),
v2.ResourcesGetResourceOptions{})
},
func() *ResourceNotFoundError {
return &ResourceNotFoundError{uri: resourceURI, project: project, stage: stage, service: service}
},
func(cause error) *ResourceRetrievalFailedError {
return &ResourceRetrievalFailedError{ResourceError{uri: resourceURI, project: project, stage: stage, service: service}, cause}
},
func() *ResourceEmptyError {
return &ResourceEmptyError{uri: resourceURI, project: project, stage: stage, service: service}
})
}
// GetStageResource tries to retrieve a resource on stage level.
func (rc *ResourceHelper) GetStageResource(ctx context.Context, project string, stage string, resourceURI string) (string, error) {
return getResourceByFunc(
func() (*keptnmodels.Resource, error) {
return rc.client.GetResource(ctx,
*v2.NewResourceScope().Project(project).Stage(stage).Resource(resourceURI),
v2.ResourcesGetResourceOptions{})
},
func() *ResourceNotFoundError {
return &ResourceNotFoundError{uri: resourceURI, project: project, stage: stage}
},
func(cause error) *ResourceRetrievalFailedError {
return &ResourceRetrievalFailedError{ResourceError{uri: resourceURI, project: project, stage: stage}, cause}
},
func() *ResourceEmptyError {
return &ResourceEmptyError{uri: resourceURI, project: project, stage: stage}
})
}
// GetProjectResource tries to retrieve a resource on project level.
func (rc *ResourceHelper) GetProjectResource(ctx context.Context, project string, resourceURI string) (string, error) {
return getResourceByFunc(
func() (*keptnmodels.Resource, error) {
return rc.client.GetResource(ctx,
*v2.NewResourceScope().Project(project).Resource(resourceURI),
v2.ResourcesGetResourceOptions{})
},
func() *ResourceNotFoundError { return &ResourceNotFoundError{uri: resourceURI, project: project} },
func(cause error) *ResourceRetrievalFailedError {
return &ResourceRetrievalFailedError{ResourceError{uri: resourceURI, project: project}, cause}
},
func() *ResourceEmptyError { return &ResourceEmptyError{uri: resourceURI, project: project} })
}
func getResourceByFunc(
resFunc func() (*keptnmodels.Resource, error),
rnfErrFunc func() *ResourceNotFoundError,
rrfErrFunc func(cause error) *ResourceRetrievalFailedError,
reErrFunc func() *ResourceEmptyError) (string, error) {
resource, err := resFunc()
if err != nil {
if err == v2.ResourceNotFoundError {
return "", rnfErrFunc()
}
return "", rrfErrFunc(err)
}
if resource.ResourceContent == "" {
return "", reErrFunc()
}
return resource.ResourceContent, nil
}
// UploadResource tries to upload a resource.
func (rc *ResourceHelper) UploadResource(ctx context.Context, contentToUpload []byte, remoteResourceURI string, project string, stage string, service string) error {
resources := []*keptnmodels.Resource{{ResourceContent: string(contentToUpload), ResourceURI: &remoteResourceURI}}
_, err := rc.client.CreateResources(ctx, project, stage, service, resources, v2.ResourcesCreateResourcesOptions{})
if err != nil {
return &ResourceUploadFailedError{
ResourceError{
uri: remoteResourceURI,
project: project,
stage: stage,
service: service,
},
err.GetMessage(),
}
}
log.WithField("remoteResourceURI", remoteResourceURI).Info("Uploaded file")
return nil
}