refactor: update step_update_omi_attributes and create_tags to new OSC SDK
This commit is contained in:
parent
8885a5ef31
commit
e0badb3fb7
|
@ -106,7 +106,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
|
||||||
state.Put("config", &b.config)
|
state.Put("config", &b.config)
|
||||||
state.Put("oapi", oapiconn)
|
state.Put("oapi", oapiconn)
|
||||||
state.Put("osc", oscConn)
|
state.Put("osc", oscConn)
|
||||||
state.Put("clientConfig", clientConfig)
|
state.Put("accessConfig", &b.config.AccessConfig)
|
||||||
state.Put("hook", hook)
|
state.Put("hook", hook)
|
||||||
state.Put("ui", ui)
|
state.Put("ui", ui)
|
||||||
|
|
||||||
|
@ -200,6 +200,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
|
||||||
&osccommon.StepUpdateOMIAttributes{
|
&osccommon.StepUpdateOMIAttributes{
|
||||||
AccountIds: b.config.OMIAccountIDs,
|
AccountIds: b.config.OMIAccountIDs,
|
||||||
SnapshotAccountIds: b.config.SnapshotAccountIDs,
|
SnapshotAccountIds: b.config.SnapshotAccountIDs,
|
||||||
|
RawRegion: b.config.RawRegion,
|
||||||
Ctx: b.config.ctx,
|
Ctx: b.config.ctx,
|
||||||
},
|
},
|
||||||
&osccommon.StepCreateTags{
|
&osccommon.StepCreateTags{
|
||||||
|
|
|
@ -2,16 +2,15 @@ package common
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"crypto/tls"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
|
||||||
|
|
||||||
|
"github.com/antihax/optional"
|
||||||
"github.com/aws/aws-sdk-go/aws/awserr"
|
"github.com/aws/aws-sdk-go/aws/awserr"
|
||||||
retry "github.com/hashicorp/packer/common"
|
retry "github.com/hashicorp/packer/common"
|
||||||
"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-sdk-go/osc"
|
||||||
)
|
)
|
||||||
|
|
||||||
type StepCreateTags struct {
|
type StepCreateTags struct {
|
||||||
|
@ -21,8 +20,7 @@ type StepCreateTags struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *StepCreateTags) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
|
func (s *StepCreateTags) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
|
||||||
oapiconn := state.Get("oapi").(*oapi.Client)
|
config := state.Get("accessConfig").(*AccessConfig)
|
||||||
config := state.Get("clientConfig").(*oapi.Config)
|
|
||||||
ui := state.Get("ui").(packer.Ui)
|
ui := state.Get("ui").(packer.Ui)
|
||||||
omis := state.Get("omis").(map[string]string)
|
omis := state.Get("omis").(map[string]string)
|
||||||
|
|
||||||
|
@ -34,28 +32,16 @@ func (s *StepCreateTags) Run(_ context.Context, state multistep.StateBag) multis
|
||||||
for region, ami := range omis {
|
for region, ami := range omis {
|
||||||
ui.Say(fmt.Sprintf("Adding tags to OMI (%s)...", ami))
|
ui.Say(fmt.Sprintf("Adding tags to OMI (%s)...", ami))
|
||||||
|
|
||||||
newConfig := &oapi.Config{
|
regionconn := config.NewOSCClientByRegion(region)
|
||||||
UserAgent: config.UserAgent,
|
|
||||||
SecretKey: config.SecretKey,
|
|
||||||
Service: config.Service,
|
|
||||||
Region: region, //New region
|
|
||||||
URL: config.URL,
|
|
||||||
}
|
|
||||||
|
|
||||||
skipClient := &http.Client{
|
|
||||||
Transport: &http.Transport{
|
|
||||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
regionConn := oapi.NewClient(newConfig, skipClient)
|
|
||||||
|
|
||||||
// Retrieve image list for given OMI
|
// Retrieve image list for given OMI
|
||||||
resourceIds := []string{ami}
|
resourceIds := []string{ami}
|
||||||
imageResp, err := regionConn.POST_ReadImages(oapi.ReadImagesRequest{
|
imageResp, _, err := regionconn.ImageApi.ReadImages(context.Background(), &osc.ReadImagesOpts{
|
||||||
Filters: oapi.FiltersImage{
|
ReadImagesRequest: optional.NewInterface(osc.ReadImagesRequest{
|
||||||
ImageIds: resourceIds,
|
Filters: osc.FiltersImage{
|
||||||
},
|
ImageIds: resourceIds,
|
||||||
|
},
|
||||||
|
}),
|
||||||
})
|
})
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -65,14 +51,14 @@ func (s *StepCreateTags) Run(_ context.Context, state multistep.StateBag) multis
|
||||||
return multistep.ActionHalt
|
return multistep.ActionHalt
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(imageResp.OK.Images) == 0 {
|
if len(imageResp.Images) == 0 {
|
||||||
err := fmt.Errorf("Error retrieving details for OMI (%s), no images found", ami)
|
err := fmt.Errorf("Error retrieving details for OMI (%s), no images found", ami)
|
||||||
state.Put("error", err)
|
state.Put("error", err)
|
||||||
ui.Error(err.Error())
|
ui.Error(err.Error())
|
||||||
return multistep.ActionHalt
|
return multistep.ActionHalt
|
||||||
}
|
}
|
||||||
|
|
||||||
image := imageResp.OK.Images[0]
|
image := imageResp.Images[0]
|
||||||
snapshotIds := []string{}
|
snapshotIds := []string{}
|
||||||
|
|
||||||
// Add only those with a Snapshot ID, i.e. not Ephemeral
|
// Add only those with a Snapshot ID, i.e. not Ephemeral
|
||||||
|
@ -86,7 +72,7 @@ func (s *StepCreateTags) Run(_ context.Context, state multistep.StateBag) multis
|
||||||
|
|
||||||
// Convert tags to oapi.Tag format
|
// Convert tags to oapi.Tag format
|
||||||
ui.Say("Creating OMI tags")
|
ui.Say("Creating OMI tags")
|
||||||
amiTags, err := s.Tags.OAPITags(s.Ctx, oapiconn.GetConfig().Region, state)
|
amiTags, err := s.Tags.OSCTags(s.Ctx, config.RawRegion, state)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
state.Put("error", err)
|
state.Put("error", err)
|
||||||
ui.Error(err.Error())
|
ui.Error(err.Error())
|
||||||
|
@ -95,7 +81,7 @@ func (s *StepCreateTags) Run(_ context.Context, state multistep.StateBag) multis
|
||||||
amiTags.Report(ui)
|
amiTags.Report(ui)
|
||||||
|
|
||||||
ui.Say("Creating snapshot tags")
|
ui.Say("Creating snapshot tags")
|
||||||
snapshotTags, err := s.SnapshotTags.OAPITags(s.Ctx, oapiconn.GetConfig().Region, state)
|
snapshotTags, err := s.SnapshotTags.OSCTags(s.Ctx, config.RawRegion, state)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
state.Put("error", err)
|
state.Put("error", err)
|
||||||
ui.Error(err.Error())
|
ui.Error(err.Error())
|
||||||
|
@ -106,9 +92,11 @@ func (s *StepCreateTags) Run(_ context.Context, state multistep.StateBag) multis
|
||||||
// Retry creating tags for about 2.5 minutes
|
// Retry creating tags for about 2.5 minutes
|
||||||
err = retry.Retry(0.2, 30, 11, func(_ uint) (bool, error) {
|
err = retry.Retry(0.2, 30, 11, func(_ uint) (bool, error) {
|
||||||
// Tag images and snapshots
|
// Tag images and snapshots
|
||||||
_, err := regionConn.POST_CreateTags(oapi.CreateTagsRequest{
|
_, _, err := regionconn.TagApi.CreateTags(context.Background(), &osc.CreateTagsOpts{
|
||||||
ResourceIds: resourceIds,
|
CreateTagsRequest: optional.NewInterface(osc.CreateTagsRequest{
|
||||||
Tags: amiTags,
|
ResourceIds: resourceIds,
|
||||||
|
Tags: amiTags,
|
||||||
|
}),
|
||||||
})
|
})
|
||||||
if awsErr, ok := err.(awserr.Error); ok {
|
if awsErr, ok := err.(awserr.Error); ok {
|
||||||
if awsErr.Code() == "InvalidOMIID.NotFound" ||
|
if awsErr.Code() == "InvalidOMIID.NotFound" ||
|
||||||
|
@ -119,9 +107,11 @@ func (s *StepCreateTags) Run(_ context.Context, state multistep.StateBag) multis
|
||||||
|
|
||||||
// Override tags on snapshots
|
// Override tags on snapshots
|
||||||
if len(snapshotTags) > 0 {
|
if len(snapshotTags) > 0 {
|
||||||
_, err = regionConn.POST_CreateTags(oapi.CreateTagsRequest{
|
_, _, err = regionconn.TagApi.CreateTags(context.Background(), &osc.CreateTagsOpts{
|
||||||
ResourceIds: snapshotIds,
|
CreateTagsRequest: optional.NewInterface(osc.CreateTagsRequest{
|
||||||
Tags: snapshotTags,
|
ResourceIds: snapshotIds,
|
||||||
|
Tags: snapshotTags,
|
||||||
|
}),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
|
|
@ -2,25 +2,24 @@ package common
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"crypto/tls"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
|
||||||
|
|
||||||
|
"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-sdk-go/osc"
|
||||||
)
|
)
|
||||||
|
|
||||||
type StepUpdateOMIAttributes struct {
|
type StepUpdateOMIAttributes struct {
|
||||||
AccountIds []string
|
AccountIds []string
|
||||||
SnapshotAccountIds []string
|
SnapshotAccountIds []string
|
||||||
|
RawRegion string
|
||||||
Ctx interpolate.Context
|
Ctx interpolate.Context
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *StepUpdateOMIAttributes) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
|
func (s *StepUpdateOMIAttributes) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
|
||||||
oapiconn := state.Get("oapi").(*oapi.Client)
|
config := state.Get("accessConfig").(*AccessConfig)
|
||||||
config := state.Get("clientConfig").(*oapi.Config)
|
|
||||||
ui := state.Get("ui").(packer.Ui)
|
ui := state.Get("ui").(packer.Ui)
|
||||||
omis := state.Get("omis").(map[string]string)
|
omis := state.Get("omis").(map[string]string)
|
||||||
snapshots := state.Get("snapshots").(map[string][]string)
|
snapshots := state.Get("snapshots").(map[string][]string)
|
||||||
|
@ -34,20 +33,20 @@ func (s *StepUpdateOMIAttributes) Run(_ context.Context, state multistep.StateBa
|
||||||
return multistep.ActionContinue
|
return multistep.ActionContinue
|
||||||
}
|
}
|
||||||
|
|
||||||
s.Ctx.Data = extractBuildInfo(oapiconn.GetConfig().Region, state)
|
s.Ctx.Data = extractBuildInfo(s.RawRegion, state)
|
||||||
|
|
||||||
updateSnapshoptRequest := oapi.UpdateSnapshotRequest{
|
updateSnapshoptRequest := osc.UpdateSnapshotRequest{
|
||||||
PermissionsToCreateVolume: oapi.PermissionsOnResourceCreation{
|
PermissionsToCreateVolume: osc.PermissionsOnResourceCreation{
|
||||||
Additions: oapi.PermissionsOnResource{
|
Additions: osc.PermissionsOnResource{
|
||||||
AccountIds: s.AccountIds,
|
AccountIds: s.AccountIds,
|
||||||
GlobalPermission: false,
|
GlobalPermission: false,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
updateImageRequest := oapi.UpdateImageRequest{
|
updateImageRequest := osc.UpdateImageRequest{
|
||||||
PermissionsToLaunch: oapi.PermissionsOnResourceCreation{
|
PermissionsToLaunch: osc.PermissionsOnResourceCreation{
|
||||||
Additions: oapi.PermissionsOnResource{
|
Additions: osc.PermissionsOnResource{
|
||||||
AccountIds: s.AccountIds,
|
AccountIds: s.AccountIds,
|
||||||
GlobalPermission: false,
|
GlobalPermission: false,
|
||||||
},
|
},
|
||||||
|
@ -57,26 +56,31 @@ func (s *StepUpdateOMIAttributes) Run(_ context.Context, state multistep.StateBa
|
||||||
// Updating image attributes
|
// Updating image attributes
|
||||||
for region, omi := range omis {
|
for region, omi := range omis {
|
||||||
ui.Say(fmt.Sprintf("Updating attributes on OMI (%s)...", omi))
|
ui.Say(fmt.Sprintf("Updating attributes on OMI (%s)...", omi))
|
||||||
newConfig := &oapi.Config{
|
regionconn := config.NewOSCClientByRegion(region)
|
||||||
UserAgent: config.UserAgent,
|
|
||||||
AccessKey: config.AccessKey,
|
|
||||||
SecretKey: config.SecretKey,
|
|
||||||
Service: config.Service,
|
|
||||||
Region: region, //New region
|
|
||||||
URL: config.URL,
|
|
||||||
}
|
|
||||||
|
|
||||||
skipClient := &http.Client{
|
// newConfig := &osc.Configuration{
|
||||||
Transport: &http.Transport{
|
// UserAgent: config.UserAgent,
|
||||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
// AccessKey: config.AccessKey,
|
||||||
},
|
// SecretKey: config.SecretKey,
|
||||||
}
|
// Service: config.Service,
|
||||||
|
// Region: region, //New region
|
||||||
|
// URL: config.URL,
|
||||||
|
// }
|
||||||
|
|
||||||
regionconn := oapi.NewClient(newConfig, skipClient)
|
// skipClient := &http.Client{
|
||||||
|
// Transport: &http.Transport{
|
||||||
|
// TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
||||||
|
// },
|
||||||
|
// }
|
||||||
|
|
||||||
|
//regionconn := oapi.NewClient(newConfig, skipClient)
|
||||||
|
|
||||||
ui.Message(fmt.Sprintf("Updating: %s", omi))
|
ui.Message(fmt.Sprintf("Updating: %s", omi))
|
||||||
updateImageRequest.ImageId = omi
|
updateImageRequest.ImageId = omi
|
||||||
_, err := regionconn.POST_UpdateImage(updateImageRequest)
|
_, _, err := regionconn.ImageApi.UpdateImage(context.Background(), &osc.UpdateImageOpts{
|
||||||
|
UpdateImageRequest: optional.NewInterface(updateImageRequest),
|
||||||
|
})
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err := fmt.Errorf("Error updating OMI: %s", err)
|
err := fmt.Errorf("Error updating OMI: %s", err)
|
||||||
state.Put("error", err)
|
state.Put("error", err)
|
||||||
|
@ -89,26 +93,13 @@ func (s *StepUpdateOMIAttributes) Run(_ context.Context, state multistep.StateBa
|
||||||
for region, region_snapshots := range snapshots {
|
for region, region_snapshots := range snapshots {
|
||||||
for _, snapshot := range region_snapshots {
|
for _, snapshot := range region_snapshots {
|
||||||
ui.Say(fmt.Sprintf("Updating attributes on snapshot (%s)...", snapshot))
|
ui.Say(fmt.Sprintf("Updating attributes on snapshot (%s)...", snapshot))
|
||||||
newConfig := &oapi.Config{
|
regionconn := config.NewOSCClientByRegion(region)
|
||||||
UserAgent: config.UserAgent,
|
|
||||||
AccessKey: config.AccessKey,
|
|
||||||
SecretKey: config.SecretKey,
|
|
||||||
Service: config.Service,
|
|
||||||
Region: region, //New region
|
|
||||||
URL: config.URL,
|
|
||||||
}
|
|
||||||
|
|
||||||
skipClient := &http.Client{
|
|
||||||
Transport: &http.Transport{
|
|
||||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
regionconn := oapi.NewClient(newConfig, skipClient)
|
|
||||||
|
|
||||||
ui.Message(fmt.Sprintf("Updating: %s", snapshot))
|
ui.Message(fmt.Sprintf("Updating: %s", snapshot))
|
||||||
updateSnapshoptRequest.SnapshotId = snapshot
|
updateSnapshoptRequest.SnapshotId = snapshot
|
||||||
_, err := regionconn.POST_UpdateSnapshot(updateSnapshoptRequest)
|
_, _, err := regionconn.SnapshotApi.UpdateSnapshot(context.Background(), &osc.UpdateSnapshotOpts{
|
||||||
|
UpdateSnapshotRequest: optional.NewInterface(updateSnapshoptRequest),
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err := fmt.Errorf("Error updating snapshot: %s", err)
|
err := fmt.Errorf("Error updating snapshot: %s", err)
|
||||||
state.Put("error", err)
|
state.Put("error", err)
|
||||||
|
|
Loading…
Reference in New Issue