github.com/kikitux/packer@v0.10.1-0.20160322154024-6237df566f9f/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, templateParameters *TemplateParameters) error
    17  	say      func(message string)
    18  	error    func(e error)
    19  }
    20  
    21  func NewStepValidateTemplate(client *AzureClient, ui packer.Ui) *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  	}
    27  
    28  	step.validate = step.validateTemplate
    29  	return step
    30  }
    31  
    32  func (s *StepValidateTemplate) validateTemplate(resourceGroupName string, deploymentName string, templateParameters *TemplateParameters) error {
    33  	factory := newDeploymentFactory(Linux)
    34  	deployment, err := factory.create(*templateParameters)
    35  
    36  	if err != nil {
    37  		return err
    38  	}
    39  
    40  	_, err = s.client.Validate(resourceGroupName, deploymentName, *deployment)
    41  	return err
    42  }
    43  
    44  func (s *StepValidateTemplate) Run(state multistep.StateBag) multistep.StepAction {
    45  	s.say("Validating deployment template ...")
    46  
    47  	var resourceGroupName = state.Get(constants.ArmResourceGroupName).(string)
    48  	var deploymentName = state.Get(constants.ArmDeploymentName).(string)
    49  	var templateParameters = state.Get(constants.ArmTemplateParameters).(*TemplateParameters)
    50  
    51  	s.say(fmt.Sprintf(" -> ResourceGroupName : '%s'", resourceGroupName))
    52  	s.say(fmt.Sprintf(" -> DeploymentName    : '%s'", deploymentName))
    53  
    54  	err := s.validate(resourceGroupName, deploymentName, templateParameters)
    55  	if err != nil {
    56  		state.Put(constants.Error, err)
    57  		s.error(err)
    58  
    59  		return multistep.ActionHalt
    60  	}
    61  
    62  	return multistep.ActionContinue
    63  }
    64  
    65  func (*StepValidateTemplate) Cleanup(multistep.StateBag) {
    66  }