github.com/gophercloud/gophercloud@v1.11.0/openstack/baremetal/v1/allocations/requests.go (about) 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 resp, err := client.Post(createURL(client), reqBody, &r.Body, nil) 54 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 55 return 56 } 57 58 type AllocationState string 59 60 var ( 61 Allocating AllocationState = "allocating" 62 Active = "active" 63 Error = "error" 64 ) 65 66 // ListOptsBuilder allows extensions to add additional parameters to the List request. 67 type ListOptsBuilder interface { 68 ToAllocationListQuery() (string, error) 69 } 70 71 // ListOpts allows the filtering and sorting of paginated collections through the API. 72 type ListOpts struct { 73 // Filter the list of allocations by the node UUID or name. 74 Node string `q:"node"` 75 76 // Filter the list of returned nodes, and only return the ones with the specified resource class. 77 ResourceClass string `q:"resource_class"` 78 79 // Filter the list of allocations by the allocation state, one of active, allocating or error. 80 State AllocationState `q:"state"` 81 82 // One or more fields to be returned in the response. 83 Fields []string `q:"fields"` 84 85 // Requests a page size of items. 86 Limit int `q:"limit"` 87 88 // The ID of the last-seen item 89 Marker string `q:"marker"` 90 91 // Sorts the response by the requested sort direction. 92 // Valid value is asc (ascending) or desc (descending). Default is asc. 93 SortDir string `q:"sort_dir"` 94 95 // Sorts the response by the this attribute value. Default is id. 96 SortKey string `q:"sort_key"` 97 } 98 99 // ToAllocationListQuery formats a ListOpts into a query string. 100 func (opts ListOpts) ToAllocationListQuery() (string, error) { 101 q, err := gophercloud.BuildQueryString(opts) 102 return q.String(), err 103 } 104 105 // List makes a request against the API to list allocations accessible to you. 106 func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager { 107 url := listURL(client) 108 if opts != nil { 109 query, err := opts.ToAllocationListQuery() 110 if err != nil { 111 return pagination.Pager{Err: err} 112 } 113 url += query 114 } 115 return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page { 116 return AllocationPage{pagination.LinkedPageBase{PageResult: r}} 117 }) 118 } 119 120 // Get requests the details of an allocation by ID. 121 func Get(client *gophercloud.ServiceClient, id string) (r GetResult) { 122 resp, err := client.Get(getURL(client, id), &r.Body, &gophercloud.RequestOpts{ 123 OkCodes: []int{200}, 124 }) 125 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 126 return 127 } 128 129 // Delete requests the deletion of an allocation 130 func Delete(client *gophercloud.ServiceClient, id string) (r DeleteResult) { 131 resp, err := client.Delete(deleteURL(client, id), nil) 132 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 133 return 134 }