Skip to content

Commit 7c4dd34

Browse files
nvazquezPearl1594
andauthored
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]>
1 parent f43904f commit 7c4dd34

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

cmd/command.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ Allowed flags:
6969
-p Server profile
7070
-d Enable debug mode
7171
-c Different config file path
72+
-u CloudStack's API endpoint URL
73+
-s CloudStack user's secret Key
74+
-k CloudStack user's API Key
7275
7376
Default commands:
7477
%s

cmk.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ func main() {
4747
debug := flag.Bool("d", false, "enable debug mode")
4848
profile := flag.String("p", "", "server profile")
4949
configFilePath := flag.String("c", "", "config file path")
50-
50+
acsUrl := flag.String("u", config.DEFAULT_ACS_API_ENDPOINT, "cloudStack's API endpoint URL")
51+
apiKey := flag.String("k", "", "cloudStack user's API Key")
52+
secretKey := flag.String("s", "", "cloudStack user's secret Key")
5153
flag.Parse()
5254

5355
cfg := config.NewConfig(configFilePath)
@@ -69,11 +71,32 @@ func main() {
6971
cfg.UpdateConfig("output", *outputFormat, false)
7072
}
7173

74+
if *acsUrl != config.DEFAULT_ACS_API_ENDPOINT {
75+
cfg.UpdateConfig("url", *acsUrl, false)
76+
}
77+
78+
if *apiKey != "" {
79+
cfg.UpdateConfig("apikey", *apiKey, false)
80+
}
81+
82+
if *secretKey != "" {
83+
cfg.UpdateConfig("secretkey", *secretKey, false)
84+
}
85+
7286
if *profile != "" {
7387
cfg.LoadProfile(*profile)
7488
}
7589

90+
if *apiKey != "" && *secretKey != "" {
91+
request := cmd.NewRequest(nil, cfg, nil)
92+
syncResponse, err := cmd.NewAPIRequest(request, "listApis", []string{"listall=true"}, false)
93+
if err == nil {
94+
fmt.Printf("Discovered %v APIs\n", cfg.UpdateCache(syncResponse))
95+
}
96+
}
97+
7698
cli.SetConfig(cfg)
99+
77100
args := flag.Args()
78101
config.Debug("cmdline args:", strings.Join(os.Args, ", "))
79102
if len(args) > 0 {

config/config.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ const (
4343
DEFAULT = "default"
4444
)
4545

46+
const DEFAULT_ACS_API_ENDPOINT = "http://localhost:8080/client/api"
47+
4648
// ServerProfile describes a management server
4749
type ServerProfile struct {
4850
URL string `ini:"url"`
@@ -149,7 +151,7 @@ func defaultCoreConfig() Core {
149151

150152
func defaultProfile() ServerProfile {
151153
return ServerProfile{
152-
URL: "http://localhost:8080/client/api",
154+
URL: DEFAULT_ACS_API_ENDPOINT,
153155
Username: "admin",
154156
Password: "password",
155157
Domain: "/",
@@ -256,6 +258,7 @@ func saveConfig(cfg *Config) *Config {
256258
conf.Section(ini.DEFAULT_SECTION).MapTo(core)
257259
if !conf.Section(ini.DEFAULT_SECTION).HasKey("autocomplete") {
258260
core.AutoComplete = true
261+
core.Output = JSON
259262
}
260263
cfg.Core = core
261264
}

0 commit comments

Comments
 (0)