github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/blockstorage/v1/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 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 golangsdk.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  	golangsdk.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  	volumes, err := ExtractSnapshots(r)
    93  	return len(volumes) == 0, err
    94  }
    95  
    96  // ExtractSnapshots extracts and returns Snapshots. It is used while iterating over a snapshots.List call.
    97  func ExtractSnapshots(r pagination.Page) ([]Snapshot, error) {
    98  	var s struct {
    99  		Snapshots []Snapshot `json:"snapshots"`
   100  	}
   101  	err := (r.(SnapshotPage)).ExtractInto(&s)
   102  	return s.Snapshots, err
   103  }
   104  
   105  // UpdateMetadataResult contains the response body and error from an UpdateMetadata request.
   106  type UpdateMetadataResult struct {
   107  	commonResult
   108  }
   109  
   110  // ExtractMetadata returns the metadata from a response from snapshots.UpdateMetadata.
   111  func (r UpdateMetadataResult) ExtractMetadata() (map[string]interface{}, error) {
   112  	if r.Err != nil {
   113  		return nil, r.Err
   114  	}
   115  	m := r.Body.(map[string]interface{})["metadata"]
   116  	return m.(map[string]interface{}), nil
   117  }
   118  
   119  type commonResult struct {
   120  	golangsdk.Result
   121  }
   122  
   123  // Extract will get the Snapshot object out of the commonResult object.
   124  func (r commonResult) Extract() (*Snapshot, error) {
   125  	var s struct {
   126  		Snapshot *Snapshot `json:"snapshot"`
   127  	}
   128  	err := r.ExtractInto(&s)
   129  	return s.Snapshot, err
   130  }