-
Notifications
You must be signed in to change notification settings - Fork 12
/
sdk.go
111 lines (101 loc) · 3.03 KB
/
sdk.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
package dsdk
import (
"context"
"fmt"
udc "github.com/Datera/go-udc/pkg/udc"
uuid "github.com/google/uuid"
)
const (
VERSION = "1.1.5"
VERSION_HISTORY = `
1.1.0 -- Revamped SDK to new directory structure, switched to using grequests and added UDC support
1.1.1 -- Added LDAP server support
1.1.2 -- Added logs upload, template override
1.1.3 -- Support for Go modules
1.1.4 -- AppInstance AppTemplate datastructure bugfix
1.1.5 -- HTTP 503 Retry and Connection Retry support
`
)
type SDK struct {
conf *udc.UDC
Conn *ApiConnection
Ctxt context.Context
AccessNetworkIpPools *AccessNetworkIpPools
AppInstances *AppInstances
AppTemplates *AppTemplates
Initiators *Initiators
InitiatorGroups *InitiatorGroups
LogsUpload *LogsUpload
RemoteProvider *RemoteProviders
StorageNodes *StorageNodes
StoragePools *StoragePools
System *System
Tenants *Tenants
}
func NewSDK(c *udc.UDC, secure bool) (*SDK, error) {
var err error
if c == nil {
c, err = udc.GetConfig()
if err != nil {
Log().Error(err)
return nil, err
}
}
conn := NewApiConnection(c, secure)
return &SDK{
conf: c,
Conn: conn,
AccessNetworkIpPools: newAccessNetworkIpPools("/"),
AppInstances: newAppInstances("/"),
AppTemplates: newAppTemplates("/"),
Initiators: newInitiators("/"),
InitiatorGroups: newInitiatorGroups("/"),
LogsUpload: newLogsUpload("/"),
RemoteProvider: newRemoteProviders("/"),
StorageNodes: newStorageNodes("/"),
StoragePools: newStoragePools("/"),
System: newSystem("/"),
Tenants: newTenants("/"),
}, nil
}
func (c SDK) SetDriver(d string) {
DateraDriver = d
}
func (c SDK) WithContext(ctxt context.Context) context.Context {
return context.WithValue(ctxt, "conn", c.Conn)
}
func (c SDK) NewContext() context.Context {
ctxt := context.WithValue(context.Background(), "conn", c.Conn)
ctxt = context.WithValue(ctxt, "tid", uuid.Must(uuid.NewRandom()).String())
return ctxt
}
func (c SDK) GetDateraVersion() (string, error) {
sys, apierr, err := c.System.Get(&SystemGetRequest{
Ctxt: context.WithValue(c.NewContext(), "quiet", true),
})
if err != nil {
return "", err
}
if apierr != nil {
return "", fmt.Errorf("ApiError: %s", Pretty(apierr))
}
return sys.SwVersion, nil
}
// Cleans AppInstances, AppTemplates, StorageInstances, Initiators and InitiatorGroups under
// the currently configured tenant
func (c SDK) HealthCheck() error {
sns, apierr, err := c.StorageNodes.List(&StorageNodesListRequest{
Ctxt: context.WithValue(c.NewContext(), "quiet", true),
})
if err != nil {
return err
}
if apierr != nil {
return fmt.Errorf("ApiError: %s", Pretty(apierr))
}
Log().Debugf("Connected to cluster: %s with tenant %s.\n", c.conf.MgmtIp, c.conf.Tenant)
for _, sn := range sns {
Log().Debugf("Found Storage Node: %s\n", sn.Uuid)
}
return nil
}