PageRenderTime 243ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/housecream/server
Go | 194 lines | 98 code | 33 blank | 63 comment | 14 complexity | 276e128634c983b9199806c6ff41fd14 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. // If OnlyPassing is true then we will only include nodes with passing
  31. // health checks (critical AND warning checks will cause a node to be
  32. // discarded)
  33. OnlyPassing bool
  34. // Tags are a set of required and/or disallowed tags. If a tag is in
  35. // this list it must be present. If the tag is preceded with "!" then
  36. // it is disallowed.
  37. Tags []string
  38. }
  39. // QueryTemplate carries the arguments for creating a templated query.
  40. type QueryTemplate struct {
  41. // Type specifies the type of the query template. Currently only
  42. // "name_prefix_match" is supported. This field is required.
  43. Type string
  44. // Regexp allows specifying a regex pattern to match against the name
  45. // of the query being executed.
  46. Regexp string
  47. }
  48. // PrepatedQueryDefinition defines a complete prepared query.
  49. type PreparedQueryDefinition struct {
  50. // ID is this UUID-based ID for the query, always generated by Consul.
  51. ID string
  52. // Name is an optional friendly name for the query supplied by the
  53. // user. NOTE - if this feature is used then it will reduce the security
  54. // of any read ACL associated with this query/service since this name
  55. // can be used to locate nodes with supplying any ACL.
  56. Name string
  57. // Session is an optional session to tie this query's lifetime to. If
  58. // this is omitted then the query will not expire.
  59. Session string
  60. // Token is the ACL token used when the query was created, and it is
  61. // used when a query is subsequently executed. This token, or a token
  62. // with management privileges, must be used to change the query later.
  63. Token string
  64. // Service defines a service query (leaving things open for other types
  65. // later).
  66. Service ServiceQuery
  67. // DNS has options that control how the results of this query are
  68. // served over DNS.
  69. DNS QueryDNSOptions
  70. // Template is used to pass through the arguments for creating a
  71. // prepared query with an attached template. If a template is given,
  72. // interpolations are possible in other struct fields.
  73. Template QueryTemplate
  74. }
  75. // PreparedQueryExecuteResponse has the results of executing a query.
  76. type PreparedQueryExecuteResponse struct {
  77. // Service is the service that was queried.
  78. Service string
  79. // Nodes has the nodes that were output by the query.
  80. Nodes []ServiceEntry
  81. // DNS has the options for serving these results over DNS.
  82. DNS QueryDNSOptions
  83. // Datacenter is the datacenter that these results came from.
  84. Datacenter string
  85. // Failovers is a count of how many times we had to query a remote
  86. // datacenter.
  87. Failovers int
  88. }
  89. // PreparedQuery can be used to query the prepared query endpoints.
  90. type PreparedQuery struct {
  91. c *Client
  92. }
  93. // PreparedQuery returns a handle to the prepared query endpoints.
  94. func (c *Client) PreparedQuery() *PreparedQuery {
  95. return &PreparedQuery{c}
  96. }
  97. // Create makes a new prepared query. The ID of the new query is returned.
  98. func (c *PreparedQuery) Create(query *PreparedQueryDefinition, q *WriteOptions) (string, *WriteMeta, error) {
  99. r := c.c.newRequest("POST", "/v1/query")
  100. r.setWriteOptions(q)
  101. r.obj = query
  102. rtt, resp, err := requireOK(c.c.doRequest(r))
  103. if err != nil {
  104. return "", nil, err
  105. }
  106. defer resp.Body.Close()
  107. wm := &WriteMeta{}
  108. wm.RequestTime = rtt
  109. var out struct{ ID string }
  110. if err := decodeBody(resp, &out); err != nil {
  111. return "", nil, err
  112. }
  113. return out.ID, wm, nil
  114. }
  115. // Update makes updates to an existing prepared query.
  116. func (c *PreparedQuery) Update(query *PreparedQueryDefinition, q *WriteOptions) (*WriteMeta, error) {
  117. return c.c.write("/v1/query/"+query.ID, query, nil, q)
  118. }
  119. // List is used to fetch all the prepared queries (always requires a management
  120. // token).
  121. func (c *PreparedQuery) List(q *QueryOptions) ([]*PreparedQueryDefinition, *QueryMeta, error) {
  122. var out []*PreparedQueryDefinition
  123. qm, err := c.c.query("/v1/query", &out, q)
  124. if err != nil {
  125. return nil, nil, err
  126. }
  127. return out, qm, nil
  128. }
  129. // Get is used to fetch a specific prepared query.
  130. func (c *PreparedQuery) Get(queryID string, q *QueryOptions) ([]*PreparedQueryDefinition, *QueryMeta, error) {
  131. var out []*PreparedQueryDefinition
  132. qm, err := c.c.query("/v1/query/"+queryID, &out, q)
  133. if err != nil {
  134. return nil, nil, err
  135. }
  136. return out, qm, nil
  137. }
  138. // Delete is used to delete a specific prepared query.
  139. func (c *PreparedQuery) Delete(queryID string, q *QueryOptions) (*QueryMeta, error) {
  140. r := c.c.newRequest("DELETE", "/v1/query/"+queryID)
  141. r.setQueryOptions(q)
  142. rtt, resp, err := requireOK(c.c.doRequest(r))
  143. if err != nil {
  144. return nil, err
  145. }
  146. defer resp.Body.Close()
  147. qm := &QueryMeta{}
  148. parseQueryMeta(resp, qm)
  149. qm.RequestTime = rtt
  150. return qm, nil
  151. }
  152. // Execute is used to execute a specific prepared query. You can execute using
  153. // a query ID or name.
  154. func (c *PreparedQuery) Execute(queryIDOrName string, q *QueryOptions) (*PreparedQueryExecuteResponse, *QueryMeta, error) {
  155. var out *PreparedQueryExecuteResponse
  156. qm, err := c.c.query("/v1/query/"+queryIDOrName+"/execute", &out, q)
  157. if err != nil {
  158. return nil, nil, err
  159. }
  160. return out, qm, nil
  161. }