github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/blockstorage/v3/volumes/results.go (about)

     1  package volumes
     2  
     3  import (
     4  	"encoding/json"
     5  	"time"
     6  
     7  	"github.com/huaweicloud/golangsdk"
     8  	"github.com/huaweicloud/golangsdk/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 golangsdk.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  	// Arbitrary key-value pairs defined by the user.
    67  	Metadata map[string]string `json:"metadata"`
    68  	// UserID is the id of the user who created the volume.
    69  	UserID string `json:"user_id"`
    70  	// Indicates whether this is a bootable volume.
    71  	Bootable string `json:"bootable"`
    72  	// Encrypted denotes if the volume is encrypted.
    73  	Encrypted bool `json:"encrypted"`
    74  	// ReplicationStatus is the status of replication.
    75  	ReplicationStatus string `json:"replication_status"`
    76  	// ConsistencyGroupID is the consistency group ID.
    77  	ConsistencyGroupID string `json:"consistencygroup_id"`
    78  	// Multiattach denotes if the volume is multi-attach capable.
    79  	Multiattach bool `json:"multiattach"`
    80  }
    81  
    82  // UnmarshalJSON another unmarshalling function
    83  func (r *Volume) UnmarshalJSON(b []byte) error {
    84  	type tmp Volume
    85  	var s struct {
    86  		tmp
    87  		CreatedAt golangsdk.JSONRFC3339MilliNoZ `json:"created_at"`
    88  		UpdatedAt golangsdk.JSONRFC3339MilliNoZ `json:"updated_at"`
    89  	}
    90  	err := json.Unmarshal(b, &s)
    91  	if err != nil {
    92  		return err
    93  	}
    94  	*r = Volume(s.tmp)
    95  
    96  	r.CreatedAt = time.Time(s.CreatedAt)
    97  	r.UpdatedAt = time.Time(s.UpdatedAt)
    98  
    99  	return err
   100  }
   101  
   102  // VolumePage is a pagination.pager that is returned from a call to the List function.
   103  type VolumePage struct {
   104  	pagination.LinkedPageBase
   105  }
   106  
   107  // IsEmpty returns true if a ListResult contains no Volumes.
   108  func (r VolumePage) IsEmpty() (bool, error) {
   109  	volumes, err := ExtractVolumes(r)
   110  	return len(volumes) == 0, err
   111  }
   112  
   113  func (page VolumePage) NextPageURL() (string, error) {
   114  	var s struct {
   115  		Links []golangsdk.Link `json:"volumes_links"`
   116  	}
   117  	err := page.ExtractInto(&s)
   118  	if err != nil {
   119  		return "", err
   120  	}
   121  	return golangsdk.ExtractNextURL(s.Links)
   122  }
   123  
   124  // ExtractVolumes extracts and returns Volumes. It is used while iterating over a volumes.List call.
   125  func ExtractVolumes(r pagination.Page) ([]Volume, error) {
   126  	var s []Volume
   127  	err := ExtractVolumesInto(r, &s)
   128  	return s, err
   129  }
   130  
   131  type commonResult struct {
   132  	golangsdk.Result
   133  }
   134  
   135  // Extract will get the Volume object out of the commonResult object.
   136  func (r commonResult) Extract() (*Volume, error) {
   137  	var s Volume
   138  	err := r.ExtractInto(&s)
   139  	return &s, err
   140  }
   141  
   142  // ExtractInto converts our response data into a volume struct
   143  func (r commonResult) ExtractInto(v interface{}) error {
   144  	return r.Result.ExtractIntoStructPtr(v, "volume")
   145  }
   146  
   147  // ExtractVolumesInto similar to ExtractInto but operates on a `list` of volumes
   148  func ExtractVolumesInto(r pagination.Page, v interface{}) error {
   149  	return r.(VolumePage).Result.ExtractIntoSlicePtr(v, "volumes")
   150  }
   151  
   152  // CreateResult contains the response body and error from a Create request.
   153  type CreateResult struct {
   154  	commonResult
   155  }
   156  
   157  // GetResult contains the response body and error from a Get request.
   158  type GetResult struct {
   159  	commonResult
   160  }
   161  
   162  // UpdateResult contains the response body and error from an Update request.
   163  type UpdateResult struct {
   164  	commonResult
   165  }
   166  
   167  // DeleteResult contains the response body and error from a Delete request.
   168  type DeleteResult struct {
   169  	golangsdk.ErrResult
   170  }