github.com/gophercloud/gophercloud@v1.11.0/openstack/blockstorage/v1/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 an OpenStack Snapshot.
    12  type Snapshot struct {
    13  	// Currect status of the Snapshot.
    14  	Status string `json:"status"`
    15  
    16  	// Display name.
    17  	Name string `json:"display_name"`
    18  
    19  	// Instances onto which the Snapshot is attached.
    20  	Attachments []string `json:"attachments"`
    21  
    22  	// Logical group.
    23  	AvailabilityZone string `json:"availability_zone"`
    24  
    25  	// Is the Snapshot bootable?
    26  	Bootable string `json:"bootable"`
    27  
    28  	// Date created.
    29  	CreatedAt time.Time `json:"-"`
    30  
    31  	// Display description.
    32  	Description string `json:"display_description"`
    33  
    34  	// See VolumeType object for more information.
    35  	VolumeType string `json:"volume_type"`
    36  
    37  	// ID of the Snapshot from which this Snapshot was created.
    38  	SnapshotID string `json:"snapshot_id"`
    39  
    40  	// ID of the Volume from which this Snapshot was created.
    41  	VolumeID string `json:"volume_id"`
    42  
    43  	// User-defined key-value pairs.
    44  	Metadata map[string]string `json:"metadata"`
    45  
    46  	// Unique identifier.
    47  	ID string `json:"id"`
    48  
    49  	// Size of the Snapshot, in GB.
    50  	Size int `json:"size"`
    51  }
    52  
    53  func (r *Snapshot) UnmarshalJSON(b []byte) error {
    54  	type tmp Snapshot
    55  	var s struct {
    56  		tmp
    57  		CreatedAt gophercloud.JSONRFC3339MilliNoZ `json:"created_at"`
    58  	}
    59  	err := json.Unmarshal(b, &s)
    60  	if err != nil {
    61  		return err
    62  	}
    63  	*r = Snapshot(s.tmp)
    64  
    65  	r.CreatedAt = time.Time(s.CreatedAt)
    66  
    67  	return err
    68  }
    69  
    70  // CreateResult contains the response body and error from a Create request.
    71  type CreateResult struct {
    72  	commonResult
    73  }
    74  
    75  // GetResult contains the response body and error from a Get request.
    76  type GetResult struct {
    77  	commonResult
    78  }
    79  
    80  // DeleteResult contains the response body and error from a Delete request.
    81  type DeleteResult struct {
    82  	gophercloud.ErrResult
    83  }
    84  
    85  // SnapshotPage is a pagination.Pager that is returned from a call to the List function.
    86  type SnapshotPage struct {
    87  	pagination.SinglePageBase
    88  }
    89  
    90  // IsEmpty returns true if a SnapshotPage contains no Snapshots.
    91  func (r SnapshotPage) IsEmpty() (bool, error) {
    92  	if r.StatusCode == 204 {
    93  		return true, nil
    94  	}
    95  
    96  	volumes, err := ExtractSnapshots(r)
    97  	return len(volumes) == 0, err
    98  }
    99  
   100  // ExtractSnapshots extracts and returns Snapshots. It is used while iterating over a snapshots.List call.
   101  func ExtractSnapshots(r pagination.Page) ([]Snapshot, error) {
   102  	var s struct {
   103  		Snapshots []Snapshot `json:"snapshots"`
   104  	}
   105  	err := (r.(SnapshotPage)).ExtractInto(&s)
   106  	return s.Snapshots, err
   107  }
   108  
   109  // UpdateMetadataResult contains the response body and error from an UpdateMetadata request.
   110  type UpdateMetadataResult struct {
   111  	commonResult
   112  }
   113  
   114  // ExtractMetadata returns the metadata from a response from snapshots.UpdateMetadata.
   115  func (r UpdateMetadataResult) ExtractMetadata() (map[string]interface{}, error) {
   116  	if r.Err != nil {
   117  		return nil, r.Err
   118  	}
   119  	m := r.Body.(map[string]interface{})["metadata"]
   120  	return m.(map[string]interface{}), nil
   121  }
   122  
   123  type commonResult struct {
   124  	gophercloud.Result
   125  }
   126  
   127  // Extract will get the Snapshot object out of the commonResult object.
   128  func (r commonResult) Extract() (*Snapshot, error) {
   129  	var s struct {
   130  		Snapshot *Snapshot `json:"snapshot"`
   131  	}
   132  	err := r.ExtractInto(&s)
   133  	return s.Snapshot, err
   134  }