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

https://github.com/cilium/cilium · Go · 217 lines · 101 code · 37 blank · 79 comment · 14 complexity · af04a45dcfca0757e7ee7ca3f4774587 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. // ServiceMeta is a map of required service metadata fields. If a key/value
  48. // pair is in this map it must be present on the node in order for the
  49. // service entry to be returned.
  50. ServiceMeta map[string]string
  51. // Connect if true will filter the prepared query results to only
  52. // include Connect-capable services. These include both native services
  53. // and proxies for matching services. Note that if a proxy matches,
  54. // the constraints in the query above (Near, OnlyPassing, etc.) apply
  55. // to the _proxy_ and not the service being proxied. In practice, proxies
  56. // should be directly next to their services so this isn't an issue.
  57. Connect bool
  58. }
  59. // QueryTemplate carries the arguments for creating a templated query.
  60. type QueryTemplate struct {
  61. // Type specifies the type of the query template. Currently only
  62. // "name_prefix_match" is supported. This field is required.
  63. Type string
  64. // Regexp allows specifying a regex pattern to match against the name
  65. // of the query being executed.
  66. Regexp string
  67. }
  68. // PreparedQueryDefinition defines a complete prepared query.
  69. type PreparedQueryDefinition struct {
  70. // ID is this UUID-based ID for the query, always generated by Consul.
  71. ID string
  72. // Name is an optional friendly name for the query supplied by the
  73. // user. NOTE - if this feature is used then it will reduce the security
  74. // of any read ACL associated with this query/service since this name
  75. // can be used to locate nodes with supplying any ACL.
  76. Name string
  77. // Session is an optional session to tie this query's lifetime to. If
  78. // this is omitted then the query will not expire.
  79. Session string
  80. // Token is the ACL token used when the query was created, and it is
  81. // used when a query is subsequently executed. This token, or a token
  82. // with management privileges, must be used to change the query later.
  83. Token string
  84. // Service defines a service query (leaving things open for other types
  85. // later).
  86. Service ServiceQuery
  87. // DNS has options that control how the results of this query are
  88. // served over DNS.
  89. DNS QueryDNSOptions
  90. // Template is used to pass through the arguments for creating a
  91. // prepared query with an attached template. If a template is given,
  92. // interpolations are possible in other struct fields.
  93. Template QueryTemplate
  94. }
  95. // PreparedQueryExecuteResponse has the results of executing a query.
  96. type PreparedQueryExecuteResponse struct {
  97. // Service is the service that was queried.
  98. Service string
  99. // Nodes has the nodes that were output by the query.
  100. Nodes []ServiceEntry
  101. // DNS has the options for serving these results over DNS.
  102. DNS QueryDNSOptions
  103. // Datacenter is the datacenter that these results came from.
  104. Datacenter string
  105. // Failovers is a count of how many times we had to query a remote
  106. // datacenter.
  107. Failovers int
  108. }
  109. // PreparedQuery can be used to query the prepared query endpoints.
  110. type PreparedQuery struct {
  111. c *Client
  112. }
  113. // PreparedQuery returns a handle to the prepared query endpoints.
  114. func (c *Client) PreparedQuery() *PreparedQuery {
  115. return &PreparedQuery{c}
  116. }
  117. // Create makes a new prepared query. The ID of the new query is returned.
  118. func (c *PreparedQuery) Create(query *PreparedQueryDefinition, q *WriteOptions) (string, *WriteMeta, error) {
  119. r := c.c.newRequest("POST", "/v1/query")
  120. r.setWriteOptions(q)
  121. r.obj = query
  122. rtt, resp, err := requireOK(c.c.doRequest(r))
  123. if err != nil {
  124. return "", nil, err
  125. }
  126. defer resp.Body.Close()
  127. wm := &WriteMeta{}
  128. wm.RequestTime = rtt
  129. var out struct{ ID string }
  130. if err := decodeBody(resp, &out); err != nil {
  131. return "", nil, err
  132. }
  133. return out.ID, wm, nil
  134. }
  135. // Update makes updates to an existing prepared query.
  136. func (c *PreparedQuery) Update(query *PreparedQueryDefinition, q *WriteOptions) (*WriteMeta, error) {
  137. return c.c.write("/v1/query/"+query.ID, query, nil, q)
  138. }
  139. // List is used to fetch all the prepared queries (always requires a management
  140. // token).
  141. func (c *PreparedQuery) List(q *QueryOptions) ([]*PreparedQueryDefinition, *QueryMeta, error) {
  142. var out []*PreparedQueryDefinition
  143. qm, err := c.c.query("/v1/query", &out, q)
  144. if err != nil {
  145. return nil, nil, err
  146. }
  147. return out, qm, nil
  148. }
  149. // Get is used to fetch a specific prepared query.
  150. func (c *PreparedQuery) Get(queryID string, q *QueryOptions) ([]*PreparedQueryDefinition, *QueryMeta, error) {
  151. var out []*PreparedQueryDefinition
  152. qm, err := c.c.query("/v1/query/"+queryID, &out, q)
  153. if err != nil {
  154. return nil, nil, err
  155. }
  156. return out, qm, nil
  157. }
  158. // Delete is used to delete a specific prepared query.
  159. func (c *PreparedQuery) Delete(queryID string, q *WriteOptions) (*WriteMeta, error) {
  160. r := c.c.newRequest("DELETE", "/v1/query/"+queryID)
  161. r.setWriteOptions(q)
  162. rtt, resp, err := requireOK(c.c.doRequest(r))
  163. if err != nil {
  164. return nil, err
  165. }
  166. defer resp.Body.Close()
  167. wm := &WriteMeta{}
  168. wm.RequestTime = rtt
  169. return wm, nil
  170. }
  171. // Execute is used to execute a specific prepared query. You can execute using
  172. // a query ID or name.
  173. func (c *PreparedQuery) Execute(queryIDOrName string, q *QueryOptions) (*PreparedQueryExecuteResponse, *QueryMeta, error) {
  174. var out *PreparedQueryExecuteResponse
  175. qm, err := c.c.query("/v1/query/"+queryIDOrName+"/execute", &out, q)
  176. if err != nil {
  177. return nil, nil, err
  178. }
  179. return out, qm, nil
  180. }