builder/googlecompute: artifact uses Driver, no more api

This commit is contained in:
Mitchell Hashimoto 2013-12-13 19:07:10 -08:00
parent 33a84c0938
commit 637968f2dd
6 changed files with 33 additions and 69 deletions

View File

@ -1,23 +0,0 @@
package googlecompute
import (
"code.google.com/p/google-api-go-client/compute/v1beta16"
)
// GoogleComputeClient represents a GCE client.
type GoogleComputeClient struct {
ProjectId string
Service *compute.Service
Zone string
clientSecrets *clientSecrets
}
// DeleteImage deletes the named image. Returns a Global Operation.
func (g *GoogleComputeClient) DeleteImage(name string) (*compute.Operation, error) {
imagesDeleteCall := g.Service.Images.Delete(g.ProjectId, name)
operation, err := imagesDeleteCall.Do()
if err != nil {
return nil, err
}
return operation, nil
}

View File

@ -19,14 +19,8 @@ func (*Artifact) BuilderId() string {
// Destroy destroys the GCE image represented by the artifact. // Destroy destroys the GCE image represented by the artifact.
func (a *Artifact) Destroy() error { func (a *Artifact) Destroy() error {
log.Printf("Destroying image: %s", a.imageName) log.Printf("Destroying image: %s", a.imageName)
/* errCh := a.driver.DeleteImage(a.imageName)
// Ignore the operation result as we are not waiting until it completes. return <-errCh
_, err := a.client.DeleteImage(a.imageName)
if err != nil {
return err
}
*/
return nil
} }
// Files returns the files represented by the artifact. // Files returns the files represented by the artifact.

View File

@ -7,6 +7,9 @@ type Driver interface {
// CreateImage creates an image with the given URL in Google Storage. // CreateImage creates an image with the given URL in Google Storage.
CreateImage(name, description, url string) <-chan error CreateImage(name, description, url string) <-chan error
// DeleteImage deletes the image with the given name.
DeleteImage(name string) <-chan error
// DeleteInstance deletes the given instance. // DeleteInstance deletes the given instance.
DeleteInstance(zone, name string) (<-chan error, error) DeleteInstance(zone, name string) (<-chan error, error)

View File

@ -81,6 +81,18 @@ func (d *driverGCE) CreateImage(name, description, url string) <-chan error {
return errCh return errCh
} }
func (d *driverGCE) DeleteImage(name string) <-chan error {
errCh := make(chan error, 1)
op, err := d.service.Images.Delete(d.projectId, name).Do()
if err != nil {
errCh <- err
} else {
go waitForState(errCh, "DONE", d.refreshGlobalOp(op))
}
return errCh
}
func (d *driverGCE) DeleteInstance(zone, name string) (<-chan error, error) { func (d *driverGCE) DeleteInstance(zone, name string) (<-chan error, error) {
op, err := d.service.Instances.Delete(d.projectId, zone, name).Do() op, err := d.service.Instances.Delete(d.projectId, zone, name).Do()
if err != nil { if err != nil {

View File

@ -8,6 +8,9 @@ type DriverMock struct {
CreateImageURL string CreateImageURL string
CreateImageErrCh <-chan error CreateImageErrCh <-chan error
DeleteImageName string
DeleteImageErrCh <-chan error
DeleteInstanceZone string DeleteInstanceZone string
DeleteInstanceName string DeleteInstanceName string
DeleteInstanceErrCh <-chan error DeleteInstanceErrCh <-chan error
@ -43,6 +46,19 @@ func (d *DriverMock) CreateImage(name, description, url string) <-chan error {
return resultCh return resultCh
} }
func (d *DriverMock) DeleteImage(name string) <-chan error {
d.DeleteImageName = name
resultCh := d.DeleteImageErrCh
if resultCh == nil {
ch := make(chan error)
close(ch)
resultCh = ch
}
return resultCh
}
func (d *DriverMock) DeleteInstance(zone, name string) (<-chan error, error) { func (d *DriverMock) DeleteInstance(zone, name string) (<-chan error, error) {
d.DeleteInstanceZone = zone d.DeleteInstanceZone = zone
d.DeleteInstanceName = name d.DeleteInstanceName = name

View File

@ -1,38 +0,0 @@
package googlecompute
import (
"time"
)
// waitForInstanceState.
func waitForInstanceState(desiredState string, zone string, name string, client *GoogleComputeClient, timeout time.Duration) error {
return nil
/*
f := func() (string, error) {
return client.InstanceStatus(zone, name)
}
return waitForState("instance", desiredState, f, timeout)
*/
}
// waitForZoneOperationState.
func waitForZoneOperationState(desiredState string, zone string, name string, client *GoogleComputeClient, timeout time.Duration) error {
return nil
/*
f := func() (string, error) {
return client.ZoneOperationStatus(zone, name)
}
return waitForState("operation", desiredState, f, timeout)
*/
}
// waitForGlobalOperationState.
func waitForGlobalOperationState(desiredState string, name string, client *GoogleComputeClient, timeout time.Duration) error {
/*
f := func() (string, error) {
return client.GlobalOperationStatus(name)
}
return waitForState("operation", desiredState, f, timeout)
*/
return nil
}