github.com/amanya/packer@v0.12.1-0.20161117214323-902ac5ab2eb6/builder/openstack/step_load_flavor.go (about)

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