github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/blockstorage/v1/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  // Volume contains all the information associated with an OpenStack Volume.
    12  type Volume struct {
    13  	// Current status of the volume.
    14  	Status string `json:"status"`
    15  	// Human-readable display name for the volume.
    16  	Name string `json:"display_name"`
    17  	// Instances onto which the volume is attached.
    18  	Attachments []map[string]interface{} `json:"attachments"`
    19  	// This parameter is no longer used.
    20  	AvailabilityZone string `json:"availability_zone"`
    21  	// Indicates whether this is a bootable volume.
    22  	Bootable string `json:"bootable"`
    23  	// The date when this volume was created.
    24  	CreatedAt time.Time `json:"-"`
    25  	// Human-readable description for the volume.
    26  	Description string `json:"display_description"`
    27  	// The type of volume to create, either SATA or SSD.
    28  	VolumeType string `json:"volume_type"`
    29  	// The ID of the snapshot from which the volume was created
    30  	SnapshotID string `json:"snapshot_id"`
    31  	// The ID of another block storage volume from which the current volume was created
    32  	SourceVolID string `json:"source_volid"`
    33  	// Arbitrary key-value pairs defined by the user.
    34  	Metadata map[string]string `json:"metadata"`
    35  	// Unique identifier for the volume.
    36  	ID string `json:"id"`
    37  	// Size of the volume in GB.
    38  	Size int `json:"size"`
    39  }
    40  
    41  func (r *Volume) UnmarshalJSON(b []byte) error {
    42  	type tmp Volume
    43  	var s struct {
    44  		tmp
    45  		CreatedAt golangsdk.JSONRFC3339MilliNoZ `json:"created_at"`
    46  	}
    47  	err := json.Unmarshal(b, &s)
    48  	if err != nil {
    49  		return err
    50  	}
    51  	*r = Volume(s.tmp)
    52  
    53  	r.CreatedAt = time.Time(s.CreatedAt)
    54  
    55  	return err
    56  }
    57  
    58  // CreateResult contains the response body and error from a Create request.
    59  type CreateResult struct {
    60  	commonResult
    61  }
    62  
    63  // GetResult contains the response body and error from a Get request.
    64  type GetResult struct {
    65  	commonResult
    66  }
    67  
    68  // DeleteResult contains the response body and error from a Delete request.
    69  type DeleteResult struct {
    70  	golangsdk.ErrResult
    71  }
    72  
    73  // VolumePage is a pagination.pager that is returned from a call to the List function.
    74  type VolumePage struct {
    75  	pagination.SinglePageBase
    76  }
    77  
    78  // IsEmpty returns true if a VolumePage contains no Volumes.
    79  func (r VolumePage) IsEmpty() (bool, error) {
    80  	volumes, err := ExtractVolumes(r)
    81  	return len(volumes) == 0, err
    82  }
    83  
    84  // ExtractVolumes extracts and returns Volumes. It is used while iterating over a volumes.List call.
    85  func ExtractVolumes(r pagination.Page) ([]Volume, error) {
    86  	var s struct {
    87  		Volumes []Volume `json:"volumes"`
    88  	}
    89  	err := (r.(VolumePage)).ExtractInto(&s)
    90  	return s.Volumes, err
    91  }
    92  
    93  // UpdateResult contains the response body and error from an Update request.
    94  type UpdateResult struct {
    95  	commonResult
    96  }
    97  
    98  type commonResult struct {
    99  	golangsdk.Result
   100  }
   101  
   102  // Extract will get the Volume object out of the commonResult object.
   103  func (r commonResult) Extract() (*Volume, error) {
   104  	var s struct {
   105  		Volume *Volume `json:"volume"`
   106  	}
   107  	err := r.ExtractInto(&s)
   108  	return s.Volume, err
   109  }