Validate and default Rackspace-specific options.

Signed-off-by: Ash Wilson <ash.wilson@rackspace.com>
This commit is contained in:
Ash Wilson 2014-12-11 12:08:34 -05:00 committed by Guillaume Giamarchi
parent c12d3f8ecd
commit b046940433
1 changed files with 60 additions and 2 deletions

View File

@ -1,6 +1,7 @@
package rackspace
import (
"fmt"
"os"
log "github.com/Sirupsen/logrus"
@ -11,7 +12,7 @@ import (
// Driver is a machine driver for Rackspace. It's a specialization of the generic OpenStack one.
type Driver struct {
drivers.Driver
*openstack.Driver
APIKey string
}
@ -55,9 +56,13 @@ func RegisterCreateFlags(cmd *flag.FlagSet) interface{} {
os.Getenv("OS_REGION_NAME"),
"Rackspace region name",
)
endpointDefault := os.Getenv("OS_ENDPOINT_TYPE")
if endpointDefault == "" {
endpointDefault = "publicURL"
}
createFlags.EndpointType = cmd.String(
[]string{"-rackspace-endpoint-type"},
os.Getenv("OS_ENDPOINT_TYPE"),
endpointDefault,
"Rackspace endpoint type (adminURL, internalURL or the default publicURL)",
)
createFlags.ImageID = cmd.String(
@ -99,3 +104,56 @@ func NewDriver(storePath string) (drivers.Driver, error) {
client.driver = driver
return driver, nil
}
// DriverName is the user-visible name of this driver.
func (d *Driver) DriverName() string {
return "rackspace"
}
// SetConfigFromFlags assigns and verifies the command-line arguments presented to the driver.
func (d *Driver) SetConfigFromFlags(flagsInterface interface{}) error {
flags := flagsInterface.(*CreateFlags)
d.Username = *flags.Username
d.APIKey = *flags.APIKey
d.Region = *flags.Region
d.MachineName = *flags.MachineName
d.EndpointType = *flags.EndpointType
d.ImageId = *flags.ImageID
d.FlavorId = *flags.FlavorID
d.SSHUser = *flags.SSHUser
d.SSHPort = *flags.SSHPort
return d.checkConfig()
}
func missingEnvOrOption(setting, envVar, opt string) error {
return fmt.Errorf(
"%s must be specified either using the environment variable %s or the CLI option %s",
setting,
envVar,
opt,
)
}
func (d *Driver) checkConfig() error {
if d.Username == "" {
return missingEnvOrOption("Username", "OS_USERNAME", "--rackspace-username")
}
if d.APIKey == "" {
return missingEnvOrOption("API key", "OS_API_KEY", "--rackspace-api-key")
}
if d.ImageId == "" {
// Default to the Ubuntu 14.10 image.
// This is done here, rather than in the option registration, to keep the default value
// from making "machine create --help" ugly.
d.ImageId = "0766e5df-d60a-4100-ae8c-07f27ec0148f"
}
if d.EndpointType != "publicURL" && d.EndpointType != "adminURL" && d.EndpointType != "internalURL" {
return fmt.Errorf(`Invalid endpoint type "%s". Endpoint type must be publicURL, adminURL or internalURL.`, d.EndpointType)
}
return nil
}