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