github.phpd.cn/hashicorp/packer@v1.3.2/builder/azure/arm/step_validate_template.go (about)

     1  package arm
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/hashicorp/packer/builder/azure/common/constants"
     8  	"github.com/hashicorp/packer/helper/multistep"
     9  	"github.com/hashicorp/packer/packer"
    10  )
    11  
    12  type StepValidateTemplate struct {
    13  	client   *AzureClient
    14  	validate func(ctx context.Context, resourceGroupName string, deploymentName string) error
    15  	say      func(message string)
    16  	error    func(e error)
    17  	config   *Config
    18  	factory  templateFactoryFunc
    19  }
    20  
    21  func NewStepValidateTemplate(client *AzureClient, ui packer.Ui, config *Config, factory templateFactoryFunc) *StepValidateTemplate {
    22  	var step = &StepValidateTemplate{
    23  		client:  client,
    24  		say:     func(message string) { ui.Say(message) },
    25  		error:   func(e error) { ui.Error(e.Error()) },
    26  		config:  config,
    27  		factory: factory,
    28  	}
    29  
    30  	step.validate = step.validateTemplate
    31  	return step
    32  }
    33  
    34  func (s *StepValidateTemplate) validateTemplate(ctx context.Context, resourceGroupName string, deploymentName string) error {
    35  	deployment, err := s.factory(s.config)
    36  	if err != nil {
    37  		return err
    38  	}
    39  
    40  	_, err = s.client.DeploymentsClient.Validate(ctx, resourceGroupName, deploymentName, *deployment)
    41  	if err != nil {
    42  		s.say(s.client.LastError.Error())
    43  	}
    44  	return err
    45  }
    46  
    47  func (s *StepValidateTemplate) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
    48  	s.say("Validating deployment template ...")
    49  
    50  	var resourceGroupName = state.Get(constants.ArmResourceGroupName).(string)
    51  	var deploymentName = state.Get(constants.ArmDeploymentName).(string)
    52  
    53  	s.say(fmt.Sprintf(" -> ResourceGroupName : '%s'", resourceGroupName))
    54  	s.say(fmt.Sprintf(" -> DeploymentName    : '%s'", deploymentName))
    55  
    56  	err := s.validate(ctx, resourceGroupName, deploymentName)
    57  	return processStepResult(err, s.error, state)
    58  }
    59  
    60  func (*StepValidateTemplate) Cleanup(multistep.StateBag) {
    61  }