-
Notifications
You must be signed in to change notification settings - Fork 50
/
client.go
96 lines (79 loc) · 2.33 KB
/
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
package client
import (
"github.com/akutz/gofig"
"github.com/emccode/libstorage/api/context"
"github.com/emccode/libstorage/api/registry"
"github.com/emccode/libstorage/api/types"
apicnfg "github.com/emccode/libstorage/api/utils/config"
// load the local imports
_ "github.com/emccode/libstorage/imports/local"
)
type client struct {
config gofig.Config
sd types.StorageDriver
od types.OSDriver
id types.IntegrationDriver
ctx types.Context
api types.APIClient
xli types.StorageExecutorCLI
}
// New returns a new libStorage client.
func New(config gofig.Config) (types.Client, error) {
if config == nil {
var err error
if config, err = apicnfg.NewConfig(); err != nil {
return nil, err
}
}
config = config.Scope(types.ConfigClient)
types.BackCompat(config)
var (
c *client
err error
)
c = &client{ctx: context.Background(), config: config}
c.ctx = c.ctx.WithValue(context.ClientKey, c)
if config.IsSet(types.ConfigService) {
c.ctx = c.ctx.WithValue(
context.ServiceKey, config.GetString(types.ConfigService))
}
storageDriverName := config.GetString(types.ConfigStorageDriver)
if c.sd, err = registry.NewStorageDriver(storageDriverName); err != nil {
return nil, err
}
if err = c.sd.Init(c.ctx, config); err != nil {
return nil, err
}
if papi, ok := c.sd.(types.ProvidesAPIClient); ok {
c.api = papi.API()
}
if pxli, pxliOk := c.sd.(types.ProvidesStorageExecutorCLI); pxliOk {
c.xli = pxli.XCLI()
}
c.ctx.Info("storage driver initialized")
// if the API or XLI are nil, then the storage driver is not the libStorage
// storage driver, and we should jump avoid any more initialization
if c.api == nil || c.xli == nil {
c.ctx.Info("created libStorage client")
return c, nil
}
osDriverName := config.GetString(types.ConfigOSDriver)
if c.od, err = registry.NewOSDriver(osDriverName); err != nil {
return nil, err
}
if err = c.od.Init(c.ctx, config); err != nil {
return nil, err
}
c.ctx.Info("os driver initialized")
integrationDriverName := config.GetString(types.ConfigIntegrationDriver)
if c.id, err = registry.NewIntegrationDriver(
integrationDriverName); err != nil {
return nil, err
}
if err := c.id.Init(c.ctx, config); err != nil {
return nil, err
}
c.ctx.Info("integration driver initialized")
c.ctx.Info("created libStorage client")
return c, nil
}