Files
my-rook-config/cmd/rookctl/object/bucket.go
T

170 lines
4.1 KiB
Go
Raw Normal View History

/*
Copyright 2017 The Rook Authors. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package object
import (
"bytes"
"fmt"
"os"
2017-06-20 12:10:30 -07:00
"github.com/rook/rook/cmd/rookctl/rook"
"github.com/rook/rook/pkg/rook/client"
"github.com/spf13/cobra"
)
2017-03-07 14:38:55 -08:00
var (
purge bool
)
var bucketCmd = &cobra.Command{
Use: "bucket",
2017-09-14 16:11:20 -07:00
Short: "Performs commands and operations on object store buckets",
}
func init() {
bucketCmd.AddCommand(bucketListCmd)
bucketListCmd.RunE = listBucketsEntry
2017-03-07 14:38:55 -08:00
bucketCmd.AddCommand(bucketGetCmd)
bucketGetCmd.RunE = getBucketEntry
bucketCmd.AddCommand(bucketDeleteCmd)
bucketDeleteCmd.RunE = deleteBucketEntry
bucketDeleteCmd.Flags().BoolVarP(&purge, "purge", "p", false, "delete bucket contents")
}
var bucketListCmd = &cobra.Command{
2017-09-15 14:52:24 -07:00
Use: "list [ObjectStore]",
Short: "Gets a listing with details of all buckets in the object store",
Aliases: []string{"ls"},
}
func listBucketsEntry(cmd *cobra.Command, args []string) error {
rook.SetupLogging()
2017-09-14 16:11:20 -07:00
if err := checkObjectArgs(args, []string{}); err != nil {
return err
}
c := rook.NewRookNetworkRestClient()
2017-09-14 16:11:20 -07:00
out, err := listBuckets(args[0], c)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
fmt.Print(out)
return nil
}
func listBuckets(storeName string, c client.RookRestClient) (string, error) {
buckets, err := c.ListBuckets(storeName)
if err != nil {
2017-03-07 14:38:55 -08:00
return "", fmt.Errorf("failed to list buckets: %+v", err)
}
if len(buckets) == 0 {
return "", nil
}
var buffer bytes.Buffer
w := rook.NewTableWriter(&buffer)
fmt.Fprintln(w, "NAME\tOWNER\tCREATED AT\tSIZE\tNUMBER OF OBJECTS")
for _, b := range buckets {
fmt.Fprintf(w, "%s\t%s\t%s\t%d\t%d\n", b.Name, b.Owner, b.CreatedAt, b.Size, b.NumberOfObjects)
}
w.Flush()
return buffer.String(), nil
}
2017-03-07 14:38:55 -08:00
var bucketGetCmd = &cobra.Command{
2017-09-14 16:11:20 -07:00
Use: "get [ObjectStore] [BucketName]",
Short: "Gets the details of a bucket in the object store",
2017-03-07 14:38:55 -08:00
}
func getBucketEntry(cmd *cobra.Command, args []string) error {
rook.SetupLogging()
2017-09-14 16:11:20 -07:00
if err := checkObjectArgs(args, []string{"[BucketName]"}); err != nil {
return err
2017-03-07 14:38:55 -08:00
}
c := rook.NewRookNetworkRestClient()
2017-09-14 16:11:20 -07:00
out, err := getBucket(c, args[0], args[1])
2017-03-07 14:38:55 -08:00
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
fmt.Print(out)
return nil
}
func getBucket(c client.RookRestClient, storeName, bucketName string) (string, error) {
bucket, err := c.GetBucket(storeName, bucketName)
2017-03-07 14:38:55 -08:00
if err != nil {
return "", fmt.Errorf("failed to get bucket: %+v", err)
}
var buffer bytes.Buffer
w := rook.NewTableWriter(&buffer)
fmt.Fprintf(w, "Name:\t%s\n", bucket.Name)
fmt.Fprintf(w, "Owner:\t%s\n", bucket.Owner)
fmt.Fprintf(w, "Creation time:\t%s\n", bucket.CreatedAt)
fmt.Fprintf(w, "Size:\t%d\n", bucket.Size)
fmt.Fprintf(w, "Number of Objects:\t%d\n", bucket.NumberOfObjects)
w.Flush()
return buffer.String(), nil
}
var bucketDeleteCmd = &cobra.Command{
2017-09-14 16:11:20 -07:00
Use: "delete [ObjectStore] [BucketName]",
Short: "Deletes the bucket from the object store",
2017-03-07 14:38:55 -08:00
}
func deleteBucketEntry(cmd *cobra.Command, args []string) error {
rook.SetupLogging()
2017-09-14 16:11:20 -07:00
if err := checkObjectArgs(args, []string{"[BucketName]"}); err != nil {
return err
2017-03-07 14:38:55 -08:00
}
c := rook.NewRookNetworkRestClient()
2017-09-14 16:11:20 -07:00
out, err := deleteBucket(c, args[0], args[1])
2017-03-07 14:38:55 -08:00
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
fmt.Print(out)
return nil
}
func deleteBucket(c client.RookRestClient, storeName, bucketName string) (string, error) {
err := c.DeleteBucket(storeName, bucketName, purge)
2017-03-07 14:38:55 -08:00
if err != nil {
if client.IsHttpNotFound(err) {
return "", fmt.Errorf("Unable to find bucket %s", bucketName)
}
return "", fmt.Errorf("failed to delete bucket: %+v", err)
}
return "Bucket deleted\n", nil
}