PageRenderTime 96ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/backstage/backstage
Go | 252 lines | 98 code | 54 blank | 100 comment | 4 complexity | d5563628f0f283f78c796f48cdf0ea9d MD5 | raw file
Possible License(s): Apache-2.0, MIT, BSD-3-Clause, MPL-2.0-no-copyleft-exception
  1. package structs
  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. // Failover controls what we do if there are no healthy nodes in the
  24. // local datacenter.
  25. Failover QueryDatacenterOptions
  26. // If OnlyPassing is true then we will only include nodes with passing
  27. // health checks (critical AND warning checks will cause a node to be
  28. // discarded)
  29. OnlyPassing bool
  30. // Near allows the query to always prefer the node nearest the given
  31. // node. If the node does not exist, results are returned in their
  32. // normal randomly-shuffled order. Supplying the magic "_agent" value
  33. // is supported to sort near the agent which initiated the request.
  34. Near string
  35. // Tags are a set of required and/or disallowed tags. If a tag is in
  36. // this list it must be present. If the tag is preceded with "!" then
  37. // it is disallowed.
  38. Tags []string
  39. }
  40. const (
  41. // QueryTemplateTypeNamePrefixMatch uses the Name field of the query as
  42. // a prefix to select the template.
  43. QueryTemplateTypeNamePrefixMatch = "name_prefix_match"
  44. )
  45. // QueryTemplateOptions controls settings if this query is a template.
  46. type QueryTemplateOptions struct {
  47. // Type, if non-empty, means that this query is a template. This is
  48. // set to one of the QueryTemplateType* constants above.
  49. Type string
  50. // Regexp is an optional regular expression to use to parse the full
  51. // name, once the prefix match has selected a template. This can be
  52. // used to extract parts of the name and choose a service name, set
  53. // tags, etc.
  54. Regexp string
  55. }
  56. // PreparedQuery defines a complete prepared query, and is the structure we
  57. // maintain in the state store.
  58. type PreparedQuery 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. // Template is used to configure this query as a template, which will
  74. // respond to queries based on the Name, and then will be rendered
  75. // before it is executed.
  76. Template QueryTemplateOptions
  77. // Service defines a service query (leaving things open for other types
  78. // later).
  79. Service ServiceQuery
  80. // DNS has options that control how the results of this query are
  81. // served over DNS.
  82. DNS QueryDNSOptions
  83. RaftIndex
  84. }
  85. // GetACLPrefix returns the prefix to look up the prepared_query ACL policy for
  86. // this query, and whether the prefix applies to this query. You always need to
  87. // check the ok value before using the prefix.
  88. func (pq *PreparedQuery) GetACLPrefix() (string, bool) {
  89. if pq.Name != "" || pq.Template.Type != "" {
  90. return pq.Name, true
  91. }
  92. return "", false
  93. }
  94. type PreparedQueries []*PreparedQuery
  95. type IndexedPreparedQueries struct {
  96. Queries PreparedQueries
  97. QueryMeta
  98. }
  99. type PreparedQueryOp string
  100. const (
  101. PreparedQueryCreate PreparedQueryOp = "create"
  102. PreparedQueryUpdate PreparedQueryOp = "update"
  103. PreparedQueryDelete PreparedQueryOp = "delete"
  104. )
  105. // QueryRequest is used to create or change prepared queries.
  106. type PreparedQueryRequest struct {
  107. // Datacenter is the target this request is intended for.
  108. Datacenter string
  109. // Op is the operation to apply.
  110. Op PreparedQueryOp
  111. // Query is the query itself.
  112. Query *PreparedQuery
  113. // WriteRequest holds the ACL token to go along with this request.
  114. WriteRequest
  115. }
  116. // RequestDatacenter returns the datacenter for a given request.
  117. func (q *PreparedQueryRequest) RequestDatacenter() string {
  118. return q.Datacenter
  119. }
  120. // PreparedQuerySpecificRequest is used to get information about a prepared
  121. // query.
  122. type PreparedQuerySpecificRequest struct {
  123. // Datacenter is the target this request is intended for.
  124. Datacenter string
  125. // QueryID is the ID of a query.
  126. QueryID string
  127. // QueryOptions (unfortunately named here) controls the consistency
  128. // settings for the query lookup itself, as well as the service lookups.
  129. QueryOptions
  130. }
  131. // RequestDatacenter returns the datacenter for a given request.
  132. func (q *PreparedQuerySpecificRequest) RequestDatacenter() string {
  133. return q.Datacenter
  134. }
  135. // PreparedQueryExecuteRequest is used to execute a prepared query.
  136. type PreparedQueryExecuteRequest struct {
  137. // Datacenter is the target this request is intended for.
  138. Datacenter string
  139. // QueryIDOrName is the ID of a query _or_ the name of one, either can
  140. // be provided.
  141. QueryIDOrName string
  142. // Limit will trim the resulting list down to the given limit.
  143. Limit int
  144. // Source is used to sort the results relative to a given node using
  145. // network coordinates.
  146. Source QuerySource
  147. // Agent is used to carry around a reference to the agent which initiated
  148. // the execute request. Used to distance-sort relative to the local node.
  149. Agent QuerySource
  150. // QueryOptions (unfortunately named here) controls the consistency
  151. // settings for the query lookup itself, as well as the service lookups.
  152. QueryOptions
  153. }
  154. // RequestDatacenter returns the datacenter for a given request.
  155. func (q *PreparedQueryExecuteRequest) RequestDatacenter() string {
  156. return q.Datacenter
  157. }
  158. // PreparedQueryExecuteRemoteRequest is used when running a local query in a
  159. // remote datacenter.
  160. type PreparedQueryExecuteRemoteRequest struct {
  161. // Datacenter is the target this request is intended for.
  162. Datacenter string
  163. // Query is a copy of the query to execute. We have to ship the entire
  164. // query over since it won't be present in the remote state store.
  165. Query PreparedQuery
  166. // Limit will trim the resulting list down to the given limit.
  167. Limit int
  168. // QueryOptions (unfortunately named here) controls the consistency
  169. // settings for the the service lookups.
  170. QueryOptions
  171. }
  172. // RequestDatacenter returns the datacenter for a given request.
  173. func (q *PreparedQueryExecuteRemoteRequest) RequestDatacenter() string {
  174. return q.Datacenter
  175. }
  176. // PreparedQueryExecuteResponse has the results of executing a query.
  177. type PreparedQueryExecuteResponse struct {
  178. // Service is the service that was queried.
  179. Service string
  180. // Nodes has the nodes that were output by the query.
  181. Nodes CheckServiceNodes
  182. // DNS has the options for serving these results over DNS.
  183. DNS QueryDNSOptions
  184. // Datacenter is the datacenter that these results came from.
  185. Datacenter string
  186. // Failovers is a count of how many times we had to query a remote
  187. // datacenter.
  188. Failovers int
  189. // QueryMeta has freshness information about the query.
  190. QueryMeta
  191. }
  192. // PreparedQueryExplainResponse has the results when explaining a query/
  193. type PreparedQueryExplainResponse struct {
  194. // Query has the fully-rendered query.
  195. Query PreparedQuery
  196. // QueryMeta has freshness information about the query.
  197. QueryMeta
  198. }