Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions drivers/openstack/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/bootfromvolume"
compute_ips "github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/floatingips"
"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/keypairs"
"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/schedulerhints"
"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/servergroups"
"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/startstop"
"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/volumeattach"
"github.com/gophercloud/gophercloud/openstack/compute/v2/flavors"
Expand Down Expand Up @@ -48,6 +50,7 @@ type Client interface {
GetNetworkID(d *Driver) (string, error)
GetFlavorID(d *Driver) (string, error)
GetImageID(d *Driver) (string, error)
GetServerGroupID(d *Driver) (string, error)
AssignFloatingIP(d *Driver, floatingIP *FloatingIP) error
DeleteFloatingIP(d *Driver, floatingIP *FloatingIP) error
GetFloatingIPs(d *Driver) ([]FloatingIP, error)
Expand Down Expand Up @@ -95,6 +98,15 @@ func (c *GenericClient) CreateInstance(d *Driver) (string, error) {
KeyName: d.KeyPairName,
}

if d.ServerGroupId != "" {
serverOpts = &schedulerhints.CreateOptsExt{
CreateOptsBuilder: serverOpts,
SchedulerHints: schedulerhints.SchedulerHints{
Group: d.ServerGroupId,
},
}
}

log.Info("Creating machine...")

var server *servers.Server
Expand Down Expand Up @@ -370,6 +382,29 @@ func (c *GenericClient) GetImageID(d *Driver) (string, error) {
return imageID, err
}

func (c *GenericClient) GetServerGroupID(d *Driver) (string, error) {
pager := servergroups.List(c.Compute)
serverGroupID := ""

err := pager.EachPage(func(page pagination.Page) (bool, error) {
serverGroupList, err := servergroups.ExtractServerGroups(page)
if err != nil {
return false, err
}

for _, i := range serverGroupList {
if i.Name == d.ServerGroupName {
serverGroupID = i.ID
return false, nil
}
}

return true, nil
})

return serverGroupID, err
}

func (c *GenericClient) GetPublicKey(keyPairName string) ([]byte, error) {
kp, err := keypairs.Get(c.Compute, keyPairName).Extract()
if err != nil {
Expand Down
56 changes: 47 additions & 9 deletions drivers/openstack/openstack.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ type Driver struct {
FlavorId string
ImageName string
ImageId string
ServerGroupName string
ServerGroupId string
KeyPairName string
NetworkName string
NetworkId string
Expand Down Expand Up @@ -223,6 +225,18 @@ func (d *Driver) GetCreateFlags() []mcnflag.Flag {
Usage: "OpenStack image name to use for the instance",
Value: "",
},
mcnflag.StringFlag{
EnvVar: "OS_SERVER_GROUP_ID",
Name: "openstack-server-group-id",
Usage: "OpenStack server group id to use for the instance",
Value: "",
},
mcnflag.StringFlag{
EnvVar: "OS_SERVER_GROUP_NAME",
Name: "openstack-server-group-name",
Usage: "OpenStack server group name to use for the instance",
Value: "",
},
mcnflag.StringFlag{
EnvVar: "OS_KEYPAIR_NAME",
Name: "openstack-keypair-name",
Expand Down Expand Up @@ -426,6 +440,8 @@ func (d *Driver) SetConfigFromFlags(flags drivers.DriverOptions) error {
d.FlavorName = flags.String("openstack-flavor-name")
d.ImageId = flags.String("openstack-image-id")
d.ImageName = flags.String("openstack-image-name")
d.ServerGroupId = flags.String("openstack-server-group-id")
d.ServerGroupName = flags.String("openstack-server-group-name")
d.NetworkId = flags.String("openstack-net-id")
d.NetworkName = flags.String("openstack-net-name")
if flags.String("openstack-sec-groups") != "" {
Expand Down Expand Up @@ -668,15 +684,16 @@ func (d *Driver) Remove() error {
}

const (
errorMandatoryEnvOrOption string = "%s must be specified either using the environment variable %s or the CLI option %s"
errorMandatoryOption string = "%s must be specified using the CLI option %s"
errorExclusiveOptions string = "Either %s or %s must be specified, not both"
errorBothOptions string = "Both %s and %s must be specified"
errorWrongEndpointType string = "Endpoint type must be 'publicURL', 'adminURL' or 'internalURL'"
errorUnknownFlavorName string = "Unable to find flavor named %s"
errorUnknownImageName string = "Unable to find image named %s"
errorUnknownNetworkName string = "Unable to find network named %s"
errorUnknownTenantName string = "Unable to find tenant named %s"
errorMandatoryEnvOrOption string = "%s must be specified either using the environment variable %s or the CLI option %s"
errorMandatoryOption string = "%s must be specified using the CLI option %s"
errorExclusiveOptions string = "Either %s or %s must be specified, not both"
errorBothOptions string = "Both %s and %s must be specified"
errorWrongEndpointType string = "Endpoint type must be 'publicURL', 'adminURL' or 'internalURL'"
errorUnknownFlavorName string = "Unable to find flavor named %s"
errorUnknownImageName string = "Unable to find image named %s"
errorUnknownServerGroupName string = "Unable to find server group named %s"
errorUnknownNetworkName string = "Unable to find network named %s"
errorUnknownTenantName string = "Unable to find tenant named %s"
)

func (d *Driver) parseAuthConfig() (*gophercloud.AuthOptions, error) {
Expand Down Expand Up @@ -801,6 +818,27 @@ func (d *Driver) resolveIds() error {
})
}

if d.ServerGroupName != "" {
if err := d.initCompute(); err != nil {
return err
}
serverGroupId, err := d.client.GetServerGroupID(d)

if err != nil {
return err
}

if serverGroupId == "" {
return fmt.Errorf(errorUnknownServerGroupName, d.ServerGroupName)
}

d.ServerGroupId = serverGroupId
log.Debug("Found server group id using its name", map[string]string{
"Name": d.ServerGroupName,
"ID": d.ServerGroupId,
})
}

if d.FloatingIpPool != "" && !d.ComputeNetwork {
if err := d.initNetwork(); err != nil {
return err
Expand Down