-
Notifications
You must be signed in to change notification settings - Fork 162
/
blobstore_factory.go
67 lines (56 loc) · 1.83 KB
/
blobstore_factory.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
package blobstore
import (
"fmt"
"net/http"
"net/url"
boshdavcli "github.com/cloudfoundry/bosh-davcli/client"
boshdavcliconf "github.com/cloudfoundry/bosh-davcli/config"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshlog "github.com/cloudfoundry/bosh-utils/logger"
boshsys "github.com/cloudfoundry/bosh-utils/system"
boshuuid "github.com/cloudfoundry/bosh-utils/uuid"
)
type Factory interface {
Create(string, *http.Client) (Blobstore, error)
}
type blobstoreFactory struct {
uuidGenerator boshuuid.Generator
fs boshsys.FileSystem
logger boshlog.Logger
}
func NewBlobstoreFactory(uuidGenerator boshuuid.Generator, fs boshsys.FileSystem, logger boshlog.Logger) Factory {
return blobstoreFactory{
uuidGenerator: uuidGenerator,
fs: fs,
logger: logger,
}
}
func (f blobstoreFactory) Create(blobstoreURL string, httpClient *http.Client) (Blobstore, error) {
blobstoreConfig, err := f.parseBlobstoreURL(blobstoreURL)
if err != nil {
return nil, bosherr.WrapError(err, "Creating blobstore config")
}
davClient := boshdavcli.NewClient(boshdavcliconf.Config{
Endpoint: fmt.Sprintf("%s/blobs", blobstoreConfig.Endpoint),
User: blobstoreConfig.Username,
Password: blobstoreConfig.Password,
}, httpClient, f.logger)
return NewBlobstore(davClient, f.uuidGenerator, f.fs, f.logger), nil
}
func (f blobstoreFactory) parseBlobstoreURL(blobstoreURL string) (Config, error) {
parsedURL, err := url.Parse(blobstoreURL)
if err != nil {
return Config{}, bosherr.WrapError(err, "Parsing Mbus URL")
}
var username, password string
userInfo := parsedURL.User
if userInfo != nil {
username = userInfo.Username()
password, _ = userInfo.Password()
}
return Config{
Endpoint: fmt.Sprintf("%s://%s", parsedURL.Scheme, parsedURL.Host),
Username: username,
Password: password,
}, nil
}