Class: Morpheus::Cli::NetworkFirewallsCommand
Instance Attribute Summary
Attributes included from CliCommand
#no_prompt
Instance Method Summary
collapse
#current_account, #current_user, #current_user_permissions, included, #is_master_account, #load_whoami
#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
#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_rule(server, name_or_id, options) ⇒ Object
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
|
# File 'lib/morpheus/cli/commands/network_firewalls_command.rb', line 132
def _get_rule(server, name_or_id, options)
@network_servers_interface.setopts(options)
if options[:dry_run]
if name_or_id.to_s =~ /\A\d{1,}\Z/
print_dry_run @network_servers_interface.dry.get_firewall_rule(server['id'], name_or_id.to_i)
else
print_dry_run @network_servers_interface.dry.get_firewall_rule(server['id'], {name: name_or_id})
end
return
end
if server['type']['hasFirewall']
rule = find_rule(server['id'], name_or_id)
return 1 if rule.nil?
render_response({rule: rule}, options, 'rule') do
print_h1 "Network Firewall Rule Details"
print cyan
description_cols = {
"ID" => lambda {|it| it['id']},
"Name" => lambda {|it| it['name']},
"Description" => lambda {|it| it['description']},
"Enabled" => lambda {|it| format_boolean(it['enabled'])},
"Priority" => lambda {|it| it['priority']}
}
server['type']['ruleOptionTypes'].reject {|it| it['type'] == 'hidden'}.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, rule)
end
else
print_red_alert "Firewall not supported for #{server['type']['name']}"
end
println reset
end
|
#_get_rule_group(server, name_or_id, options) ⇒ Object
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
|
# File 'lib/morpheus/cli/commands/network_firewalls_command.rb', line 456
def _get_rule_group(server, name_or_id, options)
@network_servers_interface.setopts(options)
if options[:dry_run]
if name_or_id.to_s =~ /\A\d{1,}\Z/
print_dry_run @network_servers_interface.dry.get_firewall_rule_group(server['id'], name_or_id.to_i)
else
print_dry_run @network_servers_interface.dry.list_firewall_rule_groups(server['id'], {name: name_or_id})
end
return
end
if server['type']['hasFirewallGroups']
group = find_rule_group(server['id'], name_or_id)
return 1 if group.nil?
render_response({ruleGroup: group}, options, 'ruleGroup') do
print_h1 "Network Firewall Rule Group Details"
print cyan
description_cols = {
"ID" => lambda {|it| it['id']},
"Name" => lambda {|it| it['name']},
"Description" => lambda {|it| it['description']},
"Priority" => lambda {|it| it['priority']},
"Category" => lambda {|it| it['groupLayer']}
}
server['type']['firewallGroupOptionTypes'].reject {|it| it['type'] == 'hidden'}.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
if is_master_account
description_cols["Visibility"] = lambda {|it| it['visibility']}
description_cols["Tenants"] = lambda {|it| it['tenants'].collect {|tenant| tenant['name']}.join(', ')}
end
print_description_list(description_cols, group)
end
else
print_red_alert "Network firewall rule groups not supported for #{server['type']['name']}"
end
println reset
end
|
#_list_rule_groups(server, options) ⇒ Object
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
|
# File 'lib/morpheus/cli/commands/network_firewalls_command.rb', line 405
def _list_rule_groups(server, options)
params = parse_list_options(options)
@network_servers_interface.setopts(options)
if options[:dry_run]
print_dry_run @network_servers_interface.dry.list_firewall_rule_groups(server['id'], params)
return
end
if server['type']['hasFirewallGroups']
json_response = @network_servers_interface.list_firewall_rule_groups(server['id'], params)
render_response(json_response, options, 'ruleGroups') do
print_h1 "#{server['type']['titleFirewallGroups'] || 'Network firewall rule groups'} For: #{server['name']}"
print cyan
puts as_pretty_table(json_response['ruleGroups'].collect {|it|
{id: it['id'], name: it['name'], description: it['description'], priority: it['priority'], category: it['groupLayer']}
}, [:id, :name, :description, :priority, :category])
end
else
print_red_alert "#{server['type']['titleFirewallGroups'] || 'Network firewall rule groups'} not supported for #{server['type']['name']}"
end
print reset
end
|
#_list_rules(server, options) ⇒ Object
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
|
# File 'lib/morpheus/cli/commands/network_firewalls_command.rb', line 49
def _list_rules(server, options)
params = parse_list_options(options)
@network_servers_interface.setopts(options)
if options[:dry_run]
print_dry_run @network_servers_interface.dry.list_firewall_rules(server['id'], params)
return
end
json_response = @network_servers_interface.list_firewall_rules(server['id'], params)
render_response(json_response, options, 'rules') do
print_h1 "Network Firewall Rules For: #{server['name']}"
print cyan
app_title = server['type']['titleFirewallApplications'].tr(' ', '_').downcase
rows = json_response['rules'].collect {|it|
row = {
id: it['id'], group: it['groupName'], name: it['name'], description: it['description'],
priority: it['priority'], enabled: format_boolean(it['enabled']), policy: it['policy'], direction: it['direction'],
source: it['sources'].kind_of?(Array) && it['sources'].count > 0 ? it['sources'].collect {|it| it['name']}.join(', ') : (it['sources'].nil? || it['sources'].empty? ? 'any' : it['source']),
destination: it['destinations'].kind_of?(Array) && it['destinations'].count > 0 ? it['destinations'].collect {|it| it['name']}.join(', ') : (it['destinations'].nil? || it['destinations'].empty? ? 'any' : it['destination'])
}
if it['applications'].count
row[app_title] = it['applications'].slice(0, 2).collect {|it| it['name']}.join(', ') + (it['applications'].count > 2 ? '... ' : ' ')
end
if it['protocal'] || it['portRange']
row[app_title] += "#{(it['protocol'] || 'any')} #{it['portRange'] || ''}"
end
row[app_title] = 'Any' if it['applications'].count == 0 && row['protocol'].nil? && row['portRange'].nil?
applied_to = []
if server['type']['supportsFirewallRuleAppliedTarget']
applied_to << 'All Edges' if it['config']['applyToAllEdges']
applied_to << 'Distributed Firewall' if it['config']['applyToAllDistributed']
applied_to += it['appliedTargets'].collect {|target| target['name']}
row[:applied_to] = applied_to.join(', ')
end
row
}
cols = [:id]
if server['type']['hasFirewallGroups']
cols += [:group]
end
cols += [:name, :description]
cols += [:priority] if server['type']['hasSecurityGroupRulePriority']
cols += [:applied_to] if server['type']['supportsFirewallRuleAppliedTarget']
cols += [:enabled, :policy, :direction, :source, :destination, app_title.to_sym]
puts as_pretty_table(rows, cols)
end
print reset
end
|
#add_rule(args) ⇒ Object
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
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
260
261
|
# File 'lib/morpheus/cli/commands/network_firewalls_command.rb', line 172
def add_rule(args)
options = {:options=>{}}
params = {}
optparse = Morpheus::Cli::OptionParser.new do|opts|
opts.banner = subcommand_usage("[server] [group]")
opts.on( '-g', '--group GROUP', String, "Rule group name or ID" ) do |val|
options[:group] = val
end
opts.on('-n', '--name VALUE', String, "Name for this firewall rule") do |val|
options[:options]['name'] = val
end
opts.on('-D', '--description VALUE', String, "Description for this firewall rule") do |val|
options[:options]['description'] = val
end
opts.on('--priority VALUE', Integer, "Priority for this firewall rule") do |val|
options[:options]['priority'] = val
end
opts.on('--enabled [on|off]', String, "Can be used to disable") do |val|
options[:options]['enabled'] = val.to_s == 'on' || val.to_s == 'true' || val.to_s.empty?
end
build_common_options(opts, options, [:options, :payload, :json, :dry_run, :remote])
opts. = "Create a network firewall rule." + "\n" +
"[server] is optional. This is the name or id of a network server.\n" +
"[group] is optional. This is the name of id of rule group (applicable to select network servers)."
end
optparse.parse!(args)
connect(options)
if args.count > 2
print_error Morpheus::Terminal.angry_prompt
puts_error "wrong number of arguments, expected 0-2 and got (#{args.count}) #{args.inspect}\n#{optparse}"
return 1
end
server_id = args.count > 0 ? args[0] : Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'networkServer', 'type' => 'select', 'fieldLabel' => 'Network Server', 'selectOptions' => search_network_servers.collect {|it| {'name' => it['name'], 'value' => it['id']}}, 'required' => true, 'description' => 'Select Network Server.'}],options[:options],@api_client,{})['networkServer']
server = find_network_server(server_id)
return 1 if server.nil?
options[:group] = args[1] if options[:group].nil? && args.count == 2
if !server['type']['hasFirewall']
print_red_alert "Firewall not supported for #{server['type']['name']}"
return 1
end
if options[:payload]
payload = options[:payload]
else
if server['type']['hasFirewallGroups']
if !options[:group].nil?
group = find_rule_group(server['id'], options[:group])
if group.nil?
return 1
end
group_id = group['id']
else
avail_groups = @network_servers_interface.list_firewall_rule_groups(server['id'])['ruleGroups'].collect {|it| {'name' => it['name'], 'value' => it['id']}}
group_id = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'ruleGroup', 'type' => 'select', 'fieldLabel' => 'Rule Group', 'selectOptions' => avail_groups, 'required' => true, 'description' => 'Select Rule Group.'}],options[:options],@api_client,{})['ruleGroup']
end
params['ruleGroup'] = {'id' => group_id}
end
params['name'] = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'name', 'type' => 'text', 'fieldLabel' => 'Name', 'required' => true, 'description' => 'Name.'}],options[:options],@api_client,{})['name']
params['description'] = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'description', 'type' => 'text', 'fieldLabel' => 'Description', 'required' => false, 'description' => 'Description.'}],options[:options],@api_client,{})['description']
params['enabled'] = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'enabled', 'fieldLabel' => 'Enabled', 'type' => 'checkbox', 'description' => 'Enable Router.', 'defaultValue' => true, 'required' => false}], options[:options], @api_client, {})['enabled'] == 'on'
if server['type']['hasSecurityGroupRulePriority']
params['priority'] = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'priority', 'type' => 'number', 'fieldLabel' => 'Priority', 'required' => false, 'description' => 'Priority.'}],options[:options],@api_client,{})['priority']
end
option_types = server['type']['ruleOptionTypes'].sort_by {|it| it['displayOrder']}
option_result = Morpheus::Cli::OptionTypes.prompt(option_types, options[:options].deep_merge({:context_map => {'rule' => ''}}), @api_client, {'networkServerId' => server['id']}, nil, true)
payload = {'rule' => params.deep_merge(option_result)}
end
@network_servers_interface.setopts(options)
if options[:dry_run]
print_dry_run @network_servers_interface.dry.create_firewall_rule(server['id'], payload)
return
end
json_response = @network_servers_interface.create_firewall_rule(server['id'], payload)
render_response(json_response, options, 'rule') do
print_green_success "\nAdded Network Firewall Rule #{json_response['id']}\n"
_get_rule(server, json_response['id'], options)
end
end
|
#add_rule_group(args) ⇒ Object
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
|
# File 'lib/morpheus/cli/commands/network_firewalls_command.rb', line 500
def add_rule_group(args)
options = {:options=>{}}
params = {}
optparse = Morpheus::Cli::OptionParser.new do|opts|
opts.banner = subcommand_usage("[server]")
opts.on('-n', '--name VALUE', String, "Name for this firewall rule group") do |val|
options[:options]['name'] = val
end
opts.on('-D', '--description VALUE', String, "Description for this firewall rule group") do |val|
options[:options]['description'] = val
end
opts.on('--priority VALUE', Integer, "Priority for this firewall rule group") do |val|
options[:options]['priority'] = val
end
add_perms_options(opts, options, ['plans', 'groups'])
build_common_options(opts, options, [:options, :payload, :json, :dry_run, :remote])
opts. = "Create a network firewall rule group." + "\n" +
"[server] is optional. This is the name or id of a network server.\n";
end
optparse.parse!(args)
connect(options)
if args.count > 1
print_error Morpheus::Terminal.angry_prompt
puts_error "wrong number of arguments, expected 0-1 and got (#{args.count}) #{args.inspect}\n#{optparse}"
return 1
end
server_id = args.count > 0 ? args[0] : Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'networkServer', 'type' => 'select', 'fieldLabel' => 'Network Server', 'selectOptions' => search_network_servers.collect {|it| {'name' => it['name'], 'value' => it['id']}}, 'required' => true, 'description' => 'Select Network Server.'}],options[:options],@api_client,{})['networkServer']
server = find_network_server(server_id)
return 1 if server.nil?
if !server['type']['hasFirewallGroups']
print_red_alert "Firewall rule groups not supported for #{server['type']['name']}"
return 1
end
if options[:payload]
payload = options[:payload]
else
params['name'] = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'name', 'type' => 'text', 'fieldLabel' => 'Name', 'required' => true, 'description' => 'Name.'}],options[:options],@api_client, {})['name']
params['description'] = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'description', 'type' => 'text', 'fieldLabel' => 'Description', 'required' => false, 'description' => 'Description.'}],options[:options],@api_client,{})['description']
params['priority'] = Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'priority', 'type' => 'number', 'fieldLabel' => 'Priority', 'required' => false, 'description' => 'Priority.'}],options[:options],@api_client,{})['priority']
option_types = server['type']['firewallGroupOptionTypes'].sort_by {|it| it['displayOrder']}
option_result = Morpheus::Cli::OptionTypes.prompt(option_types, options[:options].deep_merge({:context_map => {'ruleGroup' => ''}}), @api_client, {'networkServerId' => server['id']}, nil, true)
if is_master_account
params.merge!(prompt_permissions_v2(options, ['plans', 'groups']))
end
payload = {'ruleGroup' => params.deep_merge(option_result)}
end
@network_servers_interface.setopts(options)
if options[:dry_run]
print_dry_run @network_servers_interface.dry.create_firewall_rule_group(server['id'], payload)
return
end
json_response = @network_servers_interface.create_firewall_rule_group(server['id'], payload)
render_response(json_response, options, 'ruleGroup') do
print_green_success "\nAdded Network Firewall Rule Group #{json_response['id']}\n"
_get_rule_group(server, json_response['id'], options)
end
end
|
#connect(opts) ⇒ Object
13
14
15
16
17
18
|
# File 'lib/morpheus/cli/commands/network_firewalls_command.rb', line 13
def connect(opts)
@api_client = establish_remote_appliance_connection(opts)
@account_interface = @api_client.accounts
@network_servers_interface = @api_client.network_servers
@options_interface = @api_client.options
end
|
#get_rule(args) ⇒ Object
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
|
# File 'lib/morpheus/cli/commands/network_firewalls_command.rb', line 105
def get_rule(args)
options = {}
optparse = Morpheus::Cli::OptionParser.new do |opts|
opts.banner = subcommand_usage("[server] [rule]")
build_common_options(opts, options, [:json, :yaml, :csv, :fields, :dry_run, :remote])
opts. = "Display details on a network firewall rule." + "\n" +
"[server] is optional. This is the name or id of a network server.\n" +
"[rule] is optional. This is the id of a network firewall rule.\n"
end
optparse.parse!(args)
connect(options)
if args.count > 2
print_error Morpheus::Terminal.angry_prompt
puts_error "wrong number of arguments, expected 0-2 and got (#{args.count}) #{args.inspect}\n#{optparse}"
return 1
end
server_id = args.count > 0 ? args[0] : Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'networkServer', 'type' => 'select', 'fieldLabel' => 'Network Server', 'selectOptions' => search_network_servers.collect {|it| {'name' => it['name'], 'value' => it['id']}}, 'required' => true, 'description' => 'Select Network Server.'}],options[:options],@api_client,{})['networkServer']
server = find_network_server(server_id)
return 1 if server.nil?
rule_id = args.count > 1 ? args[1] : Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'rule', 'type' => 'select', 'fieldLabel' => 'Firewall Rule', 'selectOptions' => search_rules(server['id']).collect {|it| {'name' => it['name'], 'value' => it['id']}}, 'required' => true, 'description' => 'Select Firewall Rule.'}],options[:options],@api_client,{})['rule']
_get_rule(server, rule_id, options)
end
|
#get_rule_group(args) ⇒ Object
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
|
# File 'lib/morpheus/cli/commands/network_firewalls_command.rb', line 429
def get_rule_group(args)
options = {}
optparse = Morpheus::Cli::OptionParser.new do |opts|
opts.banner = subcommand_usage("[server] [group]")
build_common_options(opts, options, [:json, :yaml, :csv, :fields, :dry_run, :remote])
opts. = "Display details on a network firewall rule group." + "\n" +
"[server] is optional. This is the name or id of a network server.\n" +
"[group] is optional. This is the id of a network firewall rule group.\n"
end
optparse.parse!(args)
connect(options)
if args.count > 2
print_error Morpheus::Terminal.angry_prompt
puts_error "wrong number of arguments, expected 0-2 and got (#{args.count}) #{args.inspect}\n#{optparse}"
return 1
end
server_id = args.count > 0 ? args[0] : Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'networkServer', 'type' => 'select', 'fieldLabel' => 'Network Server', 'selectOptions' => search_network_servers.collect {|it| {'name' => it['name'], 'value' => it['id']}}, 'required' => true, 'description' => 'Select Network Server.'}],options[:options],@api_client,{})['networkServer']
server = find_network_server(server_id)
return 1 if server.nil?
group_id = args.count > 1 ? args[1] : Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'ruleGroup', 'type' => 'select', 'fieldLabel' => 'Firewall Rule Group', 'selectOptions' => search_rule_groups(server['id']).collect {|it| {'name' => it['name'], 'value' => it['id']}}, 'required' => true, 'description' => 'Select Firewall Rule Group.'}],options[:options],@api_client,{})['ruleGroup']
_get_rule_group(server, group_id, options)
end
|
#handle(args) ⇒ Object
20
21
22
|
# File 'lib/morpheus/cli/commands/network_firewalls_command.rb', line 20
def handle(args)
handle_subcommand(args)
end
|
#list_rule_groups(args) ⇒ Object
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
|
# File 'lib/morpheus/cli/commands/network_firewalls_command.rb', line 381
def list_rule_groups(args)
options = {}
optparse = Morpheus::Cli::OptionParser.new do |opts|
opts.banner = subcommand_usage("[server]")
build_common_options(opts, options, [:list, :query, :json, :yaml, :csv, :fields, :dry_run, :remote])
opts. = "List network firewall rule groups." + "\n" +
"[server] is required. This is the name or id of a network server."
end
optparse.parse!(args)
connect(options)
if args.count > 1
print_error Morpheus::Terminal.angry_prompt
puts_error "wrong number of arguments, expected 0-1 and got (#{args.count}) #{args.inspect}\n#{optparse}"
return 1
end
server_id = args.count > 0 ? args[0] : Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'networkServer', 'type' => 'select', 'fieldLabel' => 'Network Server', 'selectOptions' => search_network_servers.collect {|it| {'name' => it['name'], 'value' => it['id']}}, 'required' => true, 'description' => 'Select Network Server.'}],options[:options],@api_client,{})['networkServer']
server = find_network_server(server_id)
return 1 if server.nil?
_list_rule_groups(server, options)
end
|
#list_rules(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
47
|
# File 'lib/morpheus/cli/commands/network_firewalls_command.rb', line 24
def list_rules(args)
options = {}
optparse = Morpheus::Cli::OptionParser.new do |opts|
opts.banner = subcommand_usage("[server]")
build_common_options(opts, options, [:list, :query, :json, :yaml, :csv, :fields, :dry_run, :remote])
opts. = "List network firewall rules." + "\n" +
"[server] is optional. This is the name or id of a network server."
end
optparse.parse!(args)
connect(options)
if args.count > 1
print_error Morpheus::Terminal.angry_prompt
puts_error "wrong number of arguments, expected 0-1 and got (#{args.count}) #{args.inspect}\n#{optparse}"
return 1
end
server_id = args.count > 0 ? args[0] : Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'networkServer', 'type' => 'select', 'fieldLabel' => 'Network Server', 'selectOptions' => search_network_servers.collect {|it| {'name' => it['name'], 'value' => it['id']}}, 'required' => true, 'description' => 'Select Network Server.'}],options[:options],@api_client,{})['networkServer']
server = find_network_server(server_id)
return 1 if server.nil?
_list_rules(server, options)
end
|
#remove_rule(args) ⇒ Object
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
|
# File 'lib/morpheus/cli/commands/network_firewalls_command.rb', line 336
def remove_rule(args)
options = {}
optparse = Morpheus::Cli::OptionParser.new do |opts|
opts.banner = subcommand_usage("[server] [rule]")
build_common_options(opts, options, [:auto_confirm, :json, :dry_run, :quiet, :remote])
opts. = "Delete a network firewall rule.\n" +
"[server] is optional. This is the name or id of a network server.\n" +
"[rule] is optional. This is the name of id of an existing rule."
end
optparse.parse!(args)
if args.count > 2
raise_command_error "wrong number of arguments, expected 0-2 and got (#{args.count}) #{args}\n#{optparse}"
end
connect(options)
server_id = args.count > 0 ? args[0] : Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'networkServer', 'type' => 'select', 'fieldLabel' => 'Network Server', 'selectOptions' => search_network_servers.collect {|it| {'name' => it['name'], 'value' => it['id']}}, 'required' => true, 'description' => 'Select Network Server.'}],options[:options],@api_client,{})['networkServer']
server = find_network_server(server_id)
return 1 if server.nil?
if !server['type']['hasFirewall']
print_red_alert "Firewall not supported for #{server['type']['name']}"
return 1
end
rule_id = args.count > 1 ? args[1] : Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'rule', 'type' => 'select', 'fieldLabel' => 'Firewall Rule', 'selectOptions' => search_rules(server['id']).collect {|it| {'name' => it['name'], 'value' => it['id']}}, 'required' => true, 'description' => 'Select Firewall Rule.'}],options[:options],@api_client,{})['rule']
rule = find_rule(server['id'], rule_id)
return 1 if rule.nil?
unless options[:yes] || ::Morpheus::Cli::OptionTypes::confirm("Are you sure you would like to remove the network firewall rule '#{rule['name']}' from server '#{server['name']}'?", options)
return 9, "aborted command"
end
@network_servers_interface.setopts(options)
if options[:dry_run]
print_dry_run @network_servers_interface.dry.destroy_firewall_rule(server['id'], rule['id'])
return
end
json_response = @network_servers_interface.destroy_firewall_rule(server['id'], rule['id'])
render_response(json_response, options, 'rule') do
print_green_success "\nDeleted Network Firewall Rule #{rule['name']}\n"
_list_rules(server, options)
end
end
|
#remove_rule_group(args) ⇒ Object
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
|
# File 'lib/morpheus/cli/commands/network_firewalls_command.rb', line 642
def remove_rule_group(args)
options = {}
optparse = Morpheus::Cli::OptionParser.new do |opts|
opts.banner = subcommand_usage("[server] [group]")
build_common_options(opts, options, [:auto_confirm, :json, :dry_run, :quiet, :remote])
opts. = "Delete a network firewall group.\n" +
"[server] is optional. This is the name or id of an existing network server.\n" +
"[group] is optional. This is the name or id of an existing network firewall rule group."
end
optparse.parse!(args)
if args.count > 2
raise_command_error "wrong number of arguments, expected 0-2 and got (#{args.count}) #{args}\n#{optparse}"
end
connect(options)
server_id = args.count > 0 ? args[0] : Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'networkServer', 'type' => 'select', 'fieldLabel' => 'Network Server', 'selectOptions' => search_network_servers.collect {|it| {'name' => it['name'], 'value' => it['id']}}, 'required' => true, 'description' => 'Select Network Server.'}],options[:options],@api_client,{})['networkServer']
server = find_network_server(server_id)
return 1 if server.nil?
if !server['type']['hasFirewallGroups']
print_red_alert "Firewall rule groups not supported for #{server['type']['name']}"
return 1
end
group_id = args.count > 1 ? args[1] : Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'ruleGroup', 'type' => 'select', 'fieldLabel' => 'Firewall Rule Group', 'selectOptions' => search_rule_groups(server['id']).collect {|it| {'name' => it['name'], 'value' => it['id']}}, 'required' => true, 'description' => 'Select Firewall Rule Group.'}],options[:options],@api_client,{})['ruleGroup']
group = find_rule_group(server['id'], group_id)
return 1 if group.nil?
unless options[:yes] || ::Morpheus::Cli::OptionTypes::confirm("Are you sure you would like to remove the network firewall rule group '#{group['name']}' from server '#{server['name']}'?", options)
return 9, "aborted command"
end
@network_servers_interface.setopts(options)
if options[:dry_run]
print_dry_run @network_servers_interface.dry.destroy_firewall_rule_group(server['id'], group['id'])
return
end
json_response = @network_servers_interface.destroy_firewall_rule_group(server['id'], group['id'])
render_response(json_response, options, 'ruleGroup') do
print_green_success "\nDeleted Network Firewall Rule Group #{group['name']}\n"
_list_rule_groups(server, options)
end
end
|
#update_rule(args) ⇒ Object
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
|
# File 'lib/morpheus/cli/commands/network_firewalls_command.rb', line 263
def update_rule(args)
options = {:options=>{}}
params = {}
optparse = Morpheus::Cli::OptionParser.new do|opts|
opts.banner = subcommand_usage("[server] [rule]")
opts.on('-n', '--name VALUE', String, "Name for this firewall rule") do |val|
params['name'] = val
end
opts.on('-D', '--description VALUE', String, "Description for this firewall rule") do |val|
params['description'] = val
end
opts.on('--priority VALUE', Integer, "Priority for this firewall rule") do |val|
params['priority'] = val
end
opts.on('--enabled [on|off]', String, "Can be used to disable") do |val|
params['enabled'] = val.to_s == 'on' || val.to_s == 'true' || val.to_s.empty?
end
build_common_options(opts, options, [:options, :payload, :json, :dry_run, :remote])
opts. = "Update a network firewall rule.\n" +
"[server] is optional. This is the name or id of a network server.\n" +
"[rule] is optional. This is the name or id of an existing rule."
end
optparse.parse!(args)
if args.count > 2
raise_command_error "wrong number of arguments, expected 0-2 and got (#{args.count}) #{args}\n#{optparse}"
end
connect(options)
server_id = args.count > 0 ? args[0] : Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'networkServer', 'type' => 'select', 'fieldLabel' => 'Network Server', 'selectOptions' => search_network_servers.collect {|it| {'name' => it['name'], 'value' => it['id']}}, 'required' => true, 'description' => 'Select Network Server.'}],options[:options],@api_client,{})['networkServer']
server = find_network_server(server_id)
return 1 if server.nil?
if !server['type']['hasFirewall']
print_red_alert "Firewall not supported for #{server['type']['name']}"
return 1
end
rule_id = args.count > 1 ? args[1] : Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'rule', 'type' => 'select', 'fieldLabel' => 'Firewall Rule', 'selectOptions' => search_rules(server['id']).collect {|it| {'name' => it['name'], 'value' => it['id']}}, 'required' => true, 'description' => 'Select Firewall Rule.'}],options[:options],@api_client,{})['rule']
rule = find_rule(server['id'], rule_id)
return 1 if rule.nil?
payload = parse_payload(options) || {'rule' => params}
payload['rule'].deep_merge!(options[:options].reject {|k,v| k.is_a?(Symbol) }) if options[:options] && !payload['rule'].nil?
if payload['rule'].empty?
option_types = server['type']['ruleOptionTypes'].sort_by {|it| it['displayOrder']}
print_green_success "Nothing to update"
println cyan
edit_option_types = option_types.reject {|it| !it['editable'] || !it['showOnEdit']}
if edit_option_types.count > 0
print Morpheus::Cli::OptionTypes.display_option_types_help(
option_types,
{:include_context => true, :context_map => {'rule' => ''}, :color => cyan, :title => "Available Firewall Rule Options"}
)
end
exit 1
end
@network_servers_interface.setopts(options)
if options[:dry_run]
print_dry_run @network_servers_interface.dry.update_firewall_rule(server['id'], rule['id'], payload)
return
end
json_response = @network_servers_interface.update_firewall_rule(server['id'], rule['id'], payload)
render_response(json_response, options, 'rule') do
print_green_success "\nUpdated Network Firewall Rule #{rule['id']}\n"
_get_rule(server, rule['id'], options)
end
end
|
#update_rule_group(args) ⇒ Object
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
|
# File 'lib/morpheus/cli/commands/network_firewalls_command.rb', line 569
def update_rule_group(args)
options = {:options=>{}}
params = {}
optparse = Morpheus::Cli::OptionParser.new do|opts|
opts.banner = subcommand_usage("[server] [group]")
opts.on('-n', '--name VALUE', String, "Name for this firewall rule group") do |val|
options[:options]['name'] = val
end
opts.on('-D', '--description VALUE', String, "Description for this firewall rule group") do |val|
options[:options]['description'] = val
end
opts.on('--priority VALUE', Integer, "Priority for this firewall rule group") do |val|
options[:options]['priority'] = val
end
add_perms_options(opts, options, ['plans', 'groups'])
build_common_options(opts, options, [:options, :payload, :json, :dry_run, :remote])
opts. = "Update a network firewall rule group.\n" +
"[server] is optional. This is the name or id of an existing network server.\n" +
"[group] is optional. This is the name or id of an existing network firewall rule group."
end
optparse.parse!(args)
if args.count > 2
raise_command_error "wrong number of arguments, expected 0-2 and got (#{args.count}) #{args}\n#{optparse}"
end
connect(options)
server_id = args.count > 0 ? args[0] : Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'networkServer', 'type' => 'select', 'fieldLabel' => 'Network Server', 'selectOptions' => search_network_servers.collect {|it| {'name' => it['name'], 'value' => it['id']}}, 'required' => true, 'description' => 'Select Network Server.'}],options[:options],@api_client,{})['networkServer']
server = find_network_server(server_id)
return 1 if server.nil?
if !server['type']['hasFirewallGroups']
print_red_alert "Firewall rule groups not supported for #{server['type']['name']}"
return 1
end
group_id = args.count > 1 ? args[1] : Morpheus::Cli::OptionTypes.prompt([{'fieldName' => 'ruleGroup', 'type' => 'select', 'fieldLabel' => 'Firewall Rule Group', 'selectOptions' => search_rule_groups(server['id']).collect {|it| {'name' => it['name'], 'value' => it['id']}}, 'required' => true, 'description' => 'Select Firewall Rule Group.'}],options[:options],@api_client,{})['ruleGroup']
group = find_rule_group(server['id'], group_id)
return 1 if group.nil?
if is_master_account
params['visibility'] = options[:visibility] if !options[:visibility].nil?
params['tenants'] = options[:tenants].collect {|it| {'id' => it}} if !options[:tenants].nil?
end
payload = parse_payload(options) || {'ruleGroup' => params}
payload['ruleGroup'].deep_merge!(options[:options].reject {|k,v| k.is_a?(Symbol) }) if options[:options] && !payload['ruleGroup'].nil?
if payload['ruleGroup'].empty?
print_green_success "Nothing to update"
println cyan
option_types = server['type']['firewallGroupOptionTypes'].sort_by {|it| it['displayOrder']}
edit_option_types = option_types.reject {|it| !it['editable'] || !it['showOnEdit']}
if edit_option_types.count > 0
print Morpheus::Cli::OptionTypes.display_option_types_help(option_types, {:include_context => true, :context_map => {'ruleGroup' => ''}, :color => cyan, :title => "Available Firewall Rule Group Options"})
end
exit 1
end
@network_servers_interface.setopts(options)
if options[:dry_run]
print_dry_run @network_servers_interface.dry.update_firewall_rule_group(server['id'], group['id'], payload)
return
end
json_response = @network_servers_interface.update_firewall_rule_group(server['id'], group['id'], payload)
render_response(json_response, options, 'ruleGroup') do
print_green_success "\nUpdated Network Firewall Rule Group #{group['id']}\n"
_get_rule_group(server, group['id'], options)
end
end
|