2016-03-04 05:14:55 -05:00
|
|
|
// Copyright (c) Microsoft Corporation. All rights reserved.
|
2016-05-06 23:32:18 -04:00
|
|
|
// Licensed under the MIT License. See the LICENSE file in builder/azure for license information.
|
2016-03-04 05:14:55 -05:00
|
|
|
|
|
|
|
package arm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2016-04-21 19:50:03 -04:00
|
|
|
"github.com/mitchellh/multistep"
|
2016-05-18 20:25:57 -04:00
|
|
|
"github.com/mitchellh/packer/builder/azure/common/constants"
|
2016-03-04 05:14:55 -05:00
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
)
|
|
|
|
|
|
|
|
type StepValidateTemplate struct {
|
|
|
|
client *AzureClient
|
2016-04-21 19:50:03 -04:00
|
|
|
template string
|
2016-03-04 05:14:55 -05:00
|
|
|
validate func(resourceGroupName string, deploymentName string, templateParameters *TemplateParameters) error
|
|
|
|
say func(message string)
|
|
|
|
error func(e error)
|
|
|
|
}
|
|
|
|
|
2016-04-21 19:50:03 -04:00
|
|
|
func NewStepValidateTemplate(client *AzureClient, ui packer.Ui, template string) *StepValidateTemplate {
|
2016-03-04 05:14:55 -05:00
|
|
|
var step = &StepValidateTemplate{
|
2016-04-21 19:50:03 -04:00
|
|
|
client: client,
|
|
|
|
template: template,
|
|
|
|
say: func(message string) { ui.Say(message) },
|
|
|
|
error: func(e error) { ui.Error(e.Error()) },
|
2016-03-04 05:14:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
step.validate = step.validateTemplate
|
|
|
|
return step
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepValidateTemplate) validateTemplate(resourceGroupName string, deploymentName string, templateParameters *TemplateParameters) error {
|
2016-04-21 19:50:03 -04:00
|
|
|
factory := newDeploymentFactory(s.template)
|
2016-03-04 05:14:55 -05:00
|
|
|
deployment, err := factory.create(*templateParameters)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = s.client.Validate(resourceGroupName, deploymentName, *deployment)
|
|
|
|
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)
|
|
|
|
var templateParameters = state.Get(constants.ArmTemplateParameters).(*TemplateParameters)
|
|
|
|
|
|
|
|
s.say(fmt.Sprintf(" -> ResourceGroupName : '%s'", resourceGroupName))
|
|
|
|
s.say(fmt.Sprintf(" -> DeploymentName : '%s'", deploymentName))
|
|
|
|
|
|
|
|
err := s.validate(resourceGroupName, deploymentName, templateParameters)
|
2016-04-21 19:50:03 -04:00
|
|
|
return processStepResult(err, s.error, state)
|
2016-03-04 05:14:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (*StepValidateTemplate) Cleanup(multistep.StateBag) {
|
|
|
|
}
|