forked from pachyderm/pachyderm
-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
78 lines (64 loc) · 1.71 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
package client
import (
"errors"
"fmt"
"os"
"go.pedge.io/pb/go/google/protobuf"
"golang.org/x/net/context"
"google.golang.org/grpc"
"github.com/pachyderm/pachyderm/src/client/pfs"
"github.com/pachyderm/pachyderm/src/client/pps"
)
type PfsAPIClient pfs.APIClient
type PpsAPIClient pps.APIClient
type BlockAPIClient pfs.BlockAPIClient
type APIClient struct {
PfsAPIClient
PpsAPIClient
BlockAPIClient
}
// NewFromAddress constructs a new APIClient for the server at pachAddr.
func NewFromAddress(pachAddr string) (*APIClient, error) {
clientConn, err := grpc.Dial(pachAddr, grpc.WithInsecure())
if err != nil {
return nil, err
}
return &APIClient{
pfs.NewAPIClient(clientConn),
pps.NewAPIClient(clientConn),
pfs.NewBlockAPIClient(clientConn),
}, nil
}
// NewInCluster constructs a new APIClient using env vars that Kubernetes creates.
// This should be used to access Pachyderm from within a Kubernetes cluster
// with Pachyderm running on it.
func NewInCluster() (*APIClient, error) {
pachAddr := os.Getenv("PACHD_PORT_650_TCP_ADDR")
if pachAddr == "" {
return nil, fmt.Errorf("PACHD_PORT_650_TCP_ADDR not set")
}
return NewFromAddress(fmt.Sprintf("%v:650", pachAddr))
}
// DeleteAll deletes everything in the cluster.
// Use with caution, there is no undo.
func (c APIClient) DeleteAll() error {
if _, err := c.PpsAPIClient.DeleteAll(
context.Background(),
google_protobuf.EmptyInstance,
); err != nil {
return sanitizeErr(err)
}
if _, err := c.PfsAPIClient.DeleteAll(
context.Background(),
google_protobuf.EmptyInstance,
); err != nil {
return sanitizeErr(err)
}
return nil
}
func sanitizeErr(err error) error {
if err == nil {
return nil
}
return errors.New(grpc.ErrorDesc(err))
}