Class: Jets::Resource::Replacer

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(replacements = {}) ⇒ Replacer

Returns a new instance of Replacer.



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

def initialize(replacements={})
  @replacements = replacements
end

Class Method Details

.principal_map(type) ⇒ Object

Examples:

"AWS::Events::Rule" => "events.amazonaws.com",
"AWS::Config::ConfigRule" => "config.amazonaws.com",
"AWS::ApiGateway::Method" => "apigateway.amazonaws.com"


60
61
62
63
64
# File 'lib/jets/resource/replacer.rb', line 60

def principal_map(type)
  service = type.split('::')[1].downcase
  service = special_principal_map(service)
  "#{service}.amazonaws.com"
end

.source_arn_map(type) ⇒ Object

From AWS docs: amzn.to/2N0QXQL source_arn is “not supported by all event sources”

When it is not available the resource definition should add it.



77
78
79
80
81
82
# File 'lib/jets/resource/replacer.rb', line 77

def source_arn_map(type)
  map = {
    "AWS::ApiGateway::Method" => "!Sub arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${RestApi}/*/*",
  }
  map[type]
end

.special_principal_map(service) ⇒ Object



66
67
68
69
70
71
# File 'lib/jets/resource/replacer.rb', line 66

def special_principal_map(service)
  # special map
  # s3_event actually uses sns topic events to trigger a Lambda function
  map = { "s3" => "sns" }
  map[service] || service
end

Instance Method Details

#replace_placeholders(attributes) ⇒ Object

Replace placeholder hash values with replacements. This does a deep replacement to the hash values. The replacement “key” is the string value within the value.

Example:

attributes = {whatever: "foo REPLACE_KEY bar" }
replace_placeholders(attributes, REPLACE_KEY: "blah:arn")
=> {whatever: "foo blah:arn bar" }

Also, we always replace the special namespace value in the hash values. Example:

attributes = {whatever: "{namespace}LambdaFunction" }
replace_placeholders(attributes, {})
=> {whatever: "foo PostsControllerIndexLambdaFunction bar" }


24
25
26
# File 'lib/jets/resource/replacer.rb', line 24

def replace_placeholders(attributes)
  update_values(attributes)
end

#replace_value(value) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/jets/resource/replacer.rb', line 42

def replace_value(value)
  # Dont perform replacement on Integers
  return value if value.is_a?(Integer)
  # return value unless value.is_a?(String) or value.is_a?(Symbol)

  value = value.to_s # normalize to String
  @replacements.each do |k,v|
    # IE: Replaces {namespace} => SecurityJobCheck
    value = value.gsub("{#{k}}", v)
  end
  value
end

#update_values(original) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/jets/resource/replacer.rb', line 28

def update_values(original)
  case original
  when Array
    original.map { |v| update_values(v) }
  when Hash
    initializer = original.map do |k, v|
      [k, update_values(v)]
    end
    Hash[initializer]
  else
    replace_value(original)
  end
end