Merge pull request #4 from marinsalinas/step_public_ip

Step public ip
This commit is contained in:
Marin Salinas 2020-08-20 18:04:03 -05:00 committed by GitHub
commit d9a7626249
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 18 deletions

View File

@ -47,7 +47,8 @@ const testBuilderAccBasic = `
"vm_type": "t2.micro", "vm_type": "t2.micro",
"source_omi": "ami-abe953fa", "source_omi": "ami-abe953fa",
"ssh_username": "outscale", "ssh_username": "outscale",
"omi_name": "packer-test {{timestamp}}" "omi_name": "packer-test {{timestamp}}",
"associate_public_ip_address": true
}] }]
} }
` `

View File

@ -4,10 +4,11 @@ import (
"context" "context"
"fmt" "fmt"
"github.com/antihax/optional"
"github.com/hashicorp/packer/helper/communicator" "github.com/hashicorp/packer/helper/communicator"
"github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/packer"
"github.com/outscale/osc-go/oapi" "github.com/outscale/osc-sdk-go/osc"
) )
type StepPublicIp struct { type StepPublicIp struct {
@ -20,20 +21,24 @@ type StepPublicIp struct {
} }
func (s *StepPublicIp) Run(_ context.Context, state multistep.StateBag) multistep.StepAction { func (s *StepPublicIp) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
ui := state.Get("ui").(packer.Ui) var (
oapiconn := state.Get("oapi").(*oapi.Client) ui = state.Get("ui").(packer.Ui)
netId := state.Get("net_id").(string) conn = state.Get("osc").(*osc.APIClient)
subnetId := state.Get("subnet_id").(string) )
if !s.AssociatePublicIpAddress {
if netId == "" || subnetId == "" || !s.AssociatePublicIpAddress {
// In this case, we are in the public Cloud, so we'll // In this case, we are in the public Cloud, so we'll
// not explicitely allocate a public IP. // not explicitely allocate a public IP.
return multistep.ActionContinue 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 { if err != nil {
state.Put("error", fmt.Errorf("Error creating temporary PublicIp: %s", err)) state.Put("error", fmt.Errorf("Error creating temporary PublicIp: %s", err))
return multistep.ActionHalt return multistep.ActionHalt
@ -43,8 +48,8 @@ func (s *StepPublicIp) Run(_ context.Context, state multistep.StateBag) multiste
s.doCleanup = true s.doCleanup = true
// Set some data for use in future steps // Set some data for use in future steps
s.publicIpId = publicIpResp.OK.PublicIp.PublicIpId s.publicIpId = resp.PublicIp.PublicIpId
state.Put("publicip_id", publicIpResp.OK.PublicIp.PublicIpId) state.Put("publicip_id", resp.PublicIp.PublicIpId)
return multistep.ActionContinue return multistep.ActionContinue
} }
@ -54,15 +59,20 @@ func (s *StepPublicIp) Cleanup(state multistep.StateBag) {
return return
} }
oapiconn := state.Get("oapi").(*oapi.Client) var (
ui := state.Get("ui").(packer.Ui) conn = state.Get("osc").(*osc.APIClient)
ui = state.Get("ui").(packer.Ui)
)
// Remove the Public IP // Remove the Public IP
ui.Say("Deleting temporary PublicIp...") ui.Say("Deleting temporary PublicIp...")
_, err := oapiconn.POST_DeletePublicIp(oapi.DeletePublicIpRequest{PublicIpId: s.publicIpId}) _, _, err := conn.PublicIpApi.DeletePublicIp(context.Background(), &osc.DeletePublicIpOpts{
if err != nil { DeletePublicIpRequest: optional.NewInterface(osc.DeletePublicIpRequest{
ui.Error(fmt.Sprintf( PublicIpId: s.publicIpId,
"Error cleaning up PublicIp. Please delete the PublicIp manually: %s", s.publicIpId)) }),
} })
if err != nil {
ui.Error(fmt.Sprintf("Error cleaning up PublicIp. Please delete the PublicIp manually: %s", s.publicIpId))
}
} }