github.com/gophercloud/gophercloud@v1.11.0/openstack/blockstorage/v2/snapshots/results.go (about)

     1  package snapshots
     2  
     3  import (
     4  	"encoding/json"
     5  	"time"
     6  
     7  	"github.com/gophercloud/gophercloud"
     8  	"github.com/gophercloud/gophercloud/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  // CreateResult contains the response body and error from a Create request.
    42  type CreateResult struct {
    43  	commonResult
    44  }
    45  
    46  // GetResult contains the response body and error from a Get request.
    47  type GetResult struct {
    48  	commonResult
    49  }
    50  
    51  // DeleteResult contains the response body and error from a Delete request.
    52  type DeleteResult struct {
    53  	gophercloud.ErrResult
    54  }
    55  
    56  // SnapshotPage is a pagination.Pager that is returned from a call to the List function.
    57  type SnapshotPage struct {
    58  	pagination.SinglePageBase
    59  }
    60  
    61  func (r *Snapshot) UnmarshalJSON(b []byte) error {
    62  	type tmp Snapshot
    63  	var s struct {
    64  		tmp
    65  		CreatedAt gophercloud.JSONRFC3339MilliNoZ `json:"created_at"`
    66  		UpdatedAt gophercloud.JSONRFC3339MilliNoZ `json:"updated_at"`
    67  	}
    68  	err := json.Unmarshal(b, &s)
    69  	if err != nil {
    70  		return err
    71  	}
    72  	*r = Snapshot(s.tmp)
    73  
    74  	r.CreatedAt = time.Time(s.CreatedAt)
    75  	r.UpdatedAt = time.Time(s.UpdatedAt)
    76  
    77  	return err
    78  }
    79  
    80  // IsEmpty returns true if a SnapshotPage contains no Snapshots.
    81  func (r SnapshotPage) IsEmpty() (bool, error) {
    82  	if r.StatusCode == 204 {
    83  		return true, nil
    84  	}
    85  
    86  	volumes, err := ExtractSnapshots(r)
    87  	return len(volumes) == 0, err
    88  }
    89  
    90  // ExtractSnapshots extracts and returns Snapshots. It is used while iterating over a snapshots.List call.
    91  func ExtractSnapshots(r pagination.Page) ([]Snapshot, error) {
    92  	var s struct {
    93  		Snapshots []Snapshot `json:"snapshots"`
    94  	}
    95  	err := (r.(SnapshotPage)).ExtractInto(&s)
    96  	return s.Snapshots, err
    97  }
    98  
    99  // UpdateMetadataResult contains the response body and error from an UpdateMetadata request.
   100  type UpdateMetadataResult struct {
   101  	commonResult
   102  }
   103  
   104  // ExtractMetadata returns the metadata from a response from snapshots.UpdateMetadata.
   105  func (r UpdateMetadataResult) ExtractMetadata() (map[string]interface{}, error) {
   106  	if r.Err != nil {
   107  		return nil, r.Err
   108  	}
   109  	m := r.Body.(map[string]interface{})["metadata"]
   110  	return m.(map[string]interface{}), nil
   111  }
   112  
   113  type commonResult struct {
   114  	gophercloud.Result
   115  }
   116  
   117  // Extract will get the Snapshot object out of the commonResult object.
   118  func (r commonResult) Extract() (*Snapshot, error) {
   119  	var s struct {
   120  		Snapshot *Snapshot `json:"snapshot"`
   121  	}
   122  	err := r.ExtractInto(&s)
   123  	return s.Snapshot, err
   124  }