github.com/gophercloud/gophercloud@v1.11.0/openstack/blockstorage/v3/attachments/results.go (about)

     1  // Package attachments provides access to OpenStack Block Storage Attachment
     2  // API's. Use of this package requires Cinder version 3.27 at a minimum.
     3  package attachments
     4  
     5  import (
     6  	"encoding/json"
     7  	"time"
     8  
     9  	"github.com/gophercloud/gophercloud"
    10  	"github.com/gophercloud/gophercloud/pagination"
    11  )
    12  
    13  // Attachment contains all the information associated with an OpenStack
    14  // Attachment.
    15  type Attachment struct {
    16  	// ID is the Unique identifier for the attachment.
    17  	ID string `json:"id"`
    18  	// VolumeID is the UUID of the Volume associated with this attachment.
    19  	VolumeID string `json:"volume_id"`
    20  	// Instance is the Instance/Server UUID associated with this attachment.
    21  	Instance string `json:"instance"`
    22  	// AttachedAt is the time the attachment was created.
    23  	AttachedAt time.Time `json:"-"`
    24  	// DetachedAt is the time the attachment was detached.
    25  	DetachedAt time.Time `json:"-"`
    26  	// Status is the current attach status.
    27  	Status string `json:"status"`
    28  	// AttachMode includes things like Read Only etc.
    29  	AttachMode string `json:"attach_mode"`
    30  	// ConnectionInfo is the required info for a node to make a connection
    31  	// provided by the driver.
    32  	ConnectionInfo map[string]interface{} `json:"connection_info"`
    33  }
    34  
    35  // UnmarshalJSON is our unmarshalling helper
    36  func (r *Attachment) UnmarshalJSON(b []byte) error {
    37  	type tmp Attachment
    38  	var s struct {
    39  		tmp
    40  		AttachedAt gophercloud.JSONRFC3339MilliNoZ `json:"attached_at"`
    41  		DetachedAt gophercloud.JSONRFC3339MilliNoZ `json:"detached_at"`
    42  	}
    43  	err := json.Unmarshal(b, &s)
    44  	if err != nil {
    45  		return err
    46  	}
    47  	*r = Attachment(s.tmp)
    48  
    49  	r.AttachedAt = time.Time(s.AttachedAt)
    50  	r.DetachedAt = time.Time(s.DetachedAt)
    51  
    52  	return err
    53  }
    54  
    55  // AttachmentPage is a pagination.pager that is returned from a call to the List
    56  // function.
    57  type AttachmentPage struct {
    58  	pagination.LinkedPageBase
    59  }
    60  
    61  // IsEmpty returns true if a ListResult contains no Attachments.
    62  func (r AttachmentPage) IsEmpty() (bool, error) {
    63  	if r.StatusCode == 204 {
    64  		return true, nil
    65  	}
    66  
    67  	attachments, err := ExtractAttachments(r)
    68  	return len(attachments) == 0, err
    69  }
    70  
    71  // ExtractAttachments extracts and returns Attachments. It is used while
    72  // iterating over a attachment.List call.
    73  func ExtractAttachments(r pagination.Page) ([]Attachment, error) {
    74  	var s []Attachment
    75  	err := ExtractAttachmentsInto(r, &s)
    76  	return s, err
    77  }
    78  
    79  type commonResult struct {
    80  	gophercloud.Result
    81  }
    82  
    83  // Extract will get the Attachment object out of the commonResult object.
    84  func (r commonResult) Extract() (*Attachment, error) {
    85  	var s Attachment
    86  	err := r.ExtractInto(&s)
    87  	return &s, err
    88  }
    89  
    90  // ExtractInto converts our response data into a attachment struct.
    91  func (r commonResult) ExtractInto(a interface{}) error {
    92  	return r.Result.ExtractIntoStructPtr(a, "attachment")
    93  }
    94  
    95  // ExtractAttachmentsInto similar to ExtractInto but operates on a List of
    96  // attachments.
    97  func ExtractAttachmentsInto(r pagination.Page, a interface{}) error {
    98  	return r.(AttachmentPage).Result.ExtractIntoSlicePtr(a, "attachments")
    99  }
   100  
   101  // CreateResult contains the response body and error from a Create request.
   102  type CreateResult struct {
   103  	commonResult
   104  }
   105  
   106  // GetResult contains the response body and error from a Get request.
   107  type GetResult struct {
   108  	commonResult
   109  }
   110  
   111  // UpdateResult contains the response body and error from an Update request.
   112  type UpdateResult struct {
   113  	commonResult
   114  }
   115  
   116  // DeleteResult contains the response body and error from a Delete request.
   117  type DeleteResult struct {
   118  	gophercloud.ErrResult
   119  }
   120  
   121  // CompleteResult contains the response body and error from a Complete request.
   122  type CompleteResult struct {
   123  	gophercloud.ErrResult
   124  }