Class: Diplomat::Session

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

Overview

Methods for interacting with the Consul session 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(value = nil, options = {}) ⇒ String

Create a new session

Parameters:

  • value (Object) (defaults to: nil)

    hash or json representation of the session arguments

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

    session options

Returns:

  • (String)

    The sesssion id


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

def create(value = nil, options = {})
  # TODO: only certain keys are recognised in a session create request,
  # should raise an error on others.
  custom_params = []
  custom_params << use_named_parameter('dc', options[:dc]) if options[:dc]
  data = value.is_a?(String) ? value : JSON.generate(value) unless value.nil?
  raw = send_put_request(@conn, ['/v1/session/create'], options, data, custom_params)
  body = JSON.parse(raw.body)
  body['ID']
end

#destroy(id, options = {}) ⇒ String

Destroy a session

Parameters:

  • id (String)

    session id

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

    session options

Returns:

  • (String)

    Success or failure of the session destruction


27
28
29
30
31
32
# File 'lib/diplomat/session.rb', line 27

def destroy(id, options = {})
  custom_params = []
  custom_params << use_named_parameter('dc', options[:dc]) if options[:dc]
  raw = send_put_request(@conn, ["/v1/session/destroy/#{id}"], options, nil, custom_params)
  raw.body
end

#info(id, options = {}) ⇒ OpenStruct

Session information

Parameters:

  • id (String)

    session id

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

    session options

Returns:

  • (OpenStruct)

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

def info(id, options = {})
  custom_params = []
  custom_params << use_named_parameter('dc', options[:dc]) if options[:dc]
  raw = send_get_request(@conn, ["/v1/session/info/#{id}"], options, custom_params)
  JSON.parse(raw.body).map { |session| OpenStruct.new session }
end

#list(options = {}) ⇒ OpenStruct

List sessions

Parameters:

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

    session options

Returns:

  • (OpenStruct)

37
38
39
40
41
42
# File 'lib/diplomat/session.rb', line 37

def list(options = {})
  custom_params = []
  custom_params << use_named_parameter('dc', options[:dc]) if options[:dc]
  raw = send_get_request(@conn, ['/v1/session/list'], options, custom_params)
  JSON.parse(raw.body).map { |session| OpenStruct.new session }
end

#node(name, options = {}) ⇒ OpenStruct

Session information for a given node

Parameters:

  • name (String)

    node name

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

    session options

Returns:

  • (OpenStruct)

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

def node(name, options = {})
  custom_params = []
  custom_params << use_named_parameter('dc', options[:dc]) if options[:dc]
  raw = send_get_request(@conn, ["/v1/session/node/#{name}"], options, custom_params)
  JSON.parse(raw.body).map { |session| OpenStruct.new session }
end

#renew(id, options = {}) ⇒ OpenStruct

Renew session

Parameters:

  • id (String)

    session id

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

    session options

Returns:

  • (OpenStruct)

48
49
50
51
52
53
# File 'lib/diplomat/session.rb', line 48

def renew(id, options = {})
  custom_params = []
  custom_params << use_named_parameter('dc', options[:dc]) if options[:dc]
  raw = send_put_request(@conn, ["/v1/session/renew/#{id}"], options, nil, custom_params)
  JSON.parse(raw.body).map { |session| OpenStruct.new session }
end