class Diplomat::Token

Methods for interacting with the Consul ACL Policy API endpoint

Attributes

acl[R]
id[R]
type[R]

Public Instance Methods

clone(value, options = {}) click to toggle source

Clone an existing ACL token @param value [Hash] ACL token definition, AccessorID is mandatory @param options [Hash] options parameter hash @return [Hash] cloned ACL token

# 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 = {}) click to toggle source

Create a new ACL token @param value [Hash] ACL token definition @param options [Hash] options parameter hash @return [Hash] new ACL token

# 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 = {}) click to toggle source

Delete an existing ACL token @param id [String] UUID of the ACL token to delete @param options [Hash] options parameter hash @return [Bool]

# 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 = {}) click to toggle source

List all the ACL tokens @param policy [String] filters the token list matching the specific policy ID @param role [String] filters the token list matching the specific role ID @param authmethod [String] the token list matching the specific named auth method @param options [Hash] options parameter hash @return [List] list of [Hash] of ACL tokens

# 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) click to toggle source

Read ACL token with the given Accessor ID @param id [String] accessor ID of the ACL token to read @param options [Hash] options parameter hash @return [Hash] existing ACL token rubocop:disable Metrics/PerceivedComplexity

# 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 = {}) click to toggle source

Returns ACL token details matching X-Consul-Token header @param options [Hash] options parameter hash @return [Hash] ACL token

# 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 = {}) click to toggle source

Update an existing ACL token @param value [Hash] ACL token definition, AccessorID is mandatory @param options [Hash] options parameter hash @return [Hash] result ACL token

# 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