github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/openstack/blockstorage/v2/snapshots/results.go (about)

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