github.phpd.cn/hashicorp/packer@v1.3.2/builder/oracle/classic/step_create_ip_reservation.go (about)

     1  package classic
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/hashicorp/go-oracle-terraform/compute"
     8  	"github.com/hashicorp/packer/common/uuid"
     9  	"github.com/hashicorp/packer/helper/multistep"
    10  	"github.com/hashicorp/packer/packer"
    11  )
    12  
    13  type stepCreateIPReservation struct{}
    14  
    15  func (s *stepCreateIPReservation) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
    16  	ui := state.Get("ui").(packer.Ui)
    17  
    18  	config := state.Get("config").(*Config)
    19  	client := state.Get("client").(*compute.ComputeClient)
    20  	iprClient := client.IPReservations()
    21  	// TODO: add optional Name and Tags
    22  
    23  	ipresName := fmt.Sprintf("ipres_%s_%s", config.ImageName, uuid.TimeOrderedUUID())
    24  	ui.Message(fmt.Sprintf("Creating temporary IP reservation: %s", ipresName))
    25  
    26  	IPInput := &compute.CreateIPReservationInput{
    27  		ParentPool: compute.PublicReservationPool,
    28  		Permanent:  true,
    29  		Name:       ipresName,
    30  	}
    31  	ipRes, err := iprClient.CreateIPReservation(IPInput)
    32  
    33  	if err != nil {
    34  		err := fmt.Errorf("Error creating IP Reservation: %s", err)
    35  		state.Put("error", err)
    36  		ui.Error(err.Error())
    37  		return multistep.ActionHalt
    38  	}
    39  	state.Put("instance_ip", ipRes.IP)
    40  	state.Put("ipres_name", ipresName)
    41  	return multistep.ActionContinue
    42  }
    43  
    44  func (s *stepCreateIPReservation) Cleanup(state multistep.StateBag) {
    45  	ipResName, ok := state.GetOk("ipres_name")
    46  	if !ok {
    47  		return
    48  	}
    49  
    50  	ui := state.Get("ui").(packer.Ui)
    51  	ui.Say("Cleaning up IP reservations...")
    52  	client := state.Get("client").(*compute.ComputeClient)
    53  
    54  	input := compute.DeleteIPReservationInput{Name: ipResName.(string)}
    55  	ipClient := client.IPReservations()
    56  	err := ipClient.DeleteIPReservation(&input)
    57  	if err != nil {
    58  		fmt.Printf("error deleting IP reservation: %s", err.Error())
    59  	}
    60  
    61  }