Method: BrocadeREST::PuppetManifest#genManifest

Defined in:
lib/brocade/puppetmanifest.rb

#genManifest(outputDir, isClass = false) ⇒ Object

Generate the manifest.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/brocade/puppetmanifest.rb', line 19

def genManifest(outputDir,isClass=false)

	# If we have documentation available in the skel/docs folder then read it.
	# Else if we have a parent type, refer to that manifest.
	docfile = "skel/docs/#{@type_}.doc"
	if ( File.exist?(docfile) )
		documentation = File.read(docfile)
		if isClass
			documentation.sub!("<CLASS-OR-TYPE-DECLARATION>","class {'pulsevtm::#{@type_}':")
		else 
			documentation.sub!("<CLASS-OR-TYPE-DECLARATION>","pulsevtm::#{@type_} { 'example':")
		end
	elsif (@template != nil)
		parent = @template.chomp(".erb")
		documentation = "#\n# This class is a direct implementation of brocadvtm::#{parent}\n"
		documentation += "#\n# Please refer to the documentation in that module for more information\n"
		documentation += "#\n"
	else
		documentation = ""
	end

	# build the parameters hash from the raw JSON
	decodeJSON()

	# Built in objects should be classes, while types should get defines.
	# There is only one Ping monitor, but theyre are lots of monitors
	if isClass
		desc = "# === class: pulsevtm::#{@type_}\n"
		code = "class pulsevtm::#{@type_} (\n"
		@maxKeyLength >= 6 ? sp = " " * ( @maxKeyLength - 6 ) : sp = " "
		code += "  \$ensure#{sp} = present,\n"
	else
		desc = "# === Define: pulsevtm::#{@type_}\n"
		code = "define pulsevtm::#{@type_} (\n"
		code += "  \$ensure,\n"
	end

	# content and ensure are the only params for binaries.
	if @isBinary 
		if isClass 
			code += "  \$content = file('pulsevtm/#{@type_}.data'),\n){\n"
		else
			code += "  \$content,\n){\n"
		end
	else

		# required keys have no default, puppet will force the user to enter
		# a value when they declare an instance of this type
		@required.each_key do |key|
			$stderr.puts($key)
			code += "  \$#{key},\n"
		end

		# Everything else is optional, and we use the defaults found by
		# walking the API
		@params.each do |key,value|
			value = inspectValue(value)
			sp = " " * ( @maxKeyLength - key.length )
			code += "  \$#{key}#{sp} = #{value},\n"
		end
		code += "){\n"
	end

	# Our manifests all look the same, include the parent class and it's params
	code += "  include pulsevtm\n"
	code += "  $ip              = $pulsevtm::rest_ip\n"
	code += "  $port            = $pulsevtm::rest_port\n"
	code += "  $user            = $pulsevtm::rest_user\n"
	code += "  $pass            = $pulsevtm::rest_pass\n"
	code += "  $purge           = $pulsevtm::purge\n"
	code += "  $purge_state_dir = $pulsevtm::purge_state_dir\n\n"
	code += "  info (\"Configuring #{@type_} ${name}\")\n"

	# Now configure our customer type: vtmrest
	if isClass
		code += "  vtmrest { '#{@type}':\n"
	else
		code += "  vtmrest { \"#{@type}/\${name}\":\n"
	end
	code += "    ensure   => $ensure,\n"
	code += "    before   => Class[pulsevtm::purge],\n"
	code += "    endpoint => \"https://\${ip}:\${port}/api/tm/#{@restVersion}/config/active\",\n"
	code += "    username => $user,\n"
	code += "    password => $pass,\n"
	if @isBinary
		code += "    content  => $content,\n"
		code += "    type     => 'application/octet-stream',\n"
	else
		if @template == nil
			code += "    content  => template('pulsevtm/#{@type_}.erb'),\n"
		else
			code += "    content  => template('pulsevtm/#{@template}'),\n"
		end
		code += "    type     => 'application/json',\n"
		code += "    internal => '#{@type_}',\n"
		code += "    failfast => $pulsevtm::failfast,\n"
	end
	code += "    debug    => $pulsevtm::debug,\n"
	code += "  }\n\n"

	# Now the purge section. figure out where this manifest should store it's name if purge
	# is enabled.
	code += "  if ( $purge ) {\n"
	if (isClass)
		if @template
			cFile = @template.chomp(".erb")
		else
			if @type.include?('/')
				cFile = @type[0..@type.rindex('/')-1].gsub(/\//,'_')
			else
				cFile = @type
			end
		end
		code += "    ensure_resource('file', \"${purge_state_dir}/#{cFile}\", {ensure => present})\n"
		code += "    file_line { '#{@type}':\n"
		code += "      line => '#{@type}',\n"
		code += "      path => \"${purge_state_dir}/#{cFile}\",\n"
		code += "    }\n"
	else
		code += "    ensure_resource('file', \"${purge_state_dir}/#{@type_}\", {ensure => present})\n"
		code += "    file_line { \"#{@type}/\${name}\":\n"
		code += "      line => \"#{@type}/${name}\",\n"
		code += "      path => \"${purge_state_dir}/#{@type_}\",\n"
		code += "    }\n"
	end
	code += "  }\n"
	code += "}\n"

	# Finally write the manifest to disk
	filename = "#{outputDir}/#{@type_}.pp"
	manifest = File.open(filename, "w")
	manifest.puts desc
	manifest.puts documentation
	manifest.puts code
	manifest.close
end