Class: PortRule

Inherits:
CloudstackCli::Base show all
Defined in:
lib/cloudstack-cli/commands/port_rule.rb

Constant Summary

Constants included from CloudstackCli::Helper

CloudstackCli::Helper::ASYNC_STATES

Instance Attribute Summary

Attributes inherited from CloudstackCli::Base

#config

Instance Method Summary collapse

Methods inherited from CloudstackCli::Base

exit_on_failure?

Methods included from CloudstackCli::Helper

#ask_number, #bootstrap_server, #bootstrap_server_interactive, #create_port_rules, #create_server, #print_job_status, #print_options, #update_job_status, #watch_jobs

Instance Method Details

#create(server_name) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/cloudstack-cli/commands/port_rule.rb', line 10

def create(server_name)
  unless server = client.get_server(server_name)
    error "Server #{server_name} not found."
    exit 1
  end
  frontendip = nil
  project = client.get_project(project)
  options[:rules].each do |pf_rule|
    ip = pf_rule.split(":")[0]
    if ip != ''
      ip_addr = client.get_public_ip_address(ip)
      unless ip_addr
        say "Error: IP #{ip} not found.", :red
        next
      end
    else
      ip_addr = frontendip ||= client.associate_ip_address(
        client.get_network(options[:network], project ? project["id"] : nil)["id"]
      )
    end
    port = pf_rule.split(":")[1]
    puts
    say "Create port forwarding rule #{ip_addr["ipaddress"]}:#{port} for server #{server_name}.", :yellow
    client.create_port_forwarding_rule(ip_addr["id"], port, 'TCP', port, server["id"])
    puts
  end
end

#listObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/cloudstack-cli/commands/port_rule.rb', line 40

def list
  project_id = find_project['id'] if options[:project]
  rules = client.list_port_forwarding_rules(ip_address_id=nil, project_id)
  if rules.size < 1
    puts "No rules found."
  else
    table = [["IP", "Server", "Public-Port", "Private-Port", "Protocol", "State"]]
    rules.each do |rule|
      table << [
        rule['ipaddress'],
        rule['virtualmachinename'],
        print_ports(rule, 'public'),
        print_ports(rule, 'private'),
        rule['protocol'],
        rule['state']
      ]
    end
    print_table table
  end
end