Sha256: 67af008ef95f098b1aba9743381c5122aa94b8b0838de61465fb7cb5345b280d

Contents?: true

Size: 1.93 KB

Versions: 12

Compression:

Stored size: 1.93 KB

Contents

class PortRule < CloudstackCli::Base

  desc "create SERVER", "create portforwarding rules"
  option :rules, type: :array,
    required: true,
    desc: "Port Forwarding Rules [public_ip]:port ...",
    aliases: '-r'
  option :network, required: true, aliases: '-n'
  option :project
  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)
      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

  desc "list", "list portforwarding rules"
  option :project
  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

  no_commands do
    def print_ports(rule, type)
      if rule["#{type}port"] == rule["#{type}endport"]
        return rule["#{type}port"]
      else
        return rule["#{type}port"] + "-" + rule["#{type}endport"]
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
cloudstack-cli-0.3.4 lib/cloudstack-cli/commands/port_rule.rb
cloudstack-cli-0.3.3 lib/cloudstack-cli/commands/port_rule.rb
cloudstack-cli-0.3.2 lib/cloudstack-cli/commands/port_rule.rb
cloudstack-cli-0.3.1 lib/cloudstack-cli/commands/port_rule.rb
cloudstack-cli-0.2.2 lib/cloudstack-cli/commands/port_rule.rb
cloudstack-cli-0.2.1 lib/cloudstack-cli/commands/port_rule.rb
cloudstack-cli-0.2.0 lib/cloudstack-cli/commands/port_rule.rb
cloudstack-cli-0.1.7 lib/cloudstack-cli/commands/port_rule.rb
cloudstack-cli-0.1.6 lib/cloudstack-cli/commands/port_rule.rb
cloudstack-cli-0.1.5 lib/cloudstack-cli/commands/port_rule.rb
cloudstack-cli-0.1.4 lib/cloudstack-cli/commands/port_rule.rb
cloudstack-cli-0.1.3 lib/cloudstack-cli/commands/port_rule.rb