Class: Morpheus::Cli::NetworkServersCommand

Inherits:
Object
  • Object
show all
Includes:
CliCommand, InfrastructureHelper, RestCommand
Defined in:
lib/morpheus/cli/commands/network_servers_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, #find_network_by_id, #find_network_by_name, #find_network_by_name_or_id, #find_network_group_by_id, #find_network_group_by_name, #find_network_group_by_name_or_id, #find_network_server_type_by_id, #find_network_server_type_by_name, #find_network_server_type_by_name_or_code, #find_network_server_type_by_name_or_id, #find_network_type_by_id, #find_network_type_by_name, #find_network_type_by_name_or_id, #find_resource_pool_by_id, #find_resource_pool_by_name, #find_resource_pool_by_name_or_id, #find_resource_pool_group_by_id, #find_resource_pool_group_by_name, #find_resource_pool_group_by_name_or_id, #find_subnet_by_id, #find_subnet_by_name, #find_subnet_by_name_or_id, #find_subnet_type_by_id, #find_subnet_type_by_name, #find_subnet_type_by_name_or_id, #format_network_pool_server_status, #get_available_cloud_types, #groups_interface, included, #network_groups_interface, #network_pool_server_column_definitions, #network_pool_server_list_column_definitions, #network_pool_server_type_column_definitions, #network_pool_server_type_list_column_definitions, #network_server_type_column_definitions, #network_server_type_list_column_definitions, #network_types_interface, #networks_interface, #prompt_for_network, #prompt_for_networks, #prompt_for_pools, #prompt_for_subnets, #resource_pool_groups_interface, #resource_pools_interface, #subnet_types_interface, #subnets_interface

Methods included from RestCommand

#_get, #_get_type, #connect, #get, #get_type, included, #list, #list_types, #registered_interfaces, #remove, #render_response_for_get_type, #rest_arg, #rest_column_definitions, #rest_find_by_name_or_id, #rest_has_name, #rest_has_type, #rest_interface, #rest_interface_name, #rest_key, #rest_label, #rest_label_plural, #rest_list_column_definitions, #rest_list_key, #rest_name, #rest_object_key, #rest_option_context_map, #rest_perms_config, #rest_type_arg, #rest_type_column_definitions, #rest_type_find_by_name_or_id, #rest_type_interface, #rest_type_interface_name, #rest_type_key, #rest_type_label, #rest_type_label_plural, #rest_type_list_column_definitions, #rest_type_list_key, #rest_type_name, #rest_type_object_key

Methods included from CliCommand

#add_query_parameter, #apply_options, #build_common_options, #build_get_options, #build_list_options, #build_option_type_options, #build_standard_add_many_options, #build_standard_add_options, #build_standard_api_options, #build_standard_delete_options, #build_standard_get_options, #build_standard_list_options, #build_standard_post_options, #build_standard_put_options, #build_standard_remove_options, #build_standard_update_options, #command_description, #command_name, #confirm, #confirm!, #default_refresh_interval, #default_sigdig, #default_subcommand, #establish_remote_appliance_connection, #execute_api, #execute_api_payload, #execute_api_request, #find_all, #find_all_json, #find_by_id, #find_by_name, #find_by_name_or_id, #find_record, #find_record_json, #full_command_usage, #get_interface, #get_list_key, #get_object_key, #get_subcommand_description, #handle_each_payload, #handle_subcommand, included, #interactive?, #my_help_command, #my_terminal, #my_terminal=, #parse_array, #parse_bytes_param, #parse_get_options!, #parse_id_list, #parse_labels, #parse_list_options, #parse_list_options!, #parse_list_subtitles, #parse_options, #parse_parameter_as_resource_id!, #parse_passed_options, #parse_payload, #parse_query_options, #print, #print_error, #println, #prog_name, #puts, #puts_error, #raise_args_error, #raise_command_error, #render_response, #run_command_for_each_arg, #subcommand_aliases, #subcommand_description, #subcommand_usage, #subcommands, #usage, #validate_outfile, #verify_args!, #visible_subcommands

Instance Method Details

#add(args) ⇒ Object



25
26
27
28
29
30
31
32
33
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/morpheus/cli/commands/network_servers_command.rb', line 25

