Skip to content

Commit

Permalink
fix: '--name' flags, argument
Browse files Browse the repository at this point in the history
  • Loading branch information
itnpeople committed Apr 20, 2022
1 parent 0605d80 commit ec859b1
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 113 deletions.
76 changes: 37 additions & 39 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ $ cbctl cluster create \
--worker-spec="t2.medium"
$ cbctl cluster list
$ cbctl cluster get --name "cb-cluster"
$ cbctl cluster delete --name "cb-cluster"
$ cbctl cluster get "cb-cluster"
$ cbctl cluster delete "cb-cluster"
```

* Nodes
Expand All @@ -117,25 +117,25 @@ $ cbctl node add \
--worker-spec="t2.medium"
$ cbctl node list --cluster "cb-cluster"
$ cbctl node get --cluster "cb-cluster" --name "w-1-oiq77"
$ cbctl node delete --cluster "cb-cluster" --name "w-1-oiq77"
$ cbctl node get "w-1-oiq77" --cluster "cb-cluster"
$ cbctl node delete "w-1-oiq77" --cluster "cb-cluster"
```

* Kubeconfig

```
$ cbctl cluster update-kubeconfig --name cb-cluster
$ cbctl cluster update-kubeconfig "cb-cluster"
$ kubectl config current-context
```

* SSH private-key
```
$ cbctl node get-key --cluster cb-cluster --name w-1-j4j8z > output/w-1-j4j8z.pem
$ cbctl node get-key w-1-j4j8z --cluster cb-cluster > output/w-1-j4j8z.pem
$ chmod 400 output/w-1-j4j8z.pem
$ ssh -i output/w-1-j4j8z.pem cb-user@xxx.xxx.xxx.xxx
```

* Using Yaml File
* Using Yaml File (create a cluster & add nodes)

```
$ cbctl cluster create -f examples/yaml/create-cluster.yaml
Expand Down Expand Up @@ -171,10 +171,10 @@ EOF
* Driver

```
$ cbctl driver create --csp aws
$ cbctl driver create aws
$ cbctl driver list
$ cbctl driver get --csp aws
$ cbctl driver delete --csp aws
$ cbctl driver get aws
$ cbctl driver delete aws
```

* Credential
Expand All @@ -188,46 +188,46 @@ $ source ./examples/credentials.sh \
openstack="${HOME}/.ssh/openstack-openrc.sh" \
cloudit="${HOME}/.ssh/cloudit-credential.sh"
$ cbctl credential create --csp aws --name credential-aws --secret-id "$AWS_SECRET_ID" --secret "$AWS_SECRET_KEY"
$ cbctl credential create --csp gcp --name credential-gcp --client-email "$GCP_SA" --project-id "$GCP_PROJECT" --private-key "$GCP_PKEY"
$ cbctl credential create --csp azure --name credential-azure --secret-id "$AZURE_CLIENT_ID" --secret "$AZURE_CLIENT_SECRET" --subscription "$AZURE_SUBSCRIPTION_ID" --tenant "$AZURE_TENANT_ID"
$ cbctl credential create --csp alibaba --name credential-alibaba --secret-id "$ALIBABA_SECRET_ID" --secret "$ALIBABA_SECRET_KEY"
$ cbctl credential create --csp tencent --name credential-tencent --secret-id "$TENCENT_SECRET_ID" --secret "$TENCENT_SECRET_KEY"
$ cbctl credential create --csp ibm --name credential-ibm --api-key "$IBM_API_KEY"
$ cbctl credential create --csp openstack --name credential-openstack --endpoint "$OS_AUTH_URL" --project-id "$OS_PROJECT_ID" --username "$OS_USERNAME" --password "$OS_PASSWORD" --domain "$OS_USER_DOMAIN_NAME"
$ cbctl credential create --csp cloudit --name credential-cloudit --endpoint "$CLOUDIT_ENDPOINT" --username "$CLOUDIT_USERNAME" --password "$CLOUDIT_PASSWORD" --token "$CLOUDIT_TOKEN" --tenant "$CLOUDIT_TENANT_ID"
$ cbctl credential create credential-aws --csp aws --secret-id "$AWS_SECRET_ID" --secret "$AWS_SECRET_KEY"
$ cbctl credential create credential-gcp --csp gcp --client-email "$GCP_SA" --project-id "$GCP_PROJECT" --private-key "$GCP_PKEY"
$ cbctl credential create credential-azure --csp azure --secret-id "$AZURE_CLIENT_ID" --secret "$AZURE_CLIENT_SECRET" --subscription "$AZURE_SUBSCRIPTION_ID" --tenant "$AZURE_TENANT_ID"
$ cbctl credential create credential-alibaba --csp alibaba --secret-id "$ALIBABA_SECRET_ID" --secret "$ALIBABA_SECRET_KEY"
$ cbctl credential create credential-tencent --csp tencent --secret-id "$TENCENT_SECRET_ID" --secret "$TENCENT_SECRET_KEY"
$ cbctl credential create credential-ibm --csp ibm --api-key "$IBM_API_KEY"
$ cbctl credential create credential-openstack --csp openstack --endpoint "$OS_AUTH_URL" --project-id "$OS_PROJECT_ID" --username "$OS_USERNAME" --password "$OS_PASSWORD" --domain "$OS_USER_DOMAIN_NAME"
$ cbctl credential create credential-cloudit --csp cloudit --endpoint "$CLOUDIT_ENDPOINT" --username "$CLOUDIT_USERNAME" --password "$CLOUDIT_PASSWORD" --token "$CLOUDIT_TOKEN" --tenant "$CLOUDIT_TENANT_ID"
$ cbctl credential list
$ cbctl credential get --name credential-aws
$ cbctl credential delete --name credential-aws
$ cbctl credential get credential-aws
$ cbctl credential delete credential-aws
```

* Region
```
$ cbctl region create --csp aws --name region-aws-tokyo --region ap-northeast-1 --zone ap-northeast-1a
$ cbctl region create --csp gcp --name region-gcp-tokyo --region asia-northeast1 --zone asia-northeast1-a
$ cbctl region create --csp azure --name region-azure-tokyo --location japaneast --resource-group cb-mcks
$ cbctl region create --csp alibaba --name region-alibaba-tokyo --region ap-northeast-1 --zone ap-northeast-1a
$ cbctl region create --csp tencent --name region-tencent-tokyo --region ap-tokyo --zone ap-tokyo-2
$ cbctl region create --csp ibm --name region-ibm-tokyo --region jp-tok --zone jp-tok-1
$ cbctl region create region-aws-tokyo --csp aws --region ap-northeast-1 --zone ap-northeast-1a
$ cbctl region create region-gcp-tokyo --csp gcp --region asia-northeast1 --zone asia-northeast1-a
$ cbctl region create region-azure-tokyo --csp azure --location japaneast --resource-group cb-mcks
$ cbctl region create region-alibaba-tokyo --csp alibaba --region ap-northeast-1 --zone ap-northeast-1a
$ cbctl region create region-tencent-tokyo --csp tencent --region ap-tokyo --zone ap-tokyo-2
$ cbctl region create region-ibm-tokyo --csp ibm --region jp-tok --zone jp-tok-1
$ cbctl region list
$ cbctl region get --name region-aws-tokyo
$ cbctl region delete --name region-aws-tokyo
$ cbctl region get region-aws-tokyo
$ cbctl region delete region-aws-tokyo
```

* Connection Info.
```
$ cbctl connection create --csp aws --name config-aws-tokyo --region region-aws-tokyo --credential credential-aws
$ cbctl connection create --csp gcp --name config-gcp-tokyo --region region-gcp-tokyo --credential credential-gcp
$ cbctl connection create --csp azure --name config-azure-tokyo --region region-azure-tokyo --credential credential-azure
$ cbctl connection create --csp alibaba --name config-alibaba-tokyo --region region-alibaba-tokyo --credential credential-alibaba
$ cbctl connection create --csp tencent --name config-tencent-tokyo --region region-tencent-tokyo --credential credential-tencent
$ cbctl connection create --csp ibm --name config-ibm-tokyo --region region-ibm-tokyo --credential credential-ibm
$ cbctl connection create config-aws-tokyo --csp aws --region region-aws-tokyo --credential credential-aws
$ cbctl connection create config-gcp-tokyo --csp gcp --region region-gcp-tokyo --credential credential-gcp
$ cbctl connection create config-azure-tokyo --csp azure --region region-azure-tokyo --credential credential-azure
$ cbctl connection create config-alibaba-tokyo --csp alibaba --region region-alibaba-tokyo --credential credential-alibaba
$ cbctl connection create config-tencent-tokyo --csp tencent --region region-tencent-tokyo --credential credential-tencent
$ cbctl connection create config-ibm-tokyo --csp ibm --region region-ibm-tokyo --credential credential-ibm
$ cbctl connection list
$ cbctl connection get --name config-aws-tokyo
$ cbctl connection delete --name config-aws-tokyo
$ cbctl connection get config-aws-tokyo
$ cbctl connection delete config-aws-tokyo
```

### Plugins
Expand Down Expand Up @@ -287,7 +287,7 @@ $ cbctl config

* Context
```
$ cbctl config add-context --name ctx1 \
$ cbctl config add-context ctx1 \
--namespace default \
--url-mcks http://127.0.0.1:1470/mcks \
--url-spider http://127.0.0.1:1024/spider \
Expand All @@ -308,5 +308,3 @@ $ cbctl config set-namespace namespace1
```
$ cbctl config view
```

## CPS
16 changes: 16 additions & 0 deletions app/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"os"
"strings"

"github.com/spf13/cobra"
)

func ValidateError(err error) {
Expand All @@ -18,3 +20,17 @@ func ValidateError(err error) {
}

}

func ValidCommandArgs(idx int, value *string) func(c *cobra.Command, args []string) error {

return func(c *cobra.Command, args []string) error {
if len(args) > idx && len(args[idx]) > 0 {
*value = args[idx]
}
if *value == "" {
return fmt.Errorf("arguemnt[%d] is empty", idx)
}
return nil
}

}
12 changes: 4 additions & 8 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ func NewCmdConfig(output app.Output) *cobra.Command {
cmdC := &cobra.Command{
Use: "add-context",
Short: "Add a context",
Args: app.ValidCommandArgs(0, &o.Name),
Run: func(c *cobra.Command, args []string) {
app.ValidateError(o.Complete(c))
app.ValidateError(o.Validate())
app.ValidateError(func() error {
fmt.Println(app.Config)
if _, ok := app.Config.Contexts[o.Name]; ok {
return fmt.Errorf("the context '%s' is alreaday exist", o.Name)
} else {
Expand Down Expand Up @@ -127,12 +127,10 @@ func NewCmdConfig(output app.Output) *cobra.Command {
cmds.AddCommand(&cobra.Command{
Use: "get-context",
Short: "List all clusters",
Args: app.ValidCommandArgs(0, &o.Name),
Run: func(c *cobra.Command, args []string) {
app.ValidateError(o.Complete(c))
app.ValidateError(func() error {
if len(args) > 0 {
o.Name = utils.NVL(o.Name, args[0])
}
if app.Config.Contexts[o.Name] != nil {
outYaml(app.Config.Contexts[o.Name], o.Output)
}
Expand All @@ -149,7 +147,7 @@ func NewCmdConfig(output app.Output) *cobra.Command {
app.ValidateError(o.Complete(c))
app.ValidateError(func() error {
if len(args) > 0 {
o.Name = utils.NVL(o.Name, args[0])
o.Name = utils.NVL(args[0], o.Name)
_, ok := app.Config.Contexts[o.Name]
if ok {
app.Config.CurrentContext = o.Name
Expand Down Expand Up @@ -186,12 +184,10 @@ func NewCmdConfig(output app.Output) *cobra.Command {
cmds.AddCommand(&cobra.Command{
Use: "delete-context",
Short: "Delete a context",
Args: app.ValidCommandArgs(0, &o.Name),
Run: func(c *cobra.Command, args []string) {
app.ValidateError(o.Complete(c))
app.ValidateError(func() error {
if len(args) > 0 {
o.Name = utils.NVL(o.Name, args[0])
}
conf := app.Config
if len(conf.Contexts) > 1 {
delete(conf.Contexts, o.Name)
Expand Down
8 changes: 4 additions & 4 deletions cmd/mcks/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func NewCmdCluster(output app.Output) *cobra.Command {

cmds.PersistentFlags().StringVar(&o.RootUrl, "url", "", "mcks root url (http://localhost:1470/mcks)")
cmds.PersistentFlags().StringVarP(&o.Namespace, "namespace", "n", "", "cloud-barista namespace for cluster list")
cmds.PersistentFlags().StringVar(&o.Name, "name", "cb-cluster", "cluster name")
cmds.PersistentFlags().StringVar(&o.Name, "name", "", "cluster name")

// create
cmdC := &cobra.Command{
Expand Down Expand Up @@ -141,12 +141,10 @@ func NewCmdCluster(output app.Output) *cobra.Command {
cmds.AddCommand(&cobra.Command{
Use: "get",
Short: "Get a cluster",
Args: app.ValidCommandArgs(0, &o.Name),
Run: func(c *cobra.Command, args []string) {
app.ValidateError(o.Complete(c))
app.ValidateError(func() error {
if len(args) > 0 {
o.Name = utils.NVL(o.Name, args[0])
}
if resp, err := resty.New().SetDisableWarn(true).R().Get(fmt.Sprintf("%s/ns/%s/clusters/%s", o.RootUrl, o.Namespace, o.Name)); err != nil {
return err
} else {
Expand All @@ -161,6 +159,7 @@ func NewCmdCluster(output app.Output) *cobra.Command {
cmds.AddCommand(&cobra.Command{
Use: "delete",
Short: "Delete a cluster",
Args: app.ValidCommandArgs(0, &o.Name),
Run: func(c *cobra.Command, args []string) {
app.ValidateError(o.Complete(c))
app.ValidateError(func() error {
Expand All @@ -180,6 +179,7 @@ func NewCmdCluster(output app.Output) *cobra.Command {
cmds.AddCommand(&cobra.Command{
Use: "update-kubeconfig",
Short: "Update a kubeconfig",
Args: app.ValidCommandArgs(0, &o.Name),
Run: func(c *cobra.Command, args []string) {
app.ValidateError(o.Complete(c))
app.ValidateError(func() error {
Expand Down
12 changes: 3 additions & 9 deletions cmd/mcks/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,10 @@ func NewCmdNodes(output app.Output) *cobra.Command {
cmds.AddCommand(&cobra.Command{
Use: "get",
Short: "Get a node",
Args: app.ValidCommandArgs(0, &o.Name),
Run: func(c *cobra.Command, args []string) {
app.ValidateError(o.Complete(c))
app.ValidateError(func() error {
if len(args) > 0 {
o.Name = utils.NVL(o.Name, args[0])
}
if resp, err := resty.New().SetDisableWarn(true).R().Get(fmt.Sprintf("%s/ns/%s/clusters/%s/nodes/%s", o.RootUrl, o.Namespace, o.Cluster, o.Name)); err != nil {
return err
} else {
Expand All @@ -156,12 +154,10 @@ func NewCmdNodes(output app.Output) *cobra.Command {
cmds.AddCommand(&cobra.Command{
Use: "delete",
Short: "Delete a cluster",
Args: app.ValidCommandArgs(0, &o.Name),
Run: func(c *cobra.Command, args []string) {
app.ValidateError(o.Complete(c))
app.ValidateError(func() error {
if len(args) > 0 {
o.Name = utils.NVL(o.Name, args[0])
}
if resp, err := resty.New().SetDisableWarn(true).R().Delete(fmt.Sprintf("%s/ns/%s/clusters/%s/nodes/%s", o.RootUrl, o.Namespace, o.Cluster, o.Name)); err != nil {
return err
} else {
Expand All @@ -176,12 +172,10 @@ func NewCmdNodes(output app.Output) *cobra.Command {
cmds.AddCommand(&cobra.Command{
Use: "get-key",
Short: "Get a private key",
Args: app.ValidCommandArgs(0, &o.Name),
Run: func(c *cobra.Command, args []string) {
app.ValidateError(o.Complete(c))
app.ValidateError(func() error {
if len(args) > 0 {
o.Name = utils.NVL(o.Name, args[0])
}
res := &struct {
Credential string `json:"credential"`
}{}
Expand Down
16 changes: 4 additions & 12 deletions cmd/spider/connecton.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ func NewCmdConnection(output app.Output) *cobra.Command {
cmdC := &cobra.Command{
Use: "create",
Short: "Create a cloud connection info.",
Args: app.ValidCommandArgs(0, &o.Name),
Run: func(c *cobra.Command, args []string) {
app.ValidateError(o.Complete(c))
app.ValidateError(o.Validate())
Expand Down Expand Up @@ -118,12 +119,10 @@ func NewCmdConnection(output app.Output) *cobra.Command {
cmds.AddCommand(&cobra.Command{
Use: "get",
Short: "Get a cloud connection infos.",
Args: app.ValidCommandArgs(0, &o.Name),
Run: func(c *cobra.Command, args []string) {
app.ValidateError(o.Complete(c))
app.ValidateError(func() error {
if len(args) > 0 {
o.Name = utils.NVL(o.Name, args[0])
}
if resp, err := resty.New().SetDisableWarn(true).R().Get(fmt.Sprintf("%s/connectionconfig/%s", o.RootUrl, o.Name)); err != nil {
return err
} else {
Expand All @@ -138,12 +137,10 @@ func NewCmdConnection(output app.Output) *cobra.Command {
cmds.AddCommand(&cobra.Command{
Use: "delete",
Short: "Delete a cloud connection info.",
Args: app.ValidCommandArgs(0, &o.Name),
Run: func(c *cobra.Command, args []string) {
app.ValidateError(o.Complete(c))
app.ValidateError(func() error {
if len(args) > 0 {
o.Name = utils.NVL(o.Name, args[0])
}
if resp, err := resty.New().SetDisableWarn(true).R().Delete(fmt.Sprintf("%s/connectionconfig/%s", o.RootUrl, o.Name)); err != nil {
return err
} else {
Expand All @@ -158,15 +155,10 @@ func NewCmdConnection(output app.Output) *cobra.Command {
cmds.AddCommand(&cobra.Command{
Use: "test",
Short: "Test a cloud connection infos.",
Args: app.ValidCommandArgs(0, &o.Name),
Run: func(c *cobra.Command, args []string) {
app.ValidateError(o.Complete(c))
app.ValidateError(func() error {
if len(args) > 0 {
o.Name = utils.NVL(o.Name, args[0])
}
if o.Name == "" {
return fmt.Errorf("Invalid name flag")
}
// resty-go Get 인 경우 body를 혀용하지 않아서 "net/http" 모듈 사용
body := bytes.NewBufferString(fmt.Sprintf("{\"connectionName\": \"%s\"}", o.Name))
req, err := http.NewRequest("GET", fmt.Sprintf("%s/vmspec", o.RootUrl), body)
Expand Down
9 changes: 3 additions & 6 deletions cmd/spider/credential.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ func NewCmdCredential(output app.Output) *cobra.Command {
cmdC := &cobra.Command{
Use: "create",
Short: "Create a cloud credential",
Args: app.ValidCommandArgs(0, &o.Name),
Run: func(c *cobra.Command, args []string) {
app.ValidateError(o.Complete(c))
app.ValidateError(o.Validate())
Expand Down Expand Up @@ -160,12 +161,10 @@ func NewCmdCredential(output app.Output) *cobra.Command {
cmds.AddCommand(&cobra.Command{
Use: "get",
Short: "Get a cloud credential",
Args: app.ValidCommandArgs(0, &o.Name),
Run: func(c *cobra.Command, args []string) {
app.ValidateError(o.Complete(c))
app.ValidateError(func() error {
if len(args) > 0 {
o.Name = utils.NVL(o.Name, args[0])
}
if resp, err := resty.New().SetDisableWarn(true).R().Get(fmt.Sprintf("%s/credential/%s", o.RootUrl, o.Name)); err != nil {
return err
} else {
Expand All @@ -180,12 +179,10 @@ func NewCmdCredential(output app.Output) *cobra.Command {
cmds.AddCommand(&cobra.Command{
Use: "delete",
Short: "Delete a cloud credential",
Args: app.ValidCommandArgs(0, &o.Name),
Run: func(c *cobra.Command, args []string) {
app.ValidateError(o.Complete(c))
app.ValidateError(func() error {
if len(args) > 0 {
o.Name = utils.NVL(o.Name, args[0])
}
if resp, err := resty.New().SetDisableWarn(true).R().Delete(fmt.Sprintf("%s/credential/%s", o.RootUrl, o.Name)); err != nil {
return err
} else {
Expand Down
Loading

0 comments on commit ec859b1

Please sign in to comment.