Class: Diplomat::Policy

Inherits:
RestClient show all
Defined in:
lib/diplomat/policy.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/policy.rb', line 7

def acl
  @acl
end

#idObject (readonly)

Returns the value of attribute id.



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

def id
  @id
end

#typeObject (readonly)

Returns the value of attribute type.



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

def type
  @type
end

Instance Method Details

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

Create a new ACL policy

Parameters:

  • value (Hash)

    ACL policy definition, Name field is mandatory

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

    options parameter hash

Returns:

  • (Hash)

    new ACL policy

Raises:



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/diplomat/policy.rb', line 84

def create(value, options = {})
  blacklist = ['ID', 'iD', 'Id', :ID, :iD, :Id] & value.keys
  raise Diplomat::PolicyMalformed, 'ID should not be specified' unless blacklist.empty?

  id = value[:Name] || value['Name']
  raise Diplomat::NameParameterRequired if id.nil?

  custom_params = use_cas(@options)
  @raw = send_put_request(@conn, ['/v1/acl/policy'], options, value, custom_params)

  # rubocop:disable Style/GuardClause
  if @raw.status == 200
    return parse_body
  elsif @raw.status == 500 && @raw.body.chomp.include?('already exists')
    raise Diplomat::PolicyAlreadyExists, @raw.body
  else
    raise Diplomat::UnknownStatus, "status #{@raw.status}: #{@raw.body}"
  end
end

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

Delete an ACL policy by its UUID

Parameters:

  • id (String)

    UUID of the ACL policy to delete

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

    options parameter hash

Returns:

  • (Bool)


109
110
111
112
# File 'lib/diplomat/policy.rb', line 109

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

#list(options = {}) ⇒ List

List all the ACL policies

Parameters:

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

    options parameter hash

Returns:

  • (List)

    list of [Hash] of ACL policies

Raises:



51
52
53
54
55
56
# File 'lib/diplomat/policy.rb', line 51

def list(options = {})
  @raw = send_get_request(@conn_no_err, ['/v1/acl/policies'], options)
  raise Diplomat::AclNotFound if @raw.status == 403

  parse_body
end

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

Read ACL policy with the given UUID rubocop:disable Metrics/PerceivedComplexity

Parameters:

  • id (String)

    UUID of the ACL policy to read

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

    options parameter hash

Returns:

  • (Hash)

    existing ACL policy



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
40
41
42
43
44
45
# File 'lib/diplomat/policy.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/policy/#{id}"], options, custom_params)

  if @raw.status == 200 && @raw.body.chomp != 'null'
    case found
    when :reject
      raise Diplomat::PolicyNotFound, id
    when :return
      return parse_body
    end
  elsif @raw.status == 404
    case not_found
    when :reject
      raise Diplomat::PolicyNotFound, id
    when :return
      return nil
    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

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

Update an existing ACL policy

Parameters:

  • value (Hash)

    ACL policy definition, ID and Name fields are mandatory

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

    options parameter hash

Returns:

  • (Hash)

    result ACL policy

Raises:



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

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

  policy_name = value[:Name] || value['Name']
  raise Diplomat::NameParameterRequired if policy_name.nil?

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