:chore: implemented OSC API in pre_public_ip step

This commit is contained in:
PacoDw 2020-08-20 13:59:25 -05:00
parent 43e9d43ebd
commit 86966ecfee
1 changed files with 27 additions and 17 deletions

View File

@ -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))
}
}