github.com/Pankov404/juju@v0.0.0-20150703034450-be266991dceb/provider/gce/google/disk.go (about)

     1  // Copyright 2014 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package google
     5  
     6  import (
     7  	"google.golang.org/api/compute/v1"
     8  )
     9  
    10  // The different types of disks supported by GCE.
    11  const (
    12  	diskTypeScratch    = "SCRATCH"
    13  	diskTypePersistent = "PERSISTENT"
    14  )
    15  
    16  // The different disk modes supported by GCE.
    17  const (
    18  	diskModeRW = "READ_WRITE"
    19  	diskModeRO = "READ_ONLY"
    20  )
    21  
    22  // MinDiskSizeGB is the minimum/default size (in megabytes) for
    23  // GCE disks.
    24  //
    25  // Note: GCE does not currently have an official minimum disk size.
    26  // However, in testing we found the minimum size to be 10 GB due to
    27  // the image size. See gceapi messsage.
    28  //
    29  // gceapi: Requested disk size cannot be smaller than the image size (10 GB)
    30  const MinDiskSizeGB uint64 = 10
    31  
    32  // DiskSpec holds all the data needed to request a new disk on GCE.
    33  // Some fields are used only for attached disks (i.e. in association
    34  // with instances).
    35  type DiskSpec struct {
    36  	// SizeHintGB is the requested disk size in Gigabytes. It must be
    37  	// greater than 0.
    38  	SizeHintGB uint64
    39  	// ImageURL is the location of the image to which the disk should
    40  	// be initialized.
    41  	ImageURL string
    42  	// Boot indicates that this is a boot disk. An instance may only
    43  	// have one boot disk. (attached only)
    44  	Boot bool
    45  	// Scratch indicates that the disk should be a "scratch" disk
    46  	// instead of a "persistent" disk (the default).
    47  	Scratch bool
    48  	// Readonly indicates that the disk should not support writes.
    49  	Readonly bool
    50  	// AutoDelete indicates that the attached disk should be removed
    51  	// when the instance to which it is attached is removed.
    52  	AutoDelete bool
    53  }
    54  
    55  // TooSmall checks the spec's size hint and indicates whether or not
    56  // it is smaller than the minimum disk size.
    57  func (ds *DiskSpec) TooSmall() bool {
    58  	return ds.SizeHintGB < MinDiskSizeGB
    59  }
    60  
    61  // SizeGB returns the disk size to use for a new disk. The size hint
    62  // is returned if it isn't too small (otherwise the min size is
    63  // returned).
    64  func (ds *DiskSpec) SizeGB() uint64 {
    65  	size := ds.SizeHintGB
    66  	if ds.TooSmall() {
    67  		size = MinDiskSizeGB
    68  	}
    69  	return size
    70  }
    71  
    72  // newAttached builds a compute.AttachedDisk using the information in
    73  // the disk spec and returns it.
    74  //
    75  // Note: Not all AttachedDisk fields are set.
    76  func (ds *DiskSpec) newAttached() *compute.AttachedDisk {
    77  	// TODO(ericsnow) Fail if SizeHintGB is 0?
    78  	diskType := diskTypePersistent
    79  	if ds.Scratch {
    80  		diskType = diskTypeScratch
    81  	}
    82  	mode := diskModeRW
    83  	if ds.Readonly {
    84  		mode = diskModeRO
    85  	}
    86  
    87  	disk := compute.AttachedDisk{
    88  		Type:       diskType,
    89  		Boot:       ds.Boot,
    90  		Mode:       mode,
    91  		AutoDelete: ds.AutoDelete,
    92  		InitializeParams: &compute.AttachedDiskInitializeParams{
    93  			// DiskName (defaults to instance name)
    94  			DiskSizeGb: int64(ds.SizeGB()),
    95  			// DiskType (defaults to pd-standard, pd-ssd, local-ssd)
    96  			SourceImage: ds.ImageURL,
    97  		},
    98  		// Interface (defaults to SCSI)
    99  		// DeviceName (GCE sets this, persistent disk only)
   100  	}
   101  	return &disk
   102  }