class Jets::Resource::Replacer

def initialize(replacements={})

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

def principal_map(type)

"AWS::ApiGateway::Method" => "apigateway.amazonaws.com"
"AWS::Config::ConfigRule" => "config.amazonaws.com",
"AWS::Events::Rule" => "events.amazonaws.com",
Examples:
def principal_map(type)
  service = type.split('::')[1].downcase
  service = special_principal_map(service)
  "#{service}.amazonaws.com"
end

def replace_placeholders(attributes)


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

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

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

Example:

to the hash values. The replacement "key" is the string value within the value.
Replace placeholder hash values with replacements. This does a deep replacement
def replace_placeholders(attributes)
  update_values(attributes)
end

def replace_value(value)

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

def source_arn_map(type)

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

source_arn is "not supported by all event sources"
From AWS docs: https://amzn.to/2N0QXQL
def source_arn_map(type)
  map = {
    "AWS::ApiGateway::Method" => "!Sub arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${RestApi}/*/*",
  }
  map[type]
end

def special_principal_map(service)

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

def update_values(original)

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