Class: Jets::Resource::Iam::ClassRole

Inherits:
Base
  • Object
show all
Includes:
BaseRoleDefinition
Defined in:
lib/jets/resource/iam/class_role.rb

Instance Attribute Summary

Attributes included from BaseRoleDefinition

#managed_policy_definitions, #policy_definitions

Instance Method Summary collapse

Methods included from BaseRoleDefinition

#definition

Methods inherited from Base

#resource

Constructor Details

#initialize(app_class) ⇒ ClassRole

Returns a new instance of ClassRole.



5
6
7
8
9
# File 'lib/jets/resource/iam/class_role.rb', line 5

def initialize(app_class)
  @app_class = app_class.to_s # IE: PostsController, HardJob, Hello, HelloFunction
  @policy_definitions = lookup_iam_policies
  @managed_policy_definitions = lookup_managed_iam_policies
end

Instance Method Details

#all_classesObject

Class heirachry in top to down order



63
64
65
66
67
68
69
70
71
# File 'lib/jets/resource/iam/class_role.rb', line 63

def all_classes
  klass = @app_class.constantize
  all_classes = []
  while klass != Object
    all_classes << klass
    klass = klass.superclass
  end
  all_classes.reverse
end

#application_roleObject



43
44
45
# File 'lib/jets/resource/iam/class_role.rb', line 43

def application_role
  Jets::Resource::Iam::ApplicationRole.new
end

#inherit?Boolean

There are 2 types of inheritance: from superclasses and from higher precedence policies. This one manages the inheritance for higher precedence policies.

Returns:

  • (Boolean)


39
40
41
# File 'lib/jets/resource/iam/class_role.rb', line 39

def inherit?
  !@policy_definitions.empty? || !@managed_policy_definitions.empty?
end

#lookup_iam_policiesObject

Accounts for inherited class_iam_policies from superclasses



56
57
58
59
60
# File 'lib/jets/resource/iam/class_role.rb', line 56

def lookup_iam_policies
  all_classes.map do |k|
    k.class_iam_policy # class_iam_policy contains definitions
  end.uniq
end

#lookup_managed_iam_policiesObject

Accounts for inherited class_managed_iam_policy from superclasses



49
50
51
52
53
# File 'lib/jets/resource/iam/class_role.rb', line 49

def lookup_managed_iam_policies
  all_classes.map do |k|
    k.class_managed_iam_policy # class_managed_iam_policy contains definitions
  end.uniq
end

#managed_policy_arnsObject



32
33
34
35
# File 'lib/jets/resource/iam/class_role.rb', line 32

def managed_policy_arns
  @managed_policy_definitions += application_role.managed_policy_definitions if inherit?
  super
end

#policy_documentObject



26
27
28
29
30
# File 'lib/jets/resource/iam/class_role.rb', line 26

def policy_document
  # Handles precedence inheritance from the ApplicationRole to the ClassRole
  @policy_definitions += application_role.policy_definitions if inherit?
  super
end

#replacementsObject



20
21
22
23
24
# File 'lib/jets/resource/iam/class_role.rb', line 20

def replacements
  {
    namespace: @app_class.gsub('::','').camelize, # camelized because can be used as value
  }
end

#role_logical_idObject



11
12
13
# File 'lib/jets/resource/iam/class_role.rb', line 11

def role_logical_id
  "{namespace}_iam_role".underscore
end

#role_nameObject



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

def role_name
  class_namespace = replacements[:namespace].underscore.dasherize
  "#{Jets.config.project_namespace}-#{class_namespace}-role" # camelized because used as template value
end