Class: Morpheus::Cli::InstanceTypes

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

Instance Method Summary collapse

Methods included from CliCommand

accountScopeOptions, genericOptions, included

Constructor Details

#initialize ⇒ InstanceTypes

Returns a new instance of InstanceTypes.



12
13
14
15
16
17
18
# File 'lib/morpheus/cli/instance_types.rb', line 12

def initialize() 
  @appliance_name, @appliance_url = Morpheus::Cli::Remote.active_appliance
  @access_token = Morpheus::Cli::Credentials.new(@appliance_name,@appliance_url).request_credentials()
  @instance_types_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).instance_types
  @groups_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).groups
  @active_groups = ::Morpheus::Cli::Groups.load_group_file
end

Instance Method Details

#details(args) ⇒ Object



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

def details(args)
  if args.count < 1
    puts "\nUsage: morpheus instances stats [name]\n\n"
    return
  end
  begin
    instance_results = @instances_interface.get({name: args[0]})
    if instance_results['instances'].empty?
      puts "Instance not found by name #{args[0]}"
      return
    end
    instance = instance_results['instances'][0]
    instance_id = instance['id']
    stats = instance_results['stats'][instance_id.to_s]
    print "\n" ,cyan, bold, "#{instance['name']} (#{instance['instanceType']['name']})\n","==================", reset, "\n\n"
    print cyan, "Memory: \t#{Filesize.from("#{stats['usedMemory']} B").pretty} / #{Filesize.from("#{stats['maxMemory']} B").pretty}\n"
    print cyan, "Storage: \t#{Filesize.from("#{stats['usedStorage']} B").pretty} / #{Filesize.from("#{stats['maxStorage']} B").pretty}\n\n",reset
    puts instance
  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
    return nil
  end
end

#handle(args) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/morpheus/cli/instance_types.rb', line 21

def handle(args) 
  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
  if args.empty?
    puts "\nUsage: morpheus instance-types [list,details] [name]\n\n"
    return
  end

  case args[0]
    when 'list'
      list(args[1..-1])
    when 'details'
      details(args[1..-1])
    else
      puts "\nUsage: morpheus instance-types [list,details] [name]\n\n"
  end
end

#list(args) ⇒ Object



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

def list(args)
  options = {}
  optparse = OptionParser.new do|opts|
    opts.on( '-g', '--group GROUP', "Group Name" ) do |group|
      options[:group] = group
    end
  end
  optparse.parse(args)
  begin
    params = {}

    json_response = @instance_types_interface.get(params)
    instance_types = json_response['instanceTypes']
    print "\n" ,cyan, bold, "Morpheus Instance Types\n","==================", reset, "\n\n"
    if instance_types.empty?
      puts yellow,"No instance types currently configured.",reset
    else
      instance_types.each do |instance_type|
        versions = instance_type['versions'].join(', ')
        print cyan, "=  #{instance_type['name']} (#{instance_type['code']}) - #{versions}\n"
        instance_type['instanceTypeLayouts'].each do |layout|
          print green, "     - #{layout['name']}\n",reset
        end
      end
    end
    print reset,"\n\n"
    
  rescue => e
    puts "Error Communicating with the Appliance. Please try again later. #{e}"
    return nil
  end
end