From 2a4ce52eaa3293a2b299e5a4ed0d79a97e0ecb47 Mon Sep 17 00:00:00 2001 From: Cheng Sun Date: Mon, 15 Dec 2014 14:17:20 +0000 Subject: [PATCH] Abstract out GetSR --- builder/xenserver/builder.go | 34 +++++++++++++++++++---- builder/xenserver/step_create_instance.go | 34 +++-------------------- 2 files changed, 33 insertions(+), 35 deletions(-) diff --git a/builder/xenserver/builder.go b/builder/xenserver/builder.go index ced619b..0b51192 100644 --- a/builder/xenserver/builder.go +++ b/builder/xenserver/builder.go @@ -431,6 +431,14 @@ func (self *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (pa return artifact, nil } +func (self *Builder) Cancel() { + if self.runner != nil { + log.Println("Cancelling the step runner...") + self.runner.Cancel() + } + fmt.Println("Cancelling the builder") +} + // all steps should check config.ShouldKeepInstance first before cleaning up func (cfg config) ShouldKeepInstance(state multistep.StateBag) bool { switch cfg.KeepInstance { @@ -448,10 +456,26 @@ func (cfg config) ShouldKeepInstance(state multistep.StateBag) bool { } } -func (self *Builder) Cancel() { - if self.runner != nil { - log.Println("Cancelling the step runner...") - self.runner.Cancel() +func (config config) GetSR(client XenAPIClient) (*SR, error) { + if config.SrName == "" { + // Find the default SR + return client.GetDefaultSR() + + } else { + // Use the provided name label to find the SR to use + srs, err := client.GetSRByNameLabel(config.SrName) + + if err != nil { + return nil, err + } + + switch { + case len(srs) == 0: + return nil, fmt.Errorf("Couldn't find a SR with the specified name-label '%s'", config.SrName) + case len(srs) > 1: + return nil, fmt.Errorf("Found more than one SR with the name '%s'. The name must be unique", config.SrName) + } + + return srs[0], nil } - fmt.Println("Cancelling the builder") } diff --git a/builder/xenserver/step_create_instance.go b/builder/xenserver/step_create_instance.go index 8870755..fcb800c 100644 --- a/builder/xenserver/step_create_instance.go +++ b/builder/xenserver/step_create_instance.go @@ -61,37 +61,11 @@ func (self *stepCreateInstance) Run(state multistep.StateBag) multistep.StepActi } // Create VDI for the instance - var sr *SR - if config.SrName == "" { - // Find the default SR - default_sr, err := client.GetDefaultSR() - sr = default_sr - - if err != nil { - ui.Error(fmt.Sprintf("Error getting default SR: %s", err.Error())) - return multistep.ActionHalt - } - - } else { - // Use the provided name label to find the SR to use - srs, err := client.GetSRByNameLabel(config.SrName) - - if err != nil { - ui.Error(fmt.Sprintf("Error getting default SR: %s", err.Error())) - return multistep.ActionHalt - } - - switch { - case len(srs) == 0: - ui.Error(fmt.Sprintf("Couldn't find a SR with the specified name-label '%s'. Aborting.", config.SrName)) - return multistep.ActionHalt - case len(srs) > 1: - ui.Error(fmt.Sprintf("Found more than one SR with the name '%s'. The name must be unique. Aborting.", config.SrName)) - return multistep.ActionHalt - } - - sr = srs[0] + sr, err := config.GetSR(client) + if err != nil { + ui.Error(fmt.Sprintf("Unable to get SR: %s", err.Error())) + return multistep.ActionHalt } vdi, err := sr.CreateVdi("Packer-disk", config.RootDiskSize)