Class: Diplomat::Acl

Inherits:
RestClient show all
Includes:
ApiOptions
Defined in:
lib/diplomat/acl.rb

Overview

Methods for interacting with the Consul ACL API endpoint

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ApiOptions

#check_acl_token, #use_cas, #use_consistency, #valid_transaction_verbs, #valid_value_transactions

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

#aclObject (readonly)

Returns the value of attribute acl.



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

def acl
  @acl
end

#idObject (readonly)

Returns the value of attribute id.



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

def id
  @id
end

#typeObject (readonly)

Returns the value of attribute type.



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

def type
  @type
end

Instance Method Details

#create(value) ⇒ Hash

Create an Acl definition

Parameters:

  • value (Hash)

    Acl definition, ID field is mandatory

Returns:

  • (Hash)

    The result Acl



70
71
72
73
74
75
76
77
78
79
# File 'lib/diplomat/acl.rb', line 70

def create(value)
  @raw = @conn.put do |req|
    url = ['/v1/acl/create']
    url += check_acl_token
    url += use_cas(@options)
    req.url concat_url url
    req.body = value.to_json
  end
  parse_body
end

#destroy(id) ⇒ Bool

Destroy an ACl token by its id

Parameters:

  • ID (String)

    the Acl ID

Returns:

  • (Bool)


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

def destroy(id)
  @id = id
  url = ["/v1/acl/destroy/#{@id}"]
  url << check_acl_token
  @raw = @conn.put concat_url url
  @raw.body.chomp == 'true'
end

#info(id, options = nil, not_found = :reject, found = :return) ⇒ Hash

Get Acl info by ID rubocop:disable PerceivedComplexity, MethodLength, CyclomaticComplexity, AbcSize

Parameters:

  • id (String)

    ID of the Acl to get

Returns:

  • (Hash)


13
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
39
# File 'lib/diplomat/acl.rb', line 13

def info(id, options = nil, not_found = :reject, found = :return)
  @id = id
  @options = options
  url = ["/v1/acl/info/#{id}"]
  url << check_acl_token
  url << use_consistency(options)

  raw = @conn_no_err.get concat_url url
  if raw.status == 200 && raw.body.chomp != 'null'
    case found
    when :reject
      raise Diplomat::AclAlreadyExists, id
    when :return
      @raw = raw
      return parse_body
    end
  elsif raw.status == 200 && raw.body.chomp == 'null'
    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

#listList

List all Acls

Returns:

  • (List)

    list of [Hash] of Acls



44
45
46
47
48
49
# File 'lib/diplomat/acl.rb', line 44

def list
  url = ['/v1/acl/list']
  url += check_acl_token
  @raw = @conn_no_err.get concat_url url
  parse_body
end

#update(value) ⇒ Hash

Update an Acl definition, create if not present

Parameters:

  • value (Hash)

    Acl definition, ID field is mandatory

Returns:

  • (Hash)

    The result Acl

Raises:



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/diplomat/acl.rb', line 54

def update(value)
  raise Diplomat::IdParameterRequired unless value['ID']

  @raw = @conn.put do |req|
    url = ['/v1/acl/update']
    url += check_acl_token
    url += use_cas(@options)
    req.url concat_url url
    req.body = value.to_json
  end
  parse_body
end