forked from SAP/jenkins-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gcsClient.go
220 lines (191 loc) · 5.7 KB
/
gcsClient.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
package gcs
import (
"context"
"io"
"os"
"path/filepath"
"cloud.google.com/go/storage"
"github.com/SAP/jenkins-library/pkg/log"
"github.com/pkg/errors"
"google.golang.org/api/iterator"
"google.golang.org/api/option"
)
// Client is an interface to mock gcsClient
type Client interface {
UploadFile(bucketID string, sourcePath string, targetPath string) error
DownloadFile(bucketID string, sourcePath string, targetPath string) error
ListFiles(bucketID string) ([]string, error)
Close() error
}
// gcsClient provides functions to interact with google cloud storage API
type gcsClient struct {
context context.Context
envVars []EnvVar
client storage.Client
clientOptions []option.ClientOption
openFile func(name string) (io.ReadCloser, error)
createFile func(name string) (io.WriteCloser, error)
}
// EnvVar defines an environment variable incl. information about a potential modification to the variable
type EnvVar struct {
Name string
Value string
Modified bool
}
type gcsOption func(*gcsClient)
// WithEnvVars initializes env variables in gcsClient
func WithEnvVars(envVars []EnvVar) gcsOption {
return func(g *gcsClient) {
g.envVars = envVars
}
}
// WithEnvVars initializes the openFile function in gcsClient
func WithOpenFileFunction(openFile func(name string) (io.ReadCloser, error)) gcsOption {
return func(g *gcsClient) {
g.openFile = openFile
}
}
// WithEnvVars initializes the createFile function in gcsClient
func WithCreateFileFunction(createFile func(name string) (io.WriteCloser, error)) gcsOption {
return func(g *gcsClient) {
g.createFile = createFile
}
}
// WithEnvVars initializes the Google Cloud Storage client options
func WithClientOptions(opts ...option.ClientOption) gcsOption {
return func(g *gcsClient) {
g.clientOptions = append(g.clientOptions, opts...)
}
}
// Init intitializes the google cloud storage client
func NewClient(opts ...gcsOption) (*gcsClient, error) {
var (
defaultOpenFile = openFileFromFS
defaultCreateFile = createFileOnFS
)
ctx := context.Background()
gcsClient := &gcsClient{
context: ctx,
openFile: defaultOpenFile,
createFile: defaultCreateFile,
}
// options handling
for _, opt := range opts {
opt(gcsClient)
}
gcsClient.prepareEnv()
client, err := storage.NewClient(ctx, gcsClient.clientOptions...)
if err != nil {
return nil, errors.Wrapf(err, "bucket connection failed: %v", err)
}
gcsClient.client = *client
return gcsClient, nil
}
// UploadFile uploads a file into a google cloud storage bucket
func (g *gcsClient) UploadFile(bucketID string, sourcePath string, targetPath string) error {
target := g.client.Bucket(bucketID).Object(targetPath).NewWriter(g.context)
log.Entry().Debugf("uploading %v to %v", sourcePath, targetPath)
sourceFile, err := g.openFile(sourcePath)
if err != nil {
return errors.Wrapf(err, "could not open source file: %v", err)
}
defer sourceFile.Close()
if err := g.copy(sourceFile, target); err != nil {
return errors.Wrapf(err, "upload failed: %v", err)
}
if err := target.Close(); err != nil {
return errors.Wrapf(err, "closing bucket failed: %v", err)
}
return nil
}
// DownloadFile downloads a file from a google cloud storage bucket
func (g *gcsClient) DownloadFile(bucketID string, sourcePath string, targetPath string) error {
log.Entry().Debugf("downloading %v to %v\n", sourcePath, targetPath)
gcsReader, err := g.client.Bucket(bucketID).Object(sourcePath).NewReader(g.context)
if err != nil {
return errors.Wrapf(err, "could not open source file from a google cloud storage bucket: %v", err)
}
targetWriter, err := g.createFile(targetPath)
if err != nil {
return errors.Wrapf(err, "could not create target file: %v", err)
}
defer targetWriter.Close()
if err := g.copy(gcsReader, targetWriter); err != nil {
return errors.Wrapf(err, "download failed: %v", err)
}
if err := gcsReader.Close(); err != nil {
return errors.Wrapf(err, "closing bucket failed: %v", err)
}
return nil
}
// ListFiles lists all files in certain GCS bucket
func (g *gcsClient) ListFiles(bucketID string) ([]string, error) {
fileNames := []string{}
it := g.client.Bucket(bucketID).Objects(g.context, nil)
for {
attrs, err := it.Next()
if err == iterator.Done {
break
}
if err != nil {
return nil, err
}
fileNames = append(fileNames, attrs.Name)
}
return fileNames, nil
}
// Close closes the client and removes previously set environment variables
func (g *gcsClient) Close() error {
if err := g.client.Close(); err != nil {
return err
}
if err := g.cleanupEnv(); err != nil {
return err
}
return nil
}
func (g *gcsClient) copy(source io.Reader, target io.Writer) error {
if _, err := io.Copy(target, source); err != nil {
return err
}
return nil
}
// prepareEnv sets required environment variables in case they are not set yet
func (g *gcsClient) prepareEnv() {
for key, env := range g.envVars {
g.envVars[key].Modified = setenvIfEmpty(env.Name, env.Value)
}
}
// cleanupEnv removes environment variables set by prepareEnv
func (g *gcsClient) cleanupEnv() error {
for _, env := range g.envVars {
if err := removeEnvIfPreviouslySet(env.Name, env.Modified); err != nil {
return err
}
}
return nil
}
func setenvIfEmpty(env, val string) bool {
if len(os.Getenv(env)) == 0 {
os.Setenv(env, val)
return true
}
return false
}
func removeEnvIfPreviouslySet(env string, previouslySet bool) error {
if previouslySet {
if err := os.Setenv(env, ""); err != nil {
return err
}
}
return nil
}
func openFileFromFS(name string) (io.ReadCloser, error) {
return os.Open(name)
}
func createFileOnFS(name string) (io.WriteCloser, error) {
if err := os.MkdirAll(filepath.Dir(name), os.ModePerm); err != nil {
return nil, err
}
return os.Create(name)
}