github.phpd.cn/hashicorp/packer@v1.3.2/builder/triton/source_machine_config.go (about)

     1  package triton
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/hashicorp/packer/template/interpolate"
     7  )
     8  
     9  // SourceMachineConfig represents the configuration to run a machine using
    10  // the SDC API in order for provisioning to take place.
    11  type SourceMachineConfig struct {
    12  	MachineName            string             `mapstructure:"source_machine_name"`
    13  	MachinePackage         string             `mapstructure:"source_machine_package"`
    14  	MachineImage           string             `mapstructure:"source_machine_image"`
    15  	MachineNetworks        []string           `mapstructure:"source_machine_networks"`
    16  	MachineMetadata        map[string]string  `mapstructure:"source_machine_metadata"`
    17  	MachineTags            map[string]string  `mapstructure:"source_machine_tags"`
    18  	MachineFirewallEnabled bool               `mapstructure:"source_machine_firewall_enabled"`
    19  	MachineImageFilters    MachineImageFilter `mapstructure:"source_machine_image_filter"`
    20  }
    21  
    22  type MachineImageFilter struct {
    23  	MostRecent bool `mapstructure:"most_recent"`
    24  	Name       string
    25  	OS         string
    26  	Version    string
    27  	Public     bool
    28  	State      string
    29  	Owner      string
    30  	Type       string
    31  }
    32  
    33  func (m *MachineImageFilter) Empty() bool {
    34  	return m.Name == "" && m.OS == "" && m.Version == "" && m.State == "" && m.Owner == "" && m.Type == ""
    35  }
    36  
    37  // Prepare performs basic validation on a SourceMachineConfig struct.
    38  func (c *SourceMachineConfig) Prepare(ctx *interpolate.Context) []error {
    39  	var errs []error
    40  
    41  	if c.MachinePackage == "" {
    42  		errs = append(errs, fmt.Errorf("A source_machine_package must be specified"))
    43  	}
    44  
    45  	if c.MachineImage != "" && c.MachineImageFilters.Name != "" {
    46  		errs = append(errs, fmt.Errorf("You cannot specify a Machine Image and also Machine Name filter"))
    47  	}
    48  
    49  	if c.MachineNetworks == nil {
    50  		c.MachineNetworks = []string{}
    51  	}
    52  
    53  	if c.MachineMetadata == nil {
    54  		c.MachineMetadata = make(map[string]string)
    55  	}
    56  
    57  	if c.MachineTags == nil {
    58  		c.MachineTags = make(map[string]string)
    59  	}
    60  
    61  	if len(errs) > 0 {
    62  		return errs
    63  	}
    64  
    65  	return nil
    66  }