def add(args)
  options = {params: {}}
  params = options[:params]
  ip_range_list = nil
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[name]")
    opts.on('--name VALUE', String, "Name for this network server") do |val|
      params['name'] = val
    end
    opts.on('--type VALUE', String, "Network Server Type code") do |val|
      params['type'] = val
    end
    # opts.on('--enabled [on|off]', String, "Can be used to disable the network server") do |val|
    #   params['enabled'] = val.to_s == 'on' || val.to_s == 'true' || val.to_s.empty?
    # end
    # ['name', 'serviceUsername', 'servicePassword', 'servicePort', 'serviceHost', 'serviceUrl', 'serviceMode', 'networkFilter', 'tenantMatch']
    #build_option_type_options(opts, options, add_network_server_option_types)
    build_option_type_options(opts, options, add_network_server_advanced_option_types)
    build_standard_add_options(opts, options)
    opts.footer = <<-EOT
Create a new network server.
[name] is required and can be passed as --name instead.
Configuration options vary by network server type.
EOT
  end
  optparse.parse!(args)
  verify_args!(args:args, optparse:optparse, max: 1)
  connect(options)

  # support [name] as first argument
  if args[0]
    params['name'] = args[0]
  end

  # merge -O options into normally parsed options
  #params.deep_merge!(options[:options].reject {|k,v| k.is_a?(Symbol) }) if options[:options]

  # construct payload
  payload = nil
  if options[:payload]
    payload = options[:payload]
    payload.deep_merge!({rest_object_key => parse_passed_options(options)})
    payload.deep_merge!({rest_object_key => params})
  else
    payload = {}
    payload.deep_merge!({rest_object_key => parse_passed_options(options)})
    # Name
    if !params['name']
      params['name'] = prompt_value({'fieldName' => 'name', 'fieldLabel' => 'Name', 'type' => 'text', 'required' => true, 'description' => 'Name for this network server.'}, options)
      # params['name'] = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'name', 'fieldLabel' => 'Name', 'type' => 'text', 'required' => true, 'description' => 'Name for this network server.'}], params, @api_client, {}, options[:no_prompt])['name']
    end

    # Network Server Type
    network_type_id = nil
    # use this optionSource networkServices which is really network service 
    # where the response data is categories, each with a list ofservices (service types)
    network_services_options = @options_interface.options_for_source('networkServices',{})['data']
    networking_category = network_services_options.find {|it| it['value'] == 'networkServer'}
    service_types = networking_category ? networking_category['services'] : []
    network_server_type_options = service_types.collect {|it| {'name' => it['name'], 'value' => it['code'], 'code' => it['code'], 'id' => it['id']} }
    if network_server_type_options.empty?
      raise_command_error "No available network server types found"
    end
    # v_prompt = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'type', 'fieldLabel' => 'Network Server Type', 'type' => 'select', 'optionSource' => 'networkServerTypes', 'required' => true, 'description' => 'Choose a network server type.'}], options, @api_client, {})
    #network_type_id = v_prompt['type']
    # allow matching type by id, name or code
    selected_type = nil
    if params['type'] && params['type'].to_s =~ /\A\d{1,}\Z/
      network_type_id = params['type'].to_i
      selected_type = network_server_type_options.find {|it| it['id'] == network_type_id }
      if selected_type.nil?
        raise_command_error "Network Server Type not found by id '#{params['type']}'"
      end
      network_type_code = selected_type['code']
    else
      network_type_code = prompt_value({'fieldName' => 'type', 'fieldLabel' => 'Network Server Type', 'type' => 'select', 'selectOptions' => network_server_type_options, 'required' => true, 'description' => 'Choose a network server type.'}, options.merge(params))
      #network_type_code = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'type', 'fieldLabel' => 'Network Server Type', 'type' => 'select', 'selectOptions' => network_server_type_options, 'required' => true, 'description' => 'Choose a network server type.'}], options, @api_client, {}, options[:no_prompt])['type']
      selected_type = network_server_type_options.find {|it| it['code'] == network_type_code }
      if selected_type.nil?
        raise_command_error "Network Server Type not found by name or code '#{params['type']}'"
      end
      network_type_id = selected_type['id']
    end
    params['type'] = network_type_code
    
    # prompt options by type
    network_server_type = @network_server_types_interface.get(network_type_id.to_i)['networkServerType']
    # params['type'] = network_server_type['code']
    type_options_types = network_server_type['optionTypes'] || []
    type_options_types.reject! {|it| it['fieldName'] == 'visibility' } # skip visibility, its under advanced now
    option_result = prompt(network_server_type['optionTypes'], options.merge({:context_map => {'networkServer' => ''}}))
    params.deep_merge!(option_result)

    # advanced options
    advanced_option_types = add_network_server_advanced_option_types
    if advanced_option_types && !advanced_option_types.empty?
      v_prompt = Morpheus::Cli::OptionTypes.prompt(advanced_option_types, options[:options], @api_client, {})
      v_prompt.deep_compact!
      v_prompt.booleanize! # 'on' => true
      params.deep_merge!(v_prompt)
    end
    payload.deep_merge!({rest_object_key => params})
  end
  @network_servers_interface.setopts(options)
  if options[:dry_run]
    print_dry_run @network_servers_interface.dry.create(payload)
    return
  end
  json_response = @network_servers_interface.create(payload)
  render_response(json_response, options, 'networkServer') do
    network_server = json_response['networkServer']
    print_green_success "Added network server #{network_server['name']}"
    _get(network_server['id'], {}, options)
  end
end

#handle(args) ⇒ Object



21
22
23
# File 'lib/morpheus/cli/commands/network_servers_command.rb', line 21

def handle(args)
  handle_subcommand(args)
end

#refresh(args) ⇒ Object



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/morpheus/cli/commands/network_servers_command.rb', line 205

def refresh(args)
  options = {}
  params = {}
  ip_range_list = nil
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[network server]")
    # ['name', 'serviceUsername', 'servicePassword', 'servicePort', 'serviceHost', 'serviceUrl', 'serviceMode', 'networkFilter', 'tenantMatch']
          build_standard_update_options(opts, options, [:query])
    opts.footer = <<-EOT
Refresh a network server.
[network server] is required. This is the name or id of a network server.
EOT
  end
  optparse.parse!(args)
  verify_args!(args:args, optparse:optparse, count: 1)
  connect(options)
  # find network server to be updated
  network_server = find_network_server_by_name_or_id(args[0])
  return 1 if network_server.nil?
  # construct query parameters
  params.merge!(parse_query_options(options))
  # construct payload
  payload = nil
  if options[:payload]
    payload = options[:payload]
  else
    payload = options[:options].reject {|k,v| k.is_a?(Symbol) }
  end
  @network_servers_interface.setopts(options)
  if options[:dry_run]
    print_dry_run @network_servers_interface.dry.refresh(network_server["id"], params, payload)
    return
  end
  json_response = @network_servers_interface.refresh(network_server["id"], params, payload)
  render_response(json_response, options, 'networkServer') do
    #network_server = json_response['networkServer']
    print_green_success "Refreshing network server #{network_server['name']}"
    #_get(network_server['id'], {}, options)
  end
end

#update(args) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/morpheus/cli/commands/network_servers_command.rb', line 141

def update(args)
  options = {}
  params = {}
  ip_range_list = nil
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[network server] [options]")
    opts.on('--name VALUE', String, "Name for this network server") do |val|
      params['name'] = val
    end
    # opts.on('--enabled [on|off]', String, "Can be used to enable or disable the network server") do |val|
    #   params['enabled'] = val.to_s == 'on' || val.to_s == 'true' || val.to_s.empty?
    # end
    # ['name', 'serviceUsername', 'servicePassword', 'servicePort', 'serviceHost', 'serviceUrl', 'serviceMode', 'networkFilter', 'tenantMatch']
    #build_option_type_options(opts, options, update_network_server_option_types)
    build_option_type_options(opts, options, update_network_server_advanced_option_types)
    build_standard_update_options(opts, options)
    opts.footer = <<-EOT
Update a network server.
[network server] is required. This is the name or id of a network server.
Configuration options vary by network server type.
EOT
  end
  optparse.parse!(args)
  verify_args!(args:args, optparse:optparse, count: 1)
  connect(options)
  # find network server to be updated
  network_server = find_network_server_by_name_or_id(args[0])
  return 1 if network_server.nil?
  
  # merge -O options into normally parsed options
  params.deep_merge!(options[:options].reject {|k,v| k.is_a?(Symbol) }) if options[:options]

  # params['tenants'] = options['tenants'].collect {|it| {'id': it}}
  # advanced options
  advanced_option_types = update_network_server_advanced_option_types
  if advanced_option_types && !advanced_option_types.empty?
    v_prompt = Morpheus::Cli::OptionTypes.no_prompt(advanced_option_types, options[:options], @api_client, {})
    v_prompt.deep_compact!
    v_prompt.booleanize! # 'on' => true
    params.deep_merge!(v_prompt)
  end

  # construct payload
  payload = nil
  if options[:payload]
    payload = options[:payload]
  else
    payload = {
      'networkServer' => params
    }
  end
  @network_servers_interface.setopts(options)
  if options[:dry_run]
    print_dry_run @network_servers_interface.dry.update(network_server["id"], payload)
    return
  end
  json_response = @network_servers_interface.update(network_server["id"], payload)
  render_response(json_response, options, 'networkServer') do
    network_server = json_response['networkServer']
    print_green_success "Updated network server #{network_server['name']}"
    _get(network_server['id'], {}, options)
  end
end