Make service init calls into the driver methods.

Otherwise, because of the way Go does method dispatch, derived versions of
those methods will never be called.

Signed-off-by: Ash Wilson <ash.wilson@rackspace.com>
This commit is contained in:
Ash Wilson 2014-12-11 13:46:58 -05:00 committed by Guillaume Giamarchi
parent b046940433
commit 8466d95fa0
2 changed files with 57 additions and 46 deletions

View File

@ -16,6 +16,10 @@ import (
)
type Client interface {
Authenticate(d *Driver) error
InitComputeClient(d *Driver) error
InitNetworkClient(d *Driver) error
CreateInstance(d *Driver) (string, error)
GetInstanceState(d *Driver) (string, error)
StartInstance(d *Driver) error
@ -26,7 +30,6 @@ type Client interface {
GetInstanceIpAddresses(d *Driver) ([]IpAddress, error)
CreateKeyPair(d *Driver, name string, publicKey string) error
DeleteKeyPair(d *Driver, name string) error
Authenticate(d *Driver) error
}
type GenericClient struct {
@ -36,9 +39,6 @@ type GenericClient struct {
}
func (c *GenericClient) CreateInstance(d *Driver) (string, error) {
if err := c.initComputeClient(d); err != nil {
return "", err
}
serverOpts := servers.CreateOpts{
Name: d.MachineName,
FlavorRef: d.FlavorId,
@ -88,9 +88,6 @@ func (c *GenericClient) GetInstanceState(d *Driver) (string, error) {
}
func (c *GenericClient) StartInstance(d *Driver) error {
if err := c.initComputeClient(d); err != nil {
return err
}
if result := startstop.Start(c.Compute, d.MachineId); result.Err != nil {
return result.Err
}
@ -98,9 +95,6 @@ func (c *GenericClient) StartInstance(d *Driver) error {
}
func (c *GenericClient) StopInstance(d *Driver) error {
if err := c.initComputeClient(d); err != nil {
return err
}
if result := startstop.Stop(c.Compute, d.MachineId); result.Err != nil {
return result.Err
}
@ -108,9 +102,6 @@ func (c *GenericClient) StopInstance(d *Driver) error {
}
func (c *GenericClient) RestartInstance(d *Driver) error {
if err := c.initComputeClient(d); err != nil {
return err
}
if result := servers.Reboot(c.Compute, d.MachineId, servers.SoftReboot); result.Err != nil {
return result.Err
}
@ -118,9 +109,6 @@ func (c *GenericClient) RestartInstance(d *Driver) error {
}
func (c *GenericClient) DeleteInstance(d *Driver) error {
if err := c.initComputeClient(d); err != nil {
return err
}
if result := servers.Delete(c.Compute, d.MachineId); result.Err != nil {
return result.Err
}
@ -238,9 +226,6 @@ func (c *GenericClient) GetImageId(d *Driver, imageName string) (string, error)
}
func (c *GenericClient) CreateKeyPair(d *Driver, name string, publicKey string) error {
if err := c.initComputeClient(d); err != nil {
return err
}
opts := keypairs.CreateOpts{
Name: name,
PublicKey: publicKey,
@ -252,9 +237,6 @@ func (c *GenericClient) CreateKeyPair(d *Driver, name string, publicKey string)
}
func (c *GenericClient) DeleteKeyPair(d *Driver, name string) error {
if err := c.initComputeClient(d); err != nil {
return err
}
if result := keypairs.Delete(c.Compute, name); result.Err != nil {
return result.Err
}
@ -262,9 +244,6 @@ func (c *GenericClient) DeleteKeyPair(d *Driver, name string) error {
}
func (c *GenericClient) getServerDetail(d *Driver) (*servers.Server, error) {
if err := c.initComputeClient(d); err != nil {
return nil, err
}
server, err := servers.Get(c.Compute, d.MachineId).Extract()
if err != nil {
return nil, err
@ -273,11 +252,6 @@ func (c *GenericClient) getServerDetail(d *Driver) (*servers.Server, error) {
}
func (c *GenericClient) getFloatingIPs(d *Driver) ([]string, error) {
if err := c.initNetworkClient(d); err != nil {
return nil, err
}
pager := floatingips.List(c.Network, floatingips.ListOpts{})
err := pager.EachPage(func(page pagination.Page) (bool, error) {
@ -298,11 +272,6 @@ func (c *GenericClient) getFloatingIPs(d *Driver) ([]string, error) {
}
func (c *GenericClient) getPorts(d *Driver) ([]string, error) {
if err := c.initNetworkClient(d); err != nil {
return nil, err
}
pager := ports.List(c.Network, ports.ListOpts{
DeviceID: d.MachineId,
})
@ -324,7 +293,7 @@ func (c *GenericClient) getPorts(d *Driver) ([]string, error) {
return nil, nil
}
func (c *GenericClient) initComputeClient(d *Driver) error {
func (c *GenericClient) InitComputeClient(d *Driver) error {
if c.Provider == nil {
err := c.Authenticate(d)
if err != nil {
@ -342,13 +311,7 @@ func (c *GenericClient) initComputeClient(d *Driver) error {
return nil
}
func (c *GenericClient) initNetworkClient(d *Driver) error {
if c.Provider == nil {
err := c.Authenticate(d)
if err != nil {
return err
}
}
func (c *GenericClient) InitNetworkClient(d *Driver) error {
network, err := openstack.NewNetworkV2(c.Provider, gophercloud.EndpointOpts{
Region: d.Region,
Availability: c.getEndpointType(d),
@ -371,6 +334,10 @@ func (c *GenericClient) getEndpointType(d *Driver) gophercloud.Availability {
}
func (c *GenericClient) Authenticate(d *Driver) error {
if c.Provider != nil {
return nil
}
log.WithFields(log.Fields{
"AuthUrl": d.AuthUrl,
"Username": d.Username,

View File

@ -213,6 +213,10 @@ func (d *Driver) GetURL() (string, error) {
}
func (d *Driver) GetIP() (string, error) {
if err := d.initCompute(); err != nil {
return "", err
}
addresses, err := d.client.GetInstanceIpAddresses(d)
if err != nil {
return "", err
@ -251,8 +255,13 @@ func (d *Driver) GetIP() (string, error) {
}
func (d *Driver) GetState() (state.State, error) {
log.WithField("MachineId", d.MachineId).Debug("Get status for OpenStack instance...")
if err := d.initCompute(); err != nil {
return state.None, err
}
if err := d.initNetwork(); err != nil {
return state.None, err
}
s, err := d.client.GetInstanceState(d)
if err != nil {
@ -282,7 +291,6 @@ func (d *Driver) GetState() (state.State, error) {
}
func (d *Driver) Create() error {
d.setMachineNameIfNotSet()
d.KeyPairName = d.MachineName
@ -306,6 +314,9 @@ func (d *Driver) Create() error {
func (d *Driver) Start() error {
log.WithField("MachineId", d.MachineId).Info("Starting OpenStack instance...")
if err := d.initCompute(); err != nil {
return err
}
if err := d.client.StartInstance(d); err != nil {
return err
}
@ -313,8 +324,10 @@ func (d *Driver) Start() error {
}
func (d *Driver) Stop() error {
log.WithField("MachineId", d.MachineId).Info("Stopping OpenStack instance...")
if err := d.initCompute(); err != nil {
return err
}
if err := d.client.StopInstance(d); err != nil {
return err
}
@ -328,6 +341,9 @@ func (d *Driver) Stop() error {
func (d *Driver) Remove() error {
log.WithField("MachineId", d.MachineId).Info("Deleting OpenStack instance...")
if err := d.initCompute(); err != nil {
return err
}
if err := d.client.DeleteInstance(d); err != nil {
return err
}
@ -340,6 +356,9 @@ func (d *Driver) Remove() error {
func (d *Driver) Restart() error {
log.WithField("MachineId", d.MachineId).Info("Restarting OpenStack instance...")
if err := d.initCompute(); err != nil {
return err
}
if err := d.client.RestartInstance(d); err != nil {
return err
}
@ -482,6 +501,23 @@ func (d *Driver) resolveIds() error {
}).Debug("Found image id using its name")
}
func (d *Driver) initCompute() error {
if err := d.client.Authenticate(d); err != nil {
return err
}
if err := d.client.InitComputeClient(d); err != nil {
return err
}
return nil
}
func (d *Driver) initNetwork() error {
if err := d.client.Authenticate(d); err != nil {
return err
}
if err := d.client.InitNetworkClient(d); err != nil {
return err
}
return nil
}
@ -494,6 +530,10 @@ func (d *Driver) createSSHKey() error {
if err != nil {
return err
}
if err := d.initCompute(); err != nil {
return err
}
if err := d.client.CreateKeyPair(d, d.KeyPairName, string(publicKey)); err != nil {
return err
}
@ -505,6 +545,10 @@ func (d *Driver) createMachine() error {
"FlavorId": d.FlavorId,
"ImageId": d.ImageId,
}).Debug("Creating OpenStack instance...")
if err := d.initCompute(); err != nil {
return err
}
instanceId, err := d.client.CreateInstance(d)
if err != nil {
return err