packer-cn/builder/azure/chroot/step_verify_source_disk.go

82 lines
2.5 KiB
Go
Raw Normal View History

2019-09-26 18:17:07 -04:00
package chroot
import (
"context"
"fmt"
2019-10-04 12:59:41 -04:00
"log"
2019-09-26 18:17:07 -04:00
"strings"
"github.com/Azure/go-autorest/autorest/azure"
"github.com/Azure/go-autorest/autorest/to"
"github.com/hashicorp/packer/builder/azure/common/client"
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
)
type StepVerifySourceDisk struct {
SubscriptionID string
SourceDiskResourceID string
Location string
}
func (s StepVerifySourceDisk) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
azcli := state.Get("azureclient").(client.AzureClientSet)
ui := state.Get("ui").(packer.Ui)
ui.Say("Checking source disk location")
resource, err := azure.ParseResourceID(s.SourceDiskResourceID)
if err != nil {
2019-10-04 12:59:41 -04:00
log.Printf("StepVerifySourceDisk.Run: error: %+v", err)
err := fmt.Errorf("Could not parse resource id %q: %s", s.SourceDiskResourceID, err)
state.Put("error", err)
ui.Error(err.Error())
2019-09-26 18:17:07 -04:00
return multistep.ActionHalt
}
if !strings.EqualFold(resource.SubscriptionID, s.SubscriptionID) {
2019-10-04 12:59:41 -04:00
err := fmt.Errorf("Source disk resource %q is in a different subscription than this VM (%q). "+
2019-09-26 18:17:07 -04:00
"Packer does not know how to handle that.",
2019-10-04 12:59:41 -04:00
s.SourceDiskResourceID, s.SubscriptionID)
log.Printf("StepVerifySourceDisk.Run: error: %+v", err)
state.Put("error", err)
ui.Error(err.Error())
2019-09-26 18:17:07 -04:00
return multistep.ActionHalt
}
if !(strings.EqualFold(resource.Provider, "Microsoft.Compute") && strings.EqualFold(resource.ResourceType, "disks")) {
2019-10-04 12:59:41 -04:00
err := fmt.Errorf("Resource ID %q is not a managed disk resource", s.SourceDiskResourceID)
log.Printf("StepVerifySourceDisk.Run: error: %+v", err)
state.Put("error", err)
ui.Error(err.Error())
2019-09-26 18:17:07 -04:00
return multistep.ActionHalt
}
disk, err := azcli.DisksClient().Get(ctx,
resource.ResourceGroup, resource.ResourceName)
if err != nil {
2019-10-04 12:59:41 -04:00
err := fmt.Errorf("Unable to retrieve disk (%q): %s", s.SourceDiskResourceID, err)
log.Printf("StepVerifySourceDisk.Run: error: %+v", err)
state.Put("error", err)
ui.Error(err.Error())
2019-09-26 18:17:07 -04:00
return multistep.ActionHalt
}
location := to.String(disk.Location)
if !strings.EqualFold(location, s.Location) {
2019-10-04 12:59:41 -04:00
err := fmt.Errorf("Source disk resource %q is in a different location (%q) than this VM (%q). "+
2019-09-26 18:17:07 -04:00
"Packer does not know how to handle that.",
s.SourceDiskResourceID,
location,
2019-10-04 12:59:41 -04:00
s.Location)
log.Printf("StepVerifySourceDisk.Run: error: %+v", err)
state.Put("error", err)
ui.Error(err.Error())
2019-09-26 18:17:07 -04:00
return multistep.ActionHalt
}
return multistep.ActionContinue
}
func (s StepVerifySourceDisk) Cleanup(state multistep.StateBag) {}