Class: Morpheus::Cli::Servers

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

Instance Method Summary collapse

Constructor Details

#initializeServers

Returns a new instance of Servers.



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

def initialize() 
  @appliance_name, @appliance_url = Morpheus::Cli::Remote.active_appliance
  @access_token = Morpheus::Cli::Credentials.new(@appliance_name,@appliance_url).request_credentials()
  @servers_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).servers
  @groups_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).groups
  @zones_interface = Morpheus::APIClient.new(@access_token,nil,nil, @appliance_url).zones
  @zone_types = @zones_interface.zone_types['zoneTypes']
  @active_groups = ::Morpheus::Cli::Groups.load_group_file
end

Instance Method Details

#add(args) ⇒ Object



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

def add(args)
  if args.count < 2
    puts "\nUsage: morpheus servers add ZONE [name]\n\n"
    return
  end
  options = {zone: args[0]}
  
  zone=nil
  if !options[:group].nil?
    group = find_group_by_name(options[:group])
    if !group.nil?
      options['groupId'] = group['id']
    end
  else
    options['groupId'] = @active_groups[@appliance_name.to_sym]
  end

  if !options['groupId'].nil?
    if !options[:zone].nil?
      zone = find_zone_by_name(options['groupId'], options[:zone])
      if !zone.nil?
        options['zoneId'] = zone['id']
      end
    end
  end

  if options['zoneId'].nil?
    puts red,bold,"\nEither the zone was not specified or was not found. Please make sure a zone is specified with --zone\n\n", reset
    return
  end

  zone_type = zone_type_for_id(zone['zoneTypeId'])
  begin
    case zone_type['code']
      when 'standard'
        add_standard(args[1],options[:description],zone, args[2..-1])
        list([])
      when 'openstack'
        add_openstack(args[1],options[:description],zone, args[2..-1])
        list([])
      when 'amazon'
        add_amazon(args[1],options[:description],zone, args[2..-1])
        list([])
      else
        puts "Unsupported Zone Type: This version of the morpheus cli does not support the requested zone type"
    end   
  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



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

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 servers [list,add,remove] [name]\n\n"
  end

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

#list(args) ⇒ Object



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

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 = {}
    if !options[:group].nil?
      group = find_group_by_name(options[:group])
      if !group.nil?
        params['site'] = group['id']
      end
    end

    json_response = @servers_interface.get(params)
    servers = json_response['servers']
    print "\n" ,red, bold, "Morpheus Servers\n","==================", reset, "\n\n"
    if servers.empty?
      puts yellow,"No servers currently configured.",reset
    else
      servers.each do |server|
        print red, "=  #{server['name']} - #{server['description']} (#{server['status']})\n"
      end
    end
    print reset,"\n\n"
    
  rescue => e
    puts "Error Communicating with the Appliance. Please try again later. #{e}"
    return nil
  end
end

#remove(args) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/morpheus/cli/servers.rb', line 98

def remove(args)
  if args.count < 1
    puts "\nUsage: morpheus servers remove [name]\n\n"
    return
  end
  begin
    server_results = @servers_interface.get({name: args[0]})
    if server_results['servers'].empty?
      puts "Server not found by name #{args[0]}"
      return
    end
    @servers_interface.destroy(server_results['servers'][0]['id'])
    list([])
  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