Class: Momo::Resource

Inherits:
MomoScope show all
Includes:
MomoCondition
Defined in:
lib/momo/resource.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from MomoCondition

#and, #equals, #or

Methods inherited from MomoScope

#call, #lookup, #ref

Constructor Details

#initialize(type, name, stack) ⇒ Resource

Returns a new instance of Resource.



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/momo/resource.rb', line 9

def initialize(type, name, stack)
	@type = type
	@name = name
	@metadata = nil
	@props = {}
	@condition = nil
	@dependencies = []
	@complete = false
	@deletion_policy = nil
	@stack = stack
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/momo/resource.rb', line 21

def method_missing(name, *args, &block)

	if !@complete
		if /^[[:upper:]]/.match(name) == nil
			raise "Invalid property name: #{name}"
		end
		@props[name] = Momo.resolve(args[0], resource: name, stack: @stack, &block)

	else
		if /^[[:upper:]]/.match(name) == nil
			raise "Invalid attribute name: #{name}"
		end
		# For get attributes when the resource is completed
		# TODO: should instead instance eval with a different class
		MemberReference.new @name, name, @stack
	end
end

Instance Attribute Details

#conditionObject

Returns the value of attribute condition.



7
8
9
# File 'lib/momo/resource.rb', line 7

def condition
  @condition
end

#deletion_policyObject

Returns the value of attribute deletion_policy.



7
8
9
# File 'lib/momo/resource.rb', line 7

def deletion_policy
  @deletion_policy
end

#dependenciesObject

Returns the value of attribute dependencies.



7
8
9
# File 'lib/momo/resource.rb', line 7

def dependencies
  @dependencies
end

#metadataObject

Returns the value of attribute metadata.



7
8
9
# File 'lib/momo/resource.rb', line 7

def 
  @metadata
end

#nameObject

Returns the value of attribute name.



7
8
9
# File 'lib/momo/resource.rb', line 7

def name
  @name
end

#propsObject

Returns the value of attribute props.



7
8
9
# File 'lib/momo/resource.rb', line 7

def props
  @props
end

#typeObject

Returns the value of attribute type.



7
8
9
# File 'lib/momo/resource.rb', line 7

def type
  @type
end

Instance Method Details

#add_thing(group, name, thing) ⇒ Object



81
82
83
84
85
86
87
88
89
# File 'lib/momo/resource.rb', line 81

def add_thing(group, name, thing)
	init_group(group)
	
	if !@metadata["AWS::CloudFormation::Init"]["config"][group][name]
		@metadata["AWS::CloudFormation::Init"]["config"][group][name] = []
	end

	@metadata["AWS::CloudFormation::Init"]["config"][group][name] << thing
end

#complete!Object



43
44
45
# File 'lib/momo/resource.rb', line 43

def complete!
	@complete = true
end

#depends_on(resource) ⇒ Object



159
160
161
162
163
164
165
166
167
# File 'lib/momo/resource.rb', line 159

def depends_on(resource)
	if resource.is_a? String
		@dependencies << args[0]
	elsif resource.is_a? Resource
		@dependencies << resource.name
	else
		raise "Invalid argument to depends_on: #{resource[0]}"
	end
end

#file(name, options = {}) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/momo/resource.rb', line 120

def file(name, options={})
	info = {
		"content" => "",
		"group" => "root",
		"owner" => "root",
		"mode" => "000600",
		"context" => {},
	}

	if options[:content] and options[:source]
		raise "Momo CFL Resource: Please specify only file content or source and not both"
	end

	info["content"] = options[:content] if options[:content]
	info["source"] = options[:source] if options[:source]
	info["group"] = options[:group] if options[:group]
	info["mode"] = options[:mode] if options[:mode]
	info["owner"] = options[:owner] if options[:owner]
	info["context"] = Momo.resolve(options[:context], stack: @stack, resource: name) if options[:context]

	set_thing2("files", name, info)
end

#gem(package) ⇒ Object



112
113
114
# File 'lib/momo/resource.rb', line 112

def gem(package)
	set_thing("packages", "rubygems", package, [])
end

#init_cfinit!Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/momo/resource.rb', line 54

def init_cfinit!
	init_metadata!
	
	if !@metadata["AWS::CloudFormation::Init"]
		@metadata["AWS::CloudFormation::Init"] = {}
	end
		 
	if !@metadata["AWS::CloudFormation::Init"]["config"]
		@metadata["AWS::CloudFormation::Init"]["config"] = {
			"packages" => {},
			"groups" => {},
			"users" => {},
			"sources" => {},
			"files" => {},
			"commands" => {},
			"services" => {}
		}
	end
end

#init_group(group) ⇒ Object



74
75
76
77
78
79
# File 'lib/momo/resource.rb', line 74

def init_group(group)
	init_cfinit!
	if !@metadata["AWS::CloudFormation::Init"]["config"][group] 
		@metadata["AWS::CloudFormation::Init"]["config"][group] = {}
	end
end

#init_metadata!Object



47
48
49
50
51
52
# File 'lib/momo/resource.rb', line 47

def init_metadata!

	if !@metadata 
		@metadata = {}
	end
end

#rpm(package) ⇒ Object



116
117
118
# File 'lib/momo/resource.rb', line 116

def rpm(package)
	set_thing("packages", "rpm", package, [])
end

#service(service_name) ⇒ Object



143
144
145
# File 'lib/momo/resource.rb', line 143

def service(service_name)
	set_thing("services", "sysvinit", service_name, {enabled: true, ensureRunning: true})
end

#set_deletion_policy(value) ⇒ Object



39
40
41
# File 'lib/momo/resource.rb', line 39

def set_deletion_policy(value)
	@deletion_policy = value
end

#set_thing(group, subgroup, name, thing) ⇒ Object



92
93
94
95
96
97
98
99
100
# File 'lib/momo/resource.rb', line 92

def set_thing(group, subgroup, name, thing)
	init_group(group)
	
	if !@metadata["AWS::CloudFormation::Init"]["config"][group][subgroup]
		@metadata["AWS::CloudFormation::Init"]["config"][group][subgroup] = {}
	end

	@metadata["AWS::CloudFormation::Init"]["config"][group][subgroup][name] = thing
end

#set_thing2(group, name, thing) ⇒ Object



102
103
104
105
106
# File 'lib/momo/resource.rb', line 102

def set_thing2(group, name, thing)
	init_group(group)

	@metadata["AWS::CloudFormation::Init"]["config"][group][name] = thing
end

#tag(key, value, options = {}) ⇒ Object



148
149
150
151
152
153
154
155
156
157
# File 'lib/momo/resource.rb', line 148

def tag(key, value, options={})
	if !@props["Tags"]
		@props["Tags"] = []
	end

	theTag = { "Key" => key, "Value" => Momo.resolve(value) }
	theTag["PropagateAtLaunch"] = options[:propagate_at_launch] if options[:propagate_at_launch]

	@props["Tags"] << theTag
end

#yum(package) ⇒ Object



108
109
110
# File 'lib/momo/resource.rb', line 108

def yum(package)
	set_thing("packages", "yum", package, [])
end