2019-02-04 16:56:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information
|
|
|
|
|
|
|
|
package testplanet
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2019-04-09 18:01:45 +01:00
|
|
|
"strconv"
|
2019-04-25 09:17:26 +01:00
|
|
|
"time"
|
2019-02-04 16:56:10 +00:00
|
|
|
|
2019-02-08 12:57:35 +00:00
|
|
|
"github.com/spf13/pflag"
|
2019-02-04 16:56:10 +00:00
|
|
|
"github.com/zeebo/errs"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2019-07-23 15:58:45 +01:00
|
|
|
libuplink "storj.io/storj/lib/uplink"
|
2019-02-08 12:57:35 +00:00
|
|
|
"storj.io/storj/pkg/cfgstruct"
|
2019-02-04 16:56:10 +00:00
|
|
|
"storj.io/storj/pkg/identity"
|
2019-05-24 17:51:27 +01:00
|
|
|
"storj.io/storj/pkg/macaroon"
|
2019-02-04 16:56:10 +00:00
|
|
|
"storj.io/storj/pkg/pb"
|
2019-02-11 11:17:32 +00:00
|
|
|
"storj.io/storj/pkg/peertls/tlsopts"
|
2019-02-04 16:56:10 +00:00
|
|
|
"storj.io/storj/pkg/storj"
|
|
|
|
"storj.io/storj/pkg/transport"
|
|
|
|
"storj.io/storj/satellite"
|
2019-02-05 17:22:17 +00:00
|
|
|
"storj.io/storj/satellite/console"
|
2019-02-08 12:57:35 +00:00
|
|
|
"storj.io/storj/uplink"
|
2019-03-18 10:55:06 +00:00
|
|
|
"storj.io/storj/uplink/metainfo"
|
|
|
|
"storj.io/storj/uplink/piecestore"
|
2019-07-23 15:58:45 +01:00
|
|
|
"storj.io/storj/uplink/setup"
|
2019-02-04 16:56:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Uplink is a general purpose
|
|
|
|
type Uplink struct {
|
|
|
|
Log *zap.Logger
|
|
|
|
Info pb.Node
|
|
|
|
Identity *identity.FullIdentity
|
|
|
|
Transport transport.Client
|
|
|
|
StorageNodeCount int
|
2019-02-05 17:22:17 +00:00
|
|
|
APIKey map[storj.NodeID]string
|
2019-02-04 16:56:10 +00:00
|
|
|
}
|
|
|
|
|
2019-06-21 14:39:43 +01:00
|
|
|
// newUplinks creates initializes uplinks, requires peer to have at least one satellite
|
|
|
|
func (planet *Planet) newUplinks(prefix string, count, storageNodeCount int) ([]*Uplink, error) {
|
|
|
|
var xs []*Uplink
|
|
|
|
for i := 0; i < count; i++ {
|
|
|
|
uplink, err := planet.newUplink(prefix+strconv.Itoa(i), storageNodeCount)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
xs = append(xs, uplink)
|
|
|
|
}
|
|
|
|
|
|
|
|
return xs, nil
|
|
|
|
}
|
|
|
|
|
2019-02-04 16:56:10 +00:00
|
|
|
// newUplink creates a new uplink
|
|
|
|
func (planet *Planet) newUplink(name string, storageNodeCount int) (*Uplink, error) {
|
|
|
|
identity, err := planet.NewIdentity()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-04-08 19:15:19 +01:00
|
|
|
tlsOpts, err := tlsopts.NewOptions(identity, tlsopts.Config{
|
2019-04-09 18:01:45 +01:00
|
|
|
PeerIDVersions: strconv.Itoa(int(planet.config.IdentityVersion.Number)),
|
2019-04-08 19:15:19 +01:00
|
|
|
})
|
2019-02-11 11:17:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-02-04 16:56:10 +00:00
|
|
|
uplink := &Uplink{
|
|
|
|
Log: planet.log.Named(name),
|
|
|
|
Identity: identity,
|
|
|
|
StorageNodeCount: storageNodeCount,
|
|
|
|
}
|
|
|
|
|
|
|
|
uplink.Log.Debug("id=" + identity.ID.String())
|
|
|
|
|
2019-02-11 11:17:32 +00:00
|
|
|
uplink.Transport = transport.NewClient(tlsOpts)
|
2019-02-04 16:56:10 +00:00
|
|
|
|
|
|
|
uplink.Info = pb.Node{
|
2019-04-22 10:07:50 +01:00
|
|
|
Id: uplink.Identity.ID,
|
2019-02-04 16:56:10 +00:00
|
|
|
Address: &pb.NodeAddress{
|
|
|
|
Transport: pb.NodeTransport_TCP_TLS_GRPC,
|
|
|
|
Address: "",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-02-05 17:22:17 +00:00
|
|
|
apiKeys := make(map[storj.NodeID]string)
|
|
|
|
for j, satellite := range planet.Satellites {
|
|
|
|
// TODO: find a nicer way to do this
|
|
|
|
// populate satellites console with example
|
|
|
|
// project and API key and pass that to uplinks
|
|
|
|
consoleDB := satellite.DB.Console()
|
|
|
|
|
|
|
|
projectName := fmt.Sprintf("%s_%d", name, j)
|
2019-05-24 17:51:27 +01:00
|
|
|
key, err := macaroon.NewAPIKey([]byte("testSecret"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-02-05 17:22:17 +00:00
|
|
|
|
|
|
|
project, err := consoleDB.Projects().Insert(
|
|
|
|
context.Background(),
|
|
|
|
&console.Project{
|
|
|
|
Name: projectName,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = consoleDB.APIKeys().Create(
|
|
|
|
context.Background(),
|
2019-05-24 17:51:27 +01:00
|
|
|
key.Head(),
|
2019-02-05 17:22:17 +00:00
|
|
|
console.APIKeyInfo{
|
|
|
|
Name: "root",
|
|
|
|
ProjectID: project.ID,
|
2019-05-24 17:51:27 +01:00
|
|
|
Secret: []byte("testSecret"),
|
2019-02-05 17:22:17 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-05-24 17:51:27 +01:00
|
|
|
apiKeys[satellite.ID()] = key.Serialize()
|
2019-02-05 17:22:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uplink.APIKey = apiKeys
|
2019-02-04 16:56:10 +00:00
|
|
|
planet.uplinks = append(planet.uplinks, uplink)
|
|
|
|
|
|
|
|
return uplink, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ID returns uplink id
|
2019-07-23 15:58:45 +01:00
|
|
|
func (client *Uplink) ID() storj.NodeID { return client.Info.Id }
|
2019-02-04 16:56:10 +00:00
|
|
|
|
|
|
|
// Addr returns uplink address
|
2019-07-23 15:58:45 +01:00
|
|
|
func (client *Uplink) Addr() string { return client.Info.Address.Address }
|
2019-02-04 16:56:10 +00:00
|
|
|
|
|
|
|
// Local returns uplink info
|
2019-07-23 15:58:45 +01:00
|
|
|
func (client *Uplink) Local() pb.Node { return client.Info }
|
2019-02-04 16:56:10 +00:00
|
|
|
|
|
|
|
// Shutdown shuts down all uplink dependencies
|
2019-07-23 15:58:45 +01:00
|
|
|
func (client *Uplink) Shutdown() error { return nil }
|
2019-02-04 16:56:10 +00:00
|
|
|
|
2019-03-18 10:55:06 +00:00
|
|
|
// DialMetainfo dials destination with apikey and returns metainfo Client
|
2019-07-23 15:58:45 +01:00
|
|
|
func (client *Uplink) DialMetainfo(ctx context.Context, destination Peer, apikey string) (*metainfo.Client, error) {
|
|
|
|
return metainfo.Dial(ctx, client.Transport, destination.Addr(), apikey)
|
2019-03-18 10:55:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DialPiecestore dials destination storagenode and returns a piecestore client.
|
2019-07-23 15:58:45 +01:00
|
|
|
func (client *Uplink) DialPiecestore(ctx context.Context, destination Peer) (*piecestore.Client, error) {
|
2019-03-18 10:55:06 +00:00
|
|
|
node := destination.Local()
|
2019-07-23 15:58:45 +01:00
|
|
|
return piecestore.Dial(ctx, client.Transport, &node.Node, client.Log.Named("uplink>piecestore"), piecestore.DefaultConfig)
|
2019-03-18 10:55:06 +00:00
|
|
|
}
|
|
|
|
|
2019-02-04 16:56:10 +00:00
|
|
|
// Upload data to specific satellite
|
2019-07-23 15:58:45 +01:00
|
|
|
func (client *Uplink) Upload(ctx context.Context, satellite *satellite.Peer, bucket string, path storj.Path, data []byte) error {
|
|
|
|
return client.UploadWithExpiration(ctx, satellite, bucket, path, data, time.Time{})
|
2019-05-08 12:11:59 +01:00
|
|
|
}
|
|
|
|
|
2019-06-19 21:13:11 +01:00
|
|
|
// UploadWithExpiration data to specific satellite and expiration time
|
2019-07-23 15:58:45 +01:00
|
|
|
func (client *Uplink) UploadWithExpiration(ctx context.Context, satellite *satellite.Peer, bucket string, path storj.Path, data []byte, expiration time.Time) error {
|
|
|
|
return client.UploadWithExpirationAndConfig(ctx, satellite, nil, bucket, path, data, expiration)
|
2019-03-21 14:26:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// UploadWithConfig uploads data to specific satellite with configured values
|
2019-07-23 15:58:45 +01:00
|
|
|
func (client *Uplink) UploadWithConfig(ctx context.Context, satellite *satellite.Peer, redundancy *uplink.RSConfig, bucket string, path storj.Path, data []byte) error {
|
|
|
|
return client.UploadWithExpirationAndConfig(ctx, satellite, redundancy, bucket, path, data, time.Time{})
|
2019-06-19 21:13:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// UploadWithExpirationAndConfig uploads data to specific satellite with configured values and expiration time
|
2019-07-23 15:58:45 +01:00
|
|
|
func (client *Uplink) UploadWithExpirationAndConfig(ctx context.Context, satellite *satellite.Peer, redundancy *uplink.RSConfig, bucketName string, path storj.Path, data []byte, expiration time.Time) (err error) {
|
|
|
|
config := client.GetConfig(satellite)
|
2019-03-21 14:26:56 +00:00
|
|
|
if redundancy != nil {
|
2019-06-03 10:17:09 +01:00
|
|
|
if redundancy.MinThreshold > 0 {
|
|
|
|
config.RS.MinThreshold = redundancy.MinThreshold
|
|
|
|
}
|
|
|
|
if redundancy.RepairThreshold > 0 {
|
|
|
|
config.RS.RepairThreshold = redundancy.RepairThreshold
|
|
|
|
}
|
|
|
|
if redundancy.SuccessThreshold > 0 {
|
|
|
|
config.RS.SuccessThreshold = redundancy.SuccessThreshold
|
|
|
|
}
|
|
|
|
if redundancy.MaxThreshold > 0 {
|
|
|
|
config.RS.MaxThreshold = redundancy.MaxThreshold
|
|
|
|
}
|
|
|
|
if redundancy.ErasureShareSize > 0 {
|
|
|
|
config.RS.ErasureShareSize = redundancy.ErasureShareSize
|
|
|
|
}
|
2019-03-21 14:26:56 +00:00
|
|
|
}
|
|
|
|
|
2019-07-23 15:58:45 +01:00
|
|
|
project, bucket, err := client.GetProjectAndBucket(ctx, satellite, bucketName, config)
|
2019-03-21 14:26:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-07-23 15:58:45 +01:00
|
|
|
defer func() { err = errs.Combine(err, bucket.Close(), project.Close()) }()
|
2019-03-21 14:26:56 +00:00
|
|
|
|
2019-07-23 15:58:45 +01:00
|
|
|
opts := &libuplink.UploadOptions{}
|
|
|
|
opts.Expires = expiration
|
|
|
|
opts.Volatile.RedundancyScheme = config.GetRedundancyScheme()
|
|
|
|
opts.Volatile.EncryptionParameters = config.GetEncryptionParameters()
|
2019-02-04 16:56:10 +00:00
|
|
|
|
2019-07-23 15:58:45 +01:00
|
|
|
reader := bytes.NewReader(data)
|
|
|
|
if err := bucket.UploadObject(ctx, path, reader, opts); err != nil {
|
|
|
|
return err
|
2019-02-04 16:56:10 +00:00
|
|
|
}
|
|
|
|
|
2019-07-23 15:58:45 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Download data from specific satellite
|
|
|
|
func (client *Uplink) Download(ctx context.Context, satellite *satellite.Peer, bucketName string, path storj.Path) ([]byte, error) {
|
|
|
|
project, bucket, err := client.GetProjectAndBucket(ctx, satellite, bucketName, client.GetConfig(satellite))
|
2019-02-04 16:56:10 +00:00
|
|
|
if err != nil {
|
2019-07-23 15:58:45 +01:00
|
|
|
return nil, err
|
2019-02-04 16:56:10 +00:00
|
|
|
}
|
2019-07-23 15:58:45 +01:00
|
|
|
defer func() { err = errs.Combine(err, project.Close(), bucket.Close()) }()
|
2019-02-04 16:56:10 +00:00
|
|
|
|
2019-07-23 15:58:45 +01:00
|
|
|
object, err := bucket.OpenObject(ctx, path)
|
2019-02-04 16:56:10 +00:00
|
|
|
if err != nil {
|
2019-07-23 15:58:45 +01:00
|
|
|
return nil, err
|
2019-02-04 16:56:10 +00:00
|
|
|
}
|
|
|
|
|
2019-07-23 15:58:45 +01:00
|
|
|
rc, err := object.DownloadRange(ctx, 0, object.Meta.Size)
|
2019-07-01 15:35:10 +01:00
|
|
|
if err != nil {
|
2019-07-23 15:58:45 +01:00
|
|
|
return nil, err
|
2019-07-01 15:35:10 +01:00
|
|
|
}
|
2019-07-23 15:58:45 +01:00
|
|
|
defer func() { err = errs.Combine(err, rc.Close()) }()
|
2019-07-01 15:35:10 +01:00
|
|
|
|
2019-07-23 15:58:45 +01:00
|
|
|
data, err := ioutil.ReadAll(rc)
|
|
|
|
if err != nil {
|
|
|
|
return []byte{}, err
|
|
|
|
}
|
|
|
|
return data, nil
|
2019-02-04 16:56:10 +00:00
|
|
|
}
|
|
|
|
|
2019-07-23 15:58:45 +01:00
|
|
|
// DownloadStream returns stream for downloading data
|
|
|
|
func (client *Uplink) DownloadStream(ctx context.Context, satellite *satellite.Peer, bucketName string, path storj.Path) (_ libuplink.ReadSeekCloser, cleanup func() error, err error) {
|
|
|
|
project, bucket, err := client.GetProjectAndBucket(ctx, satellite, bucketName, client.GetConfig(satellite))
|
2019-02-04 16:56:10 +00:00
|
|
|
if err != nil {
|
2019-07-23 15:58:45 +01:00
|
|
|
return nil, nil, err
|
2019-02-04 16:56:10 +00:00
|
|
|
}
|
|
|
|
|
2019-07-23 15:58:45 +01:00
|
|
|
cleanup = func() error {
|
|
|
|
err = errs.Combine(err,
|
|
|
|
project.Close(),
|
|
|
|
bucket.Close(),
|
|
|
|
)
|
|
|
|
return err
|
|
|
|
}
|
2019-02-04 16:56:10 +00:00
|
|
|
|
2019-07-23 15:58:45 +01:00
|
|
|
downloader, err := bucket.NewReader(ctx, path)
|
|
|
|
return downloader, cleanup, err
|
2019-02-04 16:56:10 +00:00
|
|
|
}
|
|
|
|
|
2019-07-23 15:58:45 +01:00
|
|
|
// Delete deletes an object at the path in a bucket
|
|
|
|
func (client *Uplink) Delete(ctx context.Context, satellite *satellite.Peer, bucketName string, path storj.Path) error {
|
|
|
|
project, bucket, err := client.GetProjectAndBucket(ctx, satellite, bucketName, client.GetConfig(satellite))
|
2019-02-04 16:56:10 +00:00
|
|
|
if err != nil {
|
2019-07-23 15:58:45 +01:00
|
|
|
return err
|
2019-02-04 16:56:10 +00:00
|
|
|
}
|
2019-07-23 15:58:45 +01:00
|
|
|
defer func() { err = errs.Combine(err, project.Close(), bucket.Close()) }()
|
2019-02-04 16:56:10 +00:00
|
|
|
|
2019-07-23 15:58:45 +01:00
|
|
|
err = bucket.DeleteObject(ctx, path)
|
2019-02-04 16:56:10 +00:00
|
|
|
if err != nil {
|
2019-07-23 15:58:45 +01:00
|
|
|
return err
|
2019-02-04 16:56:10 +00:00
|
|
|
}
|
2019-07-23 15:58:45 +01:00
|
|
|
return nil
|
2019-03-18 10:55:06 +00:00
|
|
|
}
|
|
|
|
|
2019-07-23 15:58:45 +01:00
|
|
|
// CreateBucket creates a new bucket
|
|
|
|
func (client *Uplink) CreateBucket(ctx context.Context, satellite *satellite.Peer, bucketName string) error {
|
|
|
|
project, err := client.GetProject(ctx, satellite)
|
2019-03-18 10:55:06 +00:00
|
|
|
if err != nil {
|
2019-07-23 15:58:45 +01:00
|
|
|
return err
|
2019-03-18 10:55:06 +00:00
|
|
|
}
|
2019-07-23 15:58:45 +01:00
|
|
|
defer func() { err = errs.Combine(err, project.Close()) }()
|
2019-02-04 16:56:10 +00:00
|
|
|
|
2019-07-23 15:58:45 +01:00
|
|
|
clientCfg := client.GetConfig(satellite)
|
|
|
|
bucketCfg := &libuplink.BucketConfig{}
|
|
|
|
bucketCfg.PathCipher = clientCfg.GetPathCipherSuite()
|
|
|
|
bucketCfg.EncryptionParameters = clientCfg.GetEncryptionParameters()
|
|
|
|
bucketCfg.Volatile.RedundancyScheme = clientCfg.GetRedundancyScheme()
|
|
|
|
bucketCfg.Volatile.SegmentsSize = clientCfg.GetSegmentSize()
|
2019-02-04 16:56:10 +00:00
|
|
|
|
2019-07-23 15:58:45 +01:00
|
|
|
_, err = project.CreateBucket(ctx, bucketName, bucketCfg)
|
2019-03-18 10:55:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-07-23 15:58:45 +01:00
|
|
|
return nil
|
2019-03-18 10:55:06 +00:00
|
|
|
}
|
|
|
|
|
2019-03-30 11:21:49 +00:00
|
|
|
// GetConfig returns a default config for a given satellite.
|
2019-07-23 15:58:45 +01:00
|
|
|
func (client *Uplink) GetConfig(satellite *satellite.Peer) uplink.Config {
|
2019-02-08 12:57:35 +00:00
|
|
|
config := getDefaultConfig()
|
2019-03-22 09:01:49 +00:00
|
|
|
config.Client.SatelliteAddr = satellite.Addr()
|
2019-07-23 15:58:45 +01:00
|
|
|
config.Client.APIKey = client.APIKey[satellite.ID()]
|
2019-05-10 12:26:25 +01:00
|
|
|
config.Client.RequestTimeout = 10 * time.Second
|
|
|
|
config.Client.DialTimeout = 10 * time.Second
|
2019-02-04 16:56:10 +00:00
|
|
|
|
2019-07-23 15:58:45 +01:00
|
|
|
config.RS.MinThreshold = atLeastOne(client.StorageNodeCount * 1 / 5) // 20% of storage nodes
|
|
|
|
config.RS.RepairThreshold = atLeastOne(client.StorageNodeCount * 2 / 5) // 40% of storage nodes
|
|
|
|
config.RS.SuccessThreshold = atLeastOne(client.StorageNodeCount * 3 / 5) // 60% of storage nodes
|
|
|
|
config.RS.MaxThreshold = atLeastOne(client.StorageNodeCount * 4 / 5) // 80% of storage nodes
|
2019-02-04 16:56:10 +00:00
|
|
|
|
2019-02-11 11:17:32 +00:00
|
|
|
config.TLS.UsePeerCAWhitelist = false
|
|
|
|
config.TLS.Extensions.Revocation = false
|
|
|
|
config.TLS.Extensions.WhitelistSignedLeaf = false
|
|
|
|
|
2019-02-08 12:57:35 +00:00
|
|
|
return config
|
2019-02-04 16:56:10 +00:00
|
|
|
}
|
|
|
|
|
2019-02-08 12:57:35 +00:00
|
|
|
func getDefaultConfig() uplink.Config {
|
|
|
|
config := uplink.Config{}
|
2019-04-19 19:17:30 +01:00
|
|
|
cfgstruct.Bind(&pflag.FlagSet{}, &config, cfgstruct.UseDevDefaults())
|
2019-02-08 12:57:35 +00:00
|
|
|
return config
|
2019-02-04 16:56:10 +00:00
|
|
|
}
|
2019-04-17 15:00:00 +01:00
|
|
|
|
|
|
|
// atLeastOne returns 1 if value < 1, or value otherwise.
|
|
|
|
func atLeastOne(value int) int {
|
|
|
|
if value < 1 {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
return value
|
|
|
|
}
|
2019-06-27 18:36:51 +01:00
|
|
|
|
2019-07-23 15:58:45 +01:00
|
|
|
// NewLibuplink creates a libuplink.Uplink object with the testplanet Uplink config
|
|
|
|
func (client *Uplink) NewLibuplink(ctx context.Context) (*libuplink.Uplink, error) {
|
|
|
|
config := getDefaultConfig()
|
|
|
|
libuplinkCfg := &libuplink.Config{}
|
|
|
|
libuplinkCfg.Volatile.MaxInlineSize = config.Client.MaxInlineSize
|
|
|
|
libuplinkCfg.Volatile.MaxMemory = config.RS.MaxBufferMem
|
|
|
|
libuplinkCfg.Volatile.PeerIDVersion = config.TLS.PeerIDVersions
|
|
|
|
libuplinkCfg.Volatile.TLS.SkipPeerCAWhitelist = !config.TLS.UsePeerCAWhitelist
|
|
|
|
libuplinkCfg.Volatile.TLS.PeerCAWhitelistPath = config.TLS.PeerCAWhitelistPath
|
|
|
|
libuplinkCfg.Volatile.DialTimeout = config.Client.DialTimeout
|
|
|
|
libuplinkCfg.Volatile.RequestTimeout = config.Client.RequestTimeout
|
|
|
|
|
|
|
|
return libuplink.NewUplink(ctx, libuplinkCfg)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetProject returns a libuplink.Project which allows interactions with a specific project
|
|
|
|
func (client *Uplink) GetProject(ctx context.Context, satellite *satellite.Peer) (*libuplink.Project, error) {
|
|
|
|
testLibuplink, err := client.NewLibuplink(ctx)
|
2019-06-27 18:36:51 +01:00
|
|
|
if err != nil {
|
2019-07-23 15:58:45 +01:00
|
|
|
return nil, err
|
2019-06-27 18:36:51 +01:00
|
|
|
}
|
2019-07-23 15:58:45 +01:00
|
|
|
defer func() { err = errs.Combine(err, testLibuplink.Close()) }()
|
2019-06-27 18:36:51 +01:00
|
|
|
|
2019-07-23 15:58:45 +01:00
|
|
|
clientAPIKey := client.APIKey[satellite.ID()]
|
|
|
|
key, err := libuplink.ParseAPIKey(clientAPIKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-06-27 18:36:51 +01:00
|
|
|
|
2019-07-23 15:58:45 +01:00
|
|
|
project, err := testLibuplink.OpenProject(ctx, satellite.Addr(), key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-06-27 18:36:51 +01:00
|
|
|
}
|
2019-07-23 15:58:45 +01:00
|
|
|
return project, nil
|
|
|
|
}
|
2019-06-27 18:36:51 +01:00
|
|
|
|
2019-07-23 15:58:45 +01:00
|
|
|
// GetProjectAndBucket returns a libuplink.Project and Bucket which allows interactions with a specific project and its buckets
|
|
|
|
func (client *Uplink) GetProjectAndBucket(ctx context.Context, satellite *satellite.Peer, bucketName string, clientCfg uplink.Config) (_ *libuplink.Project, _ *libuplink.Bucket, err error) {
|
|
|
|
project, err := client.GetProject(ctx, satellite)
|
2019-06-27 18:36:51 +01:00
|
|
|
if err != nil {
|
2019-07-23 15:58:45 +01:00
|
|
|
return nil, nil, err
|
2019-06-27 18:36:51 +01:00
|
|
|
}
|
2019-07-23 15:58:45 +01:00
|
|
|
|
2019-06-27 18:36:51 +01:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
2019-07-23 15:58:45 +01:00
|
|
|
err = errs.Combine(err, project.Close())
|
2019-06-27 18:36:51 +01:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2019-07-23 15:58:45 +01:00
|
|
|
access, err := setup.LoadEncryptionAccess(ctx, clientCfg.Enc)
|
2019-06-27 18:36:51 +01:00
|
|
|
if err != nil {
|
2019-07-23 15:58:45 +01:00
|
|
|
return nil, nil, err
|
2019-06-27 18:36:51 +01:00
|
|
|
}
|
|
|
|
|
2019-07-23 15:58:45 +01:00
|
|
|
// Check if the bucket exists, if not then create it
|
|
|
|
_, _, err = project.GetBucketInfo(ctx, bucketName)
|
2019-06-27 18:36:51 +01:00
|
|
|
if err != nil {
|
2019-07-23 15:58:45 +01:00
|
|
|
if storj.ErrBucketNotFound.Has(err) {
|
|
|
|
err := createBucket(ctx, clientCfg, *project, bucketName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2019-06-27 18:36:51 +01:00
|
|
|
}
|
|
|
|
|
2019-07-23 15:58:45 +01:00
|
|
|
bucket, err := project.OpenBucket(ctx, bucketName, access)
|
2019-06-27 18:36:51 +01:00
|
|
|
if err != nil {
|
2019-07-23 15:58:45 +01:00
|
|
|
return nil, nil, err
|
2019-06-27 18:36:51 +01:00
|
|
|
}
|
|
|
|
|
2019-07-23 15:58:45 +01:00
|
|
|
return project, bucket, nil
|
|
|
|
}
|
2019-06-27 18:36:51 +01:00
|
|
|
|
2019-07-23 15:58:45 +01:00
|
|
|
func createBucket(ctx context.Context, config uplink.Config, project libuplink.Project, bucketName string) error {
|
|
|
|
bucketCfg := &libuplink.BucketConfig{}
|
|
|
|
bucketCfg.PathCipher = config.GetPathCipherSuite()
|
|
|
|
bucketCfg.EncryptionParameters = config.GetEncryptionParameters()
|
|
|
|
bucketCfg.Volatile.RedundancyScheme = config.GetRedundancyScheme()
|
|
|
|
bucketCfg.Volatile.SegmentsSize = config.GetSegmentSize()
|
2019-06-27 18:36:51 +01:00
|
|
|
|
2019-07-23 15:58:45 +01:00
|
|
|
_, err := project.CreateBucket(ctx, bucketName, bucketCfg)
|
2019-06-27 18:36:51 +01:00
|
|
|
if err != nil {
|
2019-07-23 15:58:45 +01:00
|
|
|
return err
|
2019-06-27 18:36:51 +01:00
|
|
|
}
|
2019-07-23 15:58:45 +01:00
|
|
|
return nil
|
2019-06-27 18:36:51 +01:00
|
|
|
}
|