From 95d6795ec8de3718964275311b5ff1274adc1e46 Mon Sep 17 00:00:00 2001 From: Dongyu Zheng Date: Fri, 28 Oct 2016 10:51:05 -0700 Subject: [PATCH] Allow clone_template to use uuid:// --- builder/xenserver/iso/step_create_instance.go | 36 +++++++++++++------ docs/builders/xenserver-iso.html.markdown | 3 +- 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/builder/xenserver/iso/step_create_instance.go b/builder/xenserver/iso/step_create_instance.go index a973af45..5bda5bd0 100644 --- a/builder/xenserver/iso/step_create_instance.go +++ b/builder/xenserver/iso/step_create_instance.go @@ -22,19 +22,35 @@ func (self *stepCreateInstance) Run(state multistep.StateBag) multistep.StepActi ui.Say("Step: Create Instance") // Get the template to clone from + var template *xsclient.VM + var err error - vms, err := client.GetVMByNameLabel(config.CloneTemplate) + if len(config.CloneTemplate) >= 7 && config.CloneTemplate[:7] == "uuid://" { + templateUuid := config.CloneTemplate[7:] - switch { - case len(vms) == 0: - ui.Error(fmt.Sprintf("Couldn't find a template with the name-label '%s'. Aborting.", config.CloneTemplate)) - return multistep.ActionHalt - case len(vms) > 1: - ui.Error(fmt.Sprintf("Found more than one template with the name '%s'. The name must be unique. Aborting.", config.CloneTemplate)) - return multistep.ActionHalt - } + template, err = client.GetVMByUuid(templateUuid) + if err != nil { + ui.Error(fmt.Sprintf("Could not get template with UUID '%s': %s", templateUuid, err.Error())) + return multistep.ActionHalt + } + } else { + templates, err := client.GetVMByNameLabel(config.CloneTemplate) + if err != nil { + ui.Error(fmt.Sprintf("Error getting template: %s", err.Error())) + return multistep.ActionHalt + } - template := vms[0] + switch { + case len(templates) == 0: + ui.Error(fmt.Sprintf("Couldn't find a template with the name-label '%s'.", config.CloneTemplate)) + return multistep.ActionHalt + case len(templates) > 1: + ui.Error(fmt.Sprintf("Found more than one template with the name '%s'. The name must be unique. Aborting.", config.CloneTemplate)) + return multistep.ActionHalt + } + + template = templates[0] + } // Clone that VM template instance, err := template.Clone(config.VMName) diff --git a/docs/builders/xenserver-iso.html.markdown b/docs/builders/xenserver-iso.html.markdown index bc7c00e3..75818c98 100644 --- a/docs/builders/xenserver-iso.html.markdown +++ b/docs/builders/xenserver-iso.html.markdown @@ -98,7 +98,8 @@ each category, the available options are alphabetized and described. media", this is "other", but you can get _dramatic_ performance improvements by setting this to the proper value. To view all available values for this run `xe template-list`. Setting the correct value hints to XenServer how to - optimize the virtual hardware to work best with that operating system. + optimize the virtual hardware to work best with that operating system. Setting + this to a template's UUID is also acceptable: "uuid://c6a9dee0-0069-24a5-d45d-4a4101a9d7f4". * `disk_size` (integer) - The size, in megabytes, of the hard disk to create for the VM. By default, this is 40000 (about 40 GB).