Class: KnifeCloudstack::CsFirewallruleCreate

Inherits:
Chef::Knife
  • Object
show all
Includes:
Chef::Knife::KnifeCloudstackBase
Defined in:
lib/chef/knife/cs_firewallrule_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



77
78
79
80
81
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
110
# File 'lib/chef/knife/cs_firewallrule_create.rb', line 77

def create_port_forwarding_rule(ip_address, server_id, rule, connection, other_params)
  args = rule.split(':')
  startport = args[0]
  endport   = args[1] || args[0]
  protocol  = args[2] || "TCP"
  cidrlist  = args[3] || "0.0.0.0/0"      

  # Required parameters
  params = {
    'command' => 'createFirewallRule',
    'ipaddressId' => ip_address['id'],
    'protocol' => protocol
  }

  # Optional parameters
  opt_params = {
    'startport' => startport,
    'endport' => endport,
    'cidrlist' => cidrlist
  }
  
  params.merge!(opt_params)
 
  Chef::Log.debug("Creating Firewall Rule for
    #{ip_address['ipaddress']} with protocol: #{protocol}, start: #{startport} end: #{endport} cidr: #{cidrlist}")

  if locate_config_value(:syncrequest) 
    result = connection.send_request(params)
    Chef::Log.debug("JobResult: #{result}")
  else
    result = connection.send_async_request(params)
    Chef::Log.debug("AsyncJobResult: #{result}")
  end
end

#runObject



38
39
40
41
42
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
# File 'lib/chef/knife/cs_firewallrule_create.rb', line 38

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