/vendor/github.com/hashicorp/consul/api/prepared_query.go

https://github.com/alibaba/pouch · Go · 204 lines · 99 code · 35 blank · 70 comment · 14 complexity · c579312550813cb4419aa09a6df508fb MD5 · raw file

  1. package api
  2. // QueryDatacenterOptions sets options about how we fail over if there are no
  3. // healthy nodes in the local datacenter.
  4. type QueryDatacenterOptions struct {
  5. // NearestN is set to the number of remote datacenters to try, based on
  6. // network coordinates.
  7. NearestN int
  8. // Datacenters is a fixed list of datacenters to try after NearestN. We
  9. // never try a datacenter multiple times, so those are subtracted from
  10. // this list before proceeding.
  11. Datacenters []string
  12. }
  13. // QueryDNSOptions controls settings when query results are served over DNS.
  14. type QueryDNSOptions struct {
  15. // TTL is the time to live for the served DNS results.
  16. TTL string
  17. }
  18. // ServiceQuery is used to query for a set of healthy nodes offering a specific
  19. // service.
  20. type ServiceQuery struct {
  21. // Service is the service to query.
  22. Service string
  23. // Near allows baking in the name of a node to automatically distance-
  24. // sort from. The magic "_agent" value is supported, which sorts near
  25. // the agent which initiated the request by default.
  26. Near string
  27. // Failover controls what we do if there are no healthy nodes in the
  28. // local datacenter.
  29. Failover QueryDatacenterOptions
  30. // IgnoreCheckIDs is an optional list of health check IDs to ignore when
  31. // considering which nodes are healthy. It is useful as an emergency measure
  32. // to temporarily override some health check that is producing false negatives
  33. // for example.
  34. IgnoreCheckIDs []string
  35. // If OnlyPassing is true then we will only include nodes with passing
  36. // health checks (critical AND warning checks will cause a node to be
  37. // discarded)
  38. OnlyPassing bool
  39. // Tags are a set of required and/or disallowed tags. If a tag is in
  40. // this list it must be present. If the tag is preceded with "!" then
  41. // it is disallowed.
  42. Tags []string
  43. // NodeMeta is a map of required node metadata fields. If a key/value
  44. // pair is in this map it must be present on the node in order for the
  45. // service entry to be returned.
  46. NodeMeta map[string]string
  47. }
  48. // QueryTemplate carries the arguments for creating a templated query.
  49. type QueryTemplate struct {
  50. // Type specifies the type of the query template. Currently only
  51. // "name_prefix_match" is supported. This field is required.
  52. Type string
  53. // Regexp allows specifying a regex pattern to match against the name
  54. // of the query being executed.
  55. Regexp string
  56. }
  57. // PreparedQueryDefinition defines a complete prepared query.
  58. type PreparedQueryDefinition struct {
  59. // ID is this UUID-based ID for the query, always generated by Consul.
  60. ID string
  61. // Name is an optional friendly name for the query supplied by the
  62. // user. NOTE - if this feature is used then it will reduce the security
  63. // of any read ACL associated with this query/service since this name
  64. // can be used to locate nodes with supplying any ACL.
  65. Name string
  66. // Session is an optional session to tie this query's lifetime to. If
  67. // this is omitted then the query will not expire.
  68. Session string
  69. // Token is the ACL token used when the query was created, and it is
  70. // used when a query is subsequently executed. This token, or a token
  71. // with management privileges, must be used to change the query later.
  72. Token string
  73. // Service defines a service query (leaving things open for other types
  74. // later).
  75. Service ServiceQuery
  76. // DNS has options that control how the results of this query are
  77. // served over DNS.
  78. DNS QueryDNSOptions
  79. // Template is used to pass through the arguments for creating a
  80. // prepared query with an attached template. If a template is given,
  81. // interpolations are possible in other struct fields.
  82. Template QueryTemplate
  83. }
  84. // PreparedQueryExecuteResponse has the results of executing a query.
  85. type PreparedQueryExecuteResponse struct {
  86. // Service is the service that was queried.
  87. Service string
  88. // Nodes has the nodes that were output by the query.
  89. Nodes []ServiceEntry
  90. // DNS has the options for serving these results over DNS.
  91. DNS QueryDNSOptions
  92. // Datacenter is the datacenter that these results came from.
  93. Datacenter string
  94. // Failovers is a count of how many times we had to query a remote
  95. // datacenter.
  96. Failovers int
  97. }
  98. // PreparedQuery can be used to query the prepared query endpoints.
  99. type PreparedQuery struct {
  100. c *Client
  101. }
  102. // PreparedQuery returns a handle to the prepared query endpoints.
  103. func (c *Client) PreparedQuery() *PreparedQuery {
  104. return &PreparedQuery{c}
  105. }
  106. // Create makes a new prepared query. The ID of the new query is returned.
  107. func (c *PreparedQuery) Create(query *PreparedQueryDefinition, q *WriteOptions) (string, *WriteMeta, error) {
  108. r := c.c.newRequest("POST", "/v1/query")
  109. r.setWriteOptions(q)
  110. r.obj = query
  111. rtt, resp, err := requireOK(c.c.doRequest(r))
  112. if err != nil {
  113. return "", nil, err
  114. }
  115. defer resp.Body.Close()
  116. wm := &WriteMeta{}
  117. wm.RequestTime = rtt
  118. var out struct{ ID string }
  119. if err := decodeBody(resp, &out); err != nil {
  120. return "", nil, err
  121. }
  122. return out.ID, wm, nil
  123. }
  124. // Update makes updates to an existing prepared query.
  125. func (c *PreparedQuery) Update(query *PreparedQueryDefinition, q *WriteOptions) (*WriteMeta, error) {
  126. return c.c.write("/v1/query/"+query.ID, query, nil, q)
  127. }
  128. // List is used to fetch all the prepared queries (always requires a management
  129. // token).
  130. func (c *PreparedQuery) List(q *QueryOptions) ([]*PreparedQueryDefinition, *QueryMeta, error) {
  131. var out []*PreparedQueryDefinition
  132. qm, err := c.c.query("/v1/query", &out, q)
  133. if err != nil {
  134. return nil, nil, err
  135. }
  136. return out, qm, nil
  137. }
  138. // Get is used to fetch a specific prepared query.
  139. func (c *PreparedQuery) Get(queryID string, q *QueryOptions) ([]*PreparedQueryDefinition, *QueryMeta, error) {
  140. var out []*PreparedQueryDefinition
  141. qm, err := c.c.query("/v1/query/"+queryID, &out, q)
  142. if err != nil {
  143. return nil, nil, err
  144. }
  145. return out, qm, nil
  146. }
  147. // Delete is used to delete a specific prepared query.
  148. func (c *PreparedQuery) Delete(queryID string, q *WriteOptions) (*WriteMeta, error) {
  149. r := c.c.newRequest("DELETE", "/v1/query/"+queryID)
  150. r.setWriteOptions(q)
  151. rtt, resp, err := requireOK(c.c.doRequest(r))
  152. if err != nil {
  153. return nil, err
  154. }
  155. defer resp.Body.Close()
  156. wm := &WriteMeta{}
  157. wm.RequestTime = rtt
  158. return wm, nil
  159. }
  160. // Execute is used to execute a specific prepared query. You can execute using
  161. // a query ID or name.
  162. func (c *PreparedQuery) Execute(queryIDOrName string, q *QueryOptions) (*PreparedQueryExecuteResponse, *QueryMeta, error) {
  163. var out *PreparedQueryExecuteResponse
  164. qm, err := c.c.query("/v1/query/"+queryIDOrName+"/execute", &out, q)
  165. if err != nil {
  166. return nil, nil, err
  167. }
  168. return out, qm, nil
  169. }