Module: CfnDsl::Functions

Included in:
JSONable, JSONable
Defined in:
lib/cfndsl/jsonable.rb

Overview

These functions are available anywhere inside a block for a JSONable object.

Instance Method Summary collapse

Instance Method Details

#FnAnd(array) ⇒ Object

Equivalent to the CloudFormation template built in function Fn::And



39
40
41
42
43
44
# File 'lib/cfndsl/jsonable.rb', line 39

def FnAnd(array)
  if !array || array.count < 2 || array.count > 10
    raise 'The array passed to Fn::And must have at least 2 elements and no more than 10'
  end
  Fn.new('And', array)
end

#FnBase64(value) ⇒ Object

Equivalent to the CloudFormation template built in function Fn::Base64



14
15
16
# File 'lib/cfndsl/jsonable.rb', line 14

def FnBase64(value)
  Fn.new('Base64', value)
end

#FnEquals(value1, value2) ⇒ Object

Equivalent to the Cloudformation template built in function Fn::Equals



47
48
49
# File 'lib/cfndsl/jsonable.rb', line 47

def FnEquals(value1, value2)
  Fn.new('Equals', [value1, value2])
end

#FnFindInMap(map, key, value) ⇒ Object

Equivalent to the CloudFormation template built in function Fn::FindInMap



19
20
21
# File 'lib/cfndsl/jsonable.rb', line 19

def FnFindInMap(map, key, value)
  Fn.new('FindInMap', [map, key, value])
end

#FnFormat(string, *arguments) ⇒ Object

Usage

FnFormat('This is a %0. It is 100%% %1', 'test', 'effective')

or

FnFormat('This is a %{test}. It is 100%% %{effective}',
          :test => 'test",
          :effective => 'effective')

These will each generate a call to Fn::Join that when evaluated will produce the string “This is a test. It is 100% effective.”

Think of this as %0, %1, etc in the format string being replaced by the corresponding arguments given after the format string. ‘%%’ is replaced by the ‘%’ character.

The actual Fn::Join call corresponding to the above FnFormat call would be [“”,[“This is a ”,“test”,“. It is 100”,“%”,“ ”,“effective”]]

If no arguments are given, or if a hash is given and the format variable name does not exist in the hash, it is used as a Ref to an existing resource or parameter.

TODO Can we simplyfy this somehow? rubocop:disable Metrics/AbcSize, Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/cfndsl/jsonable.rb', line 98

def FnFormat(string, *arguments)
  array = []

  if arguments.empty? || (arguments.length == 1 && arguments[0].instance_of?(Hash))
    hash = arguments[0] || {}
    string.scan(/(.*?)(?:%(%|\{([\w:]+)\})|\z)/m) do |x, y, z|
      array.push x if x && !x.empty?

      next unless y

      array.push(y == '%' ? '%' : (hash[z] || hash[z.to_sym] || Ref(z)))
    end
  else
    string.scan(/(.*?)(?:%(%|\d+)|\z)/m) do |x, y|
      array.push x if x && !x.empty?

      next unless y

      array.push(y == '%' ? '%' : arguments[y.to_i])
    end
  end
  Fn.new('Join', ['', array])
end

#FnGetAtt(logical_resource, attribute) ⇒ Object

Equivalent to the CloudFormation template built in function Fn::GetAtt



24
25
26
# File 'lib/cfndsl/jsonable.rb', line 24

def FnGetAtt(logical_resource, attribute)
  Fn.new('GetAtt', [logical_resource, attribute])
end

#FnGetAZs(region) ⇒ Object

Equivalent to the CloudFormation template built in function Fn::GetAZs



29
30
31
# File 'lib/cfndsl/jsonable.rb', line 29

def FnGetAZs(region)
  Fn.new('GetAZs', region)
end

#FnIf(condition_name, true_value, false_value) ⇒ Object

Equivalent to the Cloudformation template built in function Fn::If



52
53
54
# File 'lib/cfndsl/jsonable.rb', line 52

def FnIf(condition_name, true_value, false_value)
  Fn.new('If', [condition_name, true_value, false_value])
end

#FnJoin(string, array) ⇒ Object

Equivalent to the CloudFormation template built in function Fn::Join



34
35
36
# File 'lib/cfndsl/jsonable.rb', line 34

def FnJoin(string, array)
  Fn.new('Join', [string, array])
end

#FnNot(value) ⇒ Object

Equivalent to the Cloudformation template built in function Fn::Not



57
58
59
# File 'lib/cfndsl/jsonable.rb', line 57

def FnNot(value)
  Fn.new('Not', value)
end

#FnOr(array) ⇒ Object

Equivalent to the CloudFormation template built in function Fn::Or



62
63
64
65
66
67
# File 'lib/cfndsl/jsonable.rb', line 62

def FnOr(array)
  if !array || array.count < 2 || array.count > 10
    raise 'The array passed to Fn::Or must have at least 2 elements and no more than 10'
  end
  Fn.new('Or', array)
end

#FnSelect(index, array) ⇒ Object

Equivalent to the CloudFormation template built in function Fn::Select



70
71
72
# File 'lib/cfndsl/jsonable.rb', line 70

def FnSelect(index, array)
  Fn.new('Select', [index, array])
end

#Ref(value) ⇒ Object

Equivalent to the CloudFormation template built in function Ref



9
10
11
# File 'lib/cfndsl/jsonable.rb', line 9

def Ref(value)
  RefDefinition.new(value)
end