class PortRule
Public Instance Methods
create(server_name)
click to toggle source
# File lib/cloudstack-cli/commands/port_rule.rb 11 def create(server_name) 12 resolve_project 13 unless server = client.list_virtual_machines( 14 name: server_name, project_id: options[:project_id], listall: true 15 ).find {|vm| vm["name"] == server_name } 16 error "Server #{server_name} not found." 17 exit 1 18 end 19 ip_addr = nil 20 options[:rules].each do |pf_rule| 21 ip = pf_rule.split(":")[0] 22 unless ip == '' 23 unless ip_addr = client.list_public_ip_addresses(ipaddress: ip, project_id: options[:project_id]).first 24 say "Error: IP #{ip} not found.", :yellow 25 next 26 end 27 else 28 say "Assign a new IP address ", :yellow 29 net_id = client.list_networks(project_id: options[:project_id]).find {|n| n['name'] == options[:network]}['id'] 30 say(" OK", :green) if ip_addr = client.associate_ip_address(networkid: net_id)["ipaddress"] 31 end 32 port = pf_rule.split(":")[1] 33 say "Create port forwarding rule #{ip_addr["ipaddress"]}:#{port} for server #{server_name} ", :yellow 34 35 say(" OK", :green) if client.create_port_forwarding_rule( 36 ipaddress_id: ip_addr["id"], 37 public_port: port, 38 private_port: port, 39 virtualmachine_id: server["id"], 40 protocol: "TCP" 41 ) 42 end 43 end
list()
click to toggle source
# File lib/cloudstack-cli/commands/port_rule.rb 49 def list 50 resolve_project 51 rules = client.list_port_forwarding_rules(options) 52 if rules.size < 1 53 puts "No rules found." 54 else 55 case options[:format].to_sym 56 when :yaml 57 puts({rules: rules}.to_yaml) 58 when :json 59 puts JSON.pretty_generate(rules: rules) 60 else 61 table = [["IP", "Server", "Public-Port", "Private-Port", "Protocol", "State"]] 62 rules.each do |rule| 63 table << [ 64 rule['ipaddress'], 65 rule['virtualmachinename'], 66 print_ports(rule, 'public'), 67 print_ports(rule, 'private'), 68 rule['protocol'], 69 rule['state'] 70 ] 71 end 72 print_table table 73 say "Total number of rules: #{rules.count}" 74 end 75 end 76 end
print_ports(rule, type)
click to toggle source
# File lib/cloudstack-cli/commands/port_rule.rb 79 def print_ports(rule, type) 80 if rule["#{type}port"] == rule["#{type}endport"] 81 return rule["#{type}port"] 82 else 83 return rule["#{type}port"] + "-" + rule["#{type}endport"] 84 end 85 end