github.com/gophercloud/gophercloud@v1.11.0/openstack/blockstorage/v2/volumes/results.go (about)

     1  package volumes
     2  
     3  import (
     4  	"encoding/json"
     5  	"time"
     6  
     7  	"github.com/gophercloud/gophercloud"
     8  	"github.com/gophercloud/gophercloud/pagination"
     9  )
    10  
    11  type Attachment struct {
    12  	AttachedAt   time.Time `json:"-"`
    13  	AttachmentID string    `json:"attachment_id"`
    14  	Device       string    `json:"device"`
    15  	HostName     string    `json:"host_name"`
    16  	ID           string    `json:"id"`
    17  	ServerID     string    `json:"server_id"`
    18  	VolumeID     string    `json:"volume_id"`
    19  }
    20  
    21  func (r *Attachment) UnmarshalJSON(b []byte) error {
    22  	type tmp Attachment
    23  	var s struct {
    24  		tmp
    25  		AttachedAt gophercloud.JSONRFC3339MilliNoZ `json:"attached_at"`
    26  	}
    27  	err := json.Unmarshal(b, &s)
    28  	if err != nil {
    29  		return err
    30  	}
    31  	*r = Attachment(s.tmp)
    32  
    33  	r.AttachedAt = time.Time(s.AttachedAt)
    34  
    35  	return err
    36  }
    37  
    38  // Volume contains all the information associated with an OpenStack Volume.
    39  type Volume struct {
    40  	// Unique identifier for the volume.
    41  	ID string `json:"id"`
    42  	// Current status of the volume.
    43  	Status string `json:"status"`
    44  	// Size of the volume in GB.
    45  	Size int `json:"size"`
    46  	// AvailabilityZone is which availability zone the volume is in.
    47  	AvailabilityZone string `json:"availability_zone"`
    48  	// The date when this volume was created.
    49  	CreatedAt time.Time `json:"-"`
    50  	// The date when this volume was last updated
    51  	UpdatedAt time.Time `json:"-"`
    52  	// Instances onto which the volume is attached.
    53  	Attachments []Attachment `json:"attachments"`
    54  	// Human-readable display name for the volume.
    55  	Name string `json:"name"`
    56  	// Human-readable description for the volume.
    57  	Description string `json:"description"`
    58  	// The type of volume to create, either SATA or SSD.
    59  	VolumeType string `json:"volume_type"`
    60  	// The ID of the snapshot from which the volume was created
    61  	SnapshotID string `json:"snapshot_id"`
    62  	// The ID of another block storage volume from which the current volume was created
    63  	SourceVolID string `json:"source_volid"`
    64  	// Arbitrary key-value pairs defined by the user.
    65  	Metadata map[string]string `json:"metadata"`
    66  	// UserID is the id of the user who created the volume.
    67  	UserID string `json:"user_id"`
    68  	// Indicates whether this is a bootable volume.
    69  	Bootable string `json:"bootable"`
    70  	// Encrypted denotes if the volume is encrypted.
    71  	Encrypted bool `json:"encrypted"`
    72  	// ReplicationStatus is the status of replication.
    73  	ReplicationStatus string `json:"replication_status"`
    74  	// ConsistencyGroupID is the consistency group ID.
    75  	ConsistencyGroupID string `json:"consistencygroup_id"`
    76  	// Multiattach denotes if the volume is multi-attach capable.
    77  	Multiattach bool `json:"multiattach"`
    78  }
    79  
    80  func (r *Volume) UnmarshalJSON(b []byte) error {
    81  	type tmp Volume
    82  	var s struct {
    83  		tmp
    84  		CreatedAt gophercloud.JSONRFC3339MilliNoZ `json:"created_at"`
    85  		UpdatedAt gophercloud.JSONRFC3339MilliNoZ `json:"updated_at"`
    86  	}
    87  	err := json.Unmarshal(b, &s)
    88  	if err != nil {
    89  		return err
    90  	}
    91  	*r = Volume(s.tmp)
    92  
    93  	r.CreatedAt = time.Time(s.CreatedAt)
    94  	r.UpdatedAt = time.Time(s.UpdatedAt)
    95  
    96  	return err
    97  }
    98  
    99  // VolumePage is a pagination.pager that is returned from a call to the List function.
   100  type VolumePage struct {
   101  	pagination.LinkedPageBase
   102  }
   103  
   104  // IsEmpty returns true if a ListResult contains no Volumes.
   105  func (r VolumePage) IsEmpty() (bool, error) {
   106  	if r.StatusCode == 204 {
   107  		return true, nil
   108  	}
   109  
   110  	volumes, err := ExtractVolumes(r)
   111  	return len(volumes) == 0, err
   112  }
   113  
   114  // NextPageURL uses the response's embedded link reference to navigate to the
   115  // next page of results.
   116  func (r VolumePage) NextPageURL() (string, error) {
   117  	var s struct {
   118  		Links []gophercloud.Link `json:"volumes_links"`
   119  	}
   120  	err := r.ExtractInto(&s)
   121  	if err != nil {
   122  		return "", err
   123  	}
   124  	return gophercloud.ExtractNextURL(s.Links)
   125  }
   126  
   127  // ExtractVolumes extracts and returns Volumes. It is used while iterating over a volumes.List call.
   128  func ExtractVolumes(r pagination.Page) ([]Volume, error) {
   129  	var s []Volume
   130  	err := ExtractVolumesInto(r, &s)
   131  	return s, err
   132  }
   133  
   134  type commonResult struct {
   135  	gophercloud.Result
   136  }
   137  
   138  // Extract will get the Volume object out of the commonResult object.
   139  func (r commonResult) Extract() (*Volume, error) {
   140  	var s Volume
   141  	err := r.ExtractInto(&s)
   142  	return &s, err
   143  }
   144  
   145  func (r commonResult) ExtractInto(v interface{}) error {
   146  	return r.Result.ExtractIntoStructPtr(v, "volume")
   147  }
   148  
   149  func ExtractVolumesInto(r pagination.Page, v interface{}) error {
   150  	return r.(VolumePage).Result.ExtractIntoSlicePtr(v, "volumes")
   151  }
   152  
   153  // CreateResult contains the response body and error from a Create request.
   154  type CreateResult struct {
   155  	commonResult
   156  }
   157  
   158  // GetResult contains the response body and error from a Get request.
   159  type GetResult struct {
   160  	commonResult
   161  }
   162  
   163  // UpdateResult contains the response body and error from an Update request.
   164  type UpdateResult struct {
   165  	commonResult
   166  }
   167  
   168  // DeleteResult contains the response body and error from a Delete request.
   169  type DeleteResult struct {
   170  	gophercloud.ErrResult
   171  }