Skip to content

Commit

Permalink
add command to load sample bucket (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
avsej authored Mar 5, 2024
1 parent 8ba909e commit a1fcff2
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ Useful for testing magma buckets, advanced search indexes (1536mb for KV, 1024mb
./cbdinocluster collections add {{CLUSTER_ID}} default _default test
```

#### Load travel sample bucket

```
./cbdinocluster buckets load-sample {{CLUSTER_ID}} travel-sample
```

### Advanced Usage

#### Resetting Colima
Expand Down
31 changes: 31 additions & 0 deletions cmd/buckets-load-sample.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package cmd

import (
"github.com/spf13/cobra"
"go.uber.org/zap"
)

var bucketsLoadSampleCmd = &cobra.Command{
Use: "load-sample",
Short: "Loads a sample bucket",
Args: cobra.MinimumNArgs(2),
Run: func(cmd *cobra.Command, args []string) {
helper := CmdHelper{}
logger := helper.GetLogger()
ctx := helper.GetContext()

clusterID := args[0]
bucketName := args[1]

_, deployer, cluster := helper.IdentifyCluster(ctx, clusterID)

err := deployer.LoadSampleBucket(ctx, cluster.GetID(), bucketName)
if err != nil {
logger.Fatal("failed to load sample bucket", zap.Error(err))
}
},
}

func init() {
bucketsCmd.AddCommand(bucketsLoadSampleCmd)
}
4 changes: 4 additions & 0 deletions deployment/caodeploy/deployer.go
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,10 @@ func (d *Deployer) DeleteBucket(ctx context.Context, clusterID string, bucketNam
return errors.New("caodeploy does not support deleting buckets")
}

func (d *Deployer) LoadSampleBucket(ctx context.Context, clusterID string, bucketName string) error {
return errors.New("caodeploy does not support loading sample buckets")
}

func (d *Deployer) GetCertificate(ctx context.Context, clusterID string) (string, error) {
return "", errors.New("caodeploy does not support getting certificates")
}
Expand Down
4 changes: 4 additions & 0 deletions deployment/clouddeploy/deployer.go
Original file line number Diff line number Diff line change
Expand Up @@ -1103,6 +1103,10 @@ func (p *Deployer) DeleteBucket(ctx context.Context, clusterID string, bucketNam
return nil
}

func (d *Deployer) LoadSampleBucket(ctx context.Context, clusterID string, bucketName string) error {
return errors.New("clouddeploy does not support loading sample buckets")
}

func (p *Deployer) GetCertificate(ctx context.Context, clusterID string) (string, error) {
clusterInfo, err := p.getCluster(ctx, clusterID)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions deployment/deployer.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ type Deployer interface {
ListBuckets(ctx context.Context, clusterID string) ([]BucketInfo, error)
CreateBucket(ctx context.Context, clusterID string, opts *CreateBucketOptions) error
DeleteBucket(ctx context.Context, clusterID string, bucketName string) error
LoadSampleBucket(ctx context.Context, clusterID string, bucketName string) error
GetCertificate(ctx context.Context, clusterID string) (string, error)
GetGatewayCertificate(ctx context.Context, clusterID string) (string, error)
ExecuteQuery(ctx context.Context, clusterID string, query string) (string, error)
Expand Down
19 changes: 19 additions & 0 deletions deployment/dockerdeploy/deployer.go
Original file line number Diff line number Diff line change
Expand Up @@ -918,6 +918,25 @@ func (d *Deployer) ListBuckets(ctx context.Context, clusterID string) ([]deploym
return buckets, nil
}

func (d *Deployer) LoadSampleBucket(ctx context.Context, clusterID string, bucketName string) error {
controller, err := d.getController(ctx, clusterID)
if err != nil {
return errors.Wrap(err, "failed to get cluster controller")
}

err = controller.Controller().LoadSampleBucket(ctx, bucketName)
if err != nil {
return errors.Wrap(err, "failed to load sample bucket")
}

err = controller.WaitForNoRunningTasks(ctx)
if err != nil {
return errors.Wrap(err, "failed to wait for tasks to complete after loading sample bucket")
}

return nil
}

func (d *Deployer) CreateBucket(ctx context.Context, clusterID string, opts *deployment.CreateBucketOptions) error {
controller, err := d.getController(ctx, clusterID)
if err != nil {
Expand Down
42 changes: 42 additions & 0 deletions utils/clustercontrol/controller.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package clustercontrol

import (
"bytes"
"context"
"encoding/json"
"fmt"
Expand Down Expand Up @@ -124,6 +125,33 @@ func (c *Controller) doFormPut(ctx context.Context, path string, data url.Values
return c.doFormReq(ctx, http.MethodPut, path, data, allowRetries, out)
}

func (c *Controller) doJsonReq(ctx context.Context, method string, path string, data any, allowRetries bool, out interface{}) error {
encodedData, err := json.Marshal(data)
if err != nil {
return err
}

maxRetries := 10
if !allowRetries {
maxRetries = 0
}

return c.doRetriableReq(ctx, func() (*http.Request, error) {
req, err := http.NewRequestWithContext(ctx, method, c.Endpoint+path, bytes.NewReader(encodedData))
if err != nil {
return nil, err
}

req.Header.Add("Content-Type", "application/json")

return req, nil
}, maxRetries, out)
}

func (c *Controller) doJsonPost(ctx context.Context, path string, data any, allowRetries bool, out interface{}) error {
return c.doJsonReq(ctx, http.MethodPost, path, data, allowRetries, out)
}

func (c *Controller) Ping(ctx context.Context) error {
return c.doRetriableReq(ctx, func() (*http.Request, error) {
return http.NewRequestWithContext(ctx, http.MethodGet, c.Endpoint+"/pools", nil)
Expand Down Expand Up @@ -563,6 +591,20 @@ func (c *Controller) DeleteBucket(ctx context.Context, bucketName string) error
return nil
}

func (c *Controller) LoadSampleBucket(ctx context.Context, bucketName string) error {
samples := []string{
bucketName,
}

path := "/sampleBuckets/install"
err := c.doJsonPost(ctx, path, samples, true, nil)
if err != nil {
return err
}

return nil
}

type GetTrustedCAsResponse []GetTrustedCAsResponse_Certificate

type GetTrustedCAsResponse_Certificate struct {
Expand Down

0 comments on commit a1fcff2

Please sign in to comment.