2019-06-13 03:16:49 -04:00
|
|
|
package uhost
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2019-12-17 05:25:56 -05:00
|
|
|
|
2019-10-12 04:46:21 -04:00
|
|
|
ucloudcommon "github.com/hashicorp/packer/builder/ucloud/common"
|
2019-06-13 03:16:49 -04:00
|
|
|
|
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
)
|
|
|
|
|
|
|
|
type stepConfigVPC struct {
|
|
|
|
VPCId string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stepConfigVPC) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2019-10-12 04:46:21 -04:00
|
|
|
client := state.Get("client").(*ucloudcommon.UCloudClient)
|
2019-06-13 03:16:49 -04:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
|
|
|
|
if len(s.VPCId) != 0 {
|
|
|
|
ui.Say(fmt.Sprintf("Trying to use specified vpc %q...", s.VPCId))
|
|
|
|
|
2019-10-12 04:46:21 -04:00
|
|
|
vpcSet, err := client.DescribeVPCById(s.VPCId)
|
2019-06-13 03:16:49 -04:00
|
|
|
if err != nil {
|
2019-10-12 04:46:21 -04:00
|
|
|
if ucloudcommon.IsNotFoundError(err) {
|
2019-06-28 00:02:45 -04:00
|
|
|
err = fmt.Errorf("the specified vpc %q does not exist", s.VPCId)
|
2019-10-12 04:46:21 -04:00
|
|
|
return ucloudcommon.Halt(state, err, "")
|
2019-06-13 03:16:49 -04:00
|
|
|
}
|
2019-10-12 04:46:21 -04:00
|
|
|
return ucloudcommon.Halt(state, err, fmt.Sprintf("Error on querying specified vpc %q", s.VPCId))
|
2019-06-13 03:16:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
state.Put("vpc_id", vpcSet.VPCId)
|
|
|
|
return multistep.ActionContinue
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.Say(fmt.Sprintf("Trying to use default vpc..."))
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stepConfigVPC) Cleanup(state multistep.StateBag) {
|
|
|
|
}
|