forked from ehazlett/stellar
/
client.go
149 lines (125 loc) · 3.48 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package client
import (
"context"
"time"
applicationapi "github.com/ehazlett/stellar/api/services/application/v1"
clusterapi "github.com/ehazlett/stellar/api/services/cluster/v1"
datastoreapi "github.com/ehazlett/stellar/api/services/datastore/v1"
healthapi "github.com/ehazlett/stellar/api/services/health/v1"
nameserverapi "github.com/ehazlett/stellar/api/services/nameserver/v1"
networkapi "github.com/ehazlett/stellar/api/services/network/v1"
nodeapi "github.com/ehazlett/stellar/api/services/node/v1"
proxyapi "github.com/ehazlett/stellar/api/services/proxy/v1"
versionapi "github.com/ehazlett/stellar/api/services/version/v1"
ptypes "github.com/gogo/protobuf/types"
"google.golang.org/grpc"
)
var (
empty = &ptypes.Empty{}
)
type Client struct {
conn *grpc.ClientConn
versionService versionapi.VersionClient
healthService healthapi.HealthClient
nodeService nodeapi.NodeClient
clusterService clusterapi.ClusterClient
datastoreService datastoreapi.DatastoreClient
networkService networkapi.NetworkClient
applicationService applicationapi.ApplicationClient
nameserverService nameserverapi.NameserverClient
proxyService proxyapi.ProxyClient
}
func NewClient(addr string) (*Client, error) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
c, err := grpc.DialContext(ctx,
addr,
grpc.WithInsecure(),
grpc.WithWaitForHandshake(),
)
if err != nil {
return nil, err
}
client := &Client{
conn: c,
versionService: versionapi.NewVersionClient(c),
healthService: healthapi.NewHealthClient(c),
nodeService: nodeapi.NewNodeClient(c),
clusterService: clusterapi.NewClusterClient(c),
datastoreService: datastoreapi.NewDatastoreClient(c),
networkService: networkapi.NewNetworkClient(c),
applicationService: applicationapi.NewApplicationClient(c),
nameserverService: nameserverapi.NewNameserverClient(c),
proxyService: proxyapi.NewProxyClient(c),
}
return client, nil
}
func (c *Client) Close() error {
return c.conn.Close()
}
func (c *Client) Application() *application {
return &application{
client: c.applicationService,
}
}
func (c *Client) Node() *node {
return &node{
client: c.nodeService,
}
}
func (c *Client) Cluster() *cluster {
return &cluster{
client: c.clusterService,
}
}
func (c *Client) Datastore() *datastore {
return &datastore{
client: c.datastoreService,
}
}
func (c *Client) Network() *network {
return &network{
client: c.networkService,
}
}
func (c *Client) Nameserver() *nameserver {
return &nameserver{
client: c.nameserverService,
}
}
func (c *Client) Proxy() *proxy {
return &proxy{
client: c.proxyService,
}
}
func (c *Client) Version() *version {
return &version{
client: c.versionService,
}
}
func (c *Client) Health() *health {
return &health{
client: c.healthService,
}
}
func (c *Client) VersionService() versionapi.VersionClient {
return c.versionService
}
func (c *Client) HealthService() healthapi.HealthClient {
return c.healthService
}
func (c *Client) NodeService() nodeapi.NodeClient {
return c.nodeService
}
func (c *Client) ClusterService() clusterapi.ClusterClient {
return c.clusterService
}
func (c *Client) DatastoreService() datastoreapi.DatastoreClient {
return c.datastoreService
}
func (c *Client) NetworkService() networkapi.NetworkClient {
return c.networkService
}
func (c *Client) NameserverService() nameserverapi.NameserverClient {
return c.nameserverService
}