refactor: change tags file to new OSC SDK

This commit is contained in:
Marin Salinas 2020-08-20 19:12:38 -05:00
parent addc3dd6e7
commit e5c14044fb
1 changed files with 45 additions and 0 deletions

View File

@ -1,16 +1,20 @@
package common package common
import ( import (
"context"
"fmt" "fmt"
"github.com/antihax/optional"
"github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/packer"
"github.com/hashicorp/packer/template/interpolate" "github.com/hashicorp/packer/template/interpolate"
"github.com/outscale/osc-go/oapi" "github.com/outscale/osc-go/oapi"
"github.com/outscale/osc-sdk-go/osc"
) )
type TagMap map[string]string type TagMap map[string]string
type OAPITags []oapi.ResourceTag type OAPITags []oapi.ResourceTag
type OSCTags []osc.ResourceTag
func (t OAPITags) Report(ui packer.Ui) { func (t OAPITags) Report(ui packer.Ui) {
for _, tag := range t { 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 { func (t TagMap) IsSet() bool {
return len(t) > 0 return len(t) > 0
} }
@ -44,6 +55,27 @@ func (t TagMap) OAPITags(ctx interpolate.Context, region string, state multistep
return oapiTags, nil 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 { func CreateTags(conn *oapi.Client, resourceID string, ui packer.Ui, tags OAPITags) error {
tags.Report(ui) tags.Report(ui)
@ -54,3 +86,16 @@ func CreateTags(conn *oapi.Client, resourceID string, ui packer.Ui, tags OAPITag
return err 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
}