2015-06-12 10:32:31 -04:00
|
|
|
package openstack
|
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2015-06-12 10:32:31 -04:00
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
|
2016-11-27 19:59:26 -05:00
|
|
|
"github.com/gophercloud/gophercloud/openstack/compute/v2/flavors"
|
2020-07-13 12:01:37 -04:00
|
|
|
flavors_utils "github.com/gophercloud/utils/openstack/compute/v2/flavors"
|
2020-12-17 16:29:25 -05:00
|
|
|
"github.com/hashicorp/packer-plugin-sdk/multistep"
|
|
|
|
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
|
2015-06-12 10:32:31 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// StepLoadFlavor gets the FlavorRef from a Flavor. It first assumes
|
|
|
|
// that the Flavor is a ref and verifies it. Otherwise, it tries to find
|
|
|
|
// the flavor by name.
|
|
|
|
type StepLoadFlavor struct {
|
|
|
|
Flavor string
|
|
|
|
}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *StepLoadFlavor) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2018-09-18 10:05:49 -04:00
|
|
|
config := state.Get("config").(*Config)
|
2020-11-19 14:54:31 -05:00
|
|
|
ui := state.Get("ui").(packersdk.Ui)
|
2015-06-12 10:32:31 -04:00
|
|
|
|
|
|
|
// We need the v2 compute client
|
|
|
|
client, err := config.computeV2Client()
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("Error initializing compute client: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.Say(fmt.Sprintf("Loading flavor: %s", s.Flavor))
|
|
|
|
log.Printf("[INFO] Loading flavor by ID: %s", s.Flavor)
|
|
|
|
flavor, err := flavors.Get(client, s.Flavor).Extract()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("[ERROR] Failed to find flavor by ID: %s", err)
|
|
|
|
geterr := err
|
|
|
|
|
|
|
|
log.Printf("[INFO] Loading flavor by name: %s", s.Flavor)
|
2020-07-13 12:01:37 -04:00
|
|
|
id, err := flavors_utils.IDFromName(client, s.Flavor)
|
2015-06-12 10:32:31 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("[ERROR] Failed to find flavor by name: %s", err)
|
|
|
|
err = fmt.Errorf(
|
|
|
|
"Unable to find specified flavor by ID or name!\n\n"+
|
|
|
|
"Error from ID lookup: %s\n\n"+
|
|
|
|
"Error from name lookup: %s",
|
|
|
|
geterr,
|
|
|
|
err)
|
|
|
|
state.Put("error", err)
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
flavor = &flavors.Flavor{ID: id}
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.Message(fmt.Sprintf("Verified flavor. ID: %s", flavor.ID))
|
|
|
|
state.Put("flavor_id", flavor.ID)
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepLoadFlavor) Cleanup(state multistep.StateBag) {
|
|
|
|
}
|