File: artifact.go

package info (click to toggle)
packer 1.6.6%2Bds2-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 33,156 kB
  • sloc: sh: 1,154; python: 619; makefile: 251; ruby: 205; xml: 97
file content (68 lines) | stat: -rw-r--r-- 1,766 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package cloudstack

import (
	"fmt"
	"log"
	"strings"

	"github.com/xanzy/go-cloudstack/cloudstack"
)

// Artifact represents a CloudStack template as the result of a Packer build.
type Artifact struct {
	client   *cloudstack.CloudStackClient
	config   *Config
	template *cloudstack.CreateTemplateResponse

	// StateData should store data such as GeneratedData
	// to be shared with post-processors
	StateData map[string]interface{}
}

// BuilderId returns the builder ID.
func (a *Artifact) BuilderId() string {
	return BuilderId
}

// Destroy the CloudStack template represented by the artifact.
func (a *Artifact) Destroy() error {
	// Create a new parameter struct.
	p := a.client.Template.NewDeleteTemplateParams(a.template.Id)

	// Destroy the template.
	log.Printf("Destroying template: %s", a.template.Name)
	_, err := a.client.Template.DeleteTemplate(p)
	if err != nil {
		// This is a very poor way to be told the ID does no longer exist :(
		if strings.Contains(err.Error(), fmt.Sprintf(
			"Invalid parameter id value=%s due to incorrect long value format, "+
				"or entity does not exist", a.template.Id)) {
			return nil
		}

		return fmt.Errorf("Error destroying template %s: %s", a.template.Name, err)
	}

	return nil
}

// Files returns the files represented by the artifact.
func (a *Artifact) Files() []string {
	// We have no files.
	return nil
}

// Id returns CloudStack template ID.
func (a *Artifact) Id() string {
	return a.template.Id
}

// String returns the string representation of the artifact.
func (a *Artifact) String() string {
	return fmt.Sprintf("A template was created: %s", a.template.Name)
}

// State returns specific details from the artifact.
func (a *Artifact) State(name string) interface{} {
	return a.StateData[name]
}