packer-cn/builder/oracle/classic/step_create_ip_reservation.go

62 lines
1.7 KiB
Go
Raw Normal View History

2018-01-12 19:06:03 -05:00
package classic
import (
2018-01-25 17:43:55 -05:00
"context"
"fmt"
"github.com/hashicorp/go-oracle-terraform/compute"
"github.com/hashicorp/packer/common/uuid"
2018-01-25 17:45:09 -05:00
"github.com/hashicorp/packer/helper/multistep"
2018-01-12 19:06:03 -05:00
"github.com/hashicorp/packer/packer"
)
type stepCreateIPReservation struct{}
2018-01-25 17:42:39 -05:00
func (s *stepCreateIPReservation) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
2018-01-12 19:06:03 -05:00
ui := state.Get("ui").(packer.Ui)
2018-01-26 16:12:35 -05:00
config := state.Get("config").(*Config)
2018-10-24 17:08:11 -04:00
client := state.Get("client").(*compute.Client)
iprClient := client.IPReservations()
// TODO: add optional Name and Tags
2018-01-26 16:12:35 -05:00
ipresName := fmt.Sprintf("ipres_%s_%s", config.ImageName, uuid.TimeOrderedUUID())
2018-01-26 18:20:12 -05:00
ui.Message(fmt.Sprintf("Creating temporary IP reservation: %s", ipresName))
2018-01-26 16:12:35 -05:00
2018-01-17 16:07:41 -05:00
IPInput := &compute.CreateIPReservationInput{
ParentPool: compute.PublicReservationPool,
Permanent: true,
2018-01-26 16:12:35 -05:00
Name: ipresName,
}
2018-01-17 16:07:41 -05:00
ipRes, err := iprClient.CreateIPReservation(IPInput)
2018-01-23 17:16:51 -05:00
if err != nil {
2018-01-23 17:16:51 -05:00
err := fmt.Errorf("Error creating IP Reservation: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
2018-01-16 19:55:39 -05:00
state.Put("instance_ip", ipRes.IP)
2018-01-26 16:12:35 -05:00
state.Put("ipres_name", ipresName)
return multistep.ActionContinue
2018-01-12 19:06:03 -05:00
}
2018-01-17 16:07:41 -05:00
func (s *stepCreateIPReservation) Cleanup(state multistep.StateBag) {
ipResName, ok := state.GetOk("ipres_name")
if !ok {
return
}
2018-01-26 18:18:33 -05:00
ui := state.Get("ui").(packer.Ui)
2018-01-26 18:20:12 -05:00
ui.Say("Cleaning up IP reservations...")
2018-10-24 17:08:11 -04:00
client := state.Get("client").(*compute.Client)
2018-01-26 18:18:33 -05:00
input := compute.DeleteIPReservationInput{Name: ipResName.(string)}
2018-01-26 18:18:33 -05:00
ipClient := client.IPReservations()
err := ipClient.DeleteIPReservation(&input)
if err != nil {
fmt.Printf("error deleting IP reservation: %s", err.Error())
}
2018-01-17 16:07:41 -05:00
}