github.phpd.cn/hashicorp/packer@v1.3.2/builder/cloudstack/artifact.go (about)

     1  package cloudstack
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"strings"
     7  
     8  	"github.com/xanzy/go-cloudstack/cloudstack"
     9  )
    10  
    11  // Artifact represents a CloudStack template as the result of a Packer build.
    12  type Artifact struct {
    13  	client   *cloudstack.CloudStackClient
    14  	config   *Config
    15  	template *cloudstack.CreateTemplateResponse
    16  }
    17  
    18  // BuilderId returns the builder ID.
    19  func (a *Artifact) BuilderId() string {
    20  	return BuilderId
    21  }
    22  
    23  // Destroy the CloudStack template represented by the artifact.
    24  func (a *Artifact) Destroy() error {
    25  	// Create a new parameter struct.
    26  	p := a.client.Template.NewDeleteTemplateParams(a.template.Id)
    27  
    28  	// Destroy the template.
    29  	log.Printf("Destroying template: %s", a.template.Name)
    30  	_, err := a.client.Template.DeleteTemplate(p)
    31  	if err != nil {
    32  		// This is a very poor way to be told the ID does no longer exist :(
    33  		if strings.Contains(err.Error(), fmt.Sprintf(
    34  			"Invalid parameter id value=%s due to incorrect long value format, "+
    35  				"or entity does not exist", a.template.Id)) {
    36  			return nil
    37  		}
    38  
    39  		return fmt.Errorf("Error destroying template %s: %s", a.template.Name, err)
    40  	}
    41  
    42  	return nil
    43  }
    44  
    45  // Files returns the files represented by the artifact.
    46  func (a *Artifact) Files() []string {
    47  	// We have no files.
    48  	return nil
    49  }
    50  
    51  // Id returns CloudStack template ID.
    52  func (a *Artifact) Id() string {
    53  	return a.template.Id
    54  }
    55  
    56  // String returns the string representation of the artifact.
    57  func (a *Artifact) String() string {
    58  	return fmt.Sprintf("A template was created: %s", a.template.Name)
    59  }
    60  
    61  // State returns specific details from the artifact.
    62  func (a *Artifact) State(name string) interface{} {
    63  	return nil
    64  }