github.com/gophercloud/gophercloud@v1.11.0/openstack/messaging/v2/queues/requests.go (about)

     1  package queues
     2  
     3  import (
     4  	"github.com/gophercloud/gophercloud"
     5  	"github.com/gophercloud/gophercloud/pagination"
     6  )
     7  
     8  // ListOptsBuilder allows extensions to add additional parameters to the
     9  // List request.
    10  type ListOptsBuilder interface {
    11  	ToQueueListQuery() (string, error)
    12  }
    13  
    14  // ListOpts params to be used with List
    15  type ListOpts struct {
    16  	// Limit instructs List to refrain from sending excessively large lists of queues
    17  	Limit int `q:"limit,omitempty"`
    18  
    19  	// Marker and Limit control paging. Marker instructs List where to start listing from.
    20  	Marker string `q:"marker,omitempty"`
    21  
    22  	// Specifies if showing the detailed information when querying queues
    23  	Detailed bool `q:"detailed,omitempty"`
    24  
    25  	// Specifies if filter the queues by queue’s name when querying queues.
    26  	Name bool `q:"name,omitempty"`
    27  
    28  	// Specifies if showing the amount of queues when querying them.
    29  	WithCount bool `q:"with_count,omitempty"`
    30  }
    31  
    32  // ToQueueListQuery formats a ListOpts into a query string.
    33  func (opts ListOpts) ToQueueListQuery() (string, error) {
    34  	q, err := gophercloud.BuildQueryString(opts)
    35  	return q.String(), err
    36  }
    37  
    38  // List instructs OpenStack to provide a list of queues.
    39  func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
    40  	url := listURL(client)
    41  	if opts != nil {
    42  		query, err := opts.ToQueueListQuery()
    43  		if err != nil {
    44  			return pagination.Pager{Err: err}
    45  		}
    46  		url += query
    47  	}
    48  
    49  	pager := pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
    50  		return QueuePage{pagination.LinkedPageBase{PageResult: r}}
    51  
    52  	})
    53  	return pager
    54  }
    55  
    56  // CreateOptsBuilder allows extensions to add additional parameters to the
    57  // Create request.
    58  type CreateOptsBuilder interface {
    59  	ToQueueCreateMap() (map[string]interface{}, error)
    60  }
    61  
    62  // CreateOpts specifies the queue creation parameters.
    63  type CreateOpts struct {
    64  	// The name of the queue to create.
    65  	QueueName string `json:"queue_name" required:"true"`
    66  
    67  	// The target incoming messages will be moved to when a message can’t
    68  	// processed successfully after meet the max claim count is met.
    69  	DeadLetterQueue string `json:"_dead_letter_queue,omitempty"`
    70  
    71  	// The new TTL setting for messages when moved to dead letter queue.
    72  	DeadLetterQueueMessagesTTL int `json:"_dead_letter_queue_messages_ttl,omitempty"`
    73  
    74  	// The delay of messages defined for a queue. When the messages send to
    75  	// the queue, it will be delayed for some times and means it can not be
    76  	// claimed until the delay expired.
    77  	DefaultMessageDelay int `json:"_default_message_delay,omitempty"`
    78  
    79  	// The default TTL of messages defined for a queue, which will effect for
    80  	// any messages posted to the queue.
    81  	DefaultMessageTTL int `json:"_default_message_ttl" required:"true"`
    82  
    83  	// The flavor name which can tell Zaqar which storage pool will be used
    84  	// to create the queue.
    85  	Flavor string `json:"_flavor,omitempty"`
    86  
    87  	// The max number the message can be claimed.
    88  	MaxClaimCount int `json:"_max_claim_count,omitempty"`
    89  
    90  	// The max post size of messages defined for a queue, which will effect
    91  	// for any messages posted to the queue.
    92  	MaxMessagesPostSize int `json:"_max_messages_post_size,omitempty"`
    93  
    94  	// Does messages should be encrypted
    95  	EnableEncryptMessages *bool `json:"_enable_encrypt_messages,omitempty"`
    96  
    97  	// Extra is free-form extra key/value pairs to describe the queue.
    98  	Extra map[string]interface{} `json:"-"`
    99  }
   100  
   101  // ToQueueCreateMap constructs a request body from CreateOpts.
   102  func (opts CreateOpts) ToQueueCreateMap() (map[string]interface{}, error) {
   103  	b, err := gophercloud.BuildRequestBody(opts, "")
   104  	if err != nil {
   105  		return nil, err
   106  	}
   107  
   108  	if opts.Extra != nil {
   109  		for key, value := range opts.Extra {
   110  			b[key] = value
   111  		}
   112  
   113  	}
   114  	return b, nil
   115  }
   116  
   117  // Create requests the creation of a new queue.
   118  func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
   119  	b, err := opts.ToQueueCreateMap()
   120  	if err != nil {
   121  		r.Err = err
   122  		return
   123  	}
   124  
   125  	queueName := b["queue_name"].(string)
   126  	delete(b, "queue_name")
   127  
   128  	resp, err := client.Put(createURL(client, queueName), b, r.Body, &gophercloud.RequestOpts{
   129  		OkCodes: []int{201, 204},
   130  	})
   131  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   132  	return
   133  }
   134  
   135  // UpdateOptsBuilder allows extensions to add additional parameters to the
   136  // update request.
   137  type UpdateOptsBuilder interface {
   138  	ToQueueUpdateMap() ([]map[string]interface{}, error)
   139  }
   140  
   141  // BatchUpdateOpts is an array of UpdateOpts.
   142  type BatchUpdateOpts []UpdateOpts
   143  
   144  // UpdateOpts is the struct responsible for updating a property of a queue.
   145  type UpdateOpts struct {
   146  	Op    UpdateOp    `json:"op" required:"true"`
   147  	Path  string      `json:"path" required:"true"`
   148  	Value interface{} `json:"value" required:"true"`
   149  }
   150  
   151  type UpdateOp string
   152  
   153  const (
   154  	ReplaceOp UpdateOp = "replace"
   155  	AddOp     UpdateOp = "add"
   156  	RemoveOp  UpdateOp = "remove"
   157  )
   158  
   159  // ToQueueUpdateMap constructs a request body from UpdateOpts.
   160  func (opts BatchUpdateOpts) ToQueueUpdateMap() ([]map[string]interface{}, error) {
   161  	queuesUpdates := make([]map[string]interface{}, len(opts))
   162  	for i, queue := range opts {
   163  		queueMap, err := queue.ToMap()
   164  		if err != nil {
   165  			return nil, err
   166  		}
   167  		queuesUpdates[i] = queueMap
   168  	}
   169  	return queuesUpdates, nil
   170  }
   171  
   172  // ToMap constructs a request body from UpdateOpts.
   173  func (opts UpdateOpts) ToMap() (map[string]interface{}, error) {
   174  	return gophercloud.BuildRequestBody(opts, "")
   175  }
   176  
   177  // Update Updates the specified queue.
   178  func Update(client *gophercloud.ServiceClient, queueName string, opts UpdateOptsBuilder) (r UpdateResult) {
   179  	resp, err := client.Patch(updateURL(client, queueName), opts, &r.Body, &gophercloud.RequestOpts{
   180  		OkCodes: []int{200, 201, 204},
   181  		MoreHeaders: map[string]string{
   182  			"Content-Type": "application/openstack-messaging-v2.0-json-patch"},
   183  	})
   184  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   185  	return
   186  }
   187  
   188  // Get requests details on a single queue, by name.
   189  func Get(client *gophercloud.ServiceClient, queueName string) (r GetResult) {
   190  	resp, err := client.Get(getURL(client, queueName), &r.Body, &gophercloud.RequestOpts{
   191  		OkCodes: []int{200},
   192  	})
   193  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   194  	return
   195  }
   196  
   197  // Delete deletes the specified queue.
   198  func Delete(client *gophercloud.ServiceClient, queueName string) (r DeleteResult) {
   199  	resp, err := client.Delete(deleteURL(client, queueName), &gophercloud.RequestOpts{
   200  		OkCodes: []int{204},
   201  	})
   202  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   203  	return
   204  }
   205  
   206  // GetStats returns statistics for the specified queue.
   207  func GetStats(client *gophercloud.ServiceClient, queueName string) (r StatResult) {
   208  	resp, err := client.Get(statURL(client, queueName), &r.Body, &gophercloud.RequestOpts{
   209  		OkCodes: []int{200},
   210  	})
   211  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   212  	return
   213  }
   214  
   215  type SharePath string
   216  
   217  const (
   218  	PathMessages      SharePath = "messages"
   219  	PathClaims        SharePath = "claims"
   220  	PathSubscriptions SharePath = "subscriptions"
   221  )
   222  
   223  type ShareMethod string
   224  
   225  const (
   226  	MethodGet   ShareMethod = "GET"
   227  	MethodPatch ShareMethod = "PATCH"
   228  	MethodPost  ShareMethod = "POST"
   229  	MethodPut   ShareMethod = "PUT"
   230  )
   231  
   232  // ShareOpts specifies share creation parameters.
   233  type ShareOpts struct {
   234  	Paths   []SharePath   `json:"paths,omitempty"`
   235  	Methods []ShareMethod `json:"methods,omitempty"`
   236  	Expires string        `json:"expires,omitempty"`
   237  }
   238  
   239  // ShareOptsBuilder allows extensions to add additional attributes to the
   240  // Share request.
   241  type ShareOptsBuilder interface {
   242  	ToQueueShareMap() (map[string]interface{}, error)
   243  }
   244  
   245  // ToShareQueueMap formats a ShareOpts structure into a request body.
   246  func (opts ShareOpts) ToQueueShareMap() (map[string]interface{}, error) {
   247  	b, err := gophercloud.BuildRequestBody(opts, "")
   248  	if err != nil {
   249  		return nil, err
   250  	}
   251  	return b, nil
   252  }
   253  
   254  // Share creates a pre-signed URL for a given queue.
   255  func Share(client *gophercloud.ServiceClient, queueName string, opts ShareOptsBuilder) (r ShareResult) {
   256  	b, err := opts.ToQueueShareMap()
   257  	if err != nil {
   258  		r.Err = err
   259  		return r
   260  	}
   261  	resp, err := client.Post(shareURL(client, queueName), b, &r.Body, &gophercloud.RequestOpts{
   262  		OkCodes: []int{200},
   263  	})
   264  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   265  	return
   266  }
   267  
   268  type PurgeResource string
   269  
   270  const (
   271  	ResourceMessages      PurgeResource = "messages"
   272  	ResourceSubscriptions PurgeResource = "subscriptions"
   273  )
   274  
   275  // PurgeOpts specifies the purge parameters.
   276  type PurgeOpts struct {
   277  	ResourceTypes []PurgeResource `json:"resource_types" required:"true"`
   278  }
   279  
   280  // PurgeOptsBuilder allows extensions to add additional attributes to the
   281  // Purge request.
   282  type PurgeOptsBuilder interface {
   283  	ToQueuePurgeMap() (map[string]interface{}, error)
   284  }
   285  
   286  // ToPurgeQueueMap formats a PurgeOpts structure into a request body
   287  func (opts PurgeOpts) ToQueuePurgeMap() (map[string]interface{}, error) {
   288  	b, err := gophercloud.BuildRequestBody(opts, "")
   289  	if err != nil {
   290  		return nil, err
   291  	}
   292  
   293  	return b, nil
   294  }
   295  
   296  // Purge purges particular resource of the queue.
   297  func Purge(client *gophercloud.ServiceClient, queueName string, opts PurgeOptsBuilder) (r PurgeResult) {
   298  	b, err := opts.ToQueuePurgeMap()
   299  	if err != nil {
   300  		r.Err = err
   301  		return r
   302  	}
   303  
   304  	resp, err := client.Post(purgeURL(client, queueName), b, nil, &gophercloud.RequestOpts{
   305  		OkCodes: []int{204},
   306  	})
   307  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   308  	return
   309  }