From 24d1d886f8e8d44ea1eb60fb9ef12e22665522c8 Mon Sep 17 00:00:00 2001 From: Marin Salinas Date: Tue, 29 Jan 2019 12:18:02 -0600 Subject: [PATCH] feature: add root volume run tags config to bsusurrogate config struct --- builder/osc/bsusurrogate/builder.go | 3 +- builder/osc/common/interpolate_build_info.go | 35 +++++++++++ .../osc/common/interpolate_build_info_test.go | 62 +++++++++++++++++++ builder/osc/common/omi_config.go | 3 - builder/osc/common/tags.go | 45 ++++++++++++++ 5 files changed, 144 insertions(+), 4 deletions(-) create mode 100644 builder/osc/common/interpolate_build_info.go create mode 100644 builder/osc/common/interpolate_build_info_test.go create mode 100644 builder/osc/common/tags.go diff --git a/builder/osc/bsusurrogate/builder.go b/builder/osc/bsusurrogate/builder.go index bea12dc15..68f9cd0da 100644 --- a/builder/osc/bsusurrogate/builder.go +++ b/builder/osc/bsusurrogate/builder.go @@ -21,7 +21,8 @@ type Config struct { osccommon.BlockDevices `mapstructure:",squash"` osccommon.OMIConfig `mapstructure:",squash"` - RootDevice RootBlockDevice `mapstructure:"ami_root_device"` + RootDevice RootBlockDevice `mapstructure:"ami_root_device"` + VolumeRunTags osccommon.TagMap `mapstructure:"run_volume_tags"` ctx interpolate.Context } diff --git a/builder/osc/common/interpolate_build_info.go b/builder/osc/common/interpolate_build_info.go new file mode 100644 index 000000000..2515e9f33 --- /dev/null +++ b/builder/osc/common/interpolate_build_info.go @@ -0,0 +1,35 @@ +package common + +import ( + "github.com/hashicorp/packer/helper/multistep" + "github.com/outscale/osc-go/oapi" +) + +type BuildInfoTemplate struct { + BuildRegion string + SourceOMI string + SourceOMIName string + SourceOMITags map[string]string +} + +func extractBuildInfo(region string, state multistep.StateBag) *BuildInfoTemplate { + rawSourceOMI, hasSourceOMI := state.GetOk("source_image") + if !hasSourceOMI { + return &BuildInfoTemplate{ + BuildRegion: region, + } + } + + sourceOMI := rawSourceOMI.(*oapi.Image) + sourceOMITags := make(map[string]string, len(sourceOMI.Tags)) + for _, tag := range sourceOMI.Tags { + sourceOMITags[tag.Key] = tag.Value + } + + return &BuildInfoTemplate{ + BuildRegion: region, + SourceOMI: sourceOMI.ImageId, + SourceOMIName: sourceOMI.ImageName, + SourceOMITags: sourceOMITags, + } +} diff --git a/builder/osc/common/interpolate_build_info_test.go b/builder/osc/common/interpolate_build_info_test.go new file mode 100644 index 000000000..c15a4072b --- /dev/null +++ b/builder/osc/common/interpolate_build_info_test.go @@ -0,0 +1,62 @@ +package common + +import ( + "reflect" + "testing" + + "github.com/hashicorp/packer/helper/multistep" + "github.com/outscale/osc-go/oapi" +) + +func testImage() *oapi.Image { + return &oapi.Image{ + ImageId: "ami-abcd1234", + ImageName: "ami_test_name", + Tags: []oapi.ResourceTag{ + { + Key: "key-1", + Value: "value-1", + }, + { + Key: "key-2", + Value: "value-2", + }, + }, + } +} + +func testState() multistep.StateBag { + state := new(multistep.BasicStateBag) + return state +} + +func TestInterpolateBuildInfo_extractBuildInfo_noSourceImage(t *testing.T) { + state := testState() + buildInfo := extractBuildInfo("foo", state) + + expected := BuildInfoTemplate{ + BuildRegion: "foo", + } + if !reflect.DeepEqual(*buildInfo, expected) { + t.Fatalf("Unexpected BuildInfoTemplate: expected %#v got %#v\n", expected, *buildInfo) + } +} + +func TestInterpolateBuildInfo_extractBuildInfo_withSourceImage(t *testing.T) { + state := testState() + state.Put("source_image", testImage()) + buildInfo := extractBuildInfo("foo", state) + + expected := BuildInfoTemplate{ + BuildRegion: "foo", + SourceOMI: "ami-abcd1234", + SourceOMIName: "ami_test_name", + SourceOMITags: map[string]string{ + "key-1": "value-1", + "key-2": "value-2", + }, + } + if !reflect.DeepEqual(*buildInfo, expected) { + t.Fatalf("Unexpected BuildInfoTemplate: expected %#v got %#v\n", expected, *buildInfo) + } +} diff --git a/builder/osc/common/omi_config.go b/builder/osc/common/omi_config.go index 11055c628..fbd1bc889 100644 --- a/builder/osc/common/omi_config.go +++ b/builder/osc/common/omi_config.go @@ -8,9 +8,6 @@ import ( "github.com/hashicorp/packer/template/interpolate" ) -//Temporal -type TagMap map[string]string - // OMIConfig is for common configuration related to creating OMIs. type OMIConfig struct { OMIName string `mapstructure:"ami_name"` diff --git a/builder/osc/common/tags.go b/builder/osc/common/tags.go new file mode 100644 index 000000000..ff58c0d67 --- /dev/null +++ b/builder/osc/common/tags.go @@ -0,0 +1,45 @@ +package common + +import ( + "fmt" + + "github.com/hashicorp/packer/helper/multistep" + "github.com/hashicorp/packer/packer" + "github.com/hashicorp/packer/template/interpolate" + "github.com/outscale/osc-go/oapi" +) + +type TagMap map[string]string +type OAPI []*oapi.ResourceTag + +func (t OAPI) Report(ui packer.Ui) { + for _, tag := range t { + ui.Message(fmt.Sprintf("Adding tag: \"%s\": \"%s\"", + tag.Key, tag.Value)) + } +} + +func (t TagMap) IsSet() bool { + return len(t) > 0 +} + +func (t TagMap) OAPI(ctx interpolate.Context, region string, state multistep.StateBag) (OAPI, error) { + var oapiTags []*oapi.ResourceTag + ctx.Data = extractBuildInfo(region, state) + + for key, value := range t { + interpolatedKey, err := interpolate.Render(key, &ctx) + if err != nil { + return nil, fmt.Errorf("Error processing tag: %s:%s - %s", key, value, err) + } + interpolatedValue, err := interpolate.Render(value, &ctx) + if err != nil { + return nil, fmt.Errorf("Error processing tag: %s:%s - %s", key, value, err) + } + oapiTags = append(oapiTags, &oapi.ResourceTag{ + Key: interpolatedKey, + Value: interpolatedValue, + }) + } + return oapiTags, nil +}