|
| 1 | +package allocations |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/gophercloud/gophercloud" |
| 5 | + "github.com/gophercloud/gophercloud/pagination" |
| 6 | +) |
| 7 | + |
| 8 | +// CreateOptsBuilder allows extensions to add additional parameters to the |
| 9 | +// Create request. |
| 10 | +type CreateOptsBuilder interface { |
| 11 | + ToAllocationCreateMap() (map[string]interface{}, error) |
| 12 | +} |
| 13 | + |
| 14 | +// CreateOpts specifies allocation creation parameters |
| 15 | +type CreateOpts struct { |
| 16 | + // The requested resource class for the allocation. |
| 17 | + ResourceClass string `json:"resource_class" required:"true"` |
| 18 | + |
| 19 | + // The list of nodes (names or UUIDs) that should be considered for this allocation. If not provided, all available nodes will be considered. |
| 20 | + CandidateNodes []string `json:"candidate_nodes,omitempty"` |
| 21 | + |
| 22 | + // The unique name of the Allocation. |
| 23 | + Name string `json:"name,omitempty"` |
| 24 | + |
| 25 | + // The list of requested traits for the allocation. |
| 26 | + Traits []string `json:"traits,omitempty"` |
| 27 | + |
| 28 | + // The UUID for the resource. |
| 29 | + UUID string `json:"uuid,omitempty"` |
| 30 | + |
| 31 | + // A set of one or more arbitrary metadata key and value pairs. |
| 32 | + Extra map[string]string `json:"extra,omitempty"` |
| 33 | +} |
| 34 | + |
| 35 | +// ToAllocationCreateMap assembles a request body based on the contents of a CreateOpts. |
| 36 | +func (opts CreateOpts) ToAllocationCreateMap() (map[string]interface{}, error) { |
| 37 | + body, err := gophercloud.BuildRequestBody(opts, "") |
| 38 | + if err != nil { |
| 39 | + return nil, err |
| 40 | + } |
| 41 | + |
| 42 | + return body, nil |
| 43 | +} |
| 44 | + |
| 45 | +// Create requests a node to be created |
| 46 | +func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { |
| 47 | + reqBody, err := opts.ToAllocationCreateMap() |
| 48 | + if err != nil { |
| 49 | + r.Err = err |
| 50 | + return |
| 51 | + } |
| 52 | + |
| 53 | + _, r.Err = client.Post(createURL(client), reqBody, &r.Body, nil) |
| 54 | + return |
| 55 | +} |
| 56 | + |
| 57 | +type AllocationState string |
| 58 | + |
| 59 | +var ( |
| 60 | + Allocating AllocationState = "allocating" |
| 61 | + Active = "active" |
| 62 | + Error = "error" |
| 63 | +) |
| 64 | + |
| 65 | +// ListOptsBuilder allows extensions to add additional parameters to the List request. |
| 66 | +type ListOptsBuilder interface { |
| 67 | + ToAllocationListQuery() (string, error) |
| 68 | +} |
| 69 | + |
| 70 | +// ListOpts allows the filtering and sorting of paginated collections through the API. |
| 71 | +type ListOpts struct { |
| 72 | + // Filter the list of allocations by the node UUID or name. |
| 73 | + Node string `q:"node"` |
| 74 | + |
| 75 | + // Filter the list of returned nodes, and only return the ones with the specified resource class. |
| 76 | + ResourceClass string `q:"resource_class"` |
| 77 | + |
| 78 | + // Filter the list of allocations by the allocation state, one of active, allocating or error. |
| 79 | + State AllocationState `q:"state"` |
| 80 | + |
| 81 | + // One or more fields to be returned in the response. |
| 82 | + Fields []string `q:"fields"` |
| 83 | + |
| 84 | + // Requests a page size of items. |
| 85 | + Limit int `q:"limit"` |
| 86 | + |
| 87 | + // The ID of the last-seen item |
| 88 | + Marker string `q:"marker"` |
| 89 | + |
| 90 | + // Sorts the response by the requested sort direction. |
| 91 | + // Valid value is asc (ascending) or desc (descending). Default is asc. |
| 92 | + SortDir string `q:"sort_dir"` |
| 93 | + |
| 94 | + // Sorts the response by the this attribute value. Default is id. |
| 95 | + SortKey string `q:"sort_key"` |
| 96 | +} |
| 97 | + |
| 98 | +// ToAllocationListQuery formats a ListOpts into a query string. |
| 99 | +func (opts ListOpts) ToAllocationListQuery() (string, error) { |
| 100 | + q, err := gophercloud.BuildQueryString(opts) |
| 101 | + return q.String(), err |
| 102 | +} |
| 103 | + |
| 104 | +// List makes a request against the API to list allocations accessible to you. |
| 105 | +func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager { |
| 106 | + url := listURL(client) |
| 107 | + if opts != nil { |
| 108 | + query, err := opts.ToAllocationListQuery() |
| 109 | + if err != nil { |
| 110 | + return pagination.Pager{Err: err} |
| 111 | + } |
| 112 | + url += query |
| 113 | + } |
| 114 | + return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page { |
| 115 | + return AllocationPage{pagination.LinkedPageBase{PageResult: r}} |
| 116 | + }) |
| 117 | +} |
| 118 | + |
| 119 | +// Get requests the details of an allocation by ID. |
| 120 | +func Get(client *gophercloud.ServiceClient, id string) (r GetResult) { |
| 121 | + _, r.Err = client.Get(getURL(client, id), &r.Body, &gophercloud.RequestOpts{ |
| 122 | + OkCodes: []int{200}, |
| 123 | + }) |
| 124 | + return |
| 125 | +} |
| 126 | + |
| 127 | +// Delete requests the deletion of an allocation |
| 128 | +func Delete(client *gophercloud.ServiceClient, id string) (r DeleteResult) { |
| 129 | + _, r.Err = client.Delete(deleteURL(client, id), nil) |
| 130 | + return |
| 131 | +} |
0 commit comments