Class: CivoCLI::Firewall

Inherits:
Thor
  • Object
show all
Defined in:
lib/firewall.rb

Instance Method Summary collapse

Instance Method Details

#create(firewall_name) ⇒ Object



4
5
6
7
8
9
10
11
12
# File 'lib/firewall.rb', line 4

def create(firewall_name)
  CivoCLI::Config.set_api_auth

  Civo::Firewall.create(name: firewall_name)
  puts "Created firewall #{firewall_name.colorize(:green)}"
rescue Flexirest::HTTPException => e
  puts e.result.reason.colorize(:red)
  exit 1
end

#delete_rule(firewall_id, rule_id) ⇒ Object



76
77
78
79
80
81
82
83
84
# File 'lib/firewall.rb', line 76

def delete_rule(firewall_id, rule_id)
  CivoCLI::Config.set_api_auth

  Civo::FirewallRule.remove(firewall_id: firewall_id, id: rule_id)
  puts "Removed Firewall rule #{rule_id.colorize(:red)}"
rescue Flexirest::HTTPException => e
  puts e.result.reason.colorize(:red)
  exit 1
end

#listObject



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/firewall.rb', line 15

def list
  CivoCLI::Config.set_api_auth
  rows = []
  Civo::Firewall.all.items.each do |element|
    rows << [element.id, element.name, element.rules_count, element.instances_count]
  end
  puts Terminal::Table.new headings: ['ID', 'Name', 'No. of Rules', 'instances using'], rows: rows
rescue Flexirest::HTTPException => e
  puts e.result.reason.colorize(:red)
  exit 1
end

#list_rules(firewall_id) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/firewall.rb', line 62

def list_rules(firewall_id)
  CivoCLI::Config.set_api_auth
  rules = Civo::FirewallRule.all(firewall_id: firewall_id)
  rows = []
  rules.each do |rule|
    rows << [rule.id, rule.protocol, rule.start_port, rule.end_port, rule.direction, rule.cidr.items.join(", "), rule.label]
  end
  puts Terminal::Table.new title: "Firewall rules for #{firewall_id}", headings: ['ID', 'Protocol', 'Start Port', 'End Port', 'Direction', 'CIDR', 'Label'], rows: rows
rescue Flexirest::HTTPException => e
  puts e.result.reason.colorize(:red)
  exit 1
end

#new_ruleObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/firewall.rb', line 46

def new_rule
  CivoCLI::Config.set_api_auth

  if !options[:end_port]
    Civo::FirewallRule.create(firewall_id: options[:firewall_id], start_port: options[:start_port], end_port: options[:start_port], cidr: options[:cidr], direction: options[:direction], label: options[:label])
  end
  if options[:end_port]
    Civo::FirewallRule.create(firewall_id: options[:firewall_id], start_port: options[:start_port], end_port: options[:end_port], cidr: options[:cidr], direction: options[:direction], label: options[:label])
  end
  puts "New rule" + (options[:label].nil? ? "" : " #{options[:label].colorize(:green)}") + " created"
rescue Flexirest::HTTPException => e
  puts e.result.reason.colorize(:red)
  exit 1
end

#remove(firewall_ID) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/firewall.rb', line 29

def remove(firewall_ID)
  CivoCLI::Config.set_api_auth
  Civo::Firewall.remove(id: firewall_ID)
  puts "Removed firewall #{firewall_ID.colorize(:red)}"
rescue Flexirest::HTTPException => e
  puts e.result.reason.colorize(:red)
  exit 1
end