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

     1  package messages
     2  
     3  import (
     4  	"github.com/gophercloud/gophercloud"
     5  	"github.com/gophercloud/gophercloud/pagination"
     6  )
     7  
     8  // commonResult is the response of a base result.
     9  type commonResult struct {
    10  	gophercloud.Result
    11  }
    12  
    13  // CreateResult is the response of a Create operations.
    14  type CreateResult struct {
    15  	gophercloud.Result
    16  }
    17  
    18  // MessagePage contains a single page of all clusters from a ListDetails call.
    19  type MessagePage struct {
    20  	pagination.LinkedPageBase
    21  }
    22  
    23  // DeleteResult is the result from a Delete operation. Call its ExtractErr
    24  // method to determine if the call succeeded or failed.
    25  type DeleteResult struct {
    26  	gophercloud.ErrResult
    27  }
    28  
    29  // CreateResult is the response of a Create operations.
    30  type PopResult struct {
    31  	gophercloud.Result
    32  }
    33  
    34  // GetMessagesResult is the response of a GetMessages operations.
    35  type GetMessagesResult struct {
    36  	gophercloud.Result
    37  }
    38  
    39  // GetResult is the response of a Get operations.
    40  type GetResult struct {
    41  	gophercloud.Result
    42  }
    43  
    44  // Message represents a message on a queue.
    45  type Message struct {
    46  	Body     map[string]interface{} `json:"body"`
    47  	Age      int                    `json:"age"`
    48  	Href     string                 `json:"href"`
    49  	ID       string                 `json:"id"`
    50  	TTL      int                    `json:"ttl"`
    51  	Checksum string                 `json:"checksum"`
    52  }
    53  
    54  // PopMessage represents a message returned from PopMessages.
    55  type PopMessage struct {
    56  	Body       map[string]interface{} `json:"body"`
    57  	Age        int                    `json:"age"`
    58  	ID         string                 `json:"id"`
    59  	TTL        int                    `json:"ttl"`
    60  	ClaimCount int                    `json:"claim_count"`
    61  	ClaimID    string                 `json:"claim_id"`
    62  }
    63  
    64  // ResourceList represents the result of creating a message.
    65  type ResourceList struct {
    66  	Resources []string `json:"resources"`
    67  }
    68  
    69  // Extract interprets any CreateResult as a ResourceList.
    70  func (r CreateResult) Extract() (ResourceList, error) {
    71  	var s ResourceList
    72  	err := r.ExtractInto(&s)
    73  	return s, err
    74  }
    75  
    76  // Extract interprets any PopResult as a list of PopMessage.
    77  func (r PopResult) Extract() ([]PopMessage, error) {
    78  	var s struct {
    79  		PopMessages []PopMessage `json:"messages"`
    80  	}
    81  	err := r.ExtractInto(&s)
    82  	return s.PopMessages, err
    83  }
    84  
    85  // Extract interprets any GetMessagesResult as a list of Message.
    86  func (r GetMessagesResult) Extract() ([]Message, error) {
    87  	var s struct {
    88  		Messages []Message `json:"messages"`
    89  	}
    90  	err := r.ExtractInto(&s)
    91  	return s.Messages, err
    92  }
    93  
    94  // Extract interprets any GetResult as a Message.
    95  func (r GetResult) Extract() (Message, error) {
    96  	var s Message
    97  	err := r.ExtractInto(&s)
    98  	return s, err
    99  }
   100  
   101  // ExtractMessage extracts message into a  list of Message.
   102  func ExtractMessages(r pagination.Page) ([]Message, error) {
   103  	var s struct {
   104  		Messages []Message `json:"messages"`
   105  	}
   106  	err := (r.(MessagePage)).ExtractInto(&s)
   107  	return s.Messages, err
   108  }
   109  
   110  // IsEmpty determines if a MessagePage contains any results.
   111  func (r MessagePage) IsEmpty() (bool, error) {
   112  	if r.StatusCode == 204 {
   113  		return true, nil
   114  	}
   115  
   116  	s, err := ExtractMessages(r)
   117  	return len(s) == 0, err
   118  }
   119  
   120  // NextPageURL uses the response's embedded link reference to navigate to the
   121  // next page of results.
   122  func (r MessagePage) NextPageURL() (string, error) {
   123  	var s struct {
   124  		Links []gophercloud.Link `json:"links"`
   125  	}
   126  	err := r.ExtractInto(&s)
   127  	if err != nil {
   128  		return "", err
   129  	}
   130  
   131  	next, err := gophercloud.ExtractNextURL(s.Links)
   132  	if err != nil {
   133  		return "", err
   134  	}
   135  	return nextPageURL(r.URL.String(), next)
   136  }