From e0badb3fb7a41ac5ef307a4e7bfe65c063c0f8e2 Mon Sep 17 00:00:00 2001 From: Marin Salinas Date: Wed, 26 Aug 2020 12:25:46 -0500 Subject: [PATCH] refactor: update step_update_omi_attributes and create_tags to new OSC SDK --- builder/osc/bsu/builder.go | 3 +- builder/osc/common/step_create_tags.go | 58 ++++++++----------- builder/osc/common/step_update_omi.go | 79 ++++++++++++-------------- 3 files changed, 61 insertions(+), 79 deletions(-) diff --git a/builder/osc/bsu/builder.go b/builder/osc/bsu/builder.go index c9f38e70b..2a32f40ff 100644 --- a/builder/osc/bsu/builder.go +++ b/builder/osc/bsu/builder.go @@ -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("oapi", oapiconn) state.Put("osc", oscConn) - state.Put("clientConfig", clientConfig) + state.Put("accessConfig", &b.config.AccessConfig) state.Put("hook", hook) state.Put("ui", ui) @@ -200,6 +200,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack &osccommon.StepUpdateOMIAttributes{ AccountIds: b.config.OMIAccountIDs, SnapshotAccountIds: b.config.SnapshotAccountIDs, + RawRegion: b.config.RawRegion, Ctx: b.config.ctx, }, &osccommon.StepCreateTags{ diff --git a/builder/osc/common/step_create_tags.go b/builder/osc/common/step_create_tags.go index 44bdcc78c..f9a8e46b9 100644 --- a/builder/osc/common/step_create_tags.go +++ b/builder/osc/common/step_create_tags.go @@ -2,16 +2,15 @@ package common import ( "context" - "crypto/tls" "fmt" - "net/http" + "github.com/antihax/optional" "github.com/aws/aws-sdk-go/aws/awserr" retry "github.com/hashicorp/packer/common" "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 StepCreateTags struct { @@ -21,8 +20,7 @@ type StepCreateTags struct { } func (s *StepCreateTags) Run(_ context.Context, state multistep.StateBag) multistep.StepAction { - oapiconn := state.Get("oapi").(*oapi.Client) - config := state.Get("clientConfig").(*oapi.Config) + config := state.Get("accessConfig").(*AccessConfig) ui := state.Get("ui").(packer.Ui) 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 { ui.Say(fmt.Sprintf("Adding tags to OMI (%s)...", ami)) - newConfig := &oapi.Config{ - 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) + regionconn := config.NewOSCClientByRegion(region) // Retrieve image list for given OMI resourceIds := []string{ami} - imageResp, err := regionConn.POST_ReadImages(oapi.ReadImagesRequest{ - Filters: oapi.FiltersImage{ - ImageIds: resourceIds, - }, + imageResp, _, err := regionconn.ImageApi.ReadImages(context.Background(), &osc.ReadImagesOpts{ + ReadImagesRequest: optional.NewInterface(osc.ReadImagesRequest{ + Filters: osc.FiltersImage{ + ImageIds: resourceIds, + }, + }), }) if err != nil { @@ -65,14 +51,14 @@ func (s *StepCreateTags) Run(_ context.Context, state multistep.StateBag) multis 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) state.Put("error", err) ui.Error(err.Error()) return multistep.ActionHalt } - image := imageResp.OK.Images[0] + image := imageResp.Images[0] snapshotIds := []string{} // 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 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 { state.Put("error", err) ui.Error(err.Error()) @@ -95,7 +81,7 @@ func (s *StepCreateTags) Run(_ context.Context, state multistep.StateBag) multis amiTags.Report(ui) 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 { state.Put("error", err) 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 err = retry.Retry(0.2, 30, 11, func(_ uint) (bool, error) { // Tag images and snapshots - _, err := regionConn.POST_CreateTags(oapi.CreateTagsRequest{ - ResourceIds: resourceIds, - Tags: amiTags, + _, _, err := regionconn.TagApi.CreateTags(context.Background(), &osc.CreateTagsOpts{ + CreateTagsRequest: optional.NewInterface(osc.CreateTagsRequest{ + ResourceIds: resourceIds, + Tags: amiTags, + }), }) if awsErr, ok := err.(awserr.Error); ok { if awsErr.Code() == "InvalidOMIID.NotFound" || @@ -119,9 +107,11 @@ func (s *StepCreateTags) Run(_ context.Context, state multistep.StateBag) multis // Override tags on snapshots if len(snapshotTags) > 0 { - _, err = regionConn.POST_CreateTags(oapi.CreateTagsRequest{ - ResourceIds: snapshotIds, - Tags: snapshotTags, + _, _, err = regionconn.TagApi.CreateTags(context.Background(), &osc.CreateTagsOpts{ + CreateTagsRequest: optional.NewInterface(osc.CreateTagsRequest{ + ResourceIds: snapshotIds, + Tags: snapshotTags, + }), }) } if err == nil { diff --git a/builder/osc/common/step_update_omi.go b/builder/osc/common/step_update_omi.go index 41ce69a89..2be6b4e4f 100644 --- a/builder/osc/common/step_update_omi.go +++ b/builder/osc/common/step_update_omi.go @@ -2,25 +2,24 @@ package common import ( "context" - "crypto/tls" "fmt" - "net/http" + "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 StepUpdateOMIAttributes struct { AccountIds []string SnapshotAccountIds []string + RawRegion string Ctx interpolate.Context } func (s *StepUpdateOMIAttributes) Run(_ context.Context, state multistep.StateBag) multistep.StepAction { - oapiconn := state.Get("oapi").(*oapi.Client) - config := state.Get("clientConfig").(*oapi.Config) + config := state.Get("accessConfig").(*AccessConfig) ui := state.Get("ui").(packer.Ui) omis := state.Get("omis").(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 } - s.Ctx.Data = extractBuildInfo(oapiconn.GetConfig().Region, state) + s.Ctx.Data = extractBuildInfo(s.RawRegion, state) - updateSnapshoptRequest := oapi.UpdateSnapshotRequest{ - PermissionsToCreateVolume: oapi.PermissionsOnResourceCreation{ - Additions: oapi.PermissionsOnResource{ + updateSnapshoptRequest := osc.UpdateSnapshotRequest{ + PermissionsToCreateVolume: osc.PermissionsOnResourceCreation{ + Additions: osc.PermissionsOnResource{ AccountIds: s.AccountIds, GlobalPermission: false, }, }, } - updateImageRequest := oapi.UpdateImageRequest{ - PermissionsToLaunch: oapi.PermissionsOnResourceCreation{ - Additions: oapi.PermissionsOnResource{ + updateImageRequest := osc.UpdateImageRequest{ + PermissionsToLaunch: osc.PermissionsOnResourceCreation{ + Additions: osc.PermissionsOnResource{ AccountIds: s.AccountIds, GlobalPermission: false, }, @@ -57,26 +56,31 @@ func (s *StepUpdateOMIAttributes) Run(_ context.Context, state multistep.StateBa // Updating image attributes for region, omi := range omis { ui.Say(fmt.Sprintf("Updating attributes on OMI (%s)...", omi)) - newConfig := &oapi.Config{ - UserAgent: config.UserAgent, - AccessKey: config.AccessKey, - SecretKey: config.SecretKey, - Service: config.Service, - Region: region, //New region - URL: config.URL, - } + regionconn := config.NewOSCClientByRegion(region) - skipClient := &http.Client{ - Transport: &http.Transport{ - TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, - }, - } + // newConfig := &osc.Configuration{ + // UserAgent: config.UserAgent, + // 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)) updateImageRequest.ImageId = omi - _, err := regionconn.POST_UpdateImage(updateImageRequest) + _, _, err := regionconn.ImageApi.UpdateImage(context.Background(), &osc.UpdateImageOpts{ + UpdateImageRequest: optional.NewInterface(updateImageRequest), + }) + if err != nil { err := fmt.Errorf("Error updating OMI: %s", 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 _, snapshot := range region_snapshots { ui.Say(fmt.Sprintf("Updating attributes on snapshot (%s)...", snapshot)) - newConfig := &oapi.Config{ - 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) + regionconn := config.NewOSCClientByRegion(region) ui.Message(fmt.Sprintf("Updating: %s", snapshot)) updateSnapshoptRequest.SnapshotId = snapshot - _, err := regionconn.POST_UpdateSnapshot(updateSnapshoptRequest) + _, _, err := regionconn.SnapshotApi.UpdateSnapshot(context.Background(), &osc.UpdateSnapshotOpts{ + UpdateSnapshotRequest: optional.NewInterface(updateSnapshoptRequest), + }) if err != nil { err := fmt.Errorf("Error updating snapshot: %s", err) state.Put("error", err)