-
Notifications
You must be signed in to change notification settings - Fork 2
/
clusters.go
222 lines (178 loc) · 6.06 KB
/
clusters.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
Copyright © 2021
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 cmd
import (
"encoding/json"
"fmt"
cc "github.com/camunda-community-hub/camunda-cloud-go-client/pkg/cc/client"
"github.com/spf13/cobra"
)
var (
name string
id string
channel string
generation string
region string
plan string
)
var (
deleteExample = `
# Delete cluster by id
cc-ctl clusters delete --id=<cluster_id>`
getExample = `
# List all clusters
cc-ctl clusters get --all
# Get cluster by name
cc-ctl clusters get --name=<cluster_name> (If your cluster have a composite name, use: --name='<cluster name>')
# Get params to create a cluster
cc-ctl clusters get --params`
createExample = `
# Create cluster with default configuration
cc-ctl clusters create --default --name=<cluster_name> (If your cluster have a composite name, use: --name='<cluster name>')
# Crate cluster with custom configuration
cc-ctl clusters create
--name=<cluster_name> (If your cluster have a composite name, use: --name='<cluster name>'
--channel=<channel_id>
--generation=<generation_id>
--region=<region_id>
--plan=<plan_type_id>`
)
var clusterCmd = &cobra.Command{
Use: "clusters [options]",
Short: "Manage your cluster's resources on Camunda Cloud",
Long: "Used together [OPTIONS] like get, create, delete for manage your resources on Camunda Cloud. For example:" + getExample + createExample + deleteExample,
}
var getClusterCmd = &cobra.Command{
Use: "get",
Short: "Get clusters",
Long: "Used together with clusters command, to get your clusters on Camunda Cloud. For example:" + getExample,
Run: func(cmd *cobra.Command, args []string) {
all, _ := cmd.Flags().GetBool("all")
params, _ := cmd.Flags().GetBool("params")
if name != "" && params && all {
fmt.Println("Error: --all and --name and --params cannot be specified together")
return
}
if name != "" && params {
fmt.Println("Error: --name and --params cannot be specified together")
return
}
if name != "" && all {
fmt.Println("Error: --all and --name cannot be specified together")
return
}
if all && params {
fmt.Println("Error: --all and --params cannot be specified together")
return
}
if name != "" {
cluster, _ := client.GetClusterByName(name)
showCluster(cluster)
return
}
if all {
clusters, _ := client.GetClusters()
showClusters(clusters)
return
}
if params {
params, _ := client.GetClusterParams()
showParams(*params)
}
},
}
var createClusterCmd = &cobra.Command{
Use: "create",
Short: "Create cluster",
Long: "Used together clusters command, to create your clusters on Camunda Cloud. For example:" + createExample,
PreRun: func(cmd *cobra.Command, args []string) {
def, _ := cmd.Flags().GetBool("default")
if !def {
cmd.MarkFlagRequired("channel")
cmd.MarkFlagRequired("generation")
cmd.MarkFlagRequired("region")
cmd.MarkFlagRequired("plan")
}
},
Run: func(cmd *cobra.Command, args []string) {
def, _ := cmd.Flags().GetBool("default")
if !def {
clusterID, err := client.CreateClusterCustomConfig(cc.NewClusterCreationParams(
name, channel, generation, region, plan,
))
if err != nil {
fmt.Println(err.Error())
} else {
fmt.Println("Cluster create successfully. Cluster id:", clusterID)
}
} else {
if name != "" {
client.GetClusterParams()
clusterID, err := client.CreateClusterDefault(name)
if err != nil {
fmt.Println(err.Error())
} else {
fmt.Println("Cluster created successfully. Cluster id:", clusterID)
}
}
}
},
}
var deleteClusterCmd = &cobra.Command{
Use: "delete",
Short: "Delete cluster",
Long: "Used together with clusters command, to delete your clusters on Camunda Cloud. For example:" + deleteExample,
Run: func(cmd *cobra.Command, args []string) {
if id != "" {
success, _ := client.DeleteCluster(id)
if success {
fmt.Println("Cluster deleted successfully")
} else {
fmt.Println("Error: We can't delete your cluster")
}
}
},
}
func init() {
clusterCmd.AddCommand(getClusterCmd)
clusterCmd.AddCommand(createClusterCmd)
clusterCmd.AddCommand(deleteClusterCmd)
rootCmd.AddCommand(clusterCmd)
// get cmd
getClusterCmd.Flags().BoolP("all", "a", false, "Get all clusters: cc-ctl get --all")
getClusterCmd.Flags().BoolP("params", "p", false, "Get params to create a cluster: cc-ctl get --params")
getClusterCmd.Flags().StringVarP(&name, "name", "n", "", "cc-ctl clusters get --name='<cluster_name>'")
// delete cmd
deleteClusterCmd.PersistentFlags().StringVarP(&id, "id", "i", "", "cc-ctl clusters delete --id=<cluster_id>")
deleteClusterCmd.MarkFlagRequired("id")
// create cmd
createClusterCmd.Flags().BoolP("default", "d", false, "cc-ctl clusters create --default=(true|false)")
createClusterCmd.Flags().StringVarP(&name, "name", "n", "", "Cluster's name")
createClusterCmd.Flags().StringVarP(&channel, "channel", "c", "", "Cluster's channel id")
createClusterCmd.Flags().StringVarP(&generation, "generation", "g", "", "Cluster's generation id")
createClusterCmd.Flags().StringVarP(®ion, "region", "r", "", "Cluster's region id")
createClusterCmd.Flags().StringVarP(&plan, "plan", "p", "", "Cluster's plan type id")
createClusterCmd.MarkFlagRequired("name")
}
func showCluster(cluster cc.Cluster) {
data, _ := json.MarshalIndent(cluster, "", " ")
fmt.Println(string(data))
}
func showClusters(clusters []cc.Cluster) {
data, _ := json.MarshalIndent(clusters, "", " ")
fmt.Println(string(data))
}
func showParams(params cc.ClusterParams) {
data, _ := json.MarshalIndent(params, "", " ")
fmt.Println(string(data))
}