github.com/gophercloud/gophercloud@v1.11.0/openstack/messaging/v2/claims/requests.go (about) 1 package claims 2 3 import ( 4 "github.com/gophercloud/gophercloud" 5 ) 6 7 // CreateOptsBuilder Builder. 8 type CreateOptsBuilder interface { 9 ToClaimCreateRequest() (map[string]interface{}, string, error) 10 } 11 12 // CreateOpts params to be used with Create. 13 type CreateOpts struct { 14 // Sets the TTL for the claim. When the claim expires un-deleted messages will be able to be claimed again. 15 TTL int `json:"ttl,omitempty"` 16 17 // Sets the Grace period for the claimed messages. The server extends the lifetime of claimed messages 18 // to be at least as long as the lifetime of the claim itself, plus the specified grace period. 19 Grace int `json:"grace,omitempty"` 20 21 // Set the limit of messages returned by create. 22 Limit int `q:"limit" json:"-"` 23 } 24 25 // ToClaimCreateRequest assembles a body and URL for a Create request based on 26 // the contents of a CreateOpts. 27 func (opts CreateOpts) ToClaimCreateRequest() (map[string]interface{}, string, error) { 28 q, err := gophercloud.BuildQueryString(opts) 29 if err != nil { 30 return nil, q.String(), err 31 } 32 33 b, err := gophercloud.BuildRequestBody(opts, "") 34 if err != nil { 35 return b, "", err 36 } 37 return b, q.String(), err 38 } 39 40 // Create creates a Claim that claims messages on a specified queue. 41 func Create(client *gophercloud.ServiceClient, queueName string, opts CreateOptsBuilder) (r CreateResult) { 42 b, q, err := opts.ToClaimCreateRequest() 43 if err != nil { 44 r.Err = err 45 return 46 } 47 48 url := createURL(client, queueName) 49 if q != "" { 50 url += q 51 } 52 53 resp, err := client.Post(url, b, &r.Body, &gophercloud.RequestOpts{ 54 OkCodes: []int{201, 204}, 55 }) 56 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 57 return 58 } 59 60 // Get queries the specified claim for the specified queue. 61 func Get(client *gophercloud.ServiceClient, queueName string, claimID string) (r GetResult) { 62 resp, err := client.Get(getURL(client, queueName, claimID), &r.Body, &gophercloud.RequestOpts{ 63 OkCodes: []int{200}, 64 }) 65 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 66 return 67 } 68 69 // UpdateOptsBuilder allows extensions to add additional parameters to the 70 // Update request. 71 type UpdateOptsBuilder interface { 72 ToClaimUpdateMap() (map[string]interface{}, error) 73 } 74 75 // UpdateOpts implements UpdateOpts. 76 type UpdateOpts struct { 77 // Update the TTL for the specified Claim. 78 TTL int `json:"ttl,omitempty"` 79 80 // Update the grace period for Messages in a specified Claim. 81 Grace int `json:"grace,omitempty"` 82 } 83 84 // ToClaimUpdateMap assembles a request body based on the contents of 85 // UpdateOpts. 86 func (opts UpdateOpts) ToClaimUpdateMap() (map[string]interface{}, error) { 87 b, err := gophercloud.BuildRequestBody(opts, "") 88 if err != nil { 89 return nil, err 90 } 91 return b, nil 92 } 93 94 // Update will update the options for a specified claim. 95 func Update(client *gophercloud.ServiceClient, queueName string, claimID string, opts UpdateOptsBuilder) (r UpdateResult) { 96 b, err := opts.ToClaimUpdateMap() 97 if err != nil { 98 r.Err = err 99 return r 100 } 101 resp, err := client.Patch(updateURL(client, queueName, claimID), &b, nil, &gophercloud.RequestOpts{ 102 OkCodes: []int{204}, 103 }) 104 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 105 return 106 } 107 108 // Delete will delete a Claim for a specified Queue. 109 func Delete(client *gophercloud.ServiceClient, queueName string, claimID string) (r DeleteResult) { 110 resp, err := client.Delete(deleteURL(client, queueName, claimID), &gophercloud.RequestOpts{ 111 OkCodes: []int{204}, 112 }) 113 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 114 return 115 }