github.com/chnsz/golangsdk@v0.0.0-20240506093406-85a3fbfa605b/openstack/blockstorage/v2/volumes/results.go (about)

     1  package volumes
     2  
     3  import (
     4  	"encoding/json"
     5  	"time"
     6  
     7  	"github.com/chnsz/golangsdk"
     8  	"github.com/chnsz/golangsdk/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 golangsdk.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  	// The IOPS of the volume. Only exist when volume_type is `ESSD2` or `GPSSD2`
    79  	IOPS IOPSAndThroughput `json:"iops"`
    80  	// The throughput of the volume. Only exist when volume_type is `GPSSD2`
    81  	Throughput IOPSAndThroughput `json:"throughput"`
    82  }
    83  
    84  // IOPSAndThroughput is the struct of IOPS and throughput
    85  type IOPSAndThroughput struct {
    86  	// The frozened mark
    87  	Frozened bool `json:"frozened"`
    88  	// The iops or throughput id
    89  	ID string `json:"id"`
    90  	// The iops or throughput value
    91  	TotalVal int `json:"total_val"`
    92  	// The volume ID
    93  	VolumeId string `json:"volume_id"`
    94  }
    95  
    96  func (r *Volume) UnmarshalJSON(b []byte) error {
    97  	type tmp Volume
    98  	var s struct {
    99  		tmp
   100  		CreatedAt golangsdk.JSONRFC3339MilliNoZ `json:"created_at"`
   101  		UpdatedAt golangsdk.JSONRFC3339MilliNoZ `json:"updated_at"`
   102  	}
   103  	err := json.Unmarshal(b, &s)
   104  	if err != nil {
   105  		return err
   106  	}
   107  	*r = Volume(s.tmp)
   108  
   109  	r.CreatedAt = time.Time(s.CreatedAt)
   110  	r.UpdatedAt = time.Time(s.UpdatedAt)
   111  
   112  	return err
   113  }
   114  
   115  // VolumePage is a pagination.pager that is returned from a call to the List function.
   116  type VolumePage struct {
   117  	pagination.LinkedPageBase
   118  }
   119  
   120  // IsEmpty returns true if a ListResult contains no Volumes.
   121  func (r VolumePage) IsEmpty() (bool, error) {
   122  	volumes, err := ExtractVolumes(r)
   123  	return len(volumes) == 0, err
   124  }
   125  
   126  // NextPageURL uses the response's embedded link reference to navigate to the
   127  // next page of results.
   128  func (r VolumePage) NextPageURL() (string, error) {
   129  	var s struct {
   130  		Links []golangsdk.Link `json:"volumes_links"`
   131  	}
   132  	err := r.ExtractInto(&s)
   133  	if err != nil {
   134  		return "", err
   135  	}
   136  	return golangsdk.ExtractNextURL(s.Links)
   137  }
   138  
   139  // ExtractVolumes extracts and returns Volumes. It is used while iterating over a volumes.List call.
   140  func ExtractVolumes(r pagination.Page) ([]Volume, error) {
   141  	var s []Volume
   142  	err := ExtractVolumesInto(r, &s)
   143  	return s, err
   144  }
   145  
   146  type commonResult struct {
   147  	golangsdk.Result
   148  }
   149  
   150  // Extract will get the Volume object out of the commonResult object.
   151  func (r commonResult) Extract() (*Volume, error) {
   152  	var s Volume
   153  	err := r.ExtractInto(&s)
   154  	return &s, err
   155  }
   156  
   157  func (r commonResult) ExtractInto(v interface{}) error {
   158  	return r.Result.ExtractIntoStructPtr(v, "volume")
   159  }
   160  
   161  func ExtractVolumesInto(r pagination.Page, v interface{}) error {
   162  	return r.(VolumePage).Result.ExtractIntoSlicePtr(v, "volumes")
   163  }
   164  
   165  // CreateResult contains the response body and error from a Create request.
   166  type CreateResult struct {
   167  	commonResult
   168  }
   169  
   170  // GetResult contains the response body and error from a Get request.
   171  type GetResult struct {
   172  	commonResult
   173  }
   174  
   175  // UpdateResult contains the response body and error from an Update request.
   176  type UpdateResult struct {
   177  	commonResult
   178  }
   179  
   180  // DeleteResult contains the response body and error from a Delete request.
   181  type DeleteResult struct {
   182  	golangsdk.ErrResult
   183  }