Class: Jets::Resource::Iam::PolicyDocument

Inherits:
Object
  • Object
show all
Extended by:
Memoist
Defined in:
lib/jets/resource/iam/policy_document.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*definitions) ⇒ PolicyDocument

Returns a new instance of PolicyDocument.



6
7
8
9
10
11
12
13
# File 'lib/jets/resource/iam/policy_document.rb', line 6

def initialize(*definitions)
  @definitions = definitions.flatten
  # empty starting policy that will be altered
  @policy = {
    version: "2012-10-17",
    statement: []
  }
end

Instance Attribute Details

#definitionsObject (readonly)

Returns the value of attribute definitions.



5
6
7
# File 'lib/jets/resource/iam/policy_document.rb', line 5

def definitions
  @definitions
end

Instance Method Details

#policy_documentObject



15
16
17
18
# File 'lib/jets/resource/iam/policy_document.rb', line 15

def policy_document
  definitions.map { |definition| standardize(definition) }
  Jets::Camelizer.transform(@policy)
end

#standardize(definition) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/jets/resource/iam/policy_document.rb', line 21

def standardize(definition)
  case definition
  when String
    # Expands simple string from: logs => logs:*
    definition = "#{definition}:*" unless definition.include?(':')
    @policy[:statement] << {
      action: [definition],
      effect: "Allow",
      resource: "*",
    }
  when Hash
    definition = definition.stringify_keys
    if definition.key?("Version") # special case where we replace the policy entirely
      @policy = definition
    else
      @policy[:statement] << definition
    end
  end
end