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

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

Overview

implements wrappers for the intrinsic functions Fn::*

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ConditionFunctions

#and, #equals, #if, #not, #or

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.



116
117
118
# File 'lib/cumuliform/dsl/functions.rb', line 116

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.



113
114
115
# File 'lib/cumuliform/dsl/functions.rb', line 113

def template
  @template
end

Instance Method Details

#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



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

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

#cidr(ip_block, count, cidr_bits) ⇒ Hash

Parameters:

  • ip_block (String)

    The user-specified CIDR address block to be split into smaller CIDR blocks. (e.g. “10.0.0.0/16”)

  • count (Integer)

    The number of CIDRs to generate. Valid range is between 1 and 256.

  • cidr_bits (Integer)

    The number of subnet bits for the CIDR. For example, specifying a value “8” for this parameter will create a CIDR with a mask of “/24”.

Returns:

  • (Hash)

    The Fn::Cidr object



237
238
239
# File 'lib/cumuliform/dsl/functions.rb', line 237

def cidr(ip_block, count, cidr_bits)
  {"Fn::Cidr" => [ip_block, count, cidr_bits]}
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



129
130
131
132
# File 'lib/cumuliform/dsl/functions.rb', line 129

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



143
144
145
146
# File 'lib/cumuliform/dsl/functions.rb', line 143

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



188
189
190
# File 'lib/cumuliform/dsl/functions.rb', line 188

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

#import_value(shared_value_to_import) ⇒ Hash

Parameters:

  • shared_value_to_import (String)

    The stack output value to import

Returns:

  • (Hash)

    The Fn::ImportValue object



249
250
251
# File 'lib/cumuliform/dsl/functions.rb', line 249

def import_value(shared_value_to_import)
  {"Fn::ImportValue" => shared_value_to_import}
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)


156
157
158
159
# File 'lib/cumuliform/dsl/functions.rb', line 156

def join(separator, args)
  raise ArgumentError, "Second argument must be an Array" unless args.is_a?(Array)
  {"Fn::Join" => [separator, args]}
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



209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/cumuliform/dsl/functions.rb', line 209

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

#split(delimiter, source_string) ⇒ Hash

Parameters:

  • delimiter (String)

    The delimiter to split the source_string on

  • source_string (String)

    The string to split

Returns:

  • (Hash)

    The Fn::Split object



261
262
263
# File 'lib/cumuliform/dsl/functions.rb', line 261

def split(delimiter, source_string)
  {"Fn::Split" => [delimiter, source_string]}
end

#sub(string, substitutions = nil) ⇒ Hash

Parameters:

  • string (String)

    The string to substitute values into

  • substitutions (Hash<String => String,Hash>) (defaults to: nil)

    Optional hash of variable names and the value to substitute them for. The value can also be somethings like an Fn:Ref invocation.

Returns:

  • (Hash)

    The Fn::Sub object



275
276
277
278
279
280
281
282
# File 'lib/cumuliform/dsl/functions.rb', line 275

def sub(string, substitutions = nil)
  if substitutions.nil?
    args = string
  else
    args = [string, substitutions]
  end
  {"Fn::Sub" => args}
end

#transform(macro_name, parameters = {}) ⇒ Hash

Parameters:

  • macro_name (String)

    The name of the macro to call

  • parameters (Hash) (defaults to: {})

    The hash of parameter names/values

Returns:

  • (Hash)

    The Fn::Transform object



292
293
294
295
296
297
298
299
# File 'lib/cumuliform/dsl/functions.rb', line 292

def transform(macro_name, parameters = {})
  {
    "Fn::Transform" => {
      "Name" => macro_name,
      "Parameters" => parameters
    }
  }
end