From 86966ecfee42e90e0341ed7fcebb1426eb846cf7 Mon Sep 17 00:00:00 2001 From: PacoDw <__pm@outlook.com> Date: Thu, 20 Aug 2020 13:59:25 -0500 Subject: [PATCH] :chore: implemented OSC API in pre_public_ip step --- builder/osc/common/step_public_ip.go | 44 +++++++++++++++++----------- 1 file changed, 27 insertions(+), 17 deletions(-) diff --git a/builder/osc/common/step_public_ip.go b/builder/osc/common/step_public_ip.go index 9f26efb12..9702a0b91 100644 --- a/builder/osc/common/step_public_ip.go +++ b/builder/osc/common/step_public_ip.go @@ -4,10 +4,11 @@ import ( "context" "fmt" + "github.com/antihax/optional" "github.com/hashicorp/packer/helper/communicator" "github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/packer" - "github.com/outscale/osc-go/oapi" + "github.com/outscale/osc-sdk-go/osc" ) type StepPublicIp struct { @@ -20,20 +21,24 @@ type StepPublicIp struct { } func (s *StepPublicIp) Run(_ context.Context, state multistep.StateBag) multistep.StepAction { - ui := state.Get("ui").(packer.Ui) - oapiconn := state.Get("oapi").(*oapi.Client) - netId := state.Get("net_id").(string) - subnetId := state.Get("subnet_id").(string) + var ( + ui = state.Get("ui").(packer.Ui) + conn = state.Get("osc").(*osc.APIClient) + ) + + if !s.AssociatePublicIpAddress { - if netId == "" || subnetId == "" || !s.AssociatePublicIpAddress { // In this case, we are in the public Cloud, so we'll // not explicitely allocate a public IP. return multistep.ActionContinue } - ui.Say(fmt.Sprintf("Creating temporary PublicIp for instance in subnet %s (net %s)", subnetId, netId)) + ui.Say("Creating temporary PublicIp for instance ") + + resp, _, err := conn.PublicIpApi.CreatePublicIp(context.Background(), &osc.CreatePublicIpOpts{ + CreatePublicIpRequest: optional.NewInterface(osc.CreatePublicIpRequest{}), + }) - publicIpResp, err := oapiconn.POST_CreatePublicIp(oapi.CreatePublicIpRequest{}) if err != nil { state.Put("error", fmt.Errorf("Error creating temporary PublicIp: %s", err)) return multistep.ActionHalt @@ -43,8 +48,8 @@ func (s *StepPublicIp) Run(_ context.Context, state multistep.StateBag) multiste s.doCleanup = true // Set some data for use in future steps - s.publicIpId = publicIpResp.OK.PublicIp.PublicIpId - state.Put("publicip_id", publicIpResp.OK.PublicIp.PublicIpId) + s.publicIpId = resp.PublicIp.PublicIpId + state.Put("publicip_id", resp.PublicIp.PublicIpId) return multistep.ActionContinue } @@ -54,15 +59,20 @@ func (s *StepPublicIp) Cleanup(state multistep.StateBag) { return } - oapiconn := state.Get("oapi").(*oapi.Client) - ui := state.Get("ui").(packer.Ui) + var ( + conn = state.Get("osc").(*osc.APIClient) + ui = state.Get("ui").(packer.Ui) + ) // Remove the Public IP ui.Say("Deleting temporary PublicIp...") - _, err := oapiconn.POST_DeletePublicIp(oapi.DeletePublicIpRequest{PublicIpId: s.publicIpId}) - if err != nil { - ui.Error(fmt.Sprintf( - "Error cleaning up PublicIp. Please delete the PublicIp manually: %s", s.publicIpId)) - } + _, _, err := conn.PublicIpApi.DeletePublicIp(context.Background(), &osc.DeletePublicIpOpts{ + DeletePublicIpRequest: optional.NewInterface(osc.DeletePublicIpRequest{ + PublicIpId: s.publicIpId, + }), + }) + if err != nil { + ui.Error(fmt.Sprintf("Error cleaning up PublicIp. Please delete the PublicIp manually: %s", s.publicIpId)) + } }