Class: Morpheus::Cli::License

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

Instance Method Summary collapse

Methods included from CliCommand

#build_common_options, included

Constructor Details

#initialize ⇒ License

Returns a new instance of License.



10
11
12
# File 'lib/morpheus/cli/license.rb', line 10

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

Instance Method Details

#apply(args) ⇒ Object



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

def apply(args)
  key = args[0]
  options = {}
   = nil
  optparse = OptionParser.new do|opts|
    opts.banner = "Usage: morpheus license apply [key]"
    build_common_options(opts, options, [:json, :remote])
  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
    print_rest_exception(e, options)
    exit 1
  end
end

#connect(opts) ⇒ Object



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

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

#details(args) ⇒ Object



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

def details(args)
  options = {}
  optparse = OptionParser.new do|opts|
    opts.banner = "Usage: morpheus license details"
    build_common_options(opts, options, [:json, :remote])
  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
    print_rest_exception(e, options)
    exit 1
  end
end

#handle(args) ⇒ Object



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

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