github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/openstack/blockstorage/v3/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.LinkedPageBase
    60  }
    61  
    62  // UnmarshalJSON converts our JSON API response into our snapshot struct
    63  func (r *Snapshot) UnmarshalJSON(b []byte) error {
    64  	type tmp Snapshot
    65  	var s struct {
    66  		tmp
    67  		CreatedAt golangsdk.JSONRFC3339MilliNoZ `json:"created_at"`
    68  		UpdatedAt golangsdk.JSONRFC3339MilliNoZ `json:"updated_at"`
    69  	}
    70  	err := json.Unmarshal(b, &s)
    71  	if err != nil {
    72  		return err
    73  	}
    74  	*r = Snapshot(s.tmp)
    75  
    76  	r.CreatedAt = time.Time(s.CreatedAt)
    77  	r.UpdatedAt = time.Time(s.UpdatedAt)
    78  
    79  	return err
    80  }
    81  
    82  // IsEmpty returns true if a SnapshotPage contains no Snapshots.
    83  func (r SnapshotPage) IsEmpty() (bool, error) {
    84  	volumes, err := ExtractSnapshots(r)
    85  	return len(volumes) == 0, err
    86  }
    87  
    88  func (r SnapshotPage) NextPageURL() (string, error) {
    89  	var s struct {
    90  		Links []golangsdk.Link `json:"snapshots_links"`
    91  	}
    92  	err := r.ExtractInto(&s)
    93  	if err != nil {
    94  		return "", err
    95  	}
    96  	return golangsdk.ExtractNextURL(s.Links)
    97  }
    98  
    99  // ExtractSnapshots extracts and returns Snapshots. It is used while iterating over a snapshots.List call.
   100  func ExtractSnapshots(r pagination.Page) ([]Snapshot, error) {
   101  	var s struct {
   102  		Snapshots []Snapshot `json:"snapshots"`
   103  	}
   104  	err := (r.(SnapshotPage)).ExtractInto(&s)
   105  	return s.Snapshots, err
   106  }
   107  
   108  // UpdateMetadataResult contains the response body and error from an UpdateMetadata request.
   109  type UpdateMetadataResult struct {
   110  	commonResult
   111  }
   112  
   113  // ExtractMetadata returns the metadata from a response from snapshots.UpdateMetadata.
   114  func (r UpdateMetadataResult) ExtractMetadata() (map[string]interface{}, error) {
   115  	return metadata.Extract(r.BodyReader())
   116  }
   117  
   118  type commonResult struct {
   119  	golangsdk.Result
   120  }
   121  
   122  // Extract will get the Snapshot object out of the commonResult object.
   123  func (r commonResult) Extract() (*Snapshot, error) {
   124  	var s struct {
   125  		Snapshot *Snapshot `json:"snapshot"`
   126  	}
   127  	err := r.ExtractInto(&s)
   128  	return s.Snapshot, err
   129  }