2018-05-15 01:31:26 +01:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"flag"
|
2018-05-30 03:47:40 +01:00
|
|
|
"fmt"
|
2018-05-15 01:31:26 +01:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"google.golang.org/grpc"
|
2018-05-30 03:47:40 +01:00
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
2018-05-15 01:31:26 +01:00
|
|
|
|
|
|
|
proto "storj.io/storj/protos/netstate"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
port string
|
|
|
|
)
|
|
|
|
|
|
|
|
func initializeFlags() {
|
|
|
|
flag.StringVar(&port, "port", ":8080", "port")
|
|
|
|
flag.Parse()
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
initializeFlags()
|
|
|
|
|
|
|
|
logger, _ := zap.NewDevelopment()
|
|
|
|
defer logger.Sync()
|
|
|
|
|
|
|
|
conn, err := grpc.Dial(port, grpc.WithInsecure())
|
|
|
|
if err != nil {
|
|
|
|
logger.Error("Failed to dial: ", zap.Error(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
client := proto.NewNetStateClient(conn)
|
|
|
|
|
2018-05-30 03:47:40 +01:00
|
|
|
logger.Debug(fmt.Sprintf("client dialed port %s", port))
|
|
|
|
|
2018-05-15 01:31:26 +01:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2018-05-30 03:47:40 +01:00
|
|
|
// Example pointer paths to put
|
2018-06-04 17:45:07 +01:00
|
|
|
//pr1 passes with api creds
|
2018-05-30 03:47:40 +01:00
|
|
|
pr1 := proto.PutRequest{
|
|
|
|
Path: []byte("welcome/to/my/pointer/journey"),
|
|
|
|
Pointer: &proto.Pointer{
|
2018-06-26 16:18:05 +01:00
|
|
|
Type: proto.Pointer_INLINE,
|
2018-05-30 03:47:40 +01:00
|
|
|
InlineSegment: []byte("granola"),
|
|
|
|
},
|
2018-06-04 17:45:07 +01:00
|
|
|
APIKey: []byte("abc123"),
|
2018-05-15 01:31:26 +01:00
|
|
|
}
|
2018-06-04 17:45:07 +01:00
|
|
|
// pr2 passes with api creds
|
2018-05-30 03:47:40 +01:00
|
|
|
pr2 := proto.PutRequest{
|
|
|
|
Path: []byte("so/many/pointers"),
|
|
|
|
Pointer: &proto.Pointer{
|
2018-06-26 16:18:05 +01:00
|
|
|
Type: proto.Pointer_INLINE,
|
2018-05-30 03:47:40 +01:00
|
|
|
InlineSegment: []byte("m&ms"),
|
|
|
|
},
|
2018-06-04 17:45:07 +01:00
|
|
|
APIKey: []byte("abc123"),
|
2018-05-15 01:31:26 +01:00
|
|
|
}
|
2018-06-04 17:45:07 +01:00
|
|
|
// pr3 fails api creds
|
2018-05-30 03:47:40 +01:00
|
|
|
pr3 := proto.PutRequest{
|
|
|
|
Path: []byte("another/pointer/for/the/pile"),
|
|
|
|
Pointer: &proto.Pointer{
|
2018-06-26 16:18:05 +01:00
|
|
|
Type: proto.Pointer_INLINE,
|
2018-05-30 03:47:40 +01:00
|
|
|
InlineSegment: []byte("popcorn"),
|
|
|
|
},
|
2018-06-04 17:45:07 +01:00
|
|
|
APIKey: []byte("abc13"),
|
2018-05-15 01:31:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Example Puts
|
2018-06-04 17:45:07 +01:00
|
|
|
// puts passes api creds
|
2018-05-30 03:47:40 +01:00
|
|
|
_, err = client.Put(ctx, &pr1)
|
|
|
|
if err != nil || status.Code(err) == codes.Internal {
|
2018-05-15 01:31:26 +01:00
|
|
|
logger.Error("failed to put", zap.Error(err))
|
|
|
|
}
|
2018-05-30 03:47:40 +01:00
|
|
|
_, err = client.Put(ctx, &pr2)
|
|
|
|
if err != nil || status.Code(err) == codes.Internal {
|
2018-05-15 01:31:26 +01:00
|
|
|
logger.Error("failed to put", zap.Error(err))
|
|
|
|
}
|
2018-05-30 03:47:40 +01:00
|
|
|
_, err = client.Put(ctx, &pr3)
|
|
|
|
if err != nil || status.Code(err) == codes.Internal {
|
2018-05-15 01:31:26 +01:00
|
|
|
logger.Error("failed to put", zap.Error(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Example Get
|
2018-06-04 17:45:07 +01:00
|
|
|
// get passes api creds
|
2018-05-15 01:31:26 +01:00
|
|
|
getReq := proto.GetRequest{
|
2018-06-04 17:45:07 +01:00
|
|
|
Path: []byte("so/many/pointers"),
|
|
|
|
APIKey: []byte("abc123"),
|
2018-05-15 01:31:26 +01:00
|
|
|
}
|
|
|
|
getRes, err := client.Get(ctx, &getReq)
|
2018-05-30 03:47:40 +01:00
|
|
|
if err != nil || status.Code(err) == codes.Internal {
|
2018-05-15 01:31:26 +01:00
|
|
|
logger.Error("failed to get", zap.Error(err))
|
2018-06-04 17:45:07 +01:00
|
|
|
} else {
|
|
|
|
pointer := string(getRes.Pointer)
|
|
|
|
logger.Debug("get response: " + pointer)
|
2018-05-15 01:31:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Example List
|
2018-06-04 17:45:07 +01:00
|
|
|
// list passes api creds
|
2018-05-15 01:31:26 +01:00
|
|
|
listReq := proto.ListRequest{
|
2018-05-30 03:47:40 +01:00
|
|
|
// This pagination functionality doesn't work yet.
|
|
|
|
// The given arguments are placeholders.
|
|
|
|
StartingPathKey: []byte("test/pointer/path"),
|
|
|
|
Limit: 5,
|
2018-06-04 17:45:07 +01:00
|
|
|
APIKey: []byte("abc123"),
|
2018-05-15 01:31:26 +01:00
|
|
|
}
|
2018-06-04 17:45:07 +01:00
|
|
|
|
2018-05-15 01:31:26 +01:00
|
|
|
listRes, err := client.List(ctx, &listReq)
|
2018-05-30 03:47:40 +01:00
|
|
|
if err != nil || status.Code(err) == codes.Internal {
|
2018-05-15 01:31:26 +01:00
|
|
|
logger.Error("failed to list file paths")
|
2018-06-04 17:45:07 +01:00
|
|
|
} else {
|
|
|
|
var stringList []string
|
|
|
|
for _, pathByte := range listRes.Paths {
|
|
|
|
stringList = append(stringList, string(pathByte))
|
|
|
|
}
|
|
|
|
logger.Debug("listed paths: " + strings.Join(stringList, ", "))
|
2018-05-15 01:31:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Example Delete
|
2018-06-04 17:45:07 +01:00
|
|
|
// delete passes api creds
|
2018-05-15 01:31:26 +01:00
|
|
|
delReq := proto.DeleteRequest{
|
2018-06-04 17:45:07 +01:00
|
|
|
Path: []byte("welcome/to/my/pointer/journey"),
|
|
|
|
APIKey: []byte("abc123"),
|
2018-05-15 01:31:26 +01:00
|
|
|
}
|
2018-05-30 03:47:40 +01:00
|
|
|
_, err = client.Delete(ctx, &delReq)
|
|
|
|
if err != nil || status.Code(err) == codes.Internal {
|
|
|
|
logger.Error("failed to delete: " + string(delReq.Path))
|
2018-05-15 01:31:26 +01:00
|
|
|
}
|
|
|
|
}
|