Class: Diplomat::Query

Inherits:
RestClient show all
Defined in:
lib/diplomat/query.rb

Overview

Methods for interacting with the Consul query API endpoint

Instance Method Summary collapse

Methods inherited from RestClient

access_method?, #concat_url, #configuration, #initialize, method_missing, respond_to?, respond_to_missing?, #use_named_parameter

Constructor Details

This class inherits a constructor from Diplomat::RestClient

Instance Method Details

#create(definition, options = {}) ⇒ String

Create a prepared query or prepared query template

Parameters:

  • definition (Hash)

    Hash containing definition of prepared query

  • options (Hash) (defaults to: {})

    :dc Consul datacenter to query

Returns:

  • (String)

    the ID of the prepared query created



29
30
31
32
33
34
35
# File 'lib/diplomat/query.rb', line 29

def create(definition, options = {})
  custom_params = options[:dc] ? use_named_parameter('dc', options[:dc]) : nil
  @raw = send_post_request(@conn, ['/v1/query'], options, definition, custom_params)
  parse_body
rescue Faraday::ClientError
  raise Diplomat::QueryAlreadyExists
end

#delete(key, options = {}) ⇒ Boolean

Delete a prepared query or prepared query template

Parameters:

  • key (String)

    the prepared query ID

  • options (Hash) (defaults to: {})

    :dc Consul datacenter to query

Returns:

  • (Boolean)


41
42
43
44
45
# File 'lib/diplomat/query.rb', line 41

def delete(key, options = {})
  custom_params = options[:dc] ? use_named_parameter('dc', options[:dc]) : nil
  ret = send_delete_request(@conn, ["/v1/query/#{key}"], options, custom_params)
  ret.status == 200
end

#execute(key, options = {}) ⇒ OpenStruct

Execute a prepared query or prepared query template rubocop:disable PerceivedComplexity

Parameters:

  • key (String)

    the prepared query ID or name

  • options (Hash) (defaults to: {})

    prepared query execution options

  • dc (Hash)

    a customizable set of options

  • near (Hash)

    a customizable set of options

  • limit (Hash)

    a customizable set of options

Returns:

  • (OpenStruct)

    the list of results from the prepared query or prepared query template



67
68
69
70
71
72
73
74
# File 'lib/diplomat/query.rb', line 67

def execute(key, options = {})
  custom_params = []
  custom_params << use_named_parameter('dc', options[:dc]) if options[:dc]
  custom_params << use_named_parameter('near', options[:near]) if options[:near]
  custom_params << use_named_parameter('limit', options[:limit]) if options[:limit]
  ret = send_get_request(@conn, ["/v1/query/#{key}/execute"], options, custom_params)
  OpenStruct.new JSON.parse(ret.body)
end

#explain(key, options = {}) ⇒ OpenStruct

Get the fully rendered query template

Parameters:

  • key (String)

    the prepared query ID or name

  • options (Hash) (defaults to: {})

    :dc Consul datacenter to query

Returns:

  • (OpenStruct)

    the list of results from the prepared query or prepared query template



81
82
83
84
85
# File 'lib/diplomat/query.rb', line 81

def explain(key, options = {})
  custom_params = options[:dc] ? use_named_parameter('dc', options[:dc]) : nil
  ret = send_get_request(@conn, ["/v1/query/#{key}/explain"], options, custom_params)
  OpenStruct.new JSON.parse(ret.body)
end

#get(key, options = {}) ⇒ OpenStruct

Get a prepared query by it’s key

Parameters:

  • key (String)

    the prepared query ID

  • options (Hash) (defaults to: {})

    :dc string for dc specific query

Returns:

  • (OpenStruct)

    all data associated with the prepared query



10
11
12
13
14
# File 'lib/diplomat/query.rb', line 10

def get(key, options = {})
  custom_params = options[:dc] ? use_named_parameter('dc', options[:dc]) : nil
  ret = send_get_request(@conn, ["/v1/query/#{key}"], options, custom_params)
  JSON.parse(ret.body).map { |query| OpenStruct.new query }
end

#get_all(options = {}) ⇒ OpenStruct

Get all prepared queries

Parameters:

  • options (Hash) (defaults to: {})

    :dc Consul datacenter to query

Returns:

  • (OpenStruct)

    the list of all prepared queries



19
20
21
22
23
# File 'lib/diplomat/query.rb', line 19

def get_all(options = {})
  custom_params = options[:dc] ? use_named_parameter('dc', options[:dc]) : nil
  ret = send_get_request(@conn, ['/v1/query'], options, custom_params)
  JSON.parse(ret.body).map { |query| OpenStruct.new query }
end

#update(key, definition, options = {}) ⇒ Boolean

Update a prepared query or prepared query template

Parameters:

  • key (String)

    the prepared query ID

  • definition (Hash)

    Hash containing updated definition of prepared query

  • options (Hash) (defaults to: {})

    :dc Consul datacenter to query

Returns:

  • (Boolean)


52
53
54
55
56
# File 'lib/diplomat/query.rb', line 52

def update(key, definition, options = {})
  custom_params = options[:dc] ? use_named_parameter('dc', options[:dc]) : nil
  ret = send_put_request(@conn, ["/v1/query/#{key}"], options, definition, custom_params)
  ret.status == 200
end