github.phpd.cn/hashicorp/packer@v1.3.2/builder/googlecompute/driver.go (about)

     1  package googlecompute
     2  
     3  import (
     4  	"crypto/rsa"
     5  	"time"
     6  )
     7  
     8  // Driver is the interface that has to be implemented to communicate
     9  // with GCE. The Driver interface exists mostly to allow a mock implementation
    10  // to be used to test the steps.
    11  type Driver interface {
    12  	// CreateImage creates an image from the given disk in Google Compute
    13  	// Engine.
    14  	CreateImage(name, description, family, zone, disk string, image_labels map[string]string, image_licenses []string) (<-chan *Image, <-chan error)
    15  
    16  	// DeleteImage deletes the image with the given name.
    17  	DeleteImage(name string) <-chan error
    18  
    19  	// DeleteInstance deletes the given instance, keeping the boot disk.
    20  	DeleteInstance(zone, name string) (<-chan error, error)
    21  
    22  	// DeleteDisk deletes the disk with the given name.
    23  	DeleteDisk(zone, name string) (<-chan error, error)
    24  
    25  	// GetImage gets an image; tries the default and public projects. If
    26  	// fromFamily is true, name designates an image family instead of a
    27  	// particular image.
    28  	GetImage(name string, fromFamily bool) (*Image, error)
    29  
    30  	// GetImageFromProject gets an image from a specific project. If fromFamily
    31  	// is true, name designates an image family instead of a particular image.
    32  	GetImageFromProject(project, name string, fromFamily bool) (*Image, error)
    33  
    34  	// GetInstanceMetadata gets a metadata variable for the instance, name.
    35  	GetInstanceMetadata(zone, name, key string) (string, error)
    36  
    37  	// GetInternalIP gets the GCE-internal IP address for the instance.
    38  	GetInternalIP(zone, name string) (string, error)
    39  
    40  	// GetNatIP gets the NAT IP address for the instance.
    41  	GetNatIP(zone, name string) (string, error)
    42  
    43  	// GetSerialPortOutput gets the Serial Port contents for the instance.
    44  	GetSerialPortOutput(zone, name string) (string, error)
    45  
    46  	// ImageExists returns true if the specified image exists. If an error
    47  	// occurs calling the API, this method returns false.
    48  	ImageExists(name string) bool
    49  
    50  	// RunInstance takes the given config and launches an instance.
    51  	RunInstance(*InstanceConfig) (<-chan error, error)
    52  
    53  	// WaitForInstance waits for an instance to reach the given state.
    54  	WaitForInstance(state, zone, name string) <-chan error
    55  
    56  	// CreateOrResetWindowsPassword creates or resets the password for a user on an Windows instance.
    57  	CreateOrResetWindowsPassword(zone, name string, config *WindowsPasswordConfig) (<-chan error, error)
    58  }
    59  
    60  type InstanceConfig struct {
    61  	AcceleratorType              string
    62  	AcceleratorCount             int64
    63  	Address                      string
    64  	Description                  string
    65  	DisableDefaultServiceAccount bool
    66  	DiskSizeGb                   int64
    67  	DiskType                     string
    68  	Image                        *Image
    69  	Labels                       map[string]string
    70  	MachineType                  string
    71  	Metadata                     map[string]string
    72  	MinCpuPlatform               string
    73  	Name                         string
    74  	Network                      string
    75  	NetworkProjectId             string
    76  	OmitExternalIP               bool
    77  	OnHostMaintenance            string
    78  	Preemptible                  bool
    79  	Region                       string
    80  	ServiceAccountEmail          string
    81  	Scopes                       []string
    82  	Subnetwork                   string
    83  	Tags                         []string
    84  	Zone                         string
    85  }
    86  
    87  // WindowsPasswordConfig is the data structure that GCE needs to encrypt the created
    88  // windows password.
    89  type WindowsPasswordConfig struct {
    90  	key      *rsa.PrivateKey
    91  	password string
    92  	UserName string    `json:"userName"`
    93  	Modulus  string    `json:"modulus"`
    94  	Exponent string    `json:"exponent"`
    95  	Email    string    `json:"email"`
    96  	ExpireOn time.Time `json:"expireOn"`
    97  }
    98  
    99  type windowsPasswordResponse struct {
   100  	UserName          string `json:"userName"`
   101  	PasswordFound     bool   `json:"passwordFound"`
   102  	EncryptedPassword string `json:"encryptedPassword"`
   103  	Modulus           string `json:"modulus"`
   104  	Exponent          string `json:"exponent"`
   105  	ErrorMessage      string `json:"errorMessage"`
   106  }