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

     1  package digitalocean
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"log"
     7  	"strconv"
     8  	"strings"
     9  
    10  	"github.com/digitalocean/godo"
    11  )
    12  
    13  type Artifact struct {
    14  	// The name of the snapshot
    15  	snapshotName string
    16  
    17  	// The ID of the image
    18  	snapshotId int
    19  
    20  	// The name of the region
    21  	regionNames []string
    22  
    23  	// The client for making API calls
    24  	client *godo.Client
    25  }
    26  
    27  func (*Artifact) BuilderId() string {
    28  	return BuilderId
    29  }
    30  
    31  func (*Artifact) Files() []string {
    32  	// No files with DigitalOcean
    33  	return nil
    34  }
    35  
    36  func (a *Artifact) Id() string {
    37  	return fmt.Sprintf("%s:%s", strings.Join(a.regionNames[:], ","), strconv.FormatUint(uint64(a.snapshotId), 10))
    38  }
    39  
    40  func (a *Artifact) String() string {
    41  	return fmt.Sprintf("A snapshot was created: '%v' (ID: %v) in regions '%v'", a.snapshotName, a.snapshotId, strings.Join(a.regionNames[:], ","))
    42  }
    43  
    44  func (a *Artifact) State(name string) interface{} {
    45  	return nil
    46  }
    47  
    48  func (a *Artifact) Destroy() error {
    49  	log.Printf("Destroying image: %d (%s)", a.snapshotId, a.snapshotName)
    50  	_, err := a.client.Images.Delete(context.TODO(), a.snapshotId)
    51  	return err
    52  }