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

     1  package oci
     2  
     3  import (
     4  	"time"
     5  )
     6  
     7  // InstanceService enables communicating with the OCI compute API's instance
     8  // related endpoints.
     9  type InstanceService struct {
    10  	client *baseClient
    11  }
    12  
    13  // NewInstanceService creates a new InstanceService for communicating with the
    14  // OCI compute API's instance related endpoints.
    15  func NewInstanceService(s *baseClient) *InstanceService {
    16  	return &InstanceService{
    17  		client: s.New().Path("instances/"),
    18  	}
    19  }
    20  
    21  // Instance details a OCI compute instance.
    22  type Instance struct {
    23  	// The Availability Domain the instance is running in.
    24  	AvailabilityDomain string `json:"availabilityDomain"`
    25  
    26  	// The OCID of the compartment that contains the instance.
    27  	CompartmentID string `json:"compartmentId"`
    28  
    29  	// A user-friendly name. Does not have to be unique, and it's changeable.
    30  	DisplayName string `json:"displayName,omitempty"`
    31  
    32  	// The OCID of the instance.
    33  	ID string `json:"id"`
    34  
    35  	// The image used to boot the instance.
    36  	ImageID string `json:"imageId,omitempty"`
    37  
    38  	// The current state of the instance. Allowed values:
    39  	//  - PROVISIONING
    40  	//  - RUNNING
    41  	//  - STARTING
    42  	//  - STOPPING
    43  	//  - STOPPED
    44  	//  - CREATING_IMAGE
    45  	//  - TERMINATING
    46  	//  - TERMINATED
    47  	LifecycleState string `json:"lifecycleState"`
    48  
    49  	// Custom metadata that you provide.
    50  	Metadata map[string]string `json:"metadata,omitempty"`
    51  
    52  	// The region that contains the Availability Domain the instance is running in.
    53  	Region string `json:"region"`
    54  
    55  	// The shape of the instance. The shape determines the number of CPUs
    56  	// and the amount of memory allocated to the instance.
    57  	Shape string `json:"shape"`
    58  
    59  	// The date and time the instance was created.
    60  	TimeCreated time.Time `json:"timeCreated"`
    61  }
    62  
    63  // GetInstanceParams are the paramaters available when communicating with the
    64  // GetInstance API endpoint.
    65  type GetInstanceParams struct {
    66  	ID string `url:"instanceId,omitempty"`
    67  }
    68  
    69  // Get returns a single Instance
    70  func (s *InstanceService) Get(params *GetInstanceParams) (Instance, error) {
    71  	instance := Instance{}
    72  	e := &APIError{}
    73  
    74  	_, err := s.client.New().Get(params.ID).Receive(&instance, e)
    75  	err = firstError(err, e)
    76  
    77  	return instance, err
    78  }
    79  
    80  // LaunchInstanceParams are the parameters available when communicating with
    81  // the LunchInstance API endpoint.
    82  type LaunchInstanceParams struct {
    83  	AvailabilityDomain string            `json:"availabilityDomain,omitempty"`
    84  	CompartmentID      string            `json:"compartmentId,omitempty"`
    85  	DisplayName        string            `json:"displayName,omitempty"`
    86  	ImageID            string            `json:"imageId,omitempty"`
    87  	Metadata           map[string]string `json:"metadata,omitempty"`
    88  	OPCiPXEScript      string            `json:"opcIpxeScript,omitempty"`
    89  	Shape              string            `json:"shape,omitempty"`
    90  	SubnetID           string            `json:"subnetId,omitempty"`
    91  }
    92  
    93  // Launch creates a new OCI compute instance. It does *not* wait for the
    94  // instance to boot.
    95  func (s *InstanceService) Launch(params *LaunchInstanceParams) (Instance, error) {
    96  	instance := &Instance{}
    97  	e := &APIError{}
    98  
    99  	_, err := s.client.New().Post("").SetBody(params).Receive(instance, e)
   100  	err = firstError(err, e)
   101  
   102  	return *instance, err
   103  }
   104  
   105  // TerminateInstanceParams are the parameters available when communicating with
   106  // the TerminateInstance API endpoint.
   107  type TerminateInstanceParams struct {
   108  	ID string `url:"instanceId,omitempty"`
   109  }
   110  
   111  // Terminate terminates a running OCI compute instance.
   112  // instance to boot.
   113  func (s *InstanceService) Terminate(params *TerminateInstanceParams) error {
   114  	e := &APIError{}
   115  
   116  	_, err := s.client.New().Delete(params.ID).SetBody(params).Receive(nil, e)
   117  	err = firstError(err, e)
   118  
   119  	return err
   120  }
   121  
   122  // GetResourceState GETs the LifecycleState of the given instance id.
   123  func (s *InstanceService) GetResourceState(id string) (string, error) {
   124  	instance, err := s.Get(&GetInstanceParams{ID: id})
   125  	if err != nil {
   126  		return "", err
   127  	}
   128  	return instance.LifecycleState, nil
   129  }