forked from noobaa/noobaa-operator
/
bucket.go
167 lines (154 loc) · 4.22 KB
/
bucket.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package bucket
import (
"fmt"
"log"
"github.com/noobaa/noobaa-operator/v5/pkg/nb"
"github.com/noobaa/noobaa-operator/v5/pkg/system"
"github.com/noobaa/noobaa-operator/v5/pkg/util"
"github.com/spf13/cobra"
)
// Cmd returns a CLI command
func Cmd() *cobra.Command {
cmd := &cobra.Command{
Use: "bucket",
Short: "Manage noobaa buckets",
}
cmd.AddCommand(
CmdCreate(),
CmdDelete(),
CmdStatus(),
CmdList(),
)
return cmd
}
// CmdCreate returns a CLI command
func CmdCreate() *cobra.Command {
cmd := &cobra.Command{
Use: "create <bucket-name>",
Short: "Create a NooBaa bucket",
Run: RunCreate,
}
return cmd
}
// CmdDelete returns a CLI command
func CmdDelete() *cobra.Command {
cmd := &cobra.Command{
Use: "delete <bucket-name>",
Short: "Delete a NooBaa bucket",
Run: RunDelete,
}
return cmd
}
// CmdStatus returns a CLI command
func CmdStatus() *cobra.Command {
cmd := &cobra.Command{
Use: "status <bucket-name>",
Short: "Show the status of a NooBaa bucket",
Run: RunStatus,
}
return cmd
}
// CmdList returns a CLI command
func CmdList() *cobra.Command {
cmd := &cobra.Command{
Use: "list",
Short: "List NooBaa buckets",
Run: RunList,
Args: cobra.NoArgs,
}
return cmd
}
// RunCreate runs a CLI command
func RunCreate(cmd *cobra.Command, args []string) {
log := util.Logger()
if len(args) != 1 || args[0] == "" {
log.Fatalf(`Missing expected arguments: <bucket-name> %s`, cmd.UsageString())
}
bucketName := args[0]
nbClient := system.GetNBClient()
err := nbClient.CreateBucketAPI(nb.CreateBucketParams{Name: bucketName})
if err != nil {
log.Fatal(err)
}
}
// RunDelete runs a CLI command
func RunDelete(cmd *cobra.Command, args []string) {
log := util.Logger()
if len(args) != 1 || args[0] == "" {
log.Fatalf(`Missing expected arguments: <bucket-name> %s`, cmd.UsageString())
}
bucketName := args[0]
nbClient := system.GetNBClient()
err := nbClient.DeleteBucketAPI(nb.DeleteBucketParams{Name: bucketName})
if err != nil {
log.Fatal(err)
}
}
// RunStatus runs a CLI command
func RunStatus(cmd *cobra.Command, args []string) {
log := util.Logger()
if len(args) != 1 || args[0] == "" {
log.Fatalf(`Missing expected arguments: <bucket-name> %s`, cmd.UsageString())
}
bucketName := args[0]
nbClient := system.GetNBClient()
b, err := nbClient.ReadBucketAPI(nb.ReadBucketParams{Name: bucketName})
if err != nil {
log.Fatal(err)
}
fmt.Printf("\n")
fmt.Printf("Bucket status:\n")
fmt.Printf(" %-22s : %s\n", "Bucket", b.Name)
if b.BucketClaim != nil {
fmt.Printf(" %-22s : %s\n", "OBC Namespace", b.BucketClaim.Namespace)
fmt.Printf(" %-22s : %s\n", "OBC BucketClass", b.BucketClaim.BucketClass)
}
fmt.Printf(" %-22s : %s\n", "Type", b.BucketType)
fmt.Printf(" %-22s : %s\n", "Mode", b.Mode)
if b.PolicyModes != nil {
fmt.Printf(" %-22s : %s\n", "ResiliencyStatus", b.PolicyModes.ResiliencyStatus)
fmt.Printf(" %-22s : %s\n", "QuotaStatus", b.PolicyModes.QuotaStatus)
}
if b.Undeletable != "" {
fmt.Printf(" %-22s : %s\n", "Undeletable", b.Undeletable)
}
if b.NumObjects != nil {
if b.BucketType == "NAMESPACE" {
fmt.Printf(" %-22s : N/A\n", "Num Objects")
} else {
fmt.Printf(" %-22s : %d\n", "Num Objects", b.NumObjects.Value)
}
}
if b.DataCapacity != nil {
if b.BucketType == "NAMESPACE" {
fmt.Printf(" %-22s : N/A\n", "Data Size")
fmt.Printf(" %-22s : N/A\n", "Data Size Reduced")
fmt.Printf(" %-22s : N/A\n", "Data Space Avail")
} else {
fmt.Printf(" %-22s : %s\n", "Data Size", nb.BigIntToHumanBytes(b.DataCapacity.Size))
fmt.Printf(" %-22s : %s\n", "Data Size Reduced", nb.BigIntToHumanBytes(b.DataCapacity.SizeReduced))
fmt.Printf(" %-22s : %s\n", "Data Space Avail", nb.BigIntToHumanBytes(b.DataCapacity.AvailableToUpload))
}
}
fmt.Printf("\n")
}
// RunList runs a CLI command
func RunList(cmd *cobra.Command, args []string) {
nbClient := system.GetNBClient()
list, err := nbClient.ListBucketsAPI()
if err != nil {
log.Fatal(err)
}
if len(list.Buckets) == 0 {
fmt.Printf("No buckets found.\n")
return
}
table := (&util.PrintTable{}).AddRow("BUCKET-NAME")
for i := range list.Buckets {
b := &list.Buckets[i]
table.AddRow(b.Name)
}
fmt.Printf("\n")
fmt.Print(table.String())
fmt.Printf("\n")
}