Class: CfnModel

Inherits:
Object
  • Object
show all
Defined in:
lib/cfn-model/model/cfn_model.rb,
lib/cfn-model/transforms/serverless.rb,
lib/cfn-model/parser/transform_registry.rb,
lib/cfn-model/parser/transform_registry.rb

Defined Under Namespace

Classes: TransformRegistry, Transforms

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCfnModel

Returns a new instance of CfnModel.



14
15
16
17
18
19
20
21
# File 'lib/cfn-model/model/cfn_model.rb', line 14

def initialize
  @parameters = {}
  @resources = {}
  @conditions = {}
  @globals = {}
  @raw_model = nil
  @line_numbers = {}
end

Instance Attribute Details

#conditionsObject (readonly)

Returns the value of attribute conditions.



6
7
8
# File 'lib/cfn-model/model/cfn_model.rb', line 6

def conditions
  @conditions
end

#globalsObject (readonly)

Returns the value of attribute globals.



6
7
8
# File 'lib/cfn-model/model/cfn_model.rb', line 6

def globals
  @globals
end

#line_numbersObject (readonly)

Returns the value of attribute line_numbers.



6
7
8
# File 'lib/cfn-model/model/cfn_model.rb', line 6

def line_numbers
  @line_numbers
end

#parametersObject (readonly)

Returns the value of attribute parameters.



6
7
8
# File 'lib/cfn-model/model/cfn_model.rb', line 6

def parameters
  @parameters
end

#raw_modelObject

if you really want it, here it is - the raw Hash from YAML.load. you’ll have to mess with structural nits of CloudFormation and deal with variations between yaml/json refs and all that



12
13
14
# File 'lib/cfn-model/model/cfn_model.rb', line 12

def raw_model
  @raw_model
end

#resourcesObject (readonly)

Returns the value of attribute resources.



6
7
8
# File 'lib/cfn-model/model/cfn_model.rb', line 6

def resources
  @resources
end

Instance Method Details

#copyObject

A new instance of CfnModel with a copy of the raw_model and and resources. The resource objects themselves aren’t cloned but the Hash is a clone



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/cfn-model/model/cfn_model.rb', line 27

def copy
  new_cfn_model = CfnModel.new
  @conditions.each do |k,v|
    new_cfn_model.conditions[k] = v
  end
  @globals.each do |k,v|
    new_cfn_model.globals[k] = v
  end
  @parameters.each do |k,v|
    new_cfn_model.parameters[k] = v
  end
  @resources.each do |k, v|
    new_cfn_model.resources[k] = v
  end
  new_cfn_model.raw_model = @raw_model.dup unless @raw_model.nil?
  new_cfn_model
end

#find_security_group_by_group_id(security_group_reference) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/cfn-model/model/cfn_model.rb', line 88

def find_security_group_by_group_id(security_group_reference)
  security_group_id = References.resolve_security_group_id(security_group_reference)
  if security_group_id.nil?
    # leave it alone since external ref or something we don't grok
    security_group_reference
  else
    matched_security_group = security_groups.find do |security_group|
      security_group.logical_resource_id == security_group_id
    end
    if matched_security_group.nil?
      # leave it alone since external ref or something we don't grok
      security_group_reference
    else
      matched_security_group
    end
  end
end

#iam_usersObject



49
50
51
# File 'lib/cfn-model/model/cfn_model.rb', line 49

def iam_users
  resources_by_type 'AWS::IAM::User'
end

#resource_by_id(resource_id) ⇒ Object



67
68
69
# File 'lib/cfn-model/model/cfn_model.rb', line 67

def resource_by_id(resource_id)
  @resources.values.find { |resource| resource.logical_resource_id == resource_id }
end

#resource_by_ref(reference, attr = nil) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/cfn-model/model/cfn_model.rb', line 75

def resource_by_ref(reference, attr = nil)
  # If reference is a String, look for a matching object as is (best effort)
  # Although, the caller could just use resource_by_id on this value, since it
  # would be the logical_resource_id.
  logical_resource_id = reference if reference.is_a? String

  # Otherwise, obtain logical_resource_id from References class
  logical_resource_id ||= References.resolve_resource_id reference, attr

  # Search resources for a matching ID
  resource_by_id logical_resource_id
end

#resources_by_type(resource_type) ⇒ Object



71
72
73
# File 'lib/cfn-model/model/cfn_model.rb', line 71

def resources_by_type(resource_type)
  @resources.values.select { |resource| resource.resource_type == resource_type }
end

#security_groupsObject



45
46
47
# File 'lib/cfn-model/model/cfn_model.rb', line 45

def security_groups
  resources_by_type 'AWS::EC2::SecurityGroup'
end

#standalone_egressObject



60
61
62
63
64
65
# File 'lib/cfn-model/model/cfn_model.rb', line 60

def standalone_egress
  security_group_egresses = resources_by_type 'AWS::EC2::SecurityGroupEgress'
  security_group_egresses.select do |security_group_egress|
    References.is_security_group_id_external(security_group_egress.groupId)
  end
end

#standalone_ingressObject



53
54
55
56
57
58
# File 'lib/cfn-model/model/cfn_model.rb', line 53

def standalone_ingress
  security_group_ingresses = resources_by_type 'AWS::EC2::SecurityGroupIngress'
  security_group_ingresses.select do |security_group_ingress|
    References.is_security_group_id_external(security_group_ingress.groupId)
  end
end

#to_sObject



106
107
108
# File 'lib/cfn-model/model/cfn_model.rb', line 106

def to_s
  @resources.to_s
end