Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get all attributes of a single key #182

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion lib/diplomat/kv.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Diplomat
# Methods for interacting with the Consul KV API endpoint
class Kv < Diplomat::RestClient
@access_methods = %i[get put delete txn]
@access_methods = %i[get get_attributes put delete txn]
attr_reader :key, :value, :raw

# Get a value by its key, potentially blocking for the first or next value
Expand Down Expand Up @@ -50,6 +50,7 @@ def get(key, options = {}, not_found = :reject, found = :return)
custom_params << dc(@options)
custom_params << keys(@options)
custom_params << separator(@options)
custom_params << session(@options)

return_nil_values = @options && @options[:nil_values]
transformation = @options && @options[:transformation] && @options[:transformation].methods.find_index(:call) ? @options[:transformation] : nil
Expand Down Expand Up @@ -97,6 +98,20 @@ def get(key, options = {}, not_found = :reject, found = :return)
end
# rubocop:enable PerceivedComplexity, LineLength, CyclomaticComplexity

# Get key attributes
# @param key [String] the key
# @param options [Hash] the query params
# @return [OpenStruct]
def get_attributes(key, options = {})
@options = options
custom_params = []
custom_params << use_cas(@options)
custom_params << dc(@options)
custom_params << session(@options)
@raw = send_get_request(@conn, ["/v1/kv/#{key}"], options, custom_params)
OpenStruct.new JSON.parse(@raw.body).first
end

# Associate a value with a key
# @param key [String] the key
# @param value [String] the value
Expand All @@ -111,6 +126,7 @@ def put(key, value, options = {})
custom_params << use_cas(@options)
custom_params << dc(@options)
custom_params << acquire(@options)
custom_params << session(@options)
@raw = send_put_request(@conn, ["/v1/kv/#{key}"], options, value, custom_params)
if @raw.body.chomp == 'true'
@key = key
Expand All @@ -131,6 +147,7 @@ def delete(key, options = {})
custom_params = []
custom_params << recurse_get(@options)
custom_params << dc(@options)
custom_params << session(@options)
@raw = send_delete_request(@conn, ["/v1/kv/#{@key}"], options, custom_params)
end

Expand Down Expand Up @@ -162,6 +179,7 @@ def txn(value, options = {})
custom_params = []
custom_params << dc(options)
custom_params << transaction_consistency(options)
custom_params << session(options)
raw = send_put_request(@conn_no_err, ['/v1/txn'], options, value, custom_params)
transaction_return JSON.parse(raw.body), options
end
Expand All @@ -188,6 +206,10 @@ def separator(options)
options[:separator] ? use_named_parameter('separator', options[:separator]) : []
end

def session(options)
options[:session] ? use_named_parameter('session', options[:session]) : []
end

def transaction_consistency(options)
return [] unless options

Expand Down
26 changes: 26 additions & 0 deletions spec/kv_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,32 @@
end
end

describe '#get attributes' do
context 'of a key' do
let(:json) do
JSON.generate(
[
{
"LockIndex": 1,
"Key": 'foo',
"Flags": 0,
"Value": 'InRvdG8i',
"CreateIndex": 630,
"ModifyIndex": 630
}
]
)
end

it 'return attributes' do
kv = Diplomat::Kv.new
stub_request(:get, 'http://localhost:8500/v1/kv/foo')
.to_return(OpenStruct.new(status: 200, body: json))
kv.get_attributes('foo')
end
end
end

describe '#put' do
context 'ACLs NOT enabled' do
it 'PUT' do
Expand Down