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