github.com/rahart/packer@v0.12.2-0.20161229105310-282bb6ad370f/builder/azure/arm/step_validate_template.go (about)

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