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