2016-03-04 05:14:55 -05:00
|
|
|
// Copyright (c) Microsoft Corporation. All rights reserved.
|
2016-05-06 23:32:18 -04:00
|
|
|
// Licensed under the MIT License. See the LICENSE file in builder/azure for license information.
|
2016-03-04 05:14:55 -05:00
|
|
|
|
|
|
|
package arm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/Azure/azure-sdk-for-go/arm/resources/resources"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/builder/azure/common/constants"
|
|
|
|
"github.com/hashicorp/packer/packer"
|
2016-04-21 19:50:03 -04:00
|
|
|
"github.com/mitchellh/multistep"
|
2016-03-04 05:14:55 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type StepCreateResourceGroup struct {
|
|
|
|
client *AzureClient
|
2016-07-29 17:17:33 -04:00
|
|
|
create func(resourceGroupName string, location string, tags *map[string]*string) error
|
2016-03-04 05:14:55 -05:00
|
|
|
say func(message string)
|
|
|
|
error func(e error)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewStepCreateResourceGroup(client *AzureClient, ui packer.Ui) *StepCreateResourceGroup {
|
|
|
|
var step = &StepCreateResourceGroup{
|
|
|
|
client: client,
|
|
|
|
say: func(message string) { ui.Say(message) },
|
|
|
|
error: func(e error) { ui.Error(e.Error()) },
|
|
|
|
}
|
|
|
|
|
|
|
|
step.create = step.createResourceGroup
|
|
|
|
return step
|
|
|
|
}
|
|
|
|
|
2016-07-29 17:17:33 -04:00
|
|
|
func (s *StepCreateResourceGroup) createResourceGroup(resourceGroupName string, location string, tags *map[string]*string) error {
|
2016-03-04 05:14:55 -05:00
|
|
|
_, err := s.client.GroupsClient.CreateOrUpdate(resourceGroupName, resources.ResourceGroup{
|
|
|
|
Location: &location,
|
2016-07-29 17:17:33 -04:00
|
|
|
Tags: tags,
|
2016-03-04 05:14:55 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepCreateResourceGroup) Run(state multistep.StateBag) multistep.StepAction {
|
|
|
|
s.say("Creating resource group ...")
|
|
|
|
|
|
|
|
var resourceGroupName = state.Get(constants.ArmResourceGroupName).(string)
|
|
|
|
var location = state.Get(constants.ArmLocation).(string)
|
2016-07-29 17:17:33 -04:00
|
|
|
var tags = state.Get(constants.ArmTags).(*map[string]*string)
|
2016-03-04 05:14:55 -05:00
|
|
|
|
|
|
|
s.say(fmt.Sprintf(" -> ResourceGroupName : '%s'", resourceGroupName))
|
|
|
|
s.say(fmt.Sprintf(" -> Location : '%s'", location))
|
2016-07-29 17:17:33 -04:00
|
|
|
s.say(fmt.Sprintf(" -> Tags :"))
|
|
|
|
for k, v := range *tags {
|
|
|
|
s.say(fmt.Sprintf(" ->> %s : %s", k, *v))
|
|
|
|
}
|
2016-03-04 05:14:55 -05:00
|
|
|
|
2016-07-29 17:17:33 -04:00
|
|
|
err := s.create(resourceGroupName, location, tags)
|
2016-04-21 19:50:03 -04:00
|
|
|
if err == nil {
|
|
|
|
state.Put(constants.ArmIsResourceGroupCreated, true)
|
2016-03-04 05:14:55 -05:00
|
|
|
}
|
|
|
|
|
2016-04-21 19:50:03 -04:00
|
|
|
return processStepResult(err, s.error, state)
|
2016-03-04 05:14:55 -05:00
|
|
|
}
|
|
|
|
|
2016-04-21 19:50:03 -04:00
|
|
|
func (s *StepCreateResourceGroup) Cleanup(state multistep.StateBag) {
|
2016-05-09 17:19:55 -04:00
|
|
|
isCreated, ok := state.GetOk(constants.ArmIsResourceGroupCreated)
|
|
|
|
if !ok || !isCreated.(bool) {
|
2016-04-21 19:50:03 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
ui.Say("\nCleanup requested, deleting resource group ...")
|
|
|
|
|
|
|
|
var resourceGroupName = state.Get(constants.ArmResourceGroupName).(string)
|
|
|
|
_, err := s.client.GroupsClient.Delete(resourceGroupName, nil)
|
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Error deleting resource group. Please delete it manually.\n\n"+
|
|
|
|
"Name: %s\n"+
|
|
|
|
"Error: %s", resourceGroupName, err))
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.Say("Resource group has been deleted.")
|
2016-03-04 05:14:55 -05:00
|
|
|
}
|