Class: Diplomat::Query

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

Overview

Methods for interacting with the Consul query API endpoint

Instance Method Summary collapse

Methods included from ApiOptions

#check_acl_token, #use_cas, #use_consistency, #valid_transaction_verbs, #valid_value_transactions

Methods inherited from RestClient

access_method?, #concat_url, #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 = nil) ⇒ String

Create a prepared query or prepared query template

Parameters:

  • definition (Hash)

    Hash containing definition of prepared query

  • options (Hash) (defaults to: nil)

    :dc Consul datacenter to query

Returns:

  • (String)

    the ID of the prepared query created



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/diplomat/query.rb', line 41

def create(definition, options = nil)
  url = ['/v1/query']
  url += check_acl_token
  url << use_named_parameter('dc', options[:dc]) if options && options[:dc]
  @raw = @conn.post do |req|
    req.url concat_url url
    req.body = JSON.dump(definition)
  end

  parse_body
rescue Faraday::ClientError
  raise Diplomat::QueryAlreadyExists
end

#delete(key, options = nil) ⇒ Boolean

Delete a prepared query or prepared query template

Parameters:

  • key (String)

    the prepared query ID

  • options (Hash) (defaults to: nil)

    :dc Consul datacenter to query

Returns:

  • (Boolean)


59
60
61
62
63
64
65
66
# File 'lib/diplomat/query.rb', line 59

def delete(key, options = nil)
  url = ["/v1/query/#{key}"]
  url += check_acl_token
  url << use_named_parameter('dc', options[:dc]) if options && options[:dc]
  ret = @conn.delete concat_url url

  ret.status == 200
end

#execute(key, options = nil) ⇒ OpenStruct

Execute a prepared query or prepared query template rubocop:disable PerceivedComplexity, CyclomaticComplexity, AbcSize

Parameters:

  • key (String)

    the prepared query ID or name

  • options (Hash) (defaults to: nil)

    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



95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/diplomat/query.rb', line 95

def execute(key, options = nil)
  url = ["/v1/query/#{key}/execute"]
  url += check_acl_token
  url << use_named_parameter('dc', options[:dc]) if options && options[:dc]
  url << use_named_parameter('near', options[:near]) if options && options[:near]
  url << use_named_parameter('limit', options[:limit]) if options && options[:limit]

  ret = @conn.get concat_url url
  OpenStruct.new JSON.parse(ret.body)
rescue Faraday::ClientError
  raise Diplomat::QueryNotFound
end

#explain(key, options = nil) ⇒ OpenStruct

Get the fully rendered query template

Parameters:

  • key (String)

    the prepared query ID or name

  • options (Hash) (defaults to: nil)

    :dc Consul datacenter to query

Returns:

  • (OpenStruct)

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



113
114
115
116
117
118
119
120
121
122
# File 'lib/diplomat/query.rb', line 113

def explain(key, options = nil)
  url = ["/v1/query/#{key}/explain"]
  url += check_acl_token
  url << use_named_parameter('dc', options[:dc]) if options && options[:dc]

  ret = @conn.get concat_url url
  OpenStruct.new JSON.parse(ret.body)
rescue Faraday::ClientError
  raise Diplomat::QueryNotFound
end

#get(key, options = nil) ⇒ OpenStruct

Get a prepared query by it’s key

Parameters:

  • key (String)

    the prepared query ID

  • options (Hash) (defaults to: nil)

    :dc string for dc specific query

Returns:

  • (OpenStruct)

    all data associated with the prepared query



12
13
14
15
16
17
18
19
20
21
# File 'lib/diplomat/query.rb', line 12

def get(key, options = nil)
  url = ["/v1/query/#{key}"]
  url += check_acl_token
  url << use_named_parameter('dc', options[:dc]) if options && options[:dc]

  ret = @conn.get concat_url url
  JSON.parse(ret.body).map { |query| OpenStruct.new query }
rescue Faraday::ClientError
  raise Diplomat::QueryNotFound
end

#get_all(options = nil) ⇒ OpenStruct

Get all prepared queries

Parameters:

  • options (Hash) (defaults to: nil)

    :dc Consul datacenter to query

Returns:

  • (OpenStruct)

    the list of all prepared queries



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

def get_all(options = nil)
  url = ['/v1/query']
  url += check_acl_token
  url << use_named_parameter('dc', options[:dc]) if options && options[:dc]

  ret = @conn.get concat_url url
  JSON.parse(ret.body).map { |query| OpenStruct.new query }
rescue Faraday::ClientError
  raise Diplomat::PathNotFound
end

#update(key, definition, options = nil) ⇒ 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: nil)

    :dc Consul datacenter to query

Returns:

  • (Boolean)


73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/diplomat/query.rb', line 73

def update(key, definition, options = nil)
  url = ["/v1/query/#{key}"]
  url += check_acl_token
  url << use_named_parameter('dc', options[:dc]) if options && options[:dc]
  json_definition = JSON.dump(definition)
  ret = @conn.put do |req|
    req.url concat_url url
    req.body = json_definition
  end

  ret.status == 200
end