github.com/gophercloud/gophercloud@v1.11.0/openstack/db/v1/datastores/results.go (about) 1 package datastores 2 3 import ( 4 "github.com/gophercloud/gophercloud" 5 "github.com/gophercloud/gophercloud/pagination" 6 ) 7 8 // Version represents a version API resource. Multiple versions belong to a Datastore. 9 type Version struct { 10 ID string 11 Links []gophercloud.Link 12 Name string 13 } 14 15 // Datastore represents a Datastore API resource. 16 type Datastore struct { 17 DefaultVersion string `json:"default_version"` 18 ID string 19 Links []gophercloud.Link 20 Name string 21 Versions []Version 22 } 23 24 // DatastorePartial is a meta structure which is used in various API responses. 25 // It is a lightweight and truncated version of a full Datastore resource, 26 // offering details of the Version, Type and VersionID only. 27 type DatastorePartial struct { 28 Version string 29 Type string 30 VersionID string `json:"version_id"` 31 } 32 33 // GetResult represents the result of a Get operation. 34 type GetResult struct { 35 gophercloud.Result 36 } 37 38 // GetVersionResult represents the result of getting a version. 39 type GetVersionResult struct { 40 gophercloud.Result 41 } 42 43 // DatastorePage represents a page of datastore resources. 44 type DatastorePage struct { 45 pagination.SinglePageBase 46 } 47 48 // IsEmpty indicates whether a Datastore collection is empty. 49 func (r DatastorePage) IsEmpty() (bool, error) { 50 if r.StatusCode == 204 { 51 return true, nil 52 } 53 54 is, err := ExtractDatastores(r) 55 return len(is) == 0, err 56 } 57 58 // ExtractDatastores retrieves a slice of datastore structs from a paginated 59 // collection. 60 func ExtractDatastores(r pagination.Page) ([]Datastore, error) { 61 var s struct { 62 Datastores []Datastore `json:"datastores"` 63 } 64 err := (r.(DatastorePage)).ExtractInto(&s) 65 return s.Datastores, err 66 } 67 68 // Extract retrieves a single Datastore struct from an operation result. 69 func (r GetResult) Extract() (*Datastore, error) { 70 var s struct { 71 Datastore *Datastore `json:"datastore"` 72 } 73 err := r.ExtractInto(&s) 74 return s.Datastore, err 75 } 76 77 // VersionPage represents a page of version resources. 78 type VersionPage struct { 79 pagination.SinglePageBase 80 } 81 82 // IsEmpty indicates whether a collection of version resources is empty. 83 func (r VersionPage) IsEmpty() (bool, error) { 84 if r.StatusCode == 204 { 85 return true, nil 86 } 87 88 is, err := ExtractVersions(r) 89 return len(is) == 0, err 90 } 91 92 // ExtractVersions retrieves a slice of versions from a paginated collection. 93 func ExtractVersions(r pagination.Page) ([]Version, error) { 94 var s struct { 95 Versions []Version `json:"versions"` 96 } 97 err := (r.(VersionPage)).ExtractInto(&s) 98 return s.Versions, err 99 } 100 101 // Extract retrieves a single Version struct from an operation result. 102 func (r GetVersionResult) Extract() (*Version, error) { 103 var s struct { 104 Version *Version `json:"version"` 105 } 106 err := r.ExtractInto(&s) 107 return s.Version, err 108 }