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

Create a new session

Parameters:

  • value (Object) (defaults to: nil)

    hash or json representation of the session arguments

  • options (Hash) (defaults to: nil)

    session options

  • options (String) (defaults to: nil)

    :dc datacenter to create session for

Returns:

  • (String)

    The sesssion id



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

def create(value = nil, options = nil)
  # TODO: only certain keys are recognised in a session create request,
  # should raise an error on others.
  raw = @conn.put do |req|
    url = ['/v1/session/create']
    url += use_named_parameter('dc', options[:dc]) if options && options[:dc]

    req.url concat_url url
    req.body = (value.is_a?(String) ? value : JSON.generate(value)) unless value.nil?
  end
  body = JSON.parse(raw.body)
  body['ID']
end

#destroy(id, options = nil) ⇒ String

Destroy a session

Parameters:

  • id (String)

    session id

  • options (Hash) (defaults to: nil)

    session options

  • options (String) (defaults to: nil)

    :dc datacenter to destroy session for

Returns:

  • (String)

    Success or failure of the session destruction



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

def destroy(id, options = nil)
  raw = @conn.put do |req|
    url = ["/v1/session/destroy/#{id}"]
    url += use_named_parameter('dc', options[:dc]) if options && options[:dc]

    req.url concat_url url
  end
  raw.body
end

#info(id, options = nil) ⇒ OpenStruct

Session information

Parameters:

  • id (String)

    session id

  • options (Hash) (defaults to: nil)

    session options

  • options (String) (defaults to: nil)

    :dc datacenter to renew session for

Returns:

  • (OpenStruct)


74
75
76
77
78
79
80
81
82
# File 'lib/diplomat/session.rb', line 74

def info(id, options = nil)
  raw = @conn.get do |req|
    url = ["/v1/session/info/#{id}"]
    url += use_named_parameter('dc', options[:dc]) if options && options[:dc]

    req.url concat_url url
  end
  JSON.parse(raw.body).map { |session| OpenStruct.new session }
end

#list(options = nil) ⇒ OpenStruct

List sessions

Parameters:

  • options (Hash) (defaults to: nil)

    session options

  • options (String) (defaults to: nil)

    :dc datacenter to list sessions

Returns:

  • (OpenStruct)


44
45
46
47
48
49
50
51
52
# File 'lib/diplomat/session.rb', line 44

def list(options = nil)
  raw = @conn.get do |req|
    url = ['/v1/session/list']
    url += use_named_parameter('dc', options[:dc]) if options && options[:dc]

    req.url concat_url url
  end
  JSON.parse(raw.body).map { |session| OpenStruct.new session }
end

#node(name, options = nil) ⇒ OpenStruct

Session information for a given node

Parameters:

  • name (String)

    node name

  • options (Hash) (defaults to: nil)

    session options

  • options (String) (defaults to: nil)

    :dc datacenter to renew session for

Returns:

  • (OpenStruct)


89
90
91
92
93
94
95
96
97
# File 'lib/diplomat/session.rb', line 89

def node(name, options = nil)
  raw = @conn.get do |req|
    url = ["/v1/session/node/#{name}"]
    url += use_named_parameter('dc', options[:dc]) if options && options[:dc]

    req.url concat_url url
  end
  JSON.parse(raw.body).map { |session| OpenStruct.new session }
end

#renew(id, options = nil) ⇒ OpenStruct

Renew session

Parameters:

  • id (String)

    session id

  • options (Hash) (defaults to: nil)

    session options

  • options (String) (defaults to: nil)

    :dc datacenter to renew session for

Returns:

  • (OpenStruct)


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

def renew(id, options = nil)
  raw = @conn.put do |req|
    url = ["/v1/session/renew/#{id}"]
    url += use_named_parameter('dc', options[:dc]) if options && options[:dc]

    req.url concat_url url
  end
  JSON.parse(raw.body).map { |session| OpenStruct.new session }
end