github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/blockstorage/v2/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  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  }
    79  
    80  func (r *Volume) UnmarshalJSON(b []byte) error {
    81  	type tmp Volume
    82  	var s struct {
    83  		tmp
    84  		CreatedAt golangsdk.JSONRFC3339MilliNoZ `json:"created_at"`
    85  		UpdatedAt golangsdk.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  	volumes, err := ExtractVolumes(r)
   107  	return len(volumes) == 0, err
   108  }
   109  
   110  // NextPageURL uses the response's embedded link reference to navigate to the
   111  // next page of results.
   112  func (r VolumePage) NextPageURL() (string, error) {
   113  	var s struct {
   114  		Links []golangsdk.Link `json:"volumes_links"`
   115  	}
   116  	err := r.ExtractInto(&s)
   117  	if err != nil {
   118  		return "", err
   119  	}
   120  	return golangsdk.ExtractNextURL(s.Links)
   121  }
   122  
   123  // ExtractVolumes extracts and returns Volumes. It is used while iterating over a volumes.List call.
   124  func ExtractVolumes(r pagination.Page) ([]Volume, error) {
   125  	var s []Volume
   126  	err := ExtractVolumesInto(r, &s)
   127  	return s, err
   128  }
   129  
   130  type commonResult struct {
   131  	golangsdk.Result
   132  }
   133  
   134  // Extract will get the Volume object out of the commonResult object.
   135  func (r commonResult) Extract() (*Volume, error) {
   136  	var s Volume
   137  	err := r.ExtractInto(&s)
   138  	return &s, err
   139  }
   140  
   141  func (r commonResult) ExtractInto(v interface{}) error {
   142  	return r.Result.ExtractIntoStructPtr(v, "volume")
   143  }
   144  
   145  func ExtractVolumesInto(r pagination.Page, v interface{}) error {
   146  	return r.(VolumePage).Result.ExtractIntoSlicePtr(v, "volumes")
   147  }
   148  
   149  // CreateResult contains the response body and error from a Create request.
   150  type CreateResult struct {
   151  	commonResult
   152  }
   153  
   154  // GetResult contains the response body and error from a Get request.
   155  type GetResult struct {
   156  	commonResult
   157  }
   158  
   159  // UpdateResult contains the response body and error from an Update request.
   160  type UpdateResult struct {
   161  	commonResult
   162  }
   163  
   164  // DeleteResult contains the response body and error from a Delete request.
   165  type DeleteResult struct {
   166  	golangsdk.ErrResult
   167  }