Class: Morpheus::Cli::Clouds

Inherits:
Object
  • Object
show all
Includes:
CliCommand, Term::ANSIColor
Defined in:
lib/morpheus/cli/clouds.rb

Instance Method Summary collapse

Methods included from CliCommand

accountScopeOptions, genericOptions, included

Constructor Details

#initializeClouds

Returns a new instance of Clouds.



13
14
15
16
# File 'lib/morpheus/cli/clouds.rb', line 13

def initialize() 
	@appliance_name, @appliance_url = Morpheus::Cli::Remote.active_appliance

end

Instance Method Details

#add(args) ⇒ Object



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
# File 'lib/morpheus/cli/clouds.rb', line 65

def add(args)
	if args.count < 1
		puts "\nUsage: morpheus clouds add [name] --group GROUP --type TYPE\n\n"
		exit 1
	end
	options = {}
	params = {zone_type: 'standard'}
	optparse = OptionParser.new do|opts|
		opts.on( '-g', '--group GROUP', "Group Name" ) do |group|
			params[:group] = group
		end
		opts.on( '-t', '--type TYPE', "Cloud Type" ) do |zone_type|
			params[:zone_type] = zone_type
		end
		opts.on( '-d', '--description DESCRIPTION', "Description (optional)" ) do |desc|
			params[:description] = desc
		end
		Morpheus::Cli::CliCommand.genericOptions(opts,options)
	end
	optparse.parse!(args)
	connect(options)
	zone = {name: args[0], description: params[:description]}
	if !params[:group].nil?
		group = find_group_by_name(params[:group])
		if !group.nil?
			zone['groupId'] = group['id']
		end
	end

	if !params[:zone_type].nil?
		cloud_type = cloud_type_for_name(params[:zone_type])
		zone['zoneType'] = {code: cloud_type['code']}
	end
	
	begin
		zone.merge!(Morpheus::Cli::OptionTypes.prompt(cloud_type['optionTypes'],options[:options],@api_client))
		@clouds_interface.create(zone)
	rescue => e
		if e.response and e.response.code == 400
			error = JSON.parse(e.response.to_s)
			if options[:json]
				print JSON.pretty_generate(error)
			else
				::Morpheus::Cli::ErrorHandler.new.print_errors(error)
			end
		else
			puts "Error Communicating with the Appliance. Please try again later. #{e}"
		end
		exit 1
	end
	list([])
end

#apply_security_groups(args) ⇒ Object



290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# File 'lib/morpheus/cli/clouds.rb', line 290

def apply_security_groups(args)
	usage = <<-EOF
Usage: morpheus clouds apply_security_groups [name] [options]
EOF
	if args.count < 1
		puts usage
		exit 1
	end

	options = {}
	clear_or_secgroups_specified = false
	optparse = OptionParser.new do|opts|
		opts.banner = usage
		opts.on( '-c', '--clear', "Clear all security groups" ) do
			options[:securityGroupIds] = []
			clear_or_secgroups_specified = true
		end
		opts.on( '-s', '--secgroups SECGROUPS', "Apply the specified comma separated security group ids" ) do |secgroups|
			options[:securityGroupIds] = secgroups.split(",")
			clear_or_secgroups_specified = true
		end
		opts.on( '-h', '--help', "Prints this help" ) do
			puts opts
			exit
		end
	end
	optparse.parse(args)

	if !clear_or_secgroups_specified 
		puts usage
		exit
	end

	begin
		zone_results = @clouds_interface.get({name: args[0]})
		if zone_results['zones'].empty?
			puts "Zone not found by name #{args[0]}"
			exit 1
		end

		@clouds_interface.apply_security_groups(zone_results['zones'][0]['id'], options)
		security_groups([args[0]])
	rescue RestClient::Exception => e
		if e.response.code == 400
			error = JSON.parse(e.response.to_s)
			::Morpheus::Cli::ErrorHandler.new.print_errors(error)
		else
			puts "Error Communicating with the Appliance. Please try again later. #{e}"
		end
		exit 1
	end
end

#connect(opts) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/morpheus/cli/clouds.rb', line 19

def connect(opts)
	if opts[:remote]
		@appliance_url = opts[:remote]
		@appliance_name = opts[:remote]
		@access_token = Morpheus::Cli::Credentials.new(@appliance_name,@appliance_url).request_credentials(opts)
	else
		@access_token = Morpheus::Cli::Credentials.new(@appliance_name,@appliance_url).request_credentials(opts)
	end
	@api_client = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url)
	@clouds_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).clouds
	@groups_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).groups
	@cloud_types = @clouds_interface.cloud_types['zoneTypes']
	if @access_token.empty?
		print red,bold, "\nInvalid Credentials. Unable to acquire access token. Please verify your credentials and try again.\n\n",reset
		return 1
	end
end

#firewall_disable(args) ⇒ Object



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/morpheus/cli/clouds.rb', line 204

def firewall_disable(args)
	if args.count < 1
		puts "\nUsage: morpheus clouds firewall_disable [name]\n\n"
		return
	end
	begin
		zone_results = @clouds_interface.get({name: args[0]})
		if zone_results['zones'].empty?
			puts "Zone not found by name #{args[0]}"
			exit 1
		end
		@clouds_interface.firewall_disable(zone_results['zones'][0]['id'])
		security_groups([args[0]])
	rescue RestClient::Exception => e
		if e.response.code == 400
			error = JSON.parse(e.response.to_s)
			::Morpheus::Cli::ErrorHandler.new.print_errors(error)
		else
			puts "Error Communicating with the Appliance. Please try again later. #{e}"
		end
		exit 1
	end
end

#firewall_enable(args) ⇒ Object



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/morpheus/cli/clouds.rb', line 228

def firewall_enable(args)
	if args.count < 1
		puts "\nUsage: morpheus clouds firewall_enable [name]\n\n"
		return
	end
	begin
		zone_results = @clouds_interface.get({name: args[0]})
		if zone_results['zones'].empty?
			puts "Zone not found by name #{args[0]}"
			exit 1
		end
		@clouds_interface.firewall_enable(zone_results['zones'][0]['id'])
		security_groups([args[0]])
	rescue RestClient::Exception => e
		if e.response.code == 400
			error = JSON.parse(e.response.to_s)
			::Morpheus::Cli::ErrorHandler.new.print_errors(error)
		else
			puts "Error Communicating with the Appliance. Please try again later. #{e}"
		end
		exit 1
	end
end

#handle(args) ⇒ Object



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
# File 'lib/morpheus/cli/clouds.rb', line 37

def handle(args) 

	if args.empty?
		puts "\nUsage: morpheus clouds [list,add,remove,firewall_disable,firewall_enable,security_groups,apply_security_groups] [name]\n\n"
		return
	end

	case args[0]
		when 'list'
			list(args[1..-1])
		when 'add'
			add(args[1..-1])
		when 'remove'
			remove(args[1..-1])
		when 'firewall_disable'
			firewall_disable(args[1..-1])	
		when 'firewall_enable'
			firewall_enable(args[1..-1])	
		when 'security_groups'	
			security_groups(args[1..-1])	
		when 'apply_security_groups'	
			apply_security_groups(args[1..-1])		
		else
			puts "\nUsage: morpheus clouds [list,add,remove,firewall_disable,firewall_enable,security_groups,apply_security_groups] [name]\n\n"
			exit 127 #Command now foud exit code
	end
end

#list(args) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/morpheus/cli/clouds.rb', line 163

def list(args)
	options={}
	optparse = OptionParser.new do|opts|
		opts.on( '-g', '--group GROUP', "Group Name" ) do |group|
			options[:group] = group
		end
		Morpheus::Cli::CliCommand.genericOptions(opts,options)
	end
	optparse.parse(args)
	connect(options)
	begin
		params = {}
		if !options[:group].nil?
			group = find_group_by_name(options[:group])
			if !group.nil?
				params['groupId'] = group['id']
			end
		end

		json_response = @clouds_interface.get(params)
		clouds = json_response['zones']
		if options[:json]
			print JSON.pretty_generate(json_response)
			print "\n"
		else
			print "\n" ,cyan, bold, "Morpheus Clouds\n","==================", reset, "\n\n"
			if clouds.empty?
				puts yellow,"No clouds currently configured.",reset
			else
				clouds.each do |zone|
					print cyan, "=  #{zone['name']} (#{cloud_type_for_id(zone['zoneTypeId'])}) - #{zone['description']}\n"
				end
			end
			print reset,"\n\n"
		end
	rescue => e
		puts "Error Communicating with the Appliance. Please try again later. #{e}"
		exit 1
	end
end

#remove(args) ⇒ Object



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
155
156
157
158
159
160
161
# File 'lib/morpheus/cli/clouds.rb', line 118

def remove(args)
	if args.count < 2
		puts "\nUsage: morpheus clouds remove [name] --group GROUP\n\n"
		return
	end

	params = {}
	optparse = OptionParser.new do|opts|
		opts.on( '-g', '--group GROUP', "Group Name" ) do |group|
			params[:group] = group
		end
		Morpheus::Cli::CliCommand.genericOptions(opts,params)
	end
	optparse.parse(args)
	connect(params)
	if !params[:group].nil?
		group = find_group_by_name(params[:group])
		if !group.nil?
			params[:groupId] = group['id']
		else
			puts "\nGroup #{params[:group]} not found!"
			exit 1
		end
	end


	begin
		zone_results = @clouds_interface.get({name: args[0]})
		if zone_results['zones'].empty?
			puts "Zone not found by name #{args[0]}"
			exit 1
		end
		@clouds_interface.destroy(zone_results['zones'][0]['id'])
		list([])
	rescue RestClient::Exception => e
		if e.response.code == 400 or e.response.code == 500
			error = JSON.parse(e.response.to_s)
			::Morpheus::Cli::ErrorHandler.new.print_errors(error,params)
		else
			puts "Error Communicating with the Appliance. Please try again later. #{e}"
		end
		exit 1
	end
end

#security_groups(args) ⇒ Object



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/morpheus/cli/clouds.rb', line 252

def security_groups(args)
	if args.count < 1
		puts "\nUsage: morpheus clouds security_groups [name]\n\n"
		return
	end
	begin
		zone_results = @clouds_interface.get({name: args[0]})
		if zone_results['zones'].empty?
			puts "Zone not found by name #{args[0]}"
			exit 1
		end

		zone_id = zone_results['zones'][0]['id']
		json_response = @clouds_interface.security_groups(zone_id)

		securityGroups = json_response['securityGroups']
		print "\n" ,cyan, bold, "Morpheus Security Groups for Zone:#{zone_id}\n","==================", reset, "\n\n"
		print cyan, "Firewall Enabled=#{json_response['firewallEnabled']}\n\n"
		if securityGroups.empty?
			puts yellow,"No security groups currently applied.",reset
		else
			securityGroups.each do |securityGroup|
				print cyan, "=  #{securityGroup['id']} (#{securityGroup['name']}) - (#{securityGroup['description']})\n"
			end
		end
		print reset,"\n\n"

	rescue RestClient::Exception => e
		if e.response.code == 400
			error = JSON.parse(e.response.to_s)
			::Morpheus::Cli::ErrorHandler.new.print_errors(error)
		else
			puts "Error Communicating with the Appliance. Please try again later. #{e}"
		end
		exit 1
	end
end