-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
71 lines (57 loc) · 1.7 KB
/
Copy pathclient.go
File metadata and controls
71 lines (57 loc) · 1.7 KB
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
package client
import (
"crypto/tls"
"slices"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
apiv1 "github.com/enumco/proto-gen-go/gen/enum/api/v1"
)
const defaultAddr = "api.enum.co:443"
type KubernetesClient struct {
Clusters apiv1.KubernetesClusterServiceClient
}
type ObjectStorageClient struct {
Users apiv1.ObjectStorageUserServiceClient
AccessKeys apiv1.ObjectStorageAccessKeyServiceClient
Buckets apiv1.ObjectStorageBucketServiceClient
}
type Client struct {
Organizations apiv1.OrganizationServiceClient
Projects apiv1.ProjectServiceClient
Users apiv1.UserServiceClient
Kubernetes KubernetesClient
Storage ObjectStorageClient
conn *grpc.ClientConn
}
// c, err := client.New(client.WithToken("..."))
func New(opts ...ClientOption) (*Client, error) {
o := &options{addr: defaultAddr}
for _, opt := range opts {
opt(o)
}
if o.creds == nil {
o.creds = credentials.NewTLS(&tls.Config{})
}
dialOpts := append(slices.Clone(o.dialOpts), grpc.WithTransportCredentials(o.creds))
conn, err := grpc.NewClient(o.addr, dialOpts...)
if err != nil {
return nil, err
}
return &Client{
Organizations: apiv1.NewOrganizationServiceClient(conn),
Projects: apiv1.NewProjectServiceClient(conn),
Users: apiv1.NewUserServiceClient(conn),
Kubernetes: KubernetesClient{
Clusters: apiv1.NewKubernetesClusterServiceClient(conn),
},
Storage: ObjectStorageClient{
Users: apiv1.NewObjectStorageUserServiceClient(conn),
AccessKeys: apiv1.NewObjectStorageAccessKeyServiceClient(conn),
Buckets: apiv1.NewObjectStorageBucketServiceClient(conn),
},
conn: conn,
}, nil
}
func (c *Client) Close() error {
return c.conn.Close()
}