/
cluster.go
109 lines (94 loc) · 3.12 KB
/
cluster.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
package cluster
import (
"crypto/tls"
"net/url"
"github.com/libopenstorage/openstorage/api/client"
"github.com/libopenstorage/openstorage/cluster"
)
const (
// OsdSocket is the unix socket for cluster apis
OsdSocket = "osd"
APIVersion = cluster.APIVersion
)
// ClusterManager returns a REST wrapper for the Cluster interface.
func ClusterManager(c *client.Client) cluster.Cluster {
return newClusterClient(c)
}
// NewAuthClusterClient returns a new REST client.
// host: REST endpoint [http://<ip>:<port> OR unix://<path-to-unix-socket>]. default: [unix://var/lib/osd/cluster/osd.sock]
// version: Cluster API version
func NewAuthClusterClient(host, version string, authstring string, accesstoken string) (*client.Client, error) {
if host == "" {
host = client.GetUnixServerPath(OsdSocket, cluster.APIBase)
}
if version == "" {
// Set the default version
version = cluster.APIVersion
}
return client.NewAuthClient(host, version, authstring, accesstoken, "")
}
// NewInsecureTLSAuthClusterClient returns a new REST client that will skip TLS verification for https
// host: REST endpoint [http(s)://<ip>:<port>]
// version: ClusterAPI version
func NewInsecureTLSAuthClusterClient(host, version, auth string, accesstoken string) (*client.Client, error) {
if host == "" {
host = client.GetUnixServerPath(OsdSocket, cluster.APIBase)
}
if version == "" {
// Set the default version
version = cluster.APIVersion
}
var (
skipTLSVerify bool
)
u, err := url.Parse(host)
if err == nil && len(u.Scheme) != 0 {
if u.Scheme == "https" {
// We don't support cert validation yet
skipTLSVerify = true
} else if u.Scheme != "http" {
// In certain cases like AWS ELB - ae20db68c7cb34616b16837ab395fe9c-1428320453.us-east-2.elb.amazonaws.com
// url.Parse returns scheme as the actual endpoint
host = "http://" + host
} // else u.Scheme == http
} else {
host = "http://" + host
}
clnt, err := client.NewAuthClient(host, version, auth, accesstoken, "")
if err != nil {
return nil, err
}
if skipTLSVerify {
clnt.SetTLS(&tls.Config{InsecureSkipVerify: true})
}
return clnt, nil
}
// NewClusterClient returns a new REST client.
// host: REST endpoint [http://<ip>:<port> OR unix://<path-to-unix-socket>]. default: [unix://var/lib/osd/cluster/osd.sock]
// version: Cluster API version
func NewClusterClient(host, version string) (*client.Client, error) {
if host == "" {
host = client.GetUnixServerPath(OsdSocket, cluster.APIBase)
}
if version == "" {
// Set the default version
version = cluster.APIVersion
}
return client.NewClient(host, version, "")
}
// GetSupportedClusterVersions returns a list of supported versions of the Cluster API
// host: REST endpoint [http://<ip>:<port> OR unix://<path-to-unix-socket>]. default: [unix://var/lib/osd/cluster/osd.sock]
func GetSupportedClusterVersions(host string) ([]string, error) {
if host == "" {
host = client.GetUnixServerPath(OsdSocket, cluster.APIBase)
}
client, err := client.NewClient(host, "", "")
if err != nil {
return []string{}, err
}
versions, err := client.Versions("cluster")
if err != nil {
return []string{}, err
}
return versions, nil
}