-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathnamespace.go
171 lines (151 loc) · 4.13 KB
/
namespace.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
168
169
170
171
package cli
import (
"errors"
"fmt"
"github.com/raystack/compass/core/namespace"
"os"
"strings"
"github.com/raystack/compass/internal/client"
compassv1beta1 "github.com/raystack/compass/proto/raystack/compass/v1beta1"
"github.com/raystack/salt/printer"
"github.com/raystack/salt/term"
"github.com/MakeNowJust/heredoc"
"github.com/spf13/cobra"
)
func namespacesCommand(cfg *Config) *cobra.Command {
cmd := &cobra.Command{
Use: "namespace",
Aliases: []string{"ns"},
Short: "Manage namespaces",
Annotations: map[string]string{
"group": "core",
},
Example: heredoc.Doc(`
$ compass namespace list
$ compass namespace view
$ compass namespace create
`),
}
cmd.AddCommand(
listNamespacesCommand(cfg),
getNamespaceCommand(cfg),
createNamespaceCommand(cfg),
)
return cmd
}
func listNamespacesCommand(cfg *Config) *cobra.Command {
var json string
cmd := &cobra.Command{
Use: "list",
Short: "lists all namespaces",
Example: heredoc.Doc(`
$ compass namespace list
`),
Annotations: map[string]string{
"action:core": "true",
},
RunE: func(cmd *cobra.Command, args []string) error {
spinner := printer.Spin("")
defer spinner.Stop()
cl, cancel, err := client.Create(cmd.Context(), cfg.Client)
if err != nil {
return err
}
defer cancel()
ctx := client.SetMetadata(cmd.Context(), cfg.Client, "")
res, err := cl.ListNamespaces(ctx, &compassv1beta1.ListNamespacesRequest{})
if err != nil {
return err
}
if json != "json" {
var report [][]string
report = append(report, []string{"ID", "NAME", "STATE"})
index := 1
for _, i := range res.GetNamespaces() {
report = append(report, []string{i.GetId(), i.GetName(), i.GetState()})
index++
}
printer.Table(os.Stdout, report)
fmt.Println(term.Cyanf("To view all the data in JSON format, use flag `-o json`"))
} else {
fmt.Println(term.Bluef(prettyPrint(res.GetNamespaces())))
}
return nil
},
}
cmd.Flags().StringVarP(&json, "out", "o", "table", "flag to control output viewing, for json `-o json`")
return cmd
}
func getNamespaceCommand(cfg *Config) *cobra.Command {
cmd := &cobra.Command{
Use: "view <id>",
Short: "view namespace for the given uuid or name",
Example: heredoc.Doc(`
$ compass namespace view <id>
`),
Annotations: map[string]string{
"action:core": "true",
},
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
spinner := printer.Spin("")
defer spinner.Stop()
cl, cancel, err := client.Create(cmd.Context(), cfg.Client)
if err != nil {
return err
}
defer cancel()
urn := args[0]
ctx := client.SetMetadata(cmd.Context(), cfg.Client, "")
res, err := cl.GetNamespace(ctx, &compassv1beta1.GetNamespaceRequest{
Urn: urn,
})
if err != nil {
return err
}
spinner.Stop()
fmt.Println(term.Bluef(prettyPrint(res.GetNamespace())))
return nil
},
}
return cmd
}
func createNamespaceCommand(cfg *Config) *cobra.Command {
var name, state string
cmd := &cobra.Command{
Use: "create",
Short: "create a new namespace",
Example: heredoc.Doc(`
$ compass namespace create
`),
Annotations: map[string]string{
"action:core": "true",
},
RunE: func(cmd *cobra.Command, args []string) error {
spinner := printer.Spin("")
defer spinner.Stop()
if len(name) < 3 || strings.ContainsAny(name, " .-") {
return errors.New("namespace length should be of at least 3 character, without space and special characters")
}
cl, cancel, err := client.Create(cmd.Context(), cfg.Client)
if err != nil {
return err
}
defer cancel()
ctx := client.SetMetadata(cmd.Context(), cfg.Client, "")
res, err := cl.CreateNamespace(ctx, &compassv1beta1.CreateNamespaceRequest{
Name: name,
State: state,
})
if err != nil {
return err
}
spinner.Stop()
fmt.Println("ID: \t", term.Greenf(res.Id))
return nil
},
}
cmd.Flags().StringVarP(&name, "name", "n", "", "namespace unique name")
cmd.Flags().StringVarP(&state, "state", "s", namespace.SharedState.String(), "is namespace shared with existing tenants or a dedicated one")
return cmd
}