Class: KnifeCloudstack::CsForwardruleCreate

Inherits:
Chef::Knife
  • Object
show all
Includes:
Chef::Knife::KnifeCloudstackBase
Defined in:
lib/chef/knife/cs_forwardrule_create.rb

Instance Method Summary collapse

Methods included from Chef::Knife::KnifeCloudstackBase

included

Instance Method Details

#create_port_forwarding_rule(ip_address, server_id, rule, connection, other_params) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/chef/knife/cs_forwardrule_create.rb', line 82

def create_port_forwarding_rule(ip_address, server_id, rule, connection, other_params)
  args = rule.split(':')
  public_port = args[0]
  private_port = args[1] || args[0]
  protocol = args[2] || "TCP"

  params = {
    'ipaddressId' => ip_address['id'],
    'protocol' => protocol
  }

  if ip_address['isstaticnat'] == 'true'
    other_params['command'] = 'createIpForwardingRule'
    other_params['startport'] = public_port
    other_params['endport'] = public_port
    Chef::Log.debug("Creating IP Forwarding Rule for
      #{ip_address['ipaddress']} with protocol: #{protocol}, public port: #{public_port}")
  else
    other_params['command'] = 'createPortForwardingRule'
    other_params['privatePort'] = private_port
    other_params['publicPort'] = public_port
    other_params['virtualMachineId'] = server_id
    Chef::Log.debug("Creating Port Forwarding Rule for #{ip_address['id']} with protocol: #{protocol},
      public port: #{public_port} and private port: #{private_port} and server: #{server_id}")
  end
  locate_config_value(:syncrequest) ? result = connection.send_request(params.merge(other_params)) : result = connection.send_async_request(params.merge(other_params))
  Chef::Log.debug("AsyncJobResult: #{result}")
end

#runObject



43
44
45
46
47
48
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
# File 'lib/chef/knife/cs_forwardrule_create.rb', line 43

def run

  hostname = @name_args.shift
  unless /^[a-zA-Z0-9][a-zA-Z0-9-]*$/.match hostname then
   ui.error "Invalid hostname. Please specify a short hostname, not an fqdn (e.g. 'myhost' instead of 'myhost.domain.com')."
    exit 1
  end

  params = {}
  locate_config_value(:openfirewall) ? params['openfirewall'] = 'true' : params['openfirewall'] = 'false'

  # Lookup all server objects.
  params_for_list_object = {  'command' => 'listVirtualMachines' }
  connection_result = connection.list_object(params_for_list_object, "virtualmachine")

  # Lookup the hostname in the connection result
  server = {}
  connection_result.map { |n| server = n if n['name'].upcase == hostname.upcase }
 
  if server['name'].nil?
    ui.error "Cannot find hostname: #{hostname}."
    exit 1
  end

  # Lookup the public ip address of the server
  server_public_address = connection.get_server_public_ip(server)
  ip_address = connection.get_public_ip_address(server_public_address)

  if ip_address.nil? || ip_address['id'].nil?
    ui.error "Cannot find public ip address for hostname: #{hostname}."
    exit 1
  end
 
  @name_args.each do |rule|
    create_port_forwarding_rule(ip_address, server['id'], rule, connection, params)
  end

end