From 30ab6572f726d1166ccbb946314a126c5e97a5f1 Mon Sep 17 00:00:00 2001 From: Mark Peek Date: Tue, 27 Aug 2013 22:35:59 -0700 Subject: [PATCH] builder/openstack: return artifacts --- builder/openstack/artifact.go | 41 ++++++++++++++++++++++++++++++ builder/openstack/artifact_test.go | 39 ++++++++++++++++++++++++++++ builder/openstack/builder.go | 10 ++++++-- 3 files changed, 88 insertions(+), 2 deletions(-) create mode 100644 builder/openstack/artifact.go create mode 100644 builder/openstack/artifact_test.go diff --git a/builder/openstack/artifact.go b/builder/openstack/artifact.go new file mode 100644 index 000000000..a2ba552c5 --- /dev/null +++ b/builder/openstack/artifact.go @@ -0,0 +1,41 @@ +package openstack + +import ( + "fmt" + "github.com/rackspace/gophercloud" + "log" +) + +// Artifact is an artifact implementation that contains built images. +type Artifact struct { + // ImageId of built image + ImageId string + + // BuilderId is the unique ID for the builder that created this image + BuilderIdValue string + + // OpenStack connection for performing API stuff. + Conn gophercloud.CloudServersProvider +} + +func (a *Artifact) BuilderId() string { + return a.BuilderIdValue +} + +func (*Artifact) Files() []string { + // We have no files + return nil +} + +func (a *Artifact) Id() string { + return a.ImageId +} + +func (a *Artifact) String() string { + return fmt.Sprintf("An image was created: %v", a.ImageId) +} + +func (a *Artifact) Destroy() error { + log.Printf("Destroying image: %d", a.ImageId) + return a.Conn.DeleteImageById(a.ImageId) +} diff --git a/builder/openstack/artifact_test.go b/builder/openstack/artifact_test.go new file mode 100644 index 000000000..2fb4de928 --- /dev/null +++ b/builder/openstack/artifact_test.go @@ -0,0 +1,39 @@ +package openstack + +import ( + "cgl.tideland.biz/asserts" + "github.com/mitchellh/packer/packer" + "testing" +) + +func TestArtifact_Impl(t *testing.T) { + assert := asserts.NewTestingAsserts(t, true) + + var actual packer.Artifact + assert.Implementor(&Artifact{}, &actual, "should be an Artifact") +} + +func TestArtifactId(t *testing.T) { + assert := asserts.NewTestingAsserts(t, true) + + expected := `b8cdf55b-c916-40bd-b190-389ec144c4ed` + + a := &Artifact{ + ImageId: "b8cdf55b-c916-40bd-b190-389ec144c4ed", + } + + result := a.Id() + assert.Equal(result, expected, "should match output") +} + +func TestArtifactString(t *testing.T) { + assert := asserts.NewTestingAsserts(t, true) + + expected := "An image was created: b8cdf55b-c916-40bd-b190-389ec144c4ed" + + a := &Artifact{ + ImageId: "b8cdf55b-c916-40bd-b190-389ec144c4ed", + } + result := a.String() + assert.Equal(result, expected, "should match output") +} diff --git a/builder/openstack/builder.go b/builder/openstack/builder.go index 8bb81a7da..652cb4abb 100644 --- a/builder/openstack/builder.go +++ b/builder/openstack/builder.go @@ -112,8 +112,14 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe return nil, rawErr.(error) } - // XXX - add artifact - return nil, nil + // Build the artifact and return it + artifact := &Artifact{ + ImageId: state["image"].(string), + BuilderIdValue: BuilderId, + Conn: csp, + } + + return artifact, nil } func (b *Builder) Cancel() {