This is an automated email from the ASF dual-hosted git repository.
rohit pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/cloudstack-cloudmonkey.git
The following commit(s) were added to refs/heads/master by this push:
new c515cbb cmd: handle errors well, throw non-zero exit code on error
c515cbb is described below
commit c515cbb131855e8981ec3c1bc9318c3ad747f1cb
Author: Rohit Yadav <[email protected]>
AuthorDate: Mon Jun 25 16:18:06 2018 +0530
cmd: handle errors well, throw non-zero exit code on error
Signed-off-by: Rohit Yadav <[email protected]>
---
Makefile | 4 ++--
cmd/api.go | 4 ++++
cmd/network.go | 13 ++++++++-----
cmd/set.go | 3 +--
config/config.go | 36 ++++++++++++++++++++----------------
5 files changed, 35 insertions(+), 25 deletions(-)
diff --git a/Makefile b/Makefile
index 235ec03..fbd28c0 100644
--- a/Makefile
+++ b/Makefile
@@ -49,8 +49,8 @@ $(BASE): ; $(info $(M) Setting GOPATH…)
run: all
./bin/cloudmonkey
-debug: all
- $(GO) build -gcflags='-N -l' -o cmk cmk.go && dlv --listen=:2345
--headless=true --api-version=2 exec ./bin/cloudmonkey
+debug:
+ $(GO) build -gcflags='-N -l' -o cmk cmk.go && dlv --listen=:2345
--headless=true --api-version=2 exec ./cmk
dist:
cd $(BASE)
diff --git a/cmd/api.go b/cmd/api.go
index 7e0621a..5c0a57c 100644
--- a/cmd/api.go
+++ b/cmd/api.go
@@ -107,7 +107,11 @@ func printResult(outputType string, response
map[string]interface{}, filter []st
}
table.Render()
case config.TEXT:
+ case config.DEFAULT:
printText(response)
+ case config.CSV:
+ fmt.Println("FIXME: implement CSV output")
+ fmt.Println(response)
default:
jsonOutput, _ := json.MarshalIndent(response, "", " ")
fmt.Println(string(jsonOutput))
diff --git a/cmd/network.go b/cmd/network.go
index f31eda7..c3e5b9f 100644
--- a/cmd/network.go
+++ b/cmd/network.go
@@ -82,19 +82,19 @@ func Login(r *Request) (*http.Client, string, error) {
client := &http.Client{
Jar: jar,
Transport: &http.Transport{
- TLSClientConfig: &tls.Config{InsecureSkipVerify:
!r.Config.ActiveProfile.VerifyCert},
+ TLSClientConfig: &tls.Config{InsecureSkipVerify:
!r.Config.Core.VerifyCert},
},
}
sessionKey := ""
resp, err := client.PostForm(r.Config.ActiveProfile.URL, params)
if err != nil {
- return client, sessionKey, errors.New("failed to connect to
management server, please check the URL: " + r.Config.ActiveProfile.URL)
+ return client, sessionKey, errors.New("failed to authenticate
with the CloudStack server, please check the settings: " + err.Error())
}
if resp.StatusCode != http.StatusOK {
- e := errors.New("failed to log in, please check the
credentials")
+ e := errors.New("failed to authenticate, please check the
credentials")
if err != nil {
- e = errors.New("failed to log in due to " + err.Error())
+ e = errors.New("failed to authenticate due to " +
err.Error())
}
return client, sessionKey, e
}
@@ -175,7 +175,7 @@ func NewAPIRequest(r *Request, api string, args []string,
isAsync bool) (map[str
client = &http.Client{
Transport: &http.Transport{
- TLSClientConfig:
&tls.Config{InsecureSkipVerify: !r.Config.ActiveProfile.VerifyCert},
+ TLSClientConfig:
&tls.Config{InsecureSkipVerify: !r.Config.Core.VerifyCert},
},
}
encodedParams = encodeRequestParams(params)
@@ -215,6 +215,9 @@ func NewAPIRequest(r *Request, api string, args []string,
isAsync bool) (map[str
}
if apiResponse := getResponseData(data); apiResponse != nil {
+ if _, ok := apiResponse["errorcode"]; ok {
+ return nil, fmt.Errorf("(HTTP %v, error code %v) %v",
apiResponse["errorcode"], apiResponse["cserrorcode"], apiResponse["errortext"])
+ }
return apiResponse, nil
}
diff --git a/cmd/set.go b/cmd/set.go
index 70c7de9..a43c104 100644
--- a/cmd/set.go
+++ b/cmd/set.go
@@ -30,7 +30,7 @@ func init() {
"prompt": {"🐵", "🐱", "random"},
"asyncblock": {"true", "false"},
"timeout": {"600", "1800", "3600"},
- "output": {"json", "text", "table", "xml"},
+ "output": {"json", "text", "table", "csv", "xml"},
"profile": {},
"url": {},
"username": {},
@@ -55,7 +55,6 @@ func init() {
fmt.Println("Username: ",
r.Config.ActiveProfile.Username)
fmt.Println("Domain: ",
r.Config.ActiveProfile.Domain)
fmt.Println("API Key: ",
r.Config.ActiveProfile.APIKey)
- fmt.Println("Verify Cert:",
r.Config.ActiveProfile.VerifyCert)
fmt.Println()
r.Shell.SetPrompt(r.Config.GetPrompt())
diff --git a/config/config.go b/config/config.go
index c210e8a..5d203c0 100644
--- a/config/config.go
+++ b/config/config.go
@@ -30,20 +30,22 @@ import (
const (
CSV = "csv"
JSON = "json"
- XML = "xml"
TABLE = "table"
TEXT = "text"
+
+ // Old formats existing for some backward compatibilities
+ DEFAULT = "default" // This is same as 'text'
+ XML = "xml"
)
// ServerProfile describes a management server
type ServerProfile struct {
- URL string `ini:"url"`
- Username string `ini:"username"`
- Password string `ini:"password"`
- Domain string `ini:"domain"`
- APIKey string `ini:"apikey"`
- SecretKey string `ini:"secretkey"`
- VerifyCert bool `ini:"verifycert"`
+ URL string `ini:"url"`
+ Username string `ini:"username"`
+ Password string `ini:"password"`
+ Domain string `ini:"domain"`
+ APIKey string `ini:"apikey"`
+ SecretKey string `ini:"secretkey"`
}
// Core block describes common options for the CLI
@@ -52,6 +54,7 @@ type Core struct {
AsyncBlock bool `ini:"asyncblock"`
Timeout int `ini:"timeout"`
Output string `ini:"output"`
+ VerifyCert bool `ini:"verifycert"`
ProfileName string `ini:"profile"`
}
@@ -81,19 +84,19 @@ func defaultCoreConfig() Core {
AsyncBlock: true,
Timeout: 1800,
Output: JSON,
+ VerifyCert: true,
ProfileName: "localcloud",
}
}
func defaultProfile() ServerProfile {
return ServerProfile{
- URL: "http://localhost:8080/client/api",
- Username: "admin",
- Password: "password",
- Domain: "/",
- APIKey: "",
- SecretKey: "",
- VerifyCert: false,
+ URL: "http://localhost:8080/client/api",
+ Username: "admin",
+ Password: "password",
+ Domain: "/",
+ APIKey: "",
+ SecretKey: "",
}
}
@@ -201,6 +204,7 @@ func (c *Config) UpdateConfig(key string, value string) {
case "asyncblock":
c.Core.AsyncBlock = value == "true"
case "output":
+ case "display":
c.Core.Output = value
case "timeout":
intValue, _ := strconv.Atoi(value)
@@ -221,7 +225,7 @@ func (c *Config) UpdateConfig(key string, value string) {
case "secretkey":
c.ActiveProfile.SecretKey = value
case "verifycert":
- c.ActiveProfile.VerifyCert = value == "true"
+ c.Core.VerifyCert = value == "true"
}
reloadConfig(c)