github.com/mmcquillan/packer@v1.1.1-0.20171009221028-c85cf0483a5d/builder/azure/arm/step_create_resource_group.go (about)

     1  package arm
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/Azure/azure-sdk-for-go/arm/resources/resources"
     7  	"github.com/hashicorp/packer/builder/azure/common/constants"
     8  	"github.com/hashicorp/packer/packer"
     9  	"github.com/mitchellh/multistep"
    10  )
    11  
    12  type StepCreateResourceGroup struct {
    13  	client *AzureClient
    14  	create func(resourceGroupName string, location string, tags *map[string]*string) error
    15  	say    func(message string)
    16  	error  func(e error)
    17  }
    18  
    19  func NewStepCreateResourceGroup(client *AzureClient, ui packer.Ui) *StepCreateResourceGroup {
    20  	var step = &StepCreateResourceGroup{
    21  		client: client,
    22  		say:    func(message string) { ui.Say(message) },
    23  		error:  func(e error) { ui.Error(e.Error()) },
    24  	}
    25  
    26  	step.create = step.createResourceGroup
    27  	return step
    28  }
    29  
    30  func (s *StepCreateResourceGroup) createResourceGroup(resourceGroupName string, location string, tags *map[string]*string) error {
    31  	_, err := s.client.GroupsClient.CreateOrUpdate(resourceGroupName, resources.Group{
    32  		Location: &location,
    33  		Tags:     tags,
    34  	})
    35  
    36  	if err != nil {
    37  		s.say(s.client.LastError.Error())
    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  	_, errChan := s.client.GroupsClient.Delete(resourceGroupName, nil)
    75  	err := <-errChan
    76  
    77  	if err != nil {
    78  		ui.Error(fmt.Sprintf("Error deleting resource group.  Please delete it manually.\n\n"+
    79  			"Name: %s\n"+
    80  			"Error: %s", resourceGroupName, err))
    81  	}
    82  
    83  	ui.Say("Resource group has been deleted.")
    84  }