github.com/rahart/packer@v0.12.2-0.20161229105310-282bb6ad370f/builder/azure/arm/step_create_resource_group.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/Azure/azure-sdk-for-go/arm/resources/resources"
    10  	"github.com/mitchellh/multistep"
    11  	"github.com/mitchellh/packer/builder/azure/common/constants"
    12  	"github.com/mitchellh/packer/packer"
    13  )
    14  
    15  type StepCreateResourceGroup struct {
    16  	client *AzureClient
    17  	create func(resourceGroupName string, location string, tags *map[string]*string) error
    18  	say    func(message string)
    19  	error  func(e error)
    20  }
    21  
    22  func NewStepCreateResourceGroup(client *AzureClient, ui packer.Ui) *StepCreateResourceGroup {
    23  	var step = &StepCreateResourceGroup{
    24  		client: client,
    25  		say:    func(message string) { ui.Say(message) },
    26  		error:  func(e error) { ui.Error(e.Error()) },
    27  	}
    28  
    29  	step.create = step.createResourceGroup
    30  	return step
    31  }
    32  
    33  func (s *StepCreateResourceGroup) createResourceGroup(resourceGroupName string, location string, tags *map[string]*string) error {
    34  	_, err := s.client.GroupsClient.CreateOrUpdate(resourceGroupName, resources.ResourceGroup{
    35  		Location: &location,
    36  		Tags:     tags,
    37  	})
    38  
    39  	return err
    40  }
    41  
    42  func (s *StepCreateResourceGroup) Run(state multistep.StateBag) multistep.StepAction {
    43  	s.say("Creating resource group ...")
    44  
    45  	var resourceGroupName = state.Get(constants.ArmResourceGroupName).(string)
    46  	var location = state.Get(constants.ArmLocation).(string)
    47  	var tags = state.Get(constants.ArmTags).(*map[string]*string)
    48  
    49  	s.say(fmt.Sprintf(" -> ResourceGroupName : '%s'", resourceGroupName))
    50  	s.say(fmt.Sprintf(" -> Location          : '%s'", location))
    51  	s.say(fmt.Sprintf(" -> Tags              :"))
    52  	for k, v := range *tags {
    53  		s.say(fmt.Sprintf(" ->> %s : %s", k, *v))
    54  	}
    55  
    56  	err := s.create(resourceGroupName, location, tags)
    57  	if err == nil {
    58  		state.Put(constants.ArmIsResourceGroupCreated, true)
    59  	}
    60  
    61  	return processStepResult(err, s.error, state)
    62  }
    63  
    64  func (s *StepCreateResourceGroup) Cleanup(state multistep.StateBag) {
    65  	isCreated, ok := state.GetOk(constants.ArmIsResourceGroupCreated)
    66  	if !ok || !isCreated.(bool) {
    67  		return
    68  	}
    69  
    70  	ui := state.Get("ui").(packer.Ui)
    71  	ui.Say("\nCleanup requested, deleting resource group ...")
    72  
    73  	var resourceGroupName = state.Get(constants.ArmResourceGroupName).(string)
    74  	_, err := s.client.GroupsClient.Delete(resourceGroupName, nil)
    75  	if err != nil {
    76  		ui.Error(fmt.Sprintf("Error deleting resource group.  Please delete it manually.\n\n"+
    77  			"Name: %s\n"+
    78  			"Error: %s", resourceGroupName, err))
    79  	}
    80  
    81  	ui.Say("Resource group has been deleted.")
    82  }