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

     1  package queues
     2  
     3  import (
     4  	"encoding/json"
     5  
     6  	"github.com/gophercloud/gophercloud"
     7  	"github.com/gophercloud/gophercloud/pagination"
     8  )
     9  
    10  // commonResult is the response of a base result.
    11  type commonResult struct {
    12  	gophercloud.Result
    13  }
    14  
    15  // QueuePage contains a single page of all queues from a List operation.
    16  type QueuePage struct {
    17  	pagination.LinkedPageBase
    18  }
    19  
    20  // CreateResult is the response of a Create operation.
    21  type CreateResult struct {
    22  	gophercloud.ErrResult
    23  }
    24  
    25  // UpdateResult is the response of a Update operation.
    26  type UpdateResult struct {
    27  	commonResult
    28  }
    29  
    30  // GetResult is the response of a Get operation.
    31  type GetResult struct {
    32  	commonResult
    33  }
    34  
    35  // StatResult contains the result of a Share operation.
    36  type StatResult struct {
    37  	gophercloud.Result
    38  }
    39  
    40  // DeleteResult is the result from a Delete operation. Call its ExtractErr
    41  // method to determine if the call succeeded or failed.
    42  type DeleteResult struct {
    43  	gophercloud.ErrResult
    44  }
    45  
    46  // ShareResult contains the result of a Share operation.
    47  type ShareResult struct {
    48  	gophercloud.Result
    49  }
    50  
    51  // PurgeResult is the response of a Purge operation.
    52  type PurgeResult struct {
    53  	gophercloud.ErrResult
    54  }
    55  
    56  // Queue represents a messaging queue.
    57  type Queue struct {
    58  	Href          string       `json:"href"`
    59  	Methods       []string     `json:"methods"`
    60  	Name          string       `json:"name"`
    61  	Paths         []string     `json:"paths"`
    62  	ResourceTypes []string     `json:"resource_types"`
    63  	Metadata      QueueDetails `json:"metadata"`
    64  }
    65  
    66  // QueueDetails represents the metadata of a queue.
    67  type QueueDetails struct {
    68  	// The queue the message will be moved to when the message can’t
    69  	// be processed successfully after the max claim count is met.
    70  	DeadLetterQueue string `json:"_dead_letter_queue"`
    71  
    72  	// The TTL setting for messages when moved to dead letter queue.
    73  	DeadLetterQueueMessageTTL int `json:"_dead_letter_queue_messages_ttl"`
    74  
    75  	// The delay of messages defined for the queue.
    76  	DefaultMessageDelay int `json:"_default_message_delay"`
    77  
    78  	// The default TTL of messages defined for the queue.
    79  	DefaultMessageTTL int `json:"_default_message_ttl"`
    80  
    81  	// Extra is a collection of miscellaneous key/values.
    82  	Extra map[string]interface{} `json:"-"`
    83  
    84  	// The max number the message can be claimed from the queue.
    85  	MaxClaimCount int `json:"_max_claim_count"`
    86  
    87  	// The max post size of messages defined for the queue.
    88  	MaxMessagesPostSize int `json:"_max_messages_post_size"`
    89  
    90  	// Is message encryption enabled
    91  	EnableEncryptMessages bool `json:"_enable_encrypt_messages"`
    92  
    93  	// The flavor defined for the queue.
    94  	Flavor string `json:"flavor"`
    95  }
    96  
    97  // Stats represents a stats response.
    98  type Stats struct {
    99  	// Number of Claimed messages for a queue
   100  	Claimed int `json:"claimed"`
   101  
   102  	// Total Messages for a queue
   103  	Total int `json:"total"`
   104  
   105  	// Number of free messages
   106  	Free int `json:"free"`
   107  }
   108  
   109  // QueueShare represents a share response.
   110  type QueueShare struct {
   111  	Project   string   `json:"project"`
   112  	Paths     []string `json:"paths"`
   113  	Expires   string   `json:"expires"`
   114  	Methods   []string `json:"methods"`
   115  	Signature string   `json:"signature"`
   116  }
   117  
   118  // Extract interprets any commonResult as a Queue.
   119  func (r commonResult) Extract() (QueueDetails, error) {
   120  	var s QueueDetails
   121  	err := r.ExtractInto(&s)
   122  	return s, err
   123  }
   124  
   125  // Extract interprets any StatResult as a Stats.
   126  func (r StatResult) Extract() (Stats, error) {
   127  	var s struct {
   128  		Stats Stats `json:"messages"`
   129  	}
   130  	err := r.ExtractInto(&s)
   131  	return s.Stats, err
   132  }
   133  
   134  // Extract interprets any ShareResult as a QueueShare.
   135  func (r ShareResult) Extract() (QueueShare, error) {
   136  	var s QueueShare
   137  	err := r.ExtractInto(&s)
   138  	return s, err
   139  }
   140  
   141  // ExtractQueues interprets the results of a single page from a
   142  // List() call, producing a map of queues.
   143  func ExtractQueues(r pagination.Page) ([]Queue, error) {
   144  	var s struct {
   145  		Queues []Queue `json:"queues"`
   146  	}
   147  	err := (r.(QueuePage)).ExtractInto(&s)
   148  	return s.Queues, err
   149  }
   150  
   151  // IsEmpty determines if a QueuesPage contains any results.
   152  func (r QueuePage) IsEmpty() (bool, error) {
   153  	if r.StatusCode == 204 {
   154  		return true, nil
   155  	}
   156  
   157  	s, err := ExtractQueues(r)
   158  	return len(s) == 0, err
   159  }
   160  
   161  // NextPageURL uses the response's embedded link reference to navigate to the
   162  // next page of results.
   163  func (r QueuePage) NextPageURL() (string, error) {
   164  	var s struct {
   165  		Links []gophercloud.Link `json:"links"`
   166  	}
   167  	err := r.ExtractInto(&s)
   168  	if err != nil {
   169  		return "", err
   170  	}
   171  
   172  	next, err := gophercloud.ExtractNextURL(s.Links)
   173  	if err != nil {
   174  		return "", err
   175  	}
   176  	return nextPageURL(r.URL.String(), next)
   177  }
   178  
   179  // GetCount value if it request was supplied `WithCount` param
   180  func (r QueuePage) GetCount() (int, error) {
   181  	var s struct {
   182  		Count int `json:"count"`
   183  	}
   184  	err := r.ExtractInto(&s)
   185  	if err != nil {
   186  		return 0, err
   187  	}
   188  	return s.Count, nil
   189  }
   190  
   191  func (r *QueueDetails) UnmarshalJSON(b []byte) error {
   192  	type tmp QueueDetails
   193  	var s struct {
   194  		tmp
   195  		Extra map[string]interface{} `json:"extra"`
   196  	}
   197  	err := json.Unmarshal(b, &s)
   198  	if err != nil {
   199  		return err
   200  	}
   201  	*r = QueueDetails(s.tmp)
   202  
   203  	// Collect other fields and bundle them into Extra
   204  	// but only if a field titled "extra" wasn't sent.
   205  	if s.Extra != nil {
   206  		r.Extra = s.Extra
   207  	} else {
   208  		var result interface{}
   209  		err := json.Unmarshal(b, &result)
   210  		if err != nil {
   211  			return err
   212  		}
   213  		if resultMap, ok := result.(map[string]interface{}); ok {
   214  			r.Extra = gophercloud.RemainingKeys(QueueDetails{}, resultMap)
   215  		}
   216  	}
   217  
   218  	return err
   219  }