This repository has been archived by the owner on Sep 6, 2021. It is now read-only.
forked from cloudfoundry/bosh-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gcs_blobstore.go
108 lines (87 loc) · 2.22 KB
/
gcs_blobstore.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
package releasedir
import (
gobytes "bytes"
"context"
"encoding/json"
"os"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshsys "github.com/cloudfoundry/bosh-utils/system"
boshuuid "github.com/cloudfoundry/bosh-utils/uuid"
gcsclient "github.com/cloudfoundry/bosh-gcscli/client"
gcsconfig "github.com/cloudfoundry/bosh-gcscli/config"
)
type GCSBlobstore struct {
fs boshsys.FileSystem
uuidGen boshuuid.Generator
options map[string]interface{}
}
func NewGCSBlobstore(
fs boshsys.FileSystem,
uuidGen boshuuid.Generator,
options map[string]interface{},
) GCSBlobstore {
return GCSBlobstore{
fs: fs,
uuidGen: uuidGen,
options: options,
}
}
func (b GCSBlobstore) Get(blobID string) (string, error) {
client, err := b.client()
if err != nil {
return "", err
}
file, err := b.fs.TempFile("bosh-gcs-blob")
if err != nil {
return "", bosherr.WrapError(err, "Creating destination file")
}
defer file.Close()
if err := client.Get(blobID, file); err != nil {
return "", err
}
return file.Name(), nil
}
func (b GCSBlobstore) Create(path string) (string, error) {
client, err := b.client()
if err != nil {
return "", err
}
blobID, err := b.uuidGen.Generate()
if err != nil {
return "", bosherr.WrapError(err, "Generating blobstore ID")
}
file, err := b.fs.OpenFile(path, os.O_RDONLY, 0)
if err != nil {
return "", bosherr.WrapError(err, "Opening source file")
}
defer file.Close()
if err := client.Put(file, blobID); err != nil {
return "", err
}
return blobID, nil
}
func (b GCSBlobstore) CleanUp(path string) error {
return b.fs.RemoveAll(path)
}
func (b GCSBlobstore) Delete(blobID string) error {
panic("Not implemented")
}
func (b GCSBlobstore) Validate() error {
_, err := b.client()
return err
}
func (b GCSBlobstore) client() (*gcsclient.GCSBlobstore, error) {
bytes, err := json.Marshal(b.options)
if err != nil {
return nil, bosherr.WrapError(err, "Marshaling config")
}
conf, err := gcsconfig.NewFromReader(gobytes.NewBuffer(bytes))
if err != nil {
return nil, bosherr.WrapError(err, "Reading config")
}
client, err := gcsclient.New(context.Background(), &conf)
if err != nil {
return nil, bosherr.WrapError(err, "Validating config")
}
return client, nil
}