2016-03-04 05:14:55 -05:00
|
|
|
package arm
|
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2016-03-04 05:14:55 -05:00
|
|
|
"fmt"
|
|
|
|
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/builder/azure/common/constants"
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2016-03-04 05:14:55 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type StepPowerOffCompute struct {
|
|
|
|
client *AzureClient
|
2018-04-11 11:25:33 -04:00
|
|
|
powerOff func(ctx context.Context, resourceGroupName string, computeName string) error
|
2016-03-04 05:14:55 -05:00
|
|
|
say func(message string)
|
|
|
|
error func(e error)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewStepPowerOffCompute(client *AzureClient, ui packer.Ui) *StepPowerOffCompute {
|
|
|
|
var step = &StepPowerOffCompute{
|
|
|
|
client: client,
|
|
|
|
say: func(message string) { ui.Say(message) },
|
|
|
|
error: func(e error) { ui.Error(e.Error()) },
|
|
|
|
}
|
|
|
|
|
|
|
|
step.powerOff = step.powerOffCompute
|
|
|
|
return step
|
|
|
|
}
|
|
|
|
|
2018-04-11 11:25:33 -04:00
|
|
|
func (s *StepPowerOffCompute) powerOffCompute(ctx context.Context, resourceGroupName string, computeName string) error {
|
2019-01-15 19:31:23 -05:00
|
|
|
f, err := s.client.VirtualMachinesClient.Deallocate(ctx, resourceGroupName, computeName)
|
2018-04-06 04:12:58 -04:00
|
|
|
if err == nil {
|
2019-05-30 17:25:43 -04:00
|
|
|
err = f.WaitForCompletionRef(ctx, s.client.VirtualMachinesClient.Client)
|
2018-04-06 04:12:58 -04:00
|
|
|
}
|
2016-03-04 05:14:55 -05:00
|
|
|
if err != nil {
|
2017-06-08 20:57:59 -04:00
|
|
|
s.say(s.client.LastError.Error())
|
2016-03-04 05:14:55 -05:00
|
|
|
}
|
2017-06-08 20:57:59 -04:00
|
|
|
return err
|
2016-03-04 05:14:55 -05:00
|
|
|
}
|
|
|
|
|
2018-04-06 04:12:58 -04:00
|
|
|
func (s *StepPowerOffCompute) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2016-03-04 05:14:55 -05:00
|
|
|
s.say("Powering off machine ...")
|
|
|
|
|
|
|
|
var resourceGroupName = state.Get(constants.ArmResourceGroupName).(string)
|
|
|
|
var computeName = state.Get(constants.ArmComputeName).(string)
|
|
|
|
|
|
|
|
s.say(fmt.Sprintf(" -> ResourceGroupName : '%s'", resourceGroupName))
|
|
|
|
s.say(fmt.Sprintf(" -> ComputeName : '%s'", computeName))
|
|
|
|
|
2018-04-11 11:25:33 -04:00
|
|
|
err := s.powerOff(ctx, resourceGroupName, computeName)
|
2016-03-04 05:14:55 -05:00
|
|
|
|
2018-04-11 11:25:33 -04:00
|
|
|
return processStepResult(err, s.error, state)
|
2016-03-04 05:14:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (*StepPowerOffCompute) Cleanup(multistep.StateBag) {
|
|
|
|
}
|