feature: add root volume run tags config to bsusurrogate config struct

This commit is contained in:
Marin Salinas 2019-01-29 12:18:02 -06:00 committed by Megan Marsh
parent 837aada415
commit 24d1d886f8
5 changed files with 144 additions and 4 deletions

View File

@ -22,6 +22,7 @@ type Config struct {
osccommon.OMIConfig `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 ctx interpolate.Context
} }

View File

@ -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,
}
}

View File

@ -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)
}
}

View File

@ -8,9 +8,6 @@ import (
"github.com/hashicorp/packer/template/interpolate" "github.com/hashicorp/packer/template/interpolate"
) )
//Temporal
type TagMap map[string]string
// OMIConfig is for common configuration related to creating OMIs. // OMIConfig is for common configuration related to creating OMIs.
type OMIConfig struct { type OMIConfig struct {
OMIName string `mapstructure:"ami_name"` OMIName string `mapstructure:"ami_name"`

View File

@ -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
}