Skip to content

Commit

Permalink
feat: implments 'mcis' command
Browse files Browse the repository at this point in the history
  • Loading branch information
itnpeople committed Apr 19, 2022
1 parent 5d5ab14 commit 2985d69
Show file tree
Hide file tree
Showing 2 changed files with 185 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/itnpeople/cbctl/app"
"github.com/itnpeople/cbctl/cmd/mcks"
"github.com/itnpeople/cbctl/cmd/spider"
"github.com/itnpeople/cbctl/cmd/tumblebug"
)

const (
Expand Down Expand Up @@ -105,6 +106,7 @@ func NewCBCtlCommand(o CBCtlOptions) *cobra.Command {
cmds.AddCommand(spider.NewCmdCredential(output))
cmds.AddCommand(spider.NewCmdRegion(output))
cmds.AddCommand(spider.NewCmdConnection(output))
cmds.AddCommand(tumblebug.NewCmdMCIS(output))
cmds.AddCommand(NewCmdPlugin(o.IOStreams))

cobra.OnInitialize(func() {
Expand Down
183 changes: 183 additions & 0 deletions cmd/tumblebug/mcis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
package tumblebug

import (
"fmt"
"strings"

"github.com/go-resty/resty/v2"
"github.com/spf13/cobra"

"github.com/itnpeople/cbctl/app"
"github.com/itnpeople/cbctl/utils"
)

// a struct to support command
type ConnectionOptions struct {
app.Output
RootUrl string
Namespace string
Name string
HTTP *resty.Request
}

// returns initialized Options
func NewConnectionOptions(output app.Output) *ConnectionOptions {
return &ConnectionOptions{
Output: output,
}
}

// completes all the required options
func (o *ConnectionOptions) Complete(cmd *cobra.Command) error {
o.RootUrl = utils.NVL(o.RootUrl, app.Config.GetCurrentContext().Urls.Tumblebug)
if !strings.HasPrefix(o.RootUrl, "http://") && !strings.HasPrefix(o.RootUrl, "https://") {
return fmt.Errorf("Invalid Tumblebug ROOT URL (%s)", o.RootUrl)
}
o.Namespace = utils.NVL(o.Namespace, app.Config.GetCurrentContext().Namespace)
if o.Namespace == "" {
return fmt.Errorf("Invalid namespace")
}
o.HTTP = resty.New().SetDisableWarn(true).R().SetBasicAuth("default", "default")
return nil
}

// validates the provided options
func (o *ConnectionOptions) Validate(args []string) error {
if len(args) > 0 {
o.Name = utils.NVL(o.Name, args[0])
}
if o.Name == "" {
return fmt.Errorf("Invalid name")
}
return nil
}

// returns a cobra command
func NewCmdMCIS(output app.Output) *cobra.Command {
o := NewConnectionOptions(output)
cmds := &cobra.Command{
Use: "mcis",
Short: "Cloud Infra.",
Run: func(c *cobra.Command, args []string) {
c.Help()
},
}
cmds.PersistentFlags().StringVar(&o.RootUrl, "url", "", "Tumblebug ROOT URL (http://localhost:1323/tumblebug)")
cmds.PersistentFlags().StringVarP(&o.Namespace, "namespace", "n", "", "cloud-barista namespace for cluster list")
cmds.PersistentFlags().StringVar(&o.Name, "name", "", "name of MCIS")

// list
cmds.AddCommand(&cobra.Command{
Use: "list",
Short: "Get MCISs.",
Run: func(c *cobra.Command, args []string) {
app.ValidateError(o.Complete(c))
app.ValidateError(func() error {
if resp, err := o.HTTP.Get(fmt.Sprintf("%s/ns/%s/mcis", o.RootUrl, o.Namespace)); err != nil {
return err
} else {
o.Output.Write(resp.Body())
}
return nil
}())
},
})

// get
cmds.AddCommand(&cobra.Command{
Use: "get",
Short: "Get a MCISs.",
Run: func(c *cobra.Command, args []string) {
app.ValidateError(o.Complete(c))
app.ValidateError(o.Validate(args))
app.ValidateError(func() error {
if resp, err := o.HTTP.Get(fmt.Sprintf("%s/ns/%s/mcis/%s", o.RootUrl, o.Namespace, o.Name)); err != nil {
return err
} else {
o.Output.Write(resp.Body())
}
return nil
}())
},
})

// delete
cmds.AddCommand(&cobra.Command{
Use: "delete",
Short: "Delete a MCISs.",
Run: func(c *cobra.Command, args []string) {
app.ValidateError(o.Complete(c))
app.ValidateError(o.Validate(args))
app.ValidateError(func() error {
if resp, err := o.HTTP.Delete(fmt.Sprintf("%s/ns/%s/mcis/%s?action=terminate", o.RootUrl, o.Namespace, o.Name)); err != nil {
return err
} else {
o.Output.Write(resp.Body())
}
if resp, err := o.HTTP.Delete(fmt.Sprintf("%s/ns/%s/mcis/%s?action=refine", o.RootUrl, o.Namespace, o.Name)); err != nil {
return err
} else {
o.Output.Write(resp.Body())
}

if resp, err := o.HTTP.Delete(fmt.Sprintf("%s/ns/%s/mcis/%s", o.RootUrl, o.Namespace, o.Name)); err != nil {
return err
} else {
o.Output.Write(resp.Body())
}
return nil
}())
},
})

// delete
cmds.AddCommand(&cobra.Command{
Use: "clean",
Short: "clean-up MCISs.",
Run: func(c *cobra.Command, args []string) {
app.ValidateError(o.Complete(c))
app.ValidateError(func() error {

// mcis
if resp, err := o.HTTP.Delete(fmt.Sprintf("%s/ns/%s/mcis", o.RootUrl, o.Namespace)); err != nil {
return err
} else {
o.Output.Write(resp.Body())
}
// vpc
if resp, err := o.HTTP.Delete(fmt.Sprintf("%s/ns/%s/resources/vNet", o.RootUrl, o.Namespace)); err != nil {
return err
} else {
o.Output.Write(resp.Body())
}
// securityGroup
if resp, err := o.HTTP.Delete(fmt.Sprintf("%s/ns/%s/resources/securityGroup", o.RootUrl, o.Namespace)); err != nil {
return err
} else {
o.Output.Write(resp.Body())
}
// sshKey
if resp, err := o.HTTP.Delete(fmt.Sprintf("%s/ns/%s/resources/sshKey", o.RootUrl, o.Namespace)); err != nil {
return err
} else {
o.Output.Write(resp.Body())
}
// image
if resp, err := o.HTTP.Delete(fmt.Sprintf("%s/ns/%s/resources/image", o.RootUrl, o.Namespace)); err != nil {
return err
} else {
o.Output.Write(resp.Body())
}
// spec
if resp, err := o.HTTP.Delete(fmt.Sprintf("%s/ns/%s/resources/spec", o.RootUrl, o.Namespace)); err != nil {
return err
} else {
o.Output.Write(resp.Body())
}
return nil
}())
},
})

return cmds
}

0 comments on commit 2985d69

Please sign in to comment.