From e5c14044fb125b4ded930444c75e1656cd70eccf Mon Sep 17 00:00:00 2001 From: Marin Salinas Date: Thu, 20 Aug 2020 19:12:38 -0500 Subject: [PATCH] refactor: change tags file to new OSC SDK --- builder/osc/common/tags.go | 45 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/builder/osc/common/tags.go b/builder/osc/common/tags.go index c42d48db7..425bfbc1d 100644 --- a/builder/osc/common/tags.go +++ b/builder/osc/common/tags.go @@ -1,16 +1,20 @@ package common import ( + "context" "fmt" + "github.com/antihax/optional" "github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/template/interpolate" "github.com/outscale/osc-go/oapi" + "github.com/outscale/osc-sdk-go/osc" ) type TagMap map[string]string type OAPITags []oapi.ResourceTag +type OSCTags []osc.ResourceTag func (t OAPITags) Report(ui packer.Ui) { for _, tag := range t { @@ -19,6 +23,13 @@ func (t OAPITags) Report(ui packer.Ui) { } } +func (t OSCTags) 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 } @@ -44,6 +55,27 @@ func (t TagMap) OAPITags(ctx interpolate.Context, region string, state multistep return oapiTags, nil } +func (t TagMap) OSCTags(ctx interpolate.Context, region string, state multistep.StateBag) (OSCTags, error) { + var oscTags []osc.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) + } + oscTags = append(oscTags, osc.ResourceTag{ + Key: interpolatedKey, + Value: interpolatedValue, + }) + } + return oscTags, nil +} + func CreateTags(conn *oapi.Client, resourceID string, ui packer.Ui, tags OAPITags) error { tags.Report(ui) @@ -54,3 +86,16 @@ func CreateTags(conn *oapi.Client, resourceID string, ui packer.Ui, tags OAPITag return err } + +func CreateOSCTags(conn *osc.APIClient, resourceID string, ui packer.Ui, tags OSCTags) error { + tags.Report(ui) + + _, _, err := conn.TagApi.CreateTags(context.Background(), &osc.CreateTagsOpts{ + CreateTagsRequest: optional.NewInterface(osc.CreateTagsRequest{ + ResourceIds: []string{resourceID}, + Tags: tags, + }), + }) + + return err +}