Class: Diplomat::Token

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

Overview

Methods for interacting with the Consul ACL Policy API endpoint

Instance Attribute Summary collapse

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 Attribute Details

#aclObject (readonly)

Returns the value of attribute acl.



7
8
9
# File 'lib/diplomat/token.rb', line 7

def acl
  @acl
end

#idObject (readonly)

Returns the value of attribute id.



7
8
9
# File 'lib/diplomat/token.rb', line 7

def id
  @id
end

#typeObject (readonly)

Returns the value of attribute type.



7
8
9
# File 'lib/diplomat/token.rb', line 7

def type
  @type
end

Instance Method Details

#clone(value, options = {}) ⇒ Hash

Clone an existing ACL token

Parameters:

  • value (Hash)

    ACL token definition, AccessorID is mandatory

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

    options parameter hash

Returns:

  • (Hash)

    cloned ACL token

Raises:



107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/diplomat/token.rb', line 107

def clone(value, options = {})
  id = value[:AccessorID] || value['AccessorID']
  raise Diplomat::AccessorIdParameterRequired if id.nil?

  custom_params = use_cas(@options)
  @raw = send_put_request(@conn, ["/v1/acl/token/#{id}/clone"], options, value, custom_params)
  if @raw.status == 200
    parse_body
  elsif @raw.status == 403
    raise Diplomat::AclNotFound, id
  else
    raise Diplomat::UnknownStatus, "status #{@raw.status}: #{@raw.body}"
  end
end

#create(value, options = {}) ⇒ Hash

Create a new ACL token

Parameters:

  • value (Hash)

    ACL token definition

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

    options parameter hash

Returns:

  • (Hash)

    new ACL token

Raises:



83
84
85
86
87
88
89
# File 'lib/diplomat/token.rb', line 83

def create(value, options = {})
  custom_params = use_cas(@options)
  @raw = send_put_request(@conn, ['/v1/acl/token'], options, value, custom_params)
  return parse_body if @raw.status == 200

  raise Diplomat::UnknownStatus, "status #{@raw.status}: #{@raw.body}"
end

#delete(id, options = {}) ⇒ Bool

Delete an existing ACL token

Parameters:

  • id (String)

    UUID of the ACL token to delete

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

    options parameter hash

Returns:

  • (Bool)

Raises:

  • (Diplomat::NotPermitted)


95
96
97
98
99
100
101
# File 'lib/diplomat/token.rb', line 95

def delete(id, options = {})
  anonymous_token = '00000000-0000-0000-0000-000000000002'
  raise Diplomat::NotPermitted, "status #{@raw.status}: #{@raw.body}" if id == anonymous_token

  @raw = send_delete_request(@conn, ["/v1/acl/token/#{id}"], options, nil)
  @raw.body.chomp == 'true'
end

#list(policy = nil, role = nil, authmethod = nil, options = {}) ⇒ List

List all the ACL tokens

Parameters:

  • policy (String) (defaults to: nil)

    filters the token list matching the specific policy ID

  • role (String) (defaults to: nil)

    filters the token list matching the specific role ID

  • authmethod (String) (defaults to: nil)

    the token list matching the specific named auth method

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

    options parameter hash

Returns:

  • (List)

    list of [Hash] of ACL tokens

Raises:



47
48
49
50
51
52
53
54
55
56
# File 'lib/diplomat/token.rb', line 47

def list(policy = nil, role = nil, authmethod = nil, options = {})
  custom_params = []
  custom_params << use_named_parameter('policy', policy) if policy
  custom_params << use_named_parameter('role', policy) if role
  custom_params << use_named_parameter('authmethod', policy) if authmethod
  @raw = send_get_request(@conn_no_err, ['/v1/acl/tokens'], options, custom_params)
  raise Diplomat::AclNotFound if @raw.status == 403

  parse_body
end

#read(id, options = {}, not_found = :reject, found = :return) ⇒ Hash

Read ACL token with the given Accessor ID rubocop:disable Metrics/PerceivedComplexity

Parameters:

  • id (String)

    accessor ID of the ACL token to read

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

    options parameter hash

Returns:

  • (Hash)

    existing ACL token



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/diplomat/token.rb', line 14

def read(id, options = {}, not_found = :reject, found = :return)
  @options = options
  custom_params = []
  custom_params << use_consistency(options)

  @raw = send_get_request(@conn_no_err, ["/v1/acl/token/#{id}"], options, custom_params)

  if @raw.status == 200 && @raw.body.chomp != 'null'
    case found
    when :reject
      raise Diplomat::AclNotFound, id
    when :return
      return parse_body
    end
  elsif @raw.status == 403
    case not_found
    when :reject
      raise Diplomat::AclNotFound, id
    when :return
      return nil
    end
  else
    raise Diplomat::UnknownStatus, "status #{@raw.status}: #{@raw.body}"
  end
end

#self(options = {}) ⇒ Hash

Returns ACL token details matching X-Consul-Token header

Parameters:

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

    options parameter hash

Returns:

  • (Hash)

    ACL token



125
126
127
128
129
130
131
132
133
# File 'lib/diplomat/token.rb', line 125

def self(options = {})
  custom_params = use_cas(@options)
  @raw = send_get_request(@conn, ['/v1/acl/token/self'], options, custom_params)
  if @raw.status == 200
    parse_body
  elsif @raw.status == 403
    raise Diplomat::AclNotFound, id
  end
end

#update(value, options = {}) ⇒ Hash

Update an existing ACL token

Parameters:

  • value (Hash)

    ACL token definition, AccessorID is mandatory

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

    options parameter hash

Returns:

  • (Hash)

    result ACL token

Raises:



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/diplomat/token.rb', line 62

def update(value, options = {})
  id = value[:AccessorID] || value['AccessorID']
  raise Diplomat::AccessorIdParameterRequired if id.nil?

  custom_params = use_cas(@options)
  @raw = send_put_request(@conn, ["/v1/acl/token/#{id}"], options, value, custom_params)
  if @raw.status == 200
    parse_body
  elsif @raw.status == 403
    raise Diplomat::AclNotFound, id
  elsif @raw.status == 400
    raise Diplomat::TokenMalformed, @raw.body
  else
    raise Diplomat::UnknownStatus, "status #{@raw.status}: #{@raw.body}"
  end
end