packer-cn/builder/azure/arm/step_validate_template.go

61 lines
1.7 KiB
Go
Raw Normal View History

package arm
import (
"fmt"
2017-04-04 16:39:01 -04:00
"github.com/hashicorp/packer/builder/azure/common/constants"
"github.com/hashicorp/packer/helper/multistep"
2017-04-04 16:39:01 -04:00
"github.com/hashicorp/packer/packer"
)
type StepValidateTemplate struct {
client *AzureClient
2016-05-21 02:01:16 -04:00
validate func(resourceGroupName string, deploymentName string) error
say func(message string)
error func(e error)
2016-05-21 02:01:16 -04:00
config *Config
factory templateFactoryFunc
}
2016-05-21 02:01:16 -04:00
func NewStepValidateTemplate(client *AzureClient, ui packer.Ui, config *Config, factory templateFactoryFunc) *StepValidateTemplate {
var step = &StepValidateTemplate{
2016-05-21 02:01:16 -04:00
client: client,
say: func(message string) { ui.Say(message) },
error: func(e error) { ui.Error(e.Error()) },
config: config,
factory: factory,
}
step.validate = step.validateTemplate
return step
}
2016-05-21 02:01:16 -04:00
func (s *StepValidateTemplate) validateTemplate(resourceGroupName string, deploymentName string) error {
deployment, err := s.factory(s.config)
if err != nil {
return err
}
_, err = s.client.Validate(resourceGroupName, deploymentName, *deployment)
2017-06-08 20:57:59 -04:00
if err != nil {
s.say(s.client.LastError.Error())
}
return err
}
func (s *StepValidateTemplate) Run(state multistep.StateBag) multistep.StepAction {
s.say("Validating deployment template ...")
var resourceGroupName = state.Get(constants.ArmResourceGroupName).(string)
var deploymentName = state.Get(constants.ArmDeploymentName).(string)
s.say(fmt.Sprintf(" -> ResourceGroupName : '%s'", resourceGroupName))
s.say(fmt.Sprintf(" -> DeploymentName : '%s'", deploymentName))
2016-05-21 02:01:16 -04:00
err := s.validate(resourceGroupName, deploymentName)
return processStepResult(err, s.error, state)
}
func (*StepValidateTemplate) Cleanup(multistep.StateBag) {
}