github.com/mmcquillan/packer@v1.1.1-0.20171009221028-c85cf0483a5d/builder/oracle/oci/driver_oci.go (about)

     1  package oci
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  
     7  	client "github.com/hashicorp/packer/builder/oracle/oci/client"
     8  )
     9  
    10  // driverOCI implements the Driver interface and communicates with Oracle
    11  // OCI.
    12  type driverOCI struct {
    13  	client *client.Client
    14  	cfg    *Config
    15  }
    16  
    17  // NewDriverOCI Creates a new driverOCI with a connected client.
    18  func NewDriverOCI(cfg *Config) (Driver, error) {
    19  	client, err := client.NewClient(cfg.AccessCfg)
    20  	if err != nil {
    21  		return nil, err
    22  	}
    23  	return &driverOCI{client: client, cfg: cfg}, nil
    24  }
    25  
    26  // CreateInstance creates a new compute instance.
    27  func (d *driverOCI) CreateInstance(publicKey string) (string, error) {
    28  	params := &client.LaunchInstanceParams{
    29  		AvailabilityDomain: d.cfg.AvailabilityDomain,
    30  		CompartmentID:      d.cfg.CompartmentID,
    31  		ImageID:            d.cfg.BaseImageID,
    32  		Shape:              d.cfg.Shape,
    33  		SubnetID:           d.cfg.SubnetID,
    34  		Metadata: map[string]string{
    35  			"ssh_authorized_keys": publicKey,
    36  		},
    37  	}
    38  	instance, err := d.client.Compute.Instances.Launch(params)
    39  	if err != nil {
    40  		return "", err
    41  	}
    42  
    43  	return instance.ID, nil
    44  }
    45  
    46  // CreateImage creates a new custom image.
    47  func (d *driverOCI) CreateImage(id string) (client.Image, error) {
    48  	params := &client.CreateImageParams{
    49  		CompartmentID: d.cfg.CompartmentID,
    50  		InstanceID:    id,
    51  		DisplayName:   d.cfg.ImageName,
    52  	}
    53  	image, err := d.client.Compute.Images.Create(params)
    54  	if err != nil {
    55  		return client.Image{}, err
    56  	}
    57  
    58  	return image, nil
    59  }
    60  
    61  // DeleteImage deletes a custom image.
    62  func (d *driverOCI) DeleteImage(id string) error {
    63  	return d.client.Compute.Images.Delete(&client.DeleteImageParams{ID: id})
    64  }
    65  
    66  // GetInstanceIP returns the public IP corresponding to the given instance id.
    67  func (d *driverOCI) GetInstanceIP(id string) (string, error) {
    68  	// get nvic and cross ref to find pub ip address
    69  	vnics, err := d.client.Compute.VNICAttachments.List(
    70  		&client.ListVnicAttachmentsParams{
    71  			InstanceID:    id,
    72  			CompartmentID: d.cfg.CompartmentID,
    73  		},
    74  	)
    75  	if err != nil {
    76  		return "", err
    77  	}
    78  
    79  	if len(vnics) < 1 {
    80  		return "", errors.New("instance has zero VNICs")
    81  	}
    82  
    83  	vnic, err := d.client.Compute.VNICs.Get(&client.GetVNICParams{ID: vnics[0].VNICID})
    84  	if err != nil {
    85  		return "", fmt.Errorf("Error getting VNIC details: %s", err)
    86  	}
    87  
    88  	return vnic.PublicIP, nil
    89  }
    90  
    91  // TerminateInstance terminates a compute instance.
    92  func (d *driverOCI) TerminateInstance(id string) error {
    93  	params := &client.TerminateInstanceParams{ID: id}
    94  	return d.client.Compute.Instances.Terminate(params)
    95  }
    96  
    97  // WaitForImageCreation waits for a provisioning custom image to reach the
    98  // "AVAILABLE" state.
    99  func (d *driverOCI) WaitForImageCreation(id string) error {
   100  	return client.NewWaiter().WaitForResourceToReachState(
   101  		d.client.Compute.Images,
   102  		id,
   103  		[]string{"PROVISIONING"},
   104  		"AVAILABLE",
   105  	)
   106  }
   107  
   108  // WaitForInstanceState waits for an instance to reach the a given terminal
   109  // state.
   110  func (d *driverOCI) WaitForInstanceState(id string, waitStates []string, terminalState string) error {
   111  	return client.NewWaiter().WaitForResourceToReachState(
   112  		d.client.Compute.Instances,
   113  		id,
   114  		waitStates,
   115  		terminalState,
   116  	)
   117  }