Class: Cumuliform::DSL::Functions::IntrinsicFunctions

Inherits:
Object
  • Object
show all
Defined in:
lib/cumuliform/dsl/functions.rb

Overview

implements wrappers for the intrinsic functions Fn::*

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template) ⇒ IntrinsicFunctions

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of IntrinsicFunctions.



13
14
15
# File 'lib/cumuliform/dsl/functions.rb', line 13

def initialize(template)
  @template = template
end

Instance Attribute Details

#templateObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



10
11
12
# File 'lib/cumuliform/dsl/functions.rb', line 10

def template
  @template
end

Instance Method Details

#and(condition_1, ..., condition_n) ⇒ Hash

Wraps Fn::And

see docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-conditions.html#d0e86066

Behaves as a logical AND operator for CloudFormation conditions. Arguments should be other conditions or things that will evaluate to true or false.

Parameters:

  • condition_1 (Hash<boolean-returning ref, intrinsic function, or condition>)

    Condition / value to be ANDed

  • condition_n (Hash<boolean-returning ref, intrinsic function, or condition>)

    Condition / value to be ANDed (min 2, max 10 condition args)

Returns:

  • (Hash)

    the Fn::And object



173
174
175
176
177
178
# File 'lib/cumuliform/dsl/functions.rb', line 173

def and(*conditions)
  unless (2..10).cover?(conditions.length)
    raise ArgumentError, "You must specify AT LEAST 2 and AT MOST 10 conditions"
  end
  {"Fn::And" => conditions}
end

#base64(value) ⇒ Hash

Wraps Fn::Base64

see docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-base64.html

The argument should either be a string or an intrinsic function that evaluates to a string when CloudFormation executes the template

see docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-base64.html

Parameters:

  • value (String, Hash<string-returning instrinsic function>)

    The separator string to join the array elements with

Returns:

  • (Hash)

    the Fn::Base64 object



70
71
72
# File 'lib/cumuliform/dsl/functions.rb', line 70

def base64(value)
  {"Fn::Base64" => value}
end

#equals(value, other_value) ⇒ Hash

Wraps Fn::Equals

see docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-conditions.html#d0e86148

The arguments should be the literal values or refs you want to compare. Returns true or false when CloudFormation evaluates the template.

Parameters:

  • value (String, Hash<value-returning ref>)
  • other_value (String, Hash<value-returning ref>)

Returns:

  • (Hash)

    the Fn::Equals object



100
101
102
# File 'lib/cumuliform/dsl/functions.rb', line 100

def equals(value, other_value)
  {"Fn::Equals" => [value, other_value]}
end

#find_in_map(mapping_logical_id, level_1_key, level_2_key) ⇒ Hash

Parameters:

  • mapping_logical_id (String)

    The logical ID of the mapping we want to look up a value from

  • level_1_key (String)

    Key 1

  • level_2_key (String)

    Key 2

Returns:

  • (Hash)

    the Fn::FindInMap object



26
27
28
29
# File 'lib/cumuliform/dsl/functions.rb', line 26

def find_in_map(mapping_logical_id, level_1_key, level_2_key)
  template.verify_mapping_logical_id!(mapping_logical_id)
  {"Fn::FindInMap" => [mapping_logical_id, level_1_key, level_2_key]}
end

#get_att(resource_logical_id, attr_name) ⇒ Hash

Parameters:

  • resource_logical_id (String)

    The Logical ID of resource we want to get an attribute of

  • attr_name (String)

    The name of the attribute to get the value of

Returns:

  • (Hash)

    the Fn::GetAtt object



40
41
42
43
# File 'lib/cumuliform/dsl/functions.rb', line 40

def get_att(resource_logical_id, attr_name)
  template.verify_resource_logical_id!(resource_logical_id)
  {"Fn::GetAtt" => [resource_logical_id, attr_name]}
end

#get_azs(value = "") ⇒ Hash

Wraps Fn::GetAZs

CloudFormation evaluates this to an array of availability zone names.

see docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-getavailabilityzones.html

Parameters:

  • value (String, Hash<ref('AWS::Region')>) (defaults to: "")

    The AWS region to get the array of Availability Zones of. Empty string (the default) is equivalent to specifying ‘ref(’AWS::Region’)‘ which evaluates to the region the stack is being created in

Returns:

  • (Hash)

    the Fn::GetAZs object



85
86
87
# File 'lib/cumuliform/dsl/functions.rb', line 85

def get_azs(value = "")
  {"Fn::GetAZs" => value}
end

#if(condition, true_value, false_value) ⇒ Hash

Wraps Fn::If

see docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-conditions.html#d0e86223

CloudFormation evaluates the Condition referred to the logical ID in the condition arg and returns the true_value if true and false_value otherwise. condition cannot be an Fn::Ref, but you can use our xref() helper to ensure the logical ID is valid.

Parameters:

  • condition (String)

    the Logical ID of the Condition to be checked

  • true_value

    the value to be returned if condition evaluates true

  • false_value

    the value to be returned if condition evaluates false

Returns:

  • (Hash)

    the Fn::If object



121
122
123
# File 'lib/cumuliform/dsl/functions.rb', line 121

def if(condition, true_value, false_value)
  {"Fn::If" => [condition, true_value, false_value]}
end

#join(separator, args) ⇒ Hash

Parameters:

  • separator (String)

    The separator string to join the array elements with

  • args (Array<String>)

    The array of strings to join

Returns:

  • (Hash)

    the Fn::Join object

Raises:

  • (ArgumentError)


53
54
55
56
# File 'lib/cumuliform/dsl/functions.rb', line 53

def join(separator, args)
  raise ArgumentError, "Second argument must be an Array" unless args.is_a?(Array)
  {"Fn::Join" => [separator, args]}
end

#not(condition) ⇒ Hash

Wraps Fn::Not

see docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-conditions.html#d0e86402

Behaves as a logical NOT operator for CloudFormation conditions. The argument should be another condition or something that will evaluate to true or false

Parameters:

  • condition (Hash<boolean-returning ref, intrinsic function, or condition>)

    Condition / value to be NOTed

Returns:

  • (Hash)

    the Fn::Not object



215
216
217
# File 'lib/cumuliform/dsl/functions.rb', line 215

def not(condition)
  {"Fn::Not" => [condition]}
end

#or(condition_1, ..., condition_n) ⇒ Hash

Wraps Fn::Or

see docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-conditions.html#d0e86490

Behaves as a logical OR operator for CloudFormation conditions. Arguments should be other conditions or things that will evaluate to true or false.

Parameters:

  • condition_1 (Hash<boolean-returning ref, intrinsic function, or condition>)

    Condition / value to be ORed

  • condition_n (Hash<boolean-returning ref, intrinsic function, or condition>)

    Condition / value to be ORed (min 2, max 10 condition args)

Returns:

  • (Hash)

    the Fn::Or object



196
197
198
199
200
201
# File 'lib/cumuliform/dsl/functions.rb', line 196

def or(*conditions)
  unless (2..10).cover?(conditions.length)
    raise ArgumentError, "You must specify AT LEAST 2 and AT MOST 10 conditions"
  end
  {"Fn::Or" => conditions}
end

#select(index, array) ⇒ Hash

Wraps Fn::Select

see docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-select.html

CloudFormation evaluates the index (which can be an integer-as-a-string or a ref which evaluates to a number) and returns the corresponding item from the array (which can be an array literal, or the result of Fn::GetAZs, or one of Fn::GetAtt, Fn::If, and Ref (if they would return an Array).

Parameters:

  • index (Integer, Hash<value-returning ref>)

    The index to retrieve from array

  • array (Array, Hash<array-returning ref of intrinsic function>)

    The array to retrieve from

Returns:

  • (Hash)

    the Fn::Select object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/cumuliform/dsl/functions.rb', line 142

def select(index, array)
  ref_style_index = index.is_a?(Hash) && index.has_key?("Fn::Ref")
  positive_int_style_index = index.is_a?(Integer) && index >= 0
  unless ref_style_index || positive_int_style_index
    raise ArgumentError, "index must be a positive integer or Fn::Ref"
  end
  if positive_int_style_index
    if array.is_a?(Array) && index >= array.length
      raise IndexError, "index must be in the range 0 <= index < array.length"
    end
    index = index.to_s
  end
  {"Fn::Select" => [index, array]}
end