Class: Morpheus::Cli::NetworkServicesCommand

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

Instance Attribute Summary

Attributes included from CliCommand

#no_prompt

Instance Method Summary collapse

Methods included from InfrastructureHelper

#cloud_type_for_id, #cloud_type_for_name, #cloud_type_for_name_or_id, #clouds_interface, #find_cloud_by_id, #find_cloud_by_name, #find_cloud_by_name_or_id, #find_group_by_id, #find_group_by_name, #find_group_by_name_or_id, #get_available_cloud_types, #groups_interface, included

Methods included from CliCommand

#build_common_options, #build_option_type_options, #command_name, #default_subcommand, #establish_remote_appliance_connection, #full_command_usage, #handle_subcommand, included, #interactive?, #my_help_command, #my_terminal, #my_terminal=, #parse_id_list, #print, #print_error, #puts, #puts_error, #raise_command_error, #run_command_for_each_arg, #subcommand_aliases, #subcommand_usage, #subcommands, #usage, #verify_access_token!

Constructor Details

#initializeNetworkServicesCommand

set_default_subcommand :list



19
20
21
# File 'lib/morpheus/cli/network_services_command.rb', line 19

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

Instance Method Details

#connect(opts) ⇒ Object



23
24
25
26
27
28
# File 'lib/morpheus/cli/network_services_command.rb', line 23

def connect(opts)
  @api_client = establish_remote_appliance_connection(opts)
  @network_services_interface = @api_client.network_services
  @clouds_interface = @api_client.clouds
  @options_interface = @api_client.options
end

#handle(args) ⇒ Object



30
31
32
# File 'lib/morpheus/cli/network_services_command.rb', line 30

def handle(args)
  handle_subcommand(args)
end

#list(args) ⇒ Object



34
35
36
37
38
39
40
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
97
98
99
# File 'lib/morpheus/cli/network_services_command.rb', line 34

def list(args)
  options = {}
  params = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage()
    build_common_options(opts, options, [:list, :json, :yaml, :csv, :fields, :json, :dry_run, :remote])
    opts.footer = "List network services."
  end
  optparse.parse!(args)
  connect(options)
  begin
    [:phrase, :offset, :max, :sort, :direction].each do |k|
      params[k] = options[k] unless options[k].nil?
    end
    if options[:dry_run]
      print_dry_run @network_services_interface.dry.list(params)
      return
    end
    json_response = @network_services_interface.list(params)
    network_services = json_response["networkServices"]
    if options[:include_fields]
      json_response = {"networkServices" => filter_data(network_services, options[:include_fields]) }
    end
    if options[:json]
      puts as_json(json_response, options)
      return 0
    elsif options[:yaml]
      puts as_yaml(json_response, options)
      return 0
    elsif options[:csv]
      puts records_as_csv(network_services, options)
      return 0
    end
    title = "Morpheus Network Services"
    subtitles = []
    if params[:phrase]
      subtitles << "Search: #{params[:phrase]}".strip
    end
    print_h1 title, subtitles
    if network_services.empty?
      print cyan,"No network services found.",reset,"\n"
    else
      rows = network_services.collect {|network_service| 
        row = {
          id: network_service['id'],
          name: network_service['name'],
          type: network_service['type'] ? network_service['type']['name'] : '',
        }
        row
      }
      columns = [:id, :name, :type]
      if options[:include_fields]
        columns = options[:include_fields]
      end
      print cyan
      print as_pretty_table(rows, columns, options)
      print reset
      print_results_pagination(json_response, {:label => "network service", :n_label => "network services"})
    end
    print reset,"\n"
    return 0
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end