Skip to content

Commit

Permalink
feat: implements connection test
Browse files Browse the repository at this point in the history
  • Loading branch information
itnpeople committed Apr 20, 2022
1 parent f7dfc01 commit 0605d80
Showing 1 changed file with 41 additions and 3 deletions.
44 changes: 41 additions & 3 deletions cmd/spider/connecton.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package spider

import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"strings"

"github.com/go-resty/resty/v2"
Expand Down Expand Up @@ -39,12 +42,12 @@ func (o *ConnectionOptions) Complete(cmd *cobra.Command) error {

// validates the provided options
func (o *ConnectionOptions) Validate() error {
if o.CSP == "" {
return fmt.Errorf("Invalid csp flag")
}
if o.Name == "" {
return fmt.Errorf("Invalid name flag")
}
if o.CSP == "" {
return fmt.Errorf("Invalid csp flag")
}
if o.Credential == "" {
return fmt.Errorf("Invalid credential flag")
}
Expand Down Expand Up @@ -151,6 +154,41 @@ func NewCmdConnection(output app.Output) *cobra.Command {
},
})

// test
cmds.AddCommand(&cobra.Command{
Use: "test",
Short: "Test a cloud connection infos.",
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)
if err != nil {
return err
}
req.Header.Add("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()

bytes, _ := ioutil.ReadAll(resp.Body)
o.Output.Write(bytes)

return nil
}())
},
})

return cmds
}

Expand Down

0 comments on commit 0605d80

Please sign in to comment.