1package allocations
2
3import (
4	"time"
5
6	"github.com/gophercloud/gophercloud"
7	"github.com/gophercloud/gophercloud/pagination"
8)
9
10type Allocation struct {
11	// The UUID for the resource.
12	UUID string `json:"uuid"`
13
14	// A list of UUIDs of the nodes that are candidates for this allocation.
15	CandidateNodes []string `json:"candidate_nodes"`
16
17	// The error message for the allocation if it is in the error state, null otherwise.
18	LastError string `json:"last_error"`
19
20	// The unique name of the allocation.
21	Name string `json:"name"`
22
23	// The UUID of the node assigned to the allocation. Will be null if a node is not yet assigned.
24	NodeUUID string `json:"node_uuid"`
25
26	// The current state of the allocation. One of: allocation, active, error
27	State string `json:"state"`
28
29	// The resource class requested for the allocation.
30	ResourceClass string `json:"resource_class"`
31
32	// The list of the traits requested for the allocation.
33	Traits []string `json:"traits"`
34
35	// A set of one or more arbitrary metadata key and value pairs.
36	Extra map[string]string `json:"extra"`
37
38	// The UTC date and time when the resource was created, ISO 8601 format.
39	CreatedAt time.Time `json:"created_at"`
40
41	// The UTC date and time when the resource was updated, ISO 8601 format. May be “null”.
42	UpdatedAt time.Time `json:"updated_at"`
43
44	// A list of relative links. Includes the self and bookmark links.
45	Links []interface{} `json:"links"`
46}
47
48type allocationResult struct {
49	gophercloud.Result
50}
51
52func (r allocationResult) Extract() (*Allocation, error) {
53	var s Allocation
54	err := r.ExtractInto(&s)
55	return &s, err
56}
57
58func (r allocationResult) ExtractInto(v interface{}) error {
59	return r.Result.ExtractIntoStructPtr(v, "")
60}
61
62func ExtractAllocationsInto(r pagination.Page, v interface{}) error {
63	return r.(AllocationPage).Result.ExtractIntoSlicePtr(v, "allocations")
64}
65
66// AllocationPage abstracts the raw results of making a List() request against
67// the API.
68type AllocationPage struct {
69	pagination.LinkedPageBase
70}
71
72// IsEmpty returns true if a page contains no Allocation results.
73func (r AllocationPage) IsEmpty() (bool, error) {
74	s, err := ExtractAllocations(r)
75	return len(s) == 0, err
76}
77
78// NextPageURL uses the response's embedded link reference to navigate to the
79// next page of results.
80func (r AllocationPage) NextPageURL() (string, error) {
81	var s struct {
82		Links []gophercloud.Link `json:"allocations_links"`
83	}
84	err := r.ExtractInto(&s)
85	if err != nil {
86		return "", err
87	}
88	return gophercloud.ExtractNextURL(s.Links)
89}
90
91// ExtractAllocations interprets the results of a single page from a List() call,
92// producing a slice of Allocation entities.
93func ExtractAllocations(r pagination.Page) ([]Allocation, error) {
94	var s []Allocation
95	err := ExtractAllocationsInto(r, &s)
96	return s, err
97}
98
99// GetResult is the response from a Get operation. Call its Extract
100// method to interpret it as a Allocation.
101type GetResult struct {
102	allocationResult
103}
104
105// CreateResult is the response from a Create operation.
106type CreateResult struct {
107	allocationResult
108}
109
110// DeleteResult is the response from a Delete operation. Call its ExtractErr
111// method to determine if the call succeeded or failed.
112type DeleteResult struct {
113	gophercloud.ErrResult
114}
115