Class: Morpheus::Cli::NetworkDhcpRelaysCommand

Inherits:
Object
  • Object
show all
Includes:
CliCommand, NetworksHelper, ProvisioningHelper, WhoamiHelper
Defined in:
lib/morpheus/cli/commands/network_dhcp_relays_command.rb

Instance Attribute Summary

Attributes included from CliCommand

#no_prompt

Instance Method Summary collapse

Methods included from WhoamiHelper

#current_account, #current_user, #current_user_permissions, included, #is_master_account, #load_whoami

Methods included from ProvisioningHelper

#accounts_interface, #add_perms_options, #api_client, #apps_interface, #cloud_datastores_interface, #clouds_interface, #datastores_interface, #find_app_by_id, #find_app_by_name, #find_app_by_name_or_id, #find_cloud_by_id_for_provisioning, #find_cloud_by_name_for_provisioning, #find_cloud_by_name_or_id_for_provisioning, #find_cloud_resource_pool_by_name_or_id, #find_group_by_id_for_provisioning, #find_group_by_name_for_provisioning, #find_group_by_name_or_id_for_provisioning, #find_host_by_id, #find_host_by_name, #find_host_by_name_or_id, #find_instance_by_id, #find_instance_by_name, #find_instance_by_name_or_id, #find_instance_type_by_code, #find_instance_type_by_id, #find_instance_type_by_name, #find_instance_type_by_name_or_id, #find_instance_type_layout_by_id, #find_server_by_id, #find_server_by_name, #find_server_by_name_or_id, #find_workflow_by_id, #find_workflow_by_name, #find_workflow_by_name_or_id, #format_app_status, #format_blueprint_type, #format_container_connection_string, #format_container_status, #format_instance_connection_string, #format_instance_container_display_name, #format_instance_status, #format_metadata, #format_snapshot_status, #get_available_accounts, #get_available_clouds, #get_available_environments, #get_available_groups, #get_available_plans, #get_provision_type_for_zone_type, #get_static_environments, included, #instance_type_layouts_interface, #instance_types_interface, #instances_interface, #load_balance_protocols_dropdown, #options_interface, #parse_blueprint_type, #parse_host_id_list, #parse_instance_id_list, #parse_metadata, #parse_resource_id_list, #parse_server_id_list, #print_permissions, #prompt_evars, #prompt_exposed_ports, #prompt_instance_load_balancer, #prompt_metadata, #prompt_network_interfaces, #prompt_new_instance, #prompt_permissions, #prompt_permissions_v2, #prompt_resize_volumes, #prompt_security_groups, #prompt_service_plan_options, #prompt_volumes, #provision_types_interface, #reject_service_plan_option_types, #servers_interface

Methods included from NetworksHelper

#find_network_server, #find_network_server_by_id, #find_network_server_by_name, included, #network_servers_interface

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

#_get(server, dhcp_relay_id, options) ⇒ Object



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
# File 'lib/morpheus/cli/commands/network_dhcp_relays_command.rb', line 92

def _get(server, dhcp_relay_id, options)
  # params = parse_query_options(options) # todo: use this
  @network_dhcp_relays_interface.setopts(options)

  if options[:dry_run]
    if dhcp_relay_id.to_s =~ /\A\d{1,}\Z/
      print_dry_run @network_dhcp_relays_interface.dry.get_dhcp_relay(server['id'], dhcp_relay_id.to_i)
    else
      print_dry_run @network_dhcp_relays_interface.dry.list_dhcp_relays(server['id'], {name: dhcp_relay_id})
    end
    return
  end

  if server['type']['hasDhcpRelays']
    dhcpRelay = find_dhcp_relay(server['id'], dhcp_relay_id)
    return 1 if dhcpRelay.nil?

    render_response({networkDhcpRelay: dhcpRelay}, options, 'networkDhcpRelay') do
      print_h1 "Network DHCP Relay Details"
      print cyan

      description_cols = {
        "ID" => lambda {|it| it['id']},
        "Name" => lambda {|it| it['name']}
      }

      server['type']['dhcpRelayOptionTypes'].sort_by {|it| it['displayOrder']}.each do |option_type|
        description_cols[option_type['fieldLabel']] = lambda {|it| Morpheus::Cli::OptionTypes.get_option_value(it, option_type, true)}
      end
      print_description_list(description_cols, dhcpRelay)
    end
  else
    print_red_alert "DHCP Relays not supported for #{server['type']['name']}"
  end
  println reset
end

#_list(server, options) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/morpheus/cli/commands/network_dhcp_relays_command.rb', line 48

def _list(server, options)
  params = parse_list_options(options)
  @network_dhcp_relays_interface.setopts(options)
  if options[:dry_run]
    print_dry_run @network_dhcp_relays_interface.dry.list_dhcp_relays(server['id'], params)
    return
  end

  if server['type']['hasDhcpRelays']
    json_response = @network_dhcp_relays_interface.list_dhcp_relays(server['id'], params)
    render_response(json_response, options, 'networkDhcpRelays') do
      print_h1 "Network DHCP Relays For: #{server['name']}"
      print_dhcp_relays(server, json_response)
    end
    return 0, nil
  else
    print_red_alert "DHCP Relays not supported for #{server['type']['name']}"
    return 1, "DHCP Relays not supported for #{server['type']['name']}"
  end
end

#add(args) ⇒ Object



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
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
# File 'lib/morpheus/cli/commands/network_dhcp_relays_command.rb', line 129

def add(args)
  options = {:options=>{}}
  params = {}
  optparse = Morpheus::Cli::OptionParser.new do|opts|
    opts.banner = subcommand_usage("[server]")
    build_standard_add_options(opts, options)
    opts.footer = "Create a network dhcp relay." + "\n" +
      "[server] is required. This is the name or id of a network server.\n";
  end
  optparse.parse!(args)
  connect(options)
  verify_args!(args:args, optparse:optparse, count:1)

  server = find_network_server(args[0])
  if server.nil?
    return 1
  end

  if !server['type']['hasDhcpRelays']
    print_red_alert "DHCP Relays not supported for #{server['type']['name']}"
    return 1
  end

  payload = nil
  if options[:payload]
    payload = options[:payload]
  else
    option_types = server['type']['dhcpRelayOptionTypes'].sort_by {|it| it['displayOrder']}
    # prompt options
    option_result = Morpheus::Cli::OptionTypes.prompt(option_types, options[:options].deep_merge({:context_map => {'networkDhcpRelay' => ''}}), @api_client, {'networkServerId' => server['id']}, nil, true)
    payload = {'networkDhcpRelay' => params.deep_merge(option_result)}
    # copy all domain level fields to networkDhcpRelay
    if payload['networkDhcpRelay']['domain']
      payload['networkDhcpRelay']['domain'].each do |k,v|
        payload['networkDhcpRelay'][k] = v
      end
    end
    payload['networkDhcpRelay'].delete('domain')

  end

  @network_dhcp_relays_interface.setopts(options)

  serverIpAddresses = payload['networkDhcpRelay']['serverIpAddresses']
	if !serverIpAddresses.nil?
 if !serverIpAddresses.kind_of?(Array)
   payload['networkDhcpRelay']['serverIpAddresses'] = serverIpAddresses.to_s.split(',')
 end
  end

  if options[:dry_run]
    print_dry_run @network_dhcp_relays_interface.dry.create_dhcp_relay(server['id'], payload)
    return
  end

  json_response = @network_dhcp_relays_interface.create_dhcp_relay(server['id'], payload)
  render_response(json_response, options, 'networkDhcpRelay') do
    print_green_success "\nAdded Network DHCP Relay #{json_response['id']}\n"
    _get(server, json_response['id'], options)
  end
end

#connect(opts) ⇒ Object



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

def connect(opts)
  @api_client = establish_remote_appliance_connection(opts)
  @account_interface = @api_client.accounts
  @network_dhcp_relays_interface = @api_client.network_dhcp_relays
  @network_servers_interface = @api_client.network_servers
  @options_interface = @api_client.options
end

#get(args) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/morpheus/cli/commands/network_dhcp_relays_command.rb', line 69

def get(args)
  options = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[server] [dhcp_relay]")
    build_standard_get_options(opts, options)
    opts.footer = "Display details on a network DHCP Relay." + "\n" +
      "[server] is required. This is the name or id of a network server.\n" +
      "[dhcp_relay] is required. This is the id of a network DHCP Relay.\n"
  end

  optparse.parse!(args)
  connect(options)

  verify_args!(args:args, optparse:optparse, count:2)

  server = find_network_server(args[0])
  if server.nil?
    return 1
  end

  _get(server, args[1], options)
end

#handle(args) ⇒ Object



20
21
22
# File 'lib/morpheus/cli/commands/network_dhcp_relays_command.rb', line 20

def handle(args)
  handle_subcommand(args)
end

#list(args) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/morpheus/cli/commands/network_dhcp_relays_command.rb', line 24

def list(args)
  options = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[server] [search]")
    build_standard_list_options(opts, options)
    opts.footer = "List network DHCP Relays." + "\n" +
      "[server] is required. This is the name or id of a network server."
  end

  optparse.parse!(args)
  connect(options)

  verify_args!(args:args, optparse:optparse, min:1)
  if args.count > 1
    options[:phrase] = args[1..-1].join(" ")
  end
  server = find_network_server(args[0])
  if server.nil?
    return 1
  end

  _list(server, options)
end

#remove(args) ⇒ Object



261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'lib/morpheus/cli/commands/network_dhcp_relays_command.rb', line 261

def remove(args)
  options = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[server] [dhcp_relay]")
    build_standard_remove_options(opts, options)
    opts.footer = "Delete a network dhcp relay.\n" +
      "[server] is required. This is the name or id of an existing network server.\n" +
      "[dhcp_relay] is required. This is the name or id of an existing network dhcp relay."
  end
  optparse.parse!(args)
  verify_args!(args:args, optparse:optparse, count:2)
  connect(options)

  server = find_network_server(args[0])
  if server.nil?
    return 1
  end

  if !server['type']['hasDhcpRelays']
    print_red_alert "DHCP Relays not supported for #{server['type']['name']}"
    return 1
  end

  dhcpRelay = find_dhcp_relay(server['id'], args[1])
  return 1 if dhcpRelay.nil?

  unless options[:yes] || ::Morpheus::Cli::OptionTypes::confirm("Are you sure you would like to remove the network dhcp relay '#{dhcpRelay['name']}' from server '#{server['name']}'?", options)
    return 9, "aborted command"
  end

  @network_dhcp_relays_interface.setopts(options)

  if options[:dry_run]
    print_dry_run @network_dhcp_relays_interface.dry.delete_dhcp_relay(server['id'], dhcpRelay['id'])
    return
  end
  json_response = @network_dhcp_relays_interface.delete_dhcp_relay(server['id'], dhcpRelay['id'])
  render_response(json_response, options, 'networkDhcpRelay') do
    print_green_success "\nDeleted Network DHCP Relay #{dhcpRelay['name']}\n"
    _list(server, options)
  end
end

#update(args) ⇒ Object



191
192
193
194
195
196
197
198
199
200
201
202
203
204
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/morpheus/cli/commands/network_dhcp_relays_command.rb', line 191

def update(args)
  options = {:options=>{}}
  params = {}
  optparse = Morpheus::Cli::OptionParser.new do|opts|
    opts.banner = subcommand_usage("[server] [dhcp_relay]")
    build_standard_update_options(opts, options)
    opts.footer = "Update a network DHCP Relay.\n" +
      "[server] is required. This is the name or id of an existing network server.\n" +
      "[dhcp_relay] is required. This is the name or id of an existing network DHCP Relay."
  end
  optparse.parse!(args)
  verify_args!(args:args, optparse:optparse, count:2)
  connect(options)

  server = find_network_server(args[0])
  if server.nil?
    return 1
  end

  if !server['type']['hasDhcpRelays']
    print_red_alert "DHCP Relays not supported for #{server['type']['name']}"
    return 1
  end

  dhcpRelay = find_dhcp_relay(server['id'], args[1])
  return 1 if dhcpRelay.nil?

  payload = parse_payload(options) || {'networkDhcpRelay' => params}
  payload['networkDhcpRelay'].deep_merge!(options[:options].reject {|k,v| k.is_a?(Symbol) }) if options[:options] && !payload['networkDhcpRelay'].nil?

  if payload['networkDhcpRelay'].empty?
    option_types = server['type']['dhcpRelayOptionTypes'].sort_by {|it| it['displayOrder']}
    print_green_success "Nothing to update"
    println cyan
    print Morpheus::Cli::OptionTypes.display_option_types_help(
      option_types,
      {:include_context => true, :context_map => {'networkDhcpRelay' => ''}, :color => cyan, :title => "Available DHCP Relay Options"}
    )
    exit 1
  end

	# copy all domain level fields to networkDhcpRelay
	if payload['networkDhcpRelay']['domain']
    payload['networkDhcpRelay']['domain'].each do |k,v|
      payload['networkDhcpRelay'][k] = v
    end
    payload['networkDhcpRelay'].delete('domain')
  end

  @network_dhcp_relays_interface.setopts(options)

	serverIpAddresses = payload['networkDhcpRelay']['serverIpAddresses']
	if !serverIpAddresses.nil?
 if !serverIpAddresses.kind_of?(Array)
   payload['networkDhcpRelay']['serverIpAddresses'] = serverIpAddresses.to_s.split(',')
 end
  end

  if options[:dry_run]
    print_dry_run @network_dhcp_relays_interface.dry.update_dhcp_relay(server['id'], dhcpRelay['id'], payload)
    return
  end

  json_response = @network_dhcp_relays_interface.update_dhcp_relay(server['id'], dhcpRelay['id'], payload)
  render_response(json_response, options, 'networkDhcpRelay') do
    print_green_success "\nUpdated Network DHCP Relay #{dhcpRelay['id']}\n"
    _get(server, dhcpRelay['id'], options)
  end
end