Module: CfnDsl

Defined in:
lib/cfndsl/names.rb,
lib/cfndsl/rules.rb,
lib/cfndsl/types.rb,
lib/cfndsl/runner.rb,
lib/cfndsl/globals.rb,
lib/cfndsl/outputs.rb,
lib/cfndsl/plurals.rb,
lib/cfndsl/version.rb,
lib/cfndsl/jsonable.rb,
lib/cfndsl/mappings.rb,
lib/cfndsl/aws/types.rb,
lib/cfndsl/rake_task.rb,
lib/cfndsl/resources.rb,
lib/cfndsl/conditions.rb,
lib/cfndsl/parameters.rb,
lib/cfndsl/properties.rb,
lib/cfndsl/specification.rb,
lib/cfndsl/update_policy.rb,
lib/cfndsl/cloudformation.rb,
lib/cfndsl/creation_policy.rb,
lib/cfndsl/external_parameters.rb,
lib/cfndsl/orchestration_template.rb,
lib/cfndsl/json_serialisable_object.rb,
lib/cfndsl/aws/cloud_formation_template.rb

Overview

CfnDsl

Defined Under Namespace

Modules: AWS, CloudFormation, Functions, JSONSerialisableObject, Plurals, Types Classes: CloudFormationTemplate, ConditionDefinition, CreationPolicyDefinition, Error, ExternalParameters, Fn, JSONable, MappingDefinition, OrchestrationTemplate, OutputDefinition, ParameterDefinition, PropertyDefinition, RakeTask, RefDefinition, ResourceDefinition, RuleDefinition, Runner, Specification, UpdatePolicyDefinition

Constant Summary collapse

LOCAL_SPEC_FILE =
File.expand_path('aws/resource_specification.json', __dir__)
REGION_SPEC_URLS_FILE =
File.expand_path('aws/region_spec_urls.json', __dir__)
SPEC_PATH_SUFFIX =
'%<version>s/gzip/CloudFormationResourceSpecification.json'
DEFAULT_SPEC_REGION =
'us-east-1'
VERSION =
'1.9.5'

Class Method Summary collapse

Class Method Details

.additional_specs(*specs) ⇒ Object



86
87
88
89
# File 'lib/cfndsl/globals.rb', line 86

def additional_specs(*specs)
  @additional_specs ||= Dir[File.expand_path('aws/patches/*.spec.json', __dir__)]
  @additional_specs.concat(specs.flatten)
end

.disable_deep_mergeObject



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

def disable_deep_merge
  @disable_deep_merge = true
end

.disable_deep_merge?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/cfndsl/globals.rb', line 28

def disable_deep_merge?
  @disable_deep_merge
end

.eval_file_with_extras(filename, extras = [], logstream = nil) ⇒ Object

This function handles the eval of the template file and returns the results. It does this with a ruby "eval", but it builds up a customized binding environment before it calls eval. The environment can be customized by passing a list of customizations in the extras parameter.

These customizations are expressed as an array of pairs of (type,filename). They are evaluated in the order they appear in the extras array. The types are as follows

:yaml - the second element is treated as a file name, which is loaded as a yaml file. The yaml file should contain a top level dictionary. Each of the keys of the top level dictionary is used as a local variable in the evalation context.

:json - the second element is treated as a file name, which is loaded as a json file. The yaml file should contain a top level dictionary. Each of the keys of the top level dictionary is used as a local variable in the evalation context.

:raw - the second element is treated as a ruby statement and is evaluated in the binding context, similar to the contents of a ruby file.

Note that the order is important, as later extra sections can overwrite or even undo things that were done by earlier sections.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/cfndsl/cloudformation.rb', line 35

def self.eval_file_with_extras(filename, extras = [], logstream = nil)
  b = binding
  params = CfnDsl::ExternalParameters.refresh!
  extras.each do |type, file|
    case type
    when :yaml, :json
      klass_name = type.to_s.upcase
      logstream.puts("Loading #{klass_name} file #{file}") if logstream
      params.load_file file
    when :raw
      file_parts = file.split('=')
      case file_parts[1].downcase
      when 'true'
        params.set_param(file_parts[0], true)
      when 'false'
        params.set_param(file_parts[0], false)
      else
        params.set_param(*file.split('='))
      end
    end
  end

  logstream.puts("Loading template file #{filename}") if logstream
  b.eval(File.read(filename), filename)
end

.method_names(name) ⇒ Object

iterates through the the valid case-insensitive names for "name"



9
10
11
12
13
# File 'lib/cfndsl/names.rb', line 9

def method_names(name)
  name_str = name.to_s.dup
  names = [name_str, name_str.gsub(/^\w/, &:swapcase)]
  block_given? ? names.each { |n| yield n.to_sym } : names
end

.region_spec_urlsObject



20
21
22
# File 'lib/cfndsl/globals.rb', line 20

def region_spec_urls
  @region_spec_urls ||= JSON.parse(File.read(REGION_SPEC_URLS_FILE)).tap { |h| h.delete('_source') }.freeze
end

.reserved_itemsObject



97
98
99
# File 'lib/cfndsl/globals.rb', line 97

def reserved_items
  %w[Resource Rule Parameter Output].freeze
end

.spec_url_for_region(region, version:) ⇒ String

Build the full specification URL for a given region and version

Parameters:

  • region (String)

    AWS region code

  • version (String)

    specification version or 'latest'

Returns:

  • (String)

    full URL to the CloudFormation resource specification



58
59
60
61
62
63
# File 'lib/cfndsl/globals.rb', line 58

def spec_url_for_region(region, version:)
  base_url = region_spec_urls.fetch(region) do
    raise Error, "Unsupported region '#{region}'. Use CfnDsl.supported_spec_regions for a list of supported regions."
  end
  "#{base_url}/#{format(SPEC_PATH_SUFFIX, version: version)}"
end

.specification_fileString .specification_file(file) ⇒ String

Deprecated.

Use specification_file= to override the specification file

Returns the specification file name.

Returns:

  • (String)

    the specification file name



42
43
44
45
46
47
# File 'lib/cfndsl/globals.rb', line 42

def specification_file(file = nil)
  self.specification_file = file if file
  @specification_file ||= user_specification_file
  @specification_file = LOCAL_SPEC_FILE unless File.exist?(@specification_file)
  @specification_file
end

.specification_file=(file) ⇒ Object

Raises:



32
33
34
35
36
# File 'lib/cfndsl/globals.rb', line 32

def specification_file=(file)
  raise Error, "Specification #{file} does not exist" unless File.exist?(file)

  @specification_file = file
end

.specification_patches(*patches) ⇒ Object



91
92
93
94
95
# File 'lib/cfndsl/globals.rb', line 91

def specification_patches(*patches)
  # TODO: This is not capturing all the files in patches dir!
  @patches ||= Dir[File.expand_path('aws/patches/*patch.json', __dir__)]
  @patches.concat(patches.flatten)
end

.supported_spec_regionsArray<String>

List all supported regions for spec downloads

Returns:

  • (Array<String>)

    sorted list of supported region codes



68
69
70
# File 'lib/cfndsl/globals.rb', line 68

def supported_spec_regions
  region_spec_urls.keys.sort
end

.update_specification_file(file: user_specification_file, version: nil, region: nil) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/cfndsl/globals.rb', line 72

def update_specification_file(file: user_specification_file, version: nil, region: nil)
  require 'open-uri'
  version ||= 'latest'
  region ||= DEFAULT_SPEC_REGION
  FileUtils.mkdir_p File.dirname(file)
  url = spec_url_for_region(region, version: version)
  content = URI.parse(url).open.read
  version = JSON.parse(content)['ResourceSpecificationVersion'] if version == 'latest'
  File.open(file, 'w') { |f| f.puts content }
  { file: file, version: version, url: url, region: region }
rescue StandardError
  raise "Failed updating specification file #{file} from #{url}"
end

.user_specification_fileObject



49
50
51
# File 'lib/cfndsl/globals.rb', line 49

def user_specification_file
  File.join(ENV['HOME'], '.cfndsl/resource_specification.json')
end