github.com/yoctocloud/packer@v0.6.2-0.20160520224004-e11a0a18423f/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) 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) error { 34 _, err := s.client.GroupsClient.CreateOrUpdate(resourceGroupName, resources.ResourceGroup{ 35 Location: &location, 36 }) 37 38 return err 39 } 40 41 func (s *StepCreateResourceGroup) Run(state multistep.StateBag) multistep.StepAction { 42 s.say("Creating resource group ...") 43 44 var resourceGroupName = state.Get(constants.ArmResourceGroupName).(string) 45 var location = state.Get(constants.ArmLocation).(string) 46 47 s.say(fmt.Sprintf(" -> ResourceGroupName : '%s'", resourceGroupName)) 48 s.say(fmt.Sprintf(" -> Location : '%s'", location)) 49 50 err := s.create(resourceGroupName, location) 51 if err == nil { 52 state.Put(constants.ArmIsResourceGroupCreated, true) 53 } 54 55 return processStepResult(err, s.error, state) 56 } 57 58 func (s *StepCreateResourceGroup) Cleanup(state multistep.StateBag) { 59 isCreated, ok := state.GetOk(constants.ArmIsResourceGroupCreated) 60 if !ok || !isCreated.(bool) { 61 return 62 } 63 64 ui := state.Get("ui").(packer.Ui) 65 ui.Say("\nCleanup requested, deleting resource group ...") 66 67 var resourceGroupName = state.Get(constants.ArmResourceGroupName).(string) 68 _, err := s.client.GroupsClient.Delete(resourceGroupName, nil) 69 if err != nil { 70 ui.Error(fmt.Sprintf("Error deleting resource group. Please delete it manually.\n\n"+ 71 "Name: %s\n"+ 72 "Error: %s", resourceGroupName, err)) 73 } 74 75 ui.Say("Resource group has been deleted.") 76 }