github.com/raghuse92/packer@v1.3.2/builder/openstack/step_load_flavor.go (about)

     1  package openstack
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"log"
     7  
     8  	"github.com/gophercloud/gophercloud/openstack/compute/v2/flavors"
     9  	"github.com/hashicorp/packer/helper/multistep"
    10  	"github.com/hashicorp/packer/packer"
    11  )
    12  
    13  // StepLoadFlavor gets the FlavorRef from a Flavor. It first assumes
    14  // that the Flavor is a ref and verifies it. Otherwise, it tries to find
    15  // the flavor by name.
    16  type StepLoadFlavor struct {
    17  	Flavor string
    18  }
    19  
    20  func (s *StepLoadFlavor) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
    21  	config := state.Get("config").(*Config)
    22  	ui := state.Get("ui").(packer.Ui)
    23  
    24  	// We need the v2 compute client
    25  	client, err := config.computeV2Client()
    26  	if err != nil {
    27  		err = fmt.Errorf("Error initializing compute client: %s", err)
    28  		state.Put("error", err)
    29  		return multistep.ActionHalt
    30  	}
    31  
    32  	ui.Say(fmt.Sprintf("Loading flavor: %s", s.Flavor))
    33  	log.Printf("[INFO] Loading flavor by ID: %s", s.Flavor)
    34  	flavor, err := flavors.Get(client, s.Flavor).Extract()
    35  	if err != nil {
    36  		log.Printf("[ERROR] Failed to find flavor by ID: %s", err)
    37  		geterr := err
    38  
    39  		log.Printf("[INFO] Loading flavor by name: %s", s.Flavor)
    40  		id, err := flavors.IDFromName(client, s.Flavor)
    41  		if err != nil {
    42  			log.Printf("[ERROR] Failed to find flavor by name: %s", err)
    43  			err = fmt.Errorf(
    44  				"Unable to find specified flavor by ID or name!\n\n"+
    45  					"Error from ID lookup: %s\n\n"+
    46  					"Error from name lookup: %s",
    47  				geterr,
    48  				err)
    49  			state.Put("error", err)
    50  			return multistep.ActionHalt
    51  		}
    52  
    53  		flavor = &flavors.Flavor{ID: id}
    54  	}
    55  
    56  	ui.Message(fmt.Sprintf("Verified flavor. ID: %s", flavor.ID))
    57  	state.Put("flavor_id", flavor.ID)
    58  	return multistep.ActionContinue
    59  }
    60  
    61  func (s *StepLoadFlavor) Cleanup(state multistep.StateBag) {
    62  }