Class: Morpheus::Cli::Clouds

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

Instance Method Summary collapse

Methods included from CliCommand

#build_common_options, included

Constructor Details

#initializeClouds

Returns a new instance of Clouds.



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

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

end

Instance Method Details

#add(args) ⇒ Object



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

def add(args)
	options = {}
	params = {zone_type: 'standard'}
	optparse = OptionParser.new do|opts|
		opts.banner = "Usage: morpheus clouds add [name] --group GROUP --type TYPE"
		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
		build_common_options(opts, options, [:options, :json, :remote])
	end
	optparse.parse!(args)

	if args.count < 1
		puts "\n#{optparse.banner}\n\n"
		exit 1
	end

	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))
		json_response = @clouds_interface.create(zone)
		if options[:json]
			print JSON.pretty_generate(json_response)
			print "\n"
		else
			list([])
		end
	rescue RestClient::Exception => e
		print_rest_exception(e, options)
		exit 1
	end
end

#apply_security_groups(args) ⇒ Object



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

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
		print_rest_exception(e, options)
		exit 1
	end
end

#connect(opts) ⇒ Object



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

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_alert "Invalid Credentials. Unable to acquire access token. Please verify your credentials and try again."
		return 1
	end
end

#firewall_disable(args) ⇒ Object



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/morpheus/cli/clouds.rb', line 223

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
		print_rest_exception(e, options)
		exit 1
	end
end

#firewall_enable(args) ⇒ Object



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/morpheus/cli/clouds.rb', line 242

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
		print_rest_exception(e, options)
		exit 1
	end
end

#handle(args) ⇒ Object



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

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



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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/morpheus/cli/clouds.rb', line 167

def list(args)
	options={}
	optparse = OptionParser.new do|opts|
		opts.banner = "Usage: morpheus clouds list"
		opts.on( '-g', '--group GROUP', "Group Name" ) do |group|
			options[:group] = group
		end
		build_common_options(opts, options, [:list, :json, :remote])
	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_table =clouds.collect do |cloud|
					status = nil
					if cloud['status'] == 'ok'
						status = "#{green}OK#{cyan}"
					elsif cloud['status'].nil?
						status = "#{white}UNKNOWN#{cyan}"
					else
						status = "#{red}#{cloud['status'] ? cloud['status'].upcase : 'N/A'}#{cloud['statusMessage'] ? "#{cyan} - #{cloud['statusMessage']}" : ''}#{cyan}"
					end
					{id: cloud['id'], name: cloud['name'], type: cloud_type_for_id(cloud['zoneTypeId']), location: cloud['location'], status: status}
					# print cyan, "= [#{server['id']}] #{server['name']} - #{server['computeServerType'] ? server['computeServerType']['name'] : 'unmanaged'} (#{server['status']}) Power: ", power_state, "\n"
				end
				print cyan
				tp clouds_table, :id, :name, :type, :location, :status
				print reset,"\n\n"
			end
			
				

		end
	rescue => e
		puts "Error Communicating with the Appliance. Please try again later. #{e}"
		exit 1
	end
end

#remove(args) ⇒ Object



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

def remove(args)
	options = {}
	optparse = OptionParser.new do|opts|
		opts.banner = "Usage: morpheus clouds remove [name] --group GROUP"
		opts.on( '-g', '--group GROUP', "Group Name" ) do |group|
			options[:group] = group
		end
		build_common_options(opts, options, [:auto_confirm, :json, :remote])
	end
	optparse.parse(args)

	if args.count < 2
		puts "\n#{optparse.banner}\n\n"
		return
	end

	connect(options)
	if !options[:group].nil?
		group = find_group_by_name(options[:group])
		if !group.nil?
			options[:groupId] = group['id']
		else
			puts "\nGroup #{options[: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
		cloud = zone_results['zones'][0]
		unless options[:yes] || Morpheus::Cli::OptionTypes.confirm("Are you sure you want to delete the cloud #{cloud['name']}?")
			exit
		end
		json_response = @clouds_interface.destroy(cloud['id'])
		if options[:json]
			print JSON.pretty_generate(json_response)
			print "\n"
		else
			list([])
		end
	rescue RestClient::Exception => e
		print_rest_exception(e, options)
		exit 1
	end
end

#security_groups(args) ⇒ Object



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
289
290
291
292
# File 'lib/morpheus/cli/clouds.rb', line 261

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
		print_rest_exception(e, options)
		exit 1
	end
end