Class: Morpheus::Cli::License

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

Instance Method Summary collapse

Methods included from CliCommand

accountScopeOptions, genericOptions, included

Constructor Details

#initializeLicense

Returns a new instance of License.



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

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

Instance Method Details

#apply(args) ⇒ Object



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/license.rb', line 88

def apply(args)
	key = args[0]
	options = {}
	 = nil
	optparse = OptionParser.new do|opts|
		opts.banner = "Usage: morpheus license apply [key]"
		Morpheus::Cli::CliCommand.genericOptions(opts,options)
	end
	if args.count < 1
		puts "\n#{optparse.banner}\n\n"
		exit 1
	end
	optparse.parse(args)

	connect(options)
	
	begin
		license_results = @license_interface.apply(key)

		if options[:json]
				puts JSON.pretty_generate(license_results)
		else
			puts "License applied successfully!"
		end
	rescue RestClient::Exception => e
		::Morpheus::Cli::ErrorHandler.new.print_rest_exception(e)
		exit 1
	end
end

#connect(opts) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/morpheus/cli/license.rb', line 16

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)		
	@license_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).license
	
	if @access_token.empty?
		print red,bold, "\nInvalid Credentials. Unable to acquire access token. Please verify your credentials and try again.\n\n",reset
		exit 1
	end
end

#details(args) ⇒ Object



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

def details(args)
	options = {}
	optparse = OptionParser.new do|opts|
		opts.banner = "Usage: morpheus license details"
		Morpheus::Cli::CliCommand.genericOptions(opts,options)
	end
	optparse.parse(args)
	connect(options)
	begin
		license = @license_interface.get()
		
		if options[:json]
				puts JSON.pretty_generate(license)
		else
			if license['license'].nil?
				puts "No License Currently Applied to the appliance."
				exit 1
			else
				print "\n", cyan, "License\n=======\n"
				max_memory = Filesize.from("#{license['license']['maxMemory']} B").pretty
				max_storage = Filesize.from("#{license['license']['maxStorage']} B").pretty
				used_memory = Filesize.from("#{license['usedMemory']} B").pretty
				puts "Account: #{license['license']['accountName']}"
				puts "Start Date: #{license['license']['startDate']}"
				puts "End Date: #{license['license']['endDate']}"
				puts "Memory: #{used_memory} / #{max_memory}"
				puts "Max Storage: #{max_storage}"
			end
			print reset,"\n\n"
		end
	rescue RestClient::Exception => e
		::Morpheus::Cli::ErrorHandler.new.print_rest_exception(e)
		exit 1
	end
end

#handle(args) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/morpheus/cli/license.rb', line 34

def handle(args) 
	if args.empty?
		puts "\nUsage: morpheus license [details, apply]\n\n"
		return 
	end

	case args[0]
		when 'apply'
			apply(args[1..-1])	
		when 'details'
			details(args[1..-1])
		else
			puts "\nUsage: morpheus license [details, apply]\n\n"
			exit 127
	end
end