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



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

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)


43
44
45
46
47
# File 'lib/diplomat/query.rb', line 43

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 Metrics/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



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

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



83
84
85
86
87
# File 'lib/diplomat/query.rb', line 83

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



12
13
14
15
16
# File 'lib/diplomat/query.rb', line 12

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



21
22
23
24
25
# File 'lib/diplomat/query.rb', line 21

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)


54
55
56
57
58
# File 'lib/diplomat/query.rb', line 54

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