Module: Jets::Cfn::Builders::Interface
- Extended by:
- Memoist
- Included in:
- ApiDeploymentBuilder, ApiGatewayBuilder, BaseChildBuilder, ParentBuilder
- Defined in:
- lib/jets/cfn/builders/interface.rb
Instance Method Summary collapse
- #add_output(name, options = {}) ⇒ Object
- #add_outputs(attributes) ⇒ Object
- #add_parameter(name, options = {}) ⇒ Object
- #add_parameters(attributes) ⇒ Object
- #add_resource(resource) ⇒ Object
- #add_resources ⇒ Object
-
#add_template_resource(logical_id, type, options) ⇒ Object
The add_resource method can take an options Hash with both with either top level attributes or properties.
- #build ⇒ Object
-
#post_process_template(text) ⇒ Object
post process the text so that “!Ref IamRole” => !Ref IamRole We strip the surrounding quotes.
- #template ⇒ Object
- #text ⇒ Object
- #write ⇒ Object
Instance Method Details
#add_output(name, options = {}) ⇒ Object
71 72 73 74 |
# File 'lib/jets/cfn/builders/interface.rb', line 71 def add_output(name, ={}) @template[:Outputs] ||= {} @template[:Outputs][name.camelize] = end |
#add_outputs(attributes) ⇒ Object
65 66 67 68 69 |
# File 'lib/jets/cfn/builders/interface.rb', line 65 def add_outputs(attributes) attributes.each do |name,value| add_output(name.to_s.camelize, Value: value) end end |
#add_parameter(name, options = {}) ⇒ Object
58 59 60 61 62 63 |
# File 'lib/jets/cfn/builders/interface.rb', line 58 def add_parameter(name, ={}) defaults = { Type: "String" } = defaults.merge() @template[:Parameters] ||= {} @template[:Parameters][name.camelize] = end |
#add_parameters(attributes) ⇒ Object
52 53 54 55 56 |
# File 'lib/jets/cfn/builders/interface.rb', line 52 def add_parameters(attributes) attributes.each do |name,value| add_parameter(name.to_s.camelize, Description: value) end end |
#add_resource(resource) ⇒ Object
86 87 88 |
# File 'lib/jets/cfn/builders/interface.rb', line 86 def add_resource(resource) add_template_resource(resource.logical_id, resource.type, resource.attributes) end |
#add_resources ⇒ Object
76 77 78 79 80 81 82 83 84 |
# File 'lib/jets/cfn/builders/interface.rb', line 76 def add_resources @app_klass.tasks.each do |task| task.resources.each do |definition| resource = Jets::Resource.new(definition, task.replacements) add_resource(resource) add_resource(resource.) end end end |
#add_template_resource(logical_id, type, options) ⇒ Object
The add_resource method can take an options Hash with both with either top level attributes or properties.
Example:
Top level options:
add_template_resource("MyId", "AWS::ApiGateway::RestApi",
type: "AWS::ApiGateway::RestApi",
properties: {
name: "my-api"
},
depends_on: ["AnotherResource"]
)
Simple options with properties only:
add_template_resource("MyId", "AWS::CloudFormationStack",
template_url: "template_url",
parameters: {},
)
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/jets/cfn/builders/interface.rb', line 112 def add_template_resource(logical_id, type, ) = Jets::Camelizer.transform() attributes = if .include?('Type') base = { 'Type' => type } base.merge() # options are top-level attributes else { 'Type' => type, 'Properties' => # options are properties } end @template['Resources'][logical_id] = attributes end |
#build ⇒ Object
9 10 11 12 13 14 15 16 |
# File 'lib/jets/cfn/builders/interface.rb', line 9 def build # Do not bother building # or writing the template unless there are functions defined return if @app_klass && !@app_klass.build? compose # must be implemented by subclass write end |
#post_process_template(text) ⇒ Object
post process the text so that “!Ref IamRole” => !Ref IamRole We strip the surrounding quotes
37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/jets/cfn/builders/interface.rb', line 37 def post_process_template(text) results = text.split("\n").map do |line| if line.include?(': "!') # IE: IamRole: "!Ref IamRole", # IamRole: "!Ref IamRole" => IamRole: !Ref IamRole line.sub(/: "(.*)"/, ': \1') elsif line.include?('- "!') # IE: - "!GetAtt Foo.Arn" # IamRole: - "!GetAtt Foo.Arn" => - !GetAtt Foo.Arn line.sub(/- "(.*)"/, '- \1') else line end end results.join("\n") + "\n" end |
#template ⇒ Object
23 24 25 26 27 |
# File 'lib/jets/cfn/builders/interface.rb', line 23 def template # need the to_hash or the YAML dump has # !ruby/hash:ActiveSupport::HashWithIndifferentAccess @template.to_hash end |
#text ⇒ Object
29 30 31 32 |
# File 'lib/jets/cfn/builders/interface.rb', line 29 def text text = YAML.dump(template) post_process_template(text) end |
#write ⇒ Object
18 19 20 21 |
# File 'lib/jets/cfn/builders/interface.rb', line 18 def write FileUtils.mkdir_p(File.dirname(template_path)) IO.write(template_path, text) end |