123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- package volumes
- import (
- "encoding/json"
- "time"
- "devel.mephi.ru/iacherepanov/openstack-gophercloud"
- "devel.mephi.ru/iacherepanov/openstack-gophercloud/pagination"
- )
- // Attachment represents a Volume Attachment record
- type Attachment struct {
- AttachedAt time.Time `json:"-"`
- AttachmentID string `json:"attachment_id"`
- Device string `json:"device"`
- HostName string `json:"host_name"`
- ID string `json:"id"`
- ServerID string `json:"server_id"`
- VolumeID string `json:"volume_id"`
- }
- // UnmarshalJSON is our unmarshalling helper
- func (r *Attachment) UnmarshalJSON(b []byte) error {
- type tmp Attachment
- var s struct {
- tmp
- AttachedAt gophercloud.JSONRFC3339MilliNoZ `json:"attached_at"`
- }
- err := json.Unmarshal(b, &s)
- if err != nil {
- return err
- }
- *r = Attachment(s.tmp)
- r.AttachedAt = time.Time(s.AttachedAt)
- return err
- }
- // Volume contains all the information associated with an OpenStack Volume.
- type Volume struct {
- // Unique identifier for the volume.
- ID string `json:"id"`
- // Current status of the volume.
- Status string `json:"status"`
- // Size of the volume in GB.
- Size int `json:"size"`
- // AvailabilityZone is which availability zone the volume is in.
- AvailabilityZone string `json:"availability_zone"`
- // The date when this volume was created.
- CreatedAt time.Time `json:"-"`
- // The date when this volume was last updated
- UpdatedAt time.Time `json:"-"`
- // Instances onto which the volume is attached.
- Attachments []Attachment `json:"attachments"`
- // Human-readable display name for the volume.
- Name string `json:"name"`
- // Human-readable description for the volume.
- Description string `json:"description"`
- // The type of volume to create, either SATA or SSD.
- VolumeType string `json:"volume_type"`
- // The ID of the snapshot from which the volume was created
- SnapshotID string `json:"snapshot_id"`
- // The ID of another block storage volume from which the current volume was created
- SourceVolID string `json:"source_volid"`
- // Arbitrary key-value pairs defined by the user.
- Metadata map[string]string `json:"metadata"`
- // UserID is the id of the user who created the volume.
- UserID string `json:"user_id"`
- // Indicates whether this is a bootable volume.
- Bootable string `json:"bootable"`
- // Encrypted denotes if the volume is encrypted.
- Encrypted bool `json:"encrypted"`
- // ReplicationStatus is the status of replication.
- ReplicationStatus string `json:"replication_status"`
- // ConsistencyGroupID is the consistency group ID.
- ConsistencyGroupID string `json:"consistencygroup_id"`
- // Multiattach denotes if the volume is multi-attach capable.
- Multiattach bool `json:"multiattach"`
- }
- // UnmarshalJSON another unmarshalling function
- func (r *Volume) UnmarshalJSON(b []byte) error {
- type tmp Volume
- var s struct {
- tmp
- CreatedAt gophercloud.JSONRFC3339MilliNoZ `json:"created_at"`
- UpdatedAt gophercloud.JSONRFC3339MilliNoZ `json:"updated_at"`
- }
- err := json.Unmarshal(b, &s)
- if err != nil {
- return err
- }
- *r = Volume(s.tmp)
- r.CreatedAt = time.Time(s.CreatedAt)
- r.UpdatedAt = time.Time(s.UpdatedAt)
- return err
- }
- // VolumePage is a pagination.pager that is returned from a call to the List function.
- type VolumePage struct {
- pagination.LinkedPageBase
- }
- // IsEmpty returns true if a ListResult contains no Volumes.
- func (r VolumePage) IsEmpty() (bool, error) {
- volumes, err := ExtractVolumes(r)
- return len(volumes) == 0, err
- }
- func (page VolumePage) NextPageURL() (string, error) {
- var s struct {
- Links []gophercloud.Link `json:"volumes_links"`
- }
- err := page.ExtractInto(&s)
- if err != nil {
- return "", err
- }
- return gophercloud.ExtractNextURL(s.Links)
- }
- // ExtractVolumes extracts and returns Volumes. It is used while iterating over a volumes.List call.
- func ExtractVolumes(r pagination.Page) ([]Volume, error) {
- var s []Volume
- err := ExtractVolumesInto(r, &s)
- return s, err
- }
- type commonResult struct {
- gophercloud.Result
- }
- // Extract will get the Volume object out of the commonResult object.
- func (r commonResult) Extract() (*Volume, error) {
- var s Volume
- err := r.ExtractInto(&s)
- return &s, err
- }
- // ExtractInto converts our response data into a volume struct
- func (r commonResult) ExtractInto(v interface{}) error {
- return r.Result.ExtractIntoStructPtr(v, "volume")
- }
- // ExtractVolumesInto similar to ExtractInto but operates on a `list` of volumes
- func ExtractVolumesInto(r pagination.Page, v interface{}) error {
- return r.(VolumePage).Result.ExtractIntoSlicePtr(v, "volumes")
- }
- // CreateResult contains the response body and error from a Create request.
- type CreateResult struct {
- commonResult
- }
- // GetResult contains the response body and error from a Get request.
- type GetResult struct {
- commonResult
- }
- // UpdateResult contains the response body and error from an Update request.
- type UpdateResult struct {
- commonResult
- }
- // DeleteResult contains the response body and error from a Delete request.
- type DeleteResult struct {
- gophercloud.ErrResult
- }
|