Skip to content

Commit

Permalink
update context.background in api calls
Browse files Browse the repository at this point in the history
Signed-off-by: hanen mizouni <hanen.mizouni@outscale.com>
  • Loading branch information
outscale-hmi committed Dec 4, 2022
1 parent 4b5dfd7 commit dab8463
Show file tree
Hide file tree
Showing 32 changed files with 131 additions and 139 deletions.
3 changes: 1 addition & 2 deletions builder/osc/bsu/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
"github.com/hashicorp/packer-plugin-sdk/template/config"
"github.com/hashicorp/packer-plugin-sdk/template/interpolate"
oscgo "github.com/outscale/osc-sdk-go/v2"
osccommon "github.com/outscale/packer-plugin-outscale/builder/osc/common"
)

Expand Down Expand Up @@ -86,7 +85,7 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, []string, error) {
}

func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook) (packersdk.Artifact, error) {
var oscConn *oscgo.APIClient
var oscConn *osccommon.OscClient
var err error
if oscConn, err = b.config.NewOSCClient(); err != nil {
return nil, err
Expand Down
12 changes: 6 additions & 6 deletions builder/osc/bsu/step_create_omi.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type stepCreateOMI struct {

func (s *stepCreateOMI) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
config := state.Get("config").(*Config)
oscconn := state.Get("osc").(*oscgo.APIClient)
oscconn := state.Get("osc").(*osccommon.OscClient)
vm := state.Get("vm").(oscgo.Vm)
ui := state.Get("ui").(packersdk.Ui)

Expand All @@ -36,7 +36,7 @@ func (s *stepCreateOMI) Run(ctx context.Context, state multistep.StateBag) multi
createOpts.Description = &config.OMIDescription
}

resp, _, err := oscconn.ImageApi.CreateImage(context.Background()).CreateImageRequest(createOpts).Execute()
resp, _, err := oscconn.Api.ImageApi.CreateImage(oscconn.Auth).CreateImageRequest(createOpts).Execute()
if err != nil || resp.GetImage().ImageId == nil {
err := fmt.Errorf("Error creating OMI: %s", err)
state.Put("error", err)
Expand All @@ -59,7 +59,7 @@ func (s *stepCreateOMI) Run(ctx context.Context, state multistep.StateBag) multi
req := oscgo.ReadImagesRequest{
Filters: &oscgo.FiltersImage{ImageIds: &[]string{image.GetImageId()}},
}
imagesResp, _, err := oscconn.ImageApi.ReadImages(context.Background()).ReadImagesRequest(req).Execute()
imagesResp, _, err := oscconn.Api.ImageApi.ReadImages(oscconn.Auth).ReadImagesRequest(req).Execute()
if err != nil {
log.Printf("Unable to determine reason waiting for OMI failed: %s", err)
err = fmt.Errorf("Unknown error waiting for OMI")
Expand All @@ -76,7 +76,7 @@ func (s *stepCreateOMI) Run(ctx context.Context, state multistep.StateBag) multi
req := oscgo.ReadImagesRequest{
Filters: &oscgo.FiltersImage{ImageIds: &[]string{image.GetImageId()}},
}
imagesResp, _, err := oscconn.ImageApi.ReadImages(context.Background()).ReadImagesRequest(req).Execute()
imagesResp, _, err := oscconn.Api.ImageApi.ReadImages(oscconn.Auth).ReadImagesRequest(req).Execute()
if err != nil {
err := fmt.Errorf("Error searching for OMI: %s", err)
state.Put("error", err)
Expand Down Expand Up @@ -108,12 +108,12 @@ func (s *stepCreateOMI) Cleanup(state multistep.StateBag) {
return
}

oscconn := state.Get("osc").(*oscgo.APIClient)
oscconn := state.Get("osc").(*osccommon.OscClient)
ui := state.Get("ui").(packersdk.Ui)

ui.Say("Deregistering the OMI because cancellation or error...")
DeleteOpts := oscgo.DeleteImageRequest{ImageId: s.image.GetImageId()}
_, _, err := oscconn.ImageApi.DeleteImage(context.Background()).DeleteImageRequest(DeleteOpts).Execute()
_, _, err := oscconn.Api.ImageApi.DeleteImage(oscconn.Auth).DeleteImageRequest(DeleteOpts).Execute()
if err != nil {
ui.Error(fmt.Sprintf("Error Deleting OMI, may still be around: %s", err))
return
Expand Down
3 changes: 1 addition & 2 deletions builder/osc/bsusurrogate/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
"github.com/hashicorp/packer-plugin-sdk/template/config"
"github.com/hashicorp/packer-plugin-sdk/template/interpolate"
oscgo "github.com/outscale/osc-sdk-go/v2"
osccommon "github.com/outscale/packer-plugin-outscale/builder/osc/common"
)

Expand Down Expand Up @@ -103,7 +102,7 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, []string, error) {
}

func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook) (packersdk.Artifact, error) {
var oscConn *oscgo.APIClient
var oscConn *osccommon.OscClient
var err error

if oscConn, err = b.config.NewOSCClient(); err != nil {
Expand Down
10 changes: 5 additions & 5 deletions builder/osc/bsusurrogate/step_register_omi.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type StepRegisterOMI struct {

func (s *StepRegisterOMI) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
config := state.Get("config").(*Config)
oscconn := state.Get("osc").(*oscgo.APIClient)
oscconn := state.Get("osc").(*(osccommon.OscClient))
snapshotIds := state.Get("snapshot_ids").(map[string]string)
ui := state.Get("ui").(packersdk.Ui)

Expand All @@ -40,7 +40,7 @@ func (s *StepRegisterOMI) Run(ctx context.Context, state multistep.StateBag) mul
if config.OMIDescription != "" {
registerOpts.Description = &config.OMIDescription
}
registerResp, _, err := oscconn.ImageApi.CreateImage(context.Background()).CreateImageRequest(registerOpts).Execute()
registerResp, _, err := oscconn.Api.ImageApi.CreateImage(oscconn.Auth).CreateImageRequest(registerOpts).Execute()
if err != nil {
state.Put("error", fmt.Errorf("Error registering OMI: %s", err))
ui.Error(state.Get("error").(error).Error())
Expand All @@ -65,7 +65,7 @@ func (s *StepRegisterOMI) Run(ctx context.Context, state multistep.StateBag) mul
filterReq := oscgo.ReadImagesRequest{
Filters: &oscgo.FiltersImage{ImageIds: &[]string{*registerResp.GetImage().ImageId}},
}
imagesResp, _, err := oscconn.ImageApi.ReadImages(context.Background()).ReadImagesRequest(filterReq).Execute()
imagesResp, _, err := oscconn.Api.ImageApi.ReadImages(oscconn.Auth).ReadImagesRequest(filterReq).Execute()
if err != nil {
err := fmt.Errorf("Error searching for OMI: %s", err)
state.Put("error", err)
Expand Down Expand Up @@ -97,12 +97,12 @@ func (s *StepRegisterOMI) Cleanup(state multistep.StateBag) {
return
}

oscconn := state.Get("osc").(*oscgo.APIClient)
oscconn := state.Get("osc").(*(osccommon.OscClient))
ui := state.Get("ui").(packersdk.Ui)

ui.Say("Deregistering the OMI because cancellation or error...")
deregisterOpts := oscgo.DeleteImageRequest{ImageId: *s.image.ImageId}
_, _, err := oscconn.ImageApi.DeleteImage(context.Background()).DeleteImageRequest(deregisterOpts).Execute()
_, _, err := oscconn.Api.ImageApi.DeleteImage(oscconn.Auth).DeleteImageRequest(deregisterOpts).Execute()
if err != nil {
ui.Error(fmt.Sprintf("Error deregistering OMI, may still be around: %s", err))
return
Expand Down
8 changes: 4 additions & 4 deletions builder/osc/bsusurrogate/step_snapshop_volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type StepSnapshotVolumes struct {
}

func (s *StepSnapshotVolumes) snapshotVolume(ctx context.Context, deviceName string, state multistep.StateBag) error {
oscconn := state.Get("osc").(*oscgo.APIClient)
oscconn := state.Get("osc").(*osccommon.OscClient)
ui := state.Get("ui").(packersdk.Ui)
vm := state.Get("vm").(oscgo.Vm)

Expand All @@ -45,7 +45,7 @@ func (s *StepSnapshotVolumes) snapshotVolume(ctx context.Context, deviceName str
Description: &description,
VolumeId: &volumeId,
}
createSnapResp, _, err := oscconn.SnapshotApi.CreateSnapshot(context.Background()).CreateSnapshotRequest(request).Execute()
createSnapResp, _, err := oscconn.Api.SnapshotApi.CreateSnapshot(oscconn.Auth).CreateSnapshotRequest(request).Execute()
if err != nil {
return err
}
Expand Down Expand Up @@ -96,12 +96,12 @@ func (s *StepSnapshotVolumes) Cleanup(state multistep.StateBag) {
_, halted := state.GetOk(multistep.StateHalted)

if cancelled || halted {
oscconn := state.Get("osc").(*oscgo.APIClient)
oscconn := state.Get("osc").(*osccommon.OscClient)
ui := state.Get("ui").(packersdk.Ui)
ui.Say("Removing snapshots since we cancelled or halted...")
for _, snapshotID := range s.snapshotIds {
request := oscgo.DeleteSnapshotRequest{SnapshotId: snapshotID}
_, _, err := oscconn.SnapshotApi.DeleteSnapshot(context.Background()).DeleteSnapshotRequest(request).Execute()
_, _, err := oscconn.Api.SnapshotApi.DeleteSnapshot(oscconn.Auth).DeleteSnapshotRequest(request).Execute()
if err != nil {
ui.Error(fmt.Sprintf("Error: %s", err))
}
Expand Down
7 changes: 4 additions & 3 deletions builder/osc/bsuvolume/artifact.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package bsuvolume

import (
"context"
"fmt"
"log"
"sort"
Expand All @@ -10,6 +9,7 @@ import (
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
registryimage "github.com/hashicorp/packer-plugin-sdk/packer/registry/image"
oscgo "github.com/outscale/osc-sdk-go/v2"
osccommon "github.com/outscale/packer-plugin-outscale/builder/osc/common"
)

// map of region to list of volume IDs
Expand All @@ -30,7 +30,7 @@ type Artifact struct {
BuilderIdValue string

// Client connection for performing API stuff.
Conn *oscgo.APIClient
Conn *osccommon.OscClient

// StateData should store data such as GeneratedData
// to be shared with post-processors
Expand Down Expand Up @@ -87,7 +87,8 @@ func (a *Artifact) Destroy() error {
input := oscgo.DeleteVolumeRequest{
VolumeId: volumeID,
}
_, _, err := a.Conn.VolumeApi.DeleteVolume(context.Background()).DeleteVolumeRequest(input).Execute()
oscClient := *(a.Conn)
_, _, err := oscClient.Api.VolumeApi.DeleteVolume(oscClient.Auth).DeleteVolumeRequest(input).Execute()
if err != nil {
errors = append(errors, err)
}
Expand Down
8 changes: 4 additions & 4 deletions builder/osc/bsuvolume/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
"github.com/hashicorp/packer-plugin-sdk/template/config"
"github.com/hashicorp/packer-plugin-sdk/template/interpolate"
oscgo "github.com/outscale/osc-sdk-go/v2"
osccommon "github.com/outscale/packer-plugin-outscale/builder/osc/common"
)

Expand Down Expand Up @@ -100,9 +99,10 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
// },
// }

var oscConn *oscgo.APIClient
var err error
if oscConn, err = b.config.NewOSCClient(); err != nil {
//var oscConn *osccommon.OscClient
//var err error
oscConn, err := b.config.NewOSCClient()
if err != nil {
return nil, err
}

Expand Down
4 changes: 2 additions & 2 deletions builder/osc/chroot/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
"github.com/hashicorp/packer-plugin-sdk/template/config"
"github.com/hashicorp/packer-plugin-sdk/template/interpolate"
oscgo "github.com/outscale/osc-sdk-go/v2"
osccommon "github.com/outscale/packer-plugin-outscale/builder/osc/common"
)

Expand Down Expand Up @@ -189,7 +188,8 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
return nil, errors.New("The outscale-chroot builder only works on Linux environments.")
}

var oscConn *oscgo.APIClient
var oscConn *osccommon.OscClient
//var oscConn *oscgo.APIClient
var err error
if oscConn, err = b.config.NewOSCClient(); err != nil {
return nil, err
Expand Down
5 changes: 3 additions & 2 deletions builder/osc/chroot/step_create_omi.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ type StepCreateOMI struct {

func (s *StepCreateOMI) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
config := state.Get("config").(*Config)
osconn := state.Get("osc").(*oscgo.APIClient)
osconn := state.Get("osc").(*(osccommon.OscClient))
snapshotId := state.Get("snapshot_id").(string)
ui := state.Get("ui").(packersdk.Ui)

//regionconn := config.NewOSCClientByRegion(region)
ui.Say("Creating the OMI...")

var (
Expand Down Expand Up @@ -79,7 +80,7 @@ func (s *StepCreateOMI) Run(ctx context.Context, state multistep.StateBag) multi
registerOpts.Description = &config.OMIDescription
}

registerResp, _, err := osconn.ImageApi.CreateImage(context.Background()).CreateImageRequest(registerOpts).Execute()
registerResp, _, err := osconn.Api.ImageApi.CreateImage(osconn.Auth).CreateImageRequest(registerOpts).Execute()
if err != nil {
state.Put("error", fmt.Errorf("Error registering OMI: %s", err))
ui.Error(state.Get("error").(error).Error())
Expand Down
8 changes: 4 additions & 4 deletions builder/osc/chroot/step_create_volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type StepCreateVolume struct {

func (s *StepCreateVolume) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
config := state.Get("config").(*Config)
oscconn := state.Get("osc").(*oscgo.APIClient)
oscconn := state.Get("osc").(*osccommon.OscClient)
vm := state.Get("vm").(oscgo.Vm)
ui := state.Get("ui").(packersdk.Ui)

Expand Down Expand Up @@ -85,7 +85,7 @@ func (s *StepCreateVolume) Run(ctx context.Context, state multistep.StateBag) mu

log.Printf("Create args: %+v", createVolume)

createVolumeResp, _, err := oscconn.VolumeApi.CreateVolume(context.Background()).CreateVolumeRequest(*createVolume).Execute()
createVolumeResp, _, err := oscconn.Api.VolumeApi.CreateVolume(oscconn.Auth).CreateVolumeRequest(*createVolume).Execute()
if err != nil {
err := fmt.Errorf("Error creating root volume: %s", err)
state.Put("error", err)
Expand Down Expand Up @@ -125,11 +125,11 @@ func (s *StepCreateVolume) Cleanup(state multistep.StateBag) {
return
}

oscconn := state.Get("osc").(*oscgo.APIClient)
oscconn := state.Get("osc").(*osccommon.OscClient)
ui := state.Get("ui").(packersdk.Ui)

ui.Say("Deleting the created BSU volume...")
_, _, err := oscconn.VolumeApi.DeleteVolume(context.Background()).DeleteVolumeRequest(oscgo.DeleteVolumeRequest{VolumeId: s.volumeId}).Execute()
_, _, err := oscconn.Api.VolumeApi.DeleteVolume(oscconn.Auth).DeleteVolumeRequest(oscgo.DeleteVolumeRequest{VolumeId: s.volumeId}).Execute()
if err != nil {
ui.Error(fmt.Sprintf("Error deleting BSU volume: %s", err))
}
Expand Down
8 changes: 4 additions & 4 deletions builder/osc/chroot/step_link_volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type StepLinkVolume struct {
}

func (s *StepLinkVolume) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
oscconn := state.Get("osc").(*oscgo.APIClient)
oscconn := state.Get("osc").(*osccommon.OscClient)
device := state.Get("device").(string)
vm := state.Get("vm").(oscgo.Vm)
ui := state.Get("ui").(packersdk.Ui)
Expand All @@ -39,7 +39,7 @@ func (s *StepLinkVolume) Run(ctx context.Context, state multistep.StateBag) mult
VmId: *vm.VmId,
VolumeId: volumeId,
}
_, _, err := oscconn.VolumeApi.LinkVolume(context.Background()).LinkVolumeRequest(opts).Execute()
_, _, err := oscconn.Api.VolumeApi.LinkVolume(oscconn.Auth).LinkVolumeRequest(opts).Execute()
if err != nil {
err := fmt.Errorf("Error attaching volume: %s", err)
state.Put("error", err)
Expand Down Expand Up @@ -76,14 +76,14 @@ func (s *StepLinkVolume) CleanupFunc(state multistep.StateBag) error {
return nil
}

oscconn := state.Get("osc").(*oscgo.APIClient)
oscconn := state.Get("osc").(*osccommon.OscClient)
ui := state.Get("ui").(packersdk.Ui)

ui.Say("Detaching BSU volume...")
opts := oscgo.UnlinkVolumeRequest{
VolumeId: s.volumeId,
}
_, _, err := oscconn.VolumeApi.UnlinkVolume(context.Background()).UnlinkVolumeRequest(opts).Execute()
_, _, err := oscconn.Api.VolumeApi.UnlinkVolume(oscconn.Auth).UnlinkVolumeRequest(opts).Execute()
if err != nil {
return fmt.Errorf("Error detaching BSU volume: %s", err)
}
Expand Down
8 changes: 4 additions & 4 deletions builder/osc/chroot/step_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type StepSnapshot struct {
}

func (s *StepSnapshot) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
oscconn := state.Get("osc").(*oscgo.APIClient)
oscconn := state.Get("osc").(*osccommon.OscClient)
ui := state.Get("ui").(packersdk.Ui)
volumeId := state.Get("volume_id").(string)

Expand All @@ -33,7 +33,7 @@ func (s *StepSnapshot) Run(ctx context.Context, state multistep.StateBag) multis
Description: &description,
VolumeId: &volumeId,
}
createSnapResp, _, err := oscconn.SnapshotApi.CreateSnapshot(context.Background()).CreateSnapshotRequest(request).Execute()
createSnapResp, _, err := oscconn.Api.SnapshotApi.CreateSnapshot(oscconn.Auth).CreateSnapshotRequest(request).Execute()
if err != nil {
err := fmt.Errorf("Error creating snapshot: %s", err)
state.Put("error", err)
Expand Down Expand Up @@ -73,11 +73,11 @@ func (s *StepSnapshot) Cleanup(state multistep.StateBag) {
_, halted := state.GetOk(multistep.StateHalted)

if cancelled || halted {
oscconn := state.Get("osc").(*oscgo.APIClient)
oscconn := state.Get("osc").(*osccommon.OscClient)
ui := state.Get("ui").(packersdk.Ui)
ui.Say("Removing snapshot since we cancelled or halted...")
request := oscgo.DeleteSnapshotRequest{SnapshotId: s.snapshotId}
_, _, err := oscconn.SnapshotApi.DeleteSnapshot(context.Background()).DeleteSnapshotRequest(request).Execute()
_, _, err := oscconn.Api.SnapshotApi.DeleteSnapshot(oscconn.Auth).DeleteSnapshotRequest(request).Execute()
if err != nil {
ui.Error(fmt.Sprintf("Error: %s", err))
}
Expand Down
16 changes: 8 additions & 8 deletions builder/osc/common/access_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ func getValueFromEnvVariables(envVariables []string) (string, bool) {
}

type OscClient struct {
auth context.Context
api *osc.APIClient
Auth context.Context
Api *oscgo.APIClient
}

// NewOSCClient retrieves the Outscale OSC-SDK client
Expand Down Expand Up @@ -130,19 +130,19 @@ func (c *AccessConfig) NewOSCClientByRegion(region string) *OscClient {

skipClient.Transport = NewTransport(c.AccessKey, c.SecretKey, c.RawRegion, skipClient.Transport)

config := osc.NewConfiguration()
config := oscgo.NewConfiguration()
config.Debug = true
config.UserAgent = fmt.Sprintf("packer-plugin-outscale/%s", version.PluginVersion.String())
auth := context.WithValue(context.Background(), osc.ContextAWSv4, osc.AWSv4{
auth := context.WithValue(context.Background(), oscgo.ContextAWSv4, osc.AWSv4{
AccessKey: os.Getenv("OSC_ACCESS_KEY"),
SecretKey: os.Getenv("OSC_SECRET_KEY"),
})
auth = context.WithValue(auth, osc.ContextServerIndex, 0)
auth = context.WithValue(auth, osc.ContextServerVariables, map[string]string{"region": os.Getenv("OSC_REGION")})
auth = context.WithValue(auth, oscgo.ContextServerIndex, 0)
auth = context.WithValue(auth, oscgo.ContextServerVariables, map[string]string{"region": os.Getenv("OSC_REGION")})

oscClient := &OscClient{
api: osc.NewAPIClient(config),
auth: auth,
Api: oscgo.NewAPIClient(config),
Auth: auth,
}
return oscClient
}
Expand Down
Loading

0 comments on commit dab8463

Please sign in to comment.