This is an automated email from the ASF dual-hosted git repository.
bstoyanov pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/cloudstack-cloudmonkey.git
The following commit(s) were added to refs/heads/main by this push:
new 7c4dd34 Enable passing of profile information via commandline (#132)
7c4dd34 is described below
commit 7c4dd341ed5504e36cf36fd6c1c9af37572c04f5
Author: Nicolas Vazquez <[email protected]>
AuthorDate: Fri Apr 21 09:15:54 2023 -0300
Enable passing of profile information via commandline (#132)
* Support to pass api/secret keys & url via cmdline
* Allow cmk to work without a defined profile and use the credentials passed
---------
Co-authored-by: Pearl Dsilva <[email protected]>
---
cmd/command.go | 3 +++
cmk.go | 25 ++++++++++++++++++++++++-
config/config.go | 5 ++++-
3 files changed, 31 insertions(+), 2 deletions(-)
diff --git a/cmd/command.go b/cmd/command.go
index 6447087..1928755 100644
--- a/cmd/command.go
+++ b/cmd/command.go
@@ -69,6 +69,9 @@ Allowed flags:
-p Server profile
-d Enable debug mode
-c Different config file path
+ -u CloudStack's API endpoint URL
+ -s CloudStack user's secret Key
+ -k CloudStack user's API Key
Default commands:
%s
diff --git a/cmk.go b/cmk.go
index ff9325e..74c8873 100644
--- a/cmk.go
+++ b/cmk.go
@@ -47,7 +47,9 @@ func main() {
debug := flag.Bool("d", false, "enable debug mode")
profile := flag.String("p", "", "server profile")
configFilePath := flag.String("c", "", "config file path")
-
+ acsUrl := flag.String("u", config.DEFAULT_ACS_API_ENDPOINT,
"cloudStack's API endpoint URL")
+ apiKey := flag.String("k", "", "cloudStack user's API Key")
+ secretKey := flag.String("s", "", "cloudStack user's secret Key")
flag.Parse()
cfg := config.NewConfig(configFilePath)
@@ -69,11 +71,32 @@ func main() {
cfg.UpdateConfig("output", *outputFormat, false)
}
+ if *acsUrl != config.DEFAULT_ACS_API_ENDPOINT {
+ cfg.UpdateConfig("url", *acsUrl, false)
+ }
+
+ if *apiKey != "" {
+ cfg.UpdateConfig("apikey", *apiKey, false)
+ }
+
+ if *secretKey != "" {
+ cfg.UpdateConfig("secretkey", *secretKey, false)
+ }
+
if *profile != "" {
cfg.LoadProfile(*profile)
}
+ if *apiKey != "" && *secretKey != "" {
+ request := cmd.NewRequest(nil, cfg, nil)
+ syncResponse, err := cmd.NewAPIRequest(request, "listApis",
[]string{"listall=true"}, false)
+ if err == nil {
+ fmt.Printf("Discovered %v APIs\n",
cfg.UpdateCache(syncResponse))
+ }
+ }
+
cli.SetConfig(cfg)
+
args := flag.Args()
config.Debug("cmdline args:", strings.Join(os.Args, ", "))
if len(args) > 0 {
diff --git a/config/config.go b/config/config.go
index e9d6a02..111df88 100644
--- a/config/config.go
+++ b/config/config.go
@@ -43,6 +43,8 @@ const (
DEFAULT = "default"
)
+const DEFAULT_ACS_API_ENDPOINT = "http://localhost:8080/client/api"
+
// ServerProfile describes a management server
type ServerProfile struct {
URL string `ini:"url"`
@@ -149,7 +151,7 @@ func defaultCoreConfig() Core {
func defaultProfile() ServerProfile {
return ServerProfile{
- URL: "http://localhost:8080/client/api",
+ URL: DEFAULT_ACS_API_ENDPOINT,
Username: "admin",
Password: "password",
Domain: "/",
@@ -256,6 +258,7 @@ func saveConfig(cfg *Config) *Config {
conf.Section(ini.DEFAULT_SECTION).MapTo(core)
if !conf.Section(ini.DEFAULT_SECTION).HasKey("autocomplete") {
core.AutoComplete = true
+ core.Output = JSON
}
cfg.Core = core
}