github.com/yoctocloud/packer@v0.6.2-0.20160520224004-e11a0a18423f/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  	template string
    17  	validate func(resourceGroupName string, deploymentName string, templateParameters *TemplateParameters) error
    18  	say      func(message string)
    19  	error    func(e error)
    20  }
    21  
    22  func NewStepValidateTemplate(client *AzureClient, ui packer.Ui, template string) *StepValidateTemplate {
    23  	var step = &StepValidateTemplate{
    24  		client:   client,
    25  		template: template,
    26  		say:      func(message string) { ui.Say(message) },
    27  		error:    func(e error) { ui.Error(e.Error()) },
    28  	}
    29  
    30  	step.validate = step.validateTemplate
    31  	return step
    32  }
    33  
    34  func (s *StepValidateTemplate) validateTemplate(resourceGroupName string, deploymentName string, templateParameters *TemplateParameters) error {
    35  	factory := newDeploymentFactory(s.template)
    36  	deployment, err := factory.create(*templateParameters)
    37  
    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  	var templateParameters = state.Get(constants.ArmTemplateParameters).(*TemplateParameters)
    52  
    53  	s.say(fmt.Sprintf(" -> ResourceGroupName : '%s'", resourceGroupName))
    54  	s.say(fmt.Sprintf(" -> DeploymentName    : '%s'", deploymentName))
    55  
    56  	err := s.validate(resourceGroupName, deploymentName, templateParameters)
    57  	return processStepResult(err, s.error, state)
    58  }
    59  
    60  func (*StepValidateTemplate) Cleanup(multistep.StateBag) {
    61  }