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"
|
|
|
|
|
2020-12-17 16:29:25 -05:00
|
|
|
"github.com/hashicorp/packer-plugin-sdk/multistep"
|
|
|
|
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/builder/azure/common/constants"
|
2016-03-04 05:14:55 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type StepValidateTemplate struct {
|
|
|
|
client *AzureClient
|
2018-04-06 04:12:58 -04:00
|
|
|
validate func(ctx context.Context, resourceGroupName string, deploymentName string) error
|
2016-03-04 05:14:55 -05:00
|
|
|
say func(message string)
|
|
|
|
error func(e error)
|
2016-05-21 02:01:16 -04:00
|
|
|
config *Config
|
|
|
|
factory templateFactoryFunc
|
2016-03-04 05:14:55 -05:00
|
|
|
}
|
|
|
|
|
2020-11-19 14:54:31 -05:00
|
|
|
func NewStepValidateTemplate(client *AzureClient, ui packersdk.Ui, config *Config, factory templateFactoryFunc) *StepValidateTemplate {
|
2016-03-04 05:14:55 -05:00
|
|
|
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,
|
2016-03-04 05:14:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
step.validate = step.validateTemplate
|
|
|
|
return step
|
|
|
|
}
|
|
|
|
|
2018-04-06 04:12:58 -04:00
|
|
|
func (s *StepValidateTemplate) validateTemplate(ctx context.Context, resourceGroupName string, deploymentName string) error {
|
2016-05-21 02:01:16 -04:00
|
|
|
deployment, err := s.factory(s.config)
|
2016-03-04 05:14:55 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-04-06 04:12:58 -04:00
|
|
|
_, err = s.client.DeploymentsClient.Validate(ctx, resourceGroupName, deploymentName, *deployment)
|
2017-06-08 20:57:59 -04:00
|
|
|
if err != nil {
|
|
|
|
s.say(s.client.LastError.Error())
|
|
|
|
}
|
2016-03-04 05:14:55 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-04-06 04:12:58 -04:00
|
|
|
func (s *StepValidateTemplate) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2016-03-04 05:14:55 -05:00
|
|
|
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))
|
|
|
|
|
2018-04-06 04:12:58 -04:00
|
|
|
err := s.validate(ctx, resourceGroupName, deploymentName)
|
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) {
|
|
|
|
}
|