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

     1  package oci
     2  
     3  import (
     4  	"time"
     5  )
     6  
     7  // ImageService enables communicating with the OCI compute API's instance
     8  // related endpoints.
     9  type ImageService struct {
    10  	client *baseClient
    11  }
    12  
    13  // NewImageService creates a new ImageService for communicating with the
    14  // OCI compute API's instance related endpoints.
    15  func NewImageService(s *baseClient) *ImageService {
    16  	return &ImageService{
    17  		client: s.New().Path("images/"),
    18  	}
    19  }
    20  
    21  // Image details a OCI boot disk image.
    22  type Image struct {
    23  	// The OCID of the image originally used to launch the instance.
    24  	BaseImageID string `json:"baseImageId,omitempty"`
    25  
    26  	// The OCID of the compartment containing the instance you want to use
    27  	// as the basis for the image.
    28  	CompartmentID string `json:"compartmentId"`
    29  
    30  	// Whether instances launched with this image can be used to create new
    31  	// images.
    32  	CreateImageAllowed bool `json:"createImageAllowed"`
    33  
    34  	// A user-friendly name for the image. It does not have to be unique,
    35  	// and it's changeable. You cannot use an Oracle-provided image name
    36  	// as a custom image name.
    37  	DisplayName string `json:"displayName,omitempty"`
    38  
    39  	// The OCID of the image.
    40  	ID string `json:"id"`
    41  
    42  	// Current state of the image. Allowed values are:
    43  	//  - PROVISIONING
    44  	//  - AVAILABLE
    45  	//  - DISABLED
    46  	//  - DELETED
    47  	LifecycleState string `json:"lifecycleState"`
    48  
    49  	// The image's operating system (e.g. Oracle Linux).
    50  	OperatingSystem string `json:"operatingSystem"`
    51  
    52  	// The image's operating system version (e.g. 7.2).
    53  	OperatingSystemVersion string `json:"operatingSystemVersion"`
    54  
    55  	// The date and time the image was created.
    56  	TimeCreated time.Time `json:"timeCreated"`
    57  }
    58  
    59  // GetImageParams are the paramaters available when communicating with the
    60  // GetImage API endpoint.
    61  type GetImageParams struct {
    62  	ID string `url:"imageId"`
    63  }
    64  
    65  // Get returns a single Image
    66  func (s *ImageService) Get(params *GetImageParams) (Image, error) {
    67  	image := Image{}
    68  	e := &APIError{}
    69  
    70  	_, err := s.client.New().Get(params.ID).Receive(&image, e)
    71  	err = firstError(err, e)
    72  
    73  	return image, err
    74  }
    75  
    76  // CreateImageParams are the parameters available when communicating with
    77  // the CreateImage API endpoint.
    78  type CreateImageParams struct {
    79  	CompartmentID string `json:"compartmentId"`
    80  	DisplayName   string `json:"displayName,omitempty"`
    81  	InstanceID    string `json:"instanceId"`
    82  }
    83  
    84  // Create creates a new custom image based on a running compute instance. It
    85  // does *not* wait for the imaging process to finish.
    86  func (s *ImageService) Create(params *CreateImageParams) (Image, error) {
    87  	image := Image{}
    88  	e := &APIError{}
    89  
    90  	_, err := s.client.New().Post("").SetBody(params).Receive(&image, &e)
    91  	err = firstError(err, e)
    92  
    93  	return image, err
    94  }
    95  
    96  // GetResourceState GETs the LifecycleState of the given image id.
    97  func (s *ImageService) GetResourceState(id string) (string, error) {
    98  	image, err := s.Get(&GetImageParams{ID: id})
    99  	if err != nil {
   100  		return "", err
   101  	}
   102  	return image.LifecycleState, nil
   103  
   104  }
   105  
   106  // DeleteImageParams are the parameters available when communicating with
   107  // the DeleteImage API endpoint.
   108  type DeleteImageParams struct {
   109  	ID string `url:"imageId"`
   110  }
   111  
   112  // Delete deletes an existing custom image.
   113  // NOTE: Deleting an image results in the API endpoint returning 404 on
   114  // subsequent calls. As such deletion can't be waited on with a Waiter.
   115  func (s *ImageService) Delete(params *DeleteImageParams) error {
   116  	e := &APIError{}
   117  
   118  	_, err := s.client.New().Delete(params.ID).SetBody(params).Receive(nil, e)
   119  	err = firstError(err, e)
   120  
   121  	return err
   122  }