Skip to content
Snippets Groups Projects
Commit 7adba566 authored by Florent Gluck's avatar Florent Gluck
Browse files

Updated vmCreate so that a bulk of VMs can be created at once

parent 74e6e0b8
No related branches found
No related tags found
No related merge requests found
...@@ -32,14 +32,15 @@ func (tpl *Template)String() string { ...@@ -32,14 +32,15 @@ func (tpl *Template)String() string {
func printUsage(c cmd.Command) { func printUsage(c cmd.Command) {
u.PrintlnErr(c.GetDesc()) u.PrintlnErr(c.GetDesc())
u.PrintlnErr("Usage: ",c.GetName()," [ID] [regex]") u.PrintlnErr("Usage: ",c.GetName()," [ID ...] [regex ...]")
u.PrintlnErr("Only templates matching either the specified ID or regex will be listed.") const usage string = `Only templates matching the specified IDs or regexes will be listed.
u.PrintlnErr("Any number of ID or regex can be specified.") Any number of IDs or regexes can be specified.
u.PrintlnErr("The regex matches the template's name only and is case-insensitive.") The regex only matches the templates's name and is case-insensitive.
u.PrintlnErr("Regex examples:") Regex examples:
u.PrintlnErr(" \"\" -> matches any name") "" -> matches any templates
u.PrintlnErr(" \".\" -> matches any name") "." -> matches any templates
u.PrintlnErr(" \"bla\" -> matches any name containing \"bla\"") "bla" -> matches any templates containing "bla"`
u.PrintlnErr(usage)
} }
// Call the given route and filter the results based on ID and regex. // Call the given route and filter the results based on ID and regex.
......
...@@ -34,14 +34,15 @@ func (user *User)String() string { ...@@ -34,14 +34,15 @@ func (user *User)String() string {
func printUsage(c cmd.Command) { func printUsage(c cmd.Command) {
u.PrintlnErr(c.GetDesc()) u.PrintlnErr(c.GetDesc())
u.PrintlnErr("Usage: ",c.GetName()," [regex]") u.PrintlnErr("Usage: ",c.GetName()," [regex ...]")
u.PrintlnErr("Only users matching the specified regex will be listed.") const usage string = `Only users matching the specified regexes will be listed.
u.PrintlnErr("Any number of regex can be specified.") Any number of regexes can be specified.
u.PrintlnErr("The regex matches the user email, first name and last name and is case-insensitive.") The regex matches the user email, first name and last name and is case-insensitive.
u.PrintlnErr("Regex examples:") Regex examples:
u.PrintlnErr(" \"\" -> matches any user") "" -> matches any users
u.PrintlnErr(" \".\" -> matches any user") "." -> matches any users
u.PrintlnErr(" \"bla\" -> matches any user containing \"bla\"") "bla" -> matches any users containing "bla"`
u.PrintlnErr(usage)
} }
// Call the given route and filter the results based on a regex. // Call the given route and filter the results based on a regex.
......
...@@ -19,7 +19,7 @@ func (cmd *Add)GetDesc() string { ...@@ -19,7 +19,7 @@ func (cmd *Add)GetDesc() string {
func (cmd *Add)PrintUsage() { func (cmd *Add)PrintUsage() {
u.PrintlnErr(cmd.GetDesc()) u.PrintlnErr(cmd.GetDesc())
u.PrintlnErr("Usage: "+cmd.Name+" email firstname lastname password [capability1 capability2 ...]") u.PrintlnErr("Usage: "+cmd.Name+" email firstname lastname password [capability ...]")
} }
func (cmd *Add)Run(args []string) int { func (cmd *Add)Run(args []string) int {
......
...@@ -19,7 +19,7 @@ func (cmd *SetCaps)GetDesc() string { ...@@ -19,7 +19,7 @@ func (cmd *SetCaps)GetDesc() string {
func (cmd *SetCaps)PrintUsage() { func (cmd *SetCaps)PrintUsage() {
u.PrintlnErr(cmd.GetDesc()) u.PrintlnErr(cmd.GetDesc())
u.PrintlnErr("Usage: "+cmd.Name+" email [capability1 capability2 ...]") u.PrintlnErr("Usage: "+cmd.Name+" email [capability ...]")
} }
func (cmd *SetCaps)Run(args []string) int { func (cmd *SetCaps)Run(args []string) int {
......
...@@ -53,14 +53,15 @@ func (vm *VM)String() string { ...@@ -53,14 +53,15 @@ func (vm *VM)String() string {
func printUsage(c cmd.Command) { func printUsage(c cmd.Command) {
u.PrintlnErr(c.GetDesc()) u.PrintlnErr(c.GetDesc())
u.PrintlnErr("Usage: ",c.GetName()," [ID] [regex]") u.PrintlnErr("Usage: ",c.GetName()," [ID ...] [regex ...]")
u.PrintlnErr("Only VMs matching either the specified ID or regex will be listed.") const usage string = `Only VMs matching the specified IDs or regexes will be listed.
u.PrintlnErr("Any number of ID or regex can be specified.") Any number of IDs or regexes can be specified.
u.PrintlnErr("The regex matches the VM's name only and is case-insensitive.") The regex only matches the VM's name and is case-insensitive.
u.PrintlnErr("Regex examples:") Regex examples:
u.PrintlnErr(" \"\" -> matches any name") "" -> matches any VMs
u.PrintlnErr(" \".\" -> matches any name") "." -> matches any VMs
u.PrintlnErr(" \"bla\" -> matches any name containing \"bla\"") "bla" -> matches any VMs containing "bla" in their name`
u.PrintlnErr(usage)
} }
// Call the given route and filter the results based on ID and regex. // Call the given route and filter the results based on ID and regex.
......
...@@ -15,18 +15,21 @@ func (cmd *Create)GetName() string { ...@@ -15,18 +15,21 @@ func (cmd *Create)GetName() string {
} }
func (cmd *Create)GetDesc() string { func (cmd *Create)GetDesc() string {
return "Create a VM" return "Create one or more VMs"
} }
func (cmd *Create)PrintUsage() { func (cmd *Create)PrintUsage() {
u.PrintlnErr(cmd.GetDesc()) u.PrintlnErr(cmd.GetDesc())
u.PrintlnErr("Usage: "+cmd.Name+" name cpus ram nic template") u.PrintlnErr("Usage: "+cmd.Name+" name cpus ram nic template [count]")
u.PrintlnErr("name name of the VM") const usage string = `name name of the VM
u.PrintlnErr("cpus number of CPUs, between 1 and 16") cpus number of CPUs, between 1 and 16
u.PrintlnErr("ram amount of RAM in MB, between 512 and 32768") ram amount of RAM in MB, between 512 and 32768
u.PrintlnErr("nic network interface, either \"none\" or \"user\"") nic network interface, either "none" or "user"
u.PrintlnErr("template ID of the template to VM will be based on") template ID of the template to VM will be based on
count number of VMs to create (if not specified, one is created)
if specified and > 1, name is postfixed with [n]
where n ranges from 1..count`
u.PrintlnErr(usage)
} }
func (cmd *Create)Run(args []string) int { func (cmd *Create)Run(args []string) int {
...@@ -34,24 +37,36 @@ func (cmd *Create)Run(args []string) int { ...@@ -34,24 +37,36 @@ func (cmd *Create)Run(args []string) int {
host := g.GetInstance().Host host := g.GetInstance().Host
argc := len(args) argc := len(args)
if argc != 5 { if argc < 5 || argc > 6 {
cmd.PrintUsage() cmd.PrintUsage()
return 1 return 1
} }
name := args[0]
ncpus, err := strconv.Atoi(args[1]) ncpus, err := strconv.Atoi(args[1])
if err != nil { if err != nil {
u.PrintlnErr("Invalid number of CPU(s)") u.PrintlnErr("Invalid number of CPU(s)")
return 1 return 1
} }
ram, err := strconv.Atoi(args[2]) ram, err := strconv.Atoi(args[2])
if err != nil { if err != nil {
u.PrintlnErr("Invalid amount of RAM") u.PrintlnErr("Invalid amount of RAM")
return 1 return 1
} }
nic := args[3] nic := args[3]
template := args[4]
count := 1
if argc == 6 {
count, err = strconv.Atoi(args[5])
if err != nil {
u.PrintlnErr("Invalid count")
return 1
}
if count < 1 {
u.PrintlnErr("count must be > 0")
return 1
}
}
type VMArgs struct { type VMArgs struct {
Name string Name string
...@@ -62,24 +77,37 @@ func (cmd *Create)Run(args []string) int { ...@@ -62,24 +77,37 @@ func (cmd *Create)Run(args []string) int {
} }
vmArgs := &VMArgs { vmArgs := &VMArgs {
Name: args[0], Name: name,
Cpus: ncpus, Cpus: ncpus,
Ram: ram, Ram: ram,
Nic: nic, Nic: nic,
TemplateID: args[4], TemplateID: template,
} }
resp, err := client.R().SetBody(vmArgs).Post(host+"/vms") errors := false
if err != nil {
u.PrintlnErr("Error: "+err.Error()) for i := 1; i <= count; i++ {
return 1 if count > 1 {
vmArgs.Name = name+" ["+strconv.Itoa(i)+"]"
} }
resp, err := client.R().SetBody(vmArgs).Post(host+"/vms")
if err != nil {
u.PrintlnErr("Failed creating VM \""+vmArgs.Name+"\": "+err.Error())
errors = true
} else {
if resp.IsSuccess() { if resp.IsSuccess() {
u.Println(resp) u.Println("Successfully created VM \""+vmArgs.Name+"\"")
return 0
} else { } else {
u.PrintlnErr("Error: "+resp.Status()+": "+resp.String()) u.PrintlnErr("Failed creating VM \""+vmArgs.Name+"\": "+resp.Status()+": "+resp.String())
errors = true
}
}
}
if errors {
return 1 return 1
} else {
return 0
} }
} }
...@@ -19,7 +19,7 @@ func (cmd *SetAccess)GetDesc() string { ...@@ -19,7 +19,7 @@ func (cmd *SetAccess)GetDesc() string {
func (cmd *SetAccess)PrintUsage() { func (cmd *SetAccess)PrintUsage() {
u.PrintlnErr(cmd.GetDesc()) u.PrintlnErr(cmd.GetDesc())
u.PrintlnErr("Usage: "+cmd.Name+" vmID email [capability1 capability2 ...]") u.PrintlnErr("Usage: "+cmd.Name+" vmID email [capability ...]")
} }
func (cmd *SetAccess)Run(args []string) int { func (cmd *SetAccess)Run(args []string) int {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment