github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/evs/v2/snapshots/results.go (about)

     1  package snapshots
     2  
     3  import (
     4  	"encoding/json"
     5  	"time"
     6  
     7  	"github.com/huaweicloud/golangsdk"
     8  	"github.com/huaweicloud/golangsdk/pagination"
     9  )
    10  
    11  // Snapshot contains all the information associated with a Cinder Snapshot.
    12  type Snapshot struct {
    13  	// Unique identifier.
    14  	ID string `json:"id"`
    15  
    16  	// Date created.
    17  	CreatedAt time.Time `json:"-"`
    18  
    19  	// Date updated.
    20  	UpdatedAt time.Time `json:"-"`
    21  
    22  	// Display name.
    23  	Name string `json:"name"`
    24  
    25  	// Display description.
    26  	Description string `json:"description"`
    27  
    28  	// ID of the Volume from which this Snapshot was created.
    29  	VolumeID string `json:"volume_id"`
    30  
    31  	// Currect status of the Snapshot.
    32  	Status string `json:"status"`
    33  
    34  	// Size of the Snapshot, in GB.
    35  	Size int `json:"size"`
    36  
    37  	// User-defined key-value pairs.
    38  	Metadata map[string]string `json:"metadata"`
    39  }
    40  
    41  func (r *Snapshot) UnmarshalJSON(b []byte) error {
    42  	type tmp Snapshot
    43  	var s struct {
    44  		tmp
    45  		CreatedAt golangsdk.JSONRFC3339MilliNoZ `json:"created_at"`
    46  		UpdatedAt golangsdk.JSONRFC3339MilliNoZ `json:"updated_at"`
    47  	}
    48  	err := json.Unmarshal(b, &s)
    49  	if err != nil {
    50  		return err
    51  	}
    52  	*r = Snapshot(s.tmp)
    53  
    54  	r.CreatedAt = time.Time(s.CreatedAt)
    55  	r.UpdatedAt = time.Time(s.UpdatedAt)
    56  
    57  	return err
    58  }
    59  
    60  type commonResult struct {
    61  	golangsdk.Result
    62  }
    63  
    64  // CreateResult contains the response body and error from a Create request.
    65  type CreateResult struct {
    66  	commonResult
    67  }
    68  
    69  // GetResult contains the response body and error from a Get request.
    70  type GetResult struct {
    71  	commonResult
    72  }
    73  
    74  // UpdateResult contains the response body and error from an Update request.
    75  type UpdateResult struct {
    76  	commonResult
    77  }
    78  
    79  // DeleteResult contains the response body and error from a Delete request.
    80  type DeleteResult struct {
    81  	golangsdk.ErrResult
    82  }
    83  
    84  // SnapshotPage is a pagination.Pager that is returned from a call to the List function.
    85  type SnapshotPage struct {
    86  	pagination.SinglePageBase
    87  }
    88  
    89  // IsEmpty returns true if a SnapshotPage contains no Snapshots.
    90  func (r SnapshotPage) IsEmpty() (bool, error) {
    91  	volumes, err := ExtractSnapshots(r)
    92  	return len(volumes) == 0, err
    93  }
    94  
    95  // ExtractSnapshots extracts and returns Snapshots. It is used while iterating over a snapshots.List call.
    96  func ExtractSnapshots(r pagination.Page) ([]Snapshot, error) {
    97  	var s struct {
    98  		Snapshots []Snapshot `json:"snapshots"`
    99  	}
   100  	err := (r.(SnapshotPage)).ExtractInto(&s)
   101  	return s.Snapshots, err
   102  }
   103  
   104  // Extract will get the Snapshot object out of the commonResult object.
   105  func (r commonResult) Extract() (*Snapshot, error) {
   106  	var s struct {
   107  		Snapshot *Snapshot `json:"snapshot"`
   108  	}
   109  	err := r.ExtractInto(&s)
   110  	return s.Snapshot, err
   111  }