lib/cloudstack-cli/commands/network.rb in cloudstack-cli-0.15.1 vs lib/cloudstack-cli/commands/network.rb in cloudstack-cli-1.0.0.rc1

- old
+ new

@@ -1,41 +1,23 @@ class Network < CloudstackCli::Base desc "list", "list networks" option :project, desc: 'the project name of the network' option :account, desc: 'the owner of the network' - option :zone, desc: 'typehe name of the zone the network belongs to' + option :zone, desc: 'the name of the zone the network belongs to' option :type, desc: 'the type of the network' option :showid, type: :boolean, desc: 'show the network id' option :showvlan, type: :boolean, desc: 'show the VLAN' def list - project = find_project if options[:project] - if options[:zone] - unless zone = client.get_zone(options[:zone]) - say "Zone '#{options[:zone]}' not found.", :red - exit 1 - end - zone_id = zone['id'] - end + resolve_zone if options[:zone] + resolve_project + networks = client.list_networks(options) - networks = [] - if project - networks = client.list_networks(project_id: project['id'], zone_id: zone_id) - elsif options[:account] - networks = client.list_networks(account: options[:account], zone_id: zone_id) - else - networks = client.list_networks(zone_id: zone_id) - networks += client.list_networks(project_id: -1, zone_id: zone_id) - end - - if options[:type] - networks = filter_by(networks, 'type', options[:type]) - end - if networks.size < 1 puts "No networks found." else + networks = filter_by(networks, 'type', options[:type]) if options[:type] table = [%w(Name Displaytext Account/Project Zone Domain State Type Offering)] table[0] << "ID" if options[:showid] table[0] << "VLAN" if options[:showvlan] networks.each do |network| table << [ @@ -54,74 +36,47 @@ print_table table say "Total number of networks: #{networks.count}" end end - desc "default", "get the default network" - option :zone - def default - network = client.get_default_network(options[:zone]) - unless network - puts "No default network found." - else - table = [["Name", "Displaytext", "Domain", "Zone"]] - table[0] << "ID" if options[:showid] - table << [ - network["name"], - network["displaytext"], - network["domain"], - network["zonename"] - ] - table[-1] << network["id"] if options[:showid] - print_table table - end - end - desc "show NAME", "show detailed infos about a network" option :project def show(name) - if options[:project] - if options[:project].downcase == "all" - options[:project_id] = -1 - else - project = find_project - options[:project_id] = project['id'] - end + resolve_project + unless network = client.list_networks(options).find {|n| n['name'] == name} + say "Error: No network with name '#{name}' found.", :red + exit end - unless server = client.get_network(name, options[:project_id]) - puts "No network found." - else - table = server.map do |key, value| - [ set_color("#{key}:", :yellow), "#{value}" ] - end - print_table table + table = network.map do |key, value| + [ set_color("#{key}:", :yellow), "#{value}" ] end + print_table table end desc "restart NAME", "restart network" - option :cleanup, type: :boolean, default: true + option :cleanup, type: :boolean, default: false + option :project def restart(name) - network = client.get_network(name) - network = client.get_network(name, -1) unless network - unless network - say "Network #{name} not found." + resolve_project + unless network = client.list_networks(options).find {|n| n['name'] == name} + say "Network with name '#{name}' not found." exit 1 end if yes? "Restart network \"#{network['name']}\" (cleanup=#{options[:cleanup]})?" - p client.restart_network(network['id'], options[:cleanup]) + client.restart_network(id: network['id'], cleanup: options[:cleanup]) end end desc "delete NAME", "delete network" + option :project def delete(name) - network = client.get_network(name) - network = client.get_network(name, -1) unless network - unless network - say "Network \"#{name}\" not found." + resolve_project + unless network = client.list_networks(options).find {|n| n['name'] == name} + say "Error: Network with name '#{name}' not found.", :red exit 1 end - if yes? "Destroy network \"#{network['name']}\"?" - p client.delete_network(network['id']) + if yes? "Delete network \"#{network['name']}\"?" + client.delete_network(id: network['id']) end end -end \ No newline at end of file +end