Class: Knifecosmic::CosmicForwardruleCreate

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

Instance Method Summary collapse

Methods included from Chef::Knife::KnifecosmicBase

included

Instance Method Details

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



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/chef/knife/cosmic_forwardrule_create.rb', line 95

def create_port_forwarding_rule(ip_address, server, rule, connection, other_params)
  server_id = server['id']
  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 other_params['vmguestip']
    # VPC based network
    # Find networkid associated with primary VM nic
    server_default_nic = connection.get_server_default_nic(server)
    networkid = server_default_nic['networkid']
    other_params['networkid'] = networkid

    # Find id of public router IP
    public_ip = connection.get_public_ip_address(ip_address['ipaddress'])
    params['ipaddressId'] = public_ip['id']
    
    other_params['command'] = 'createPortForwardingRule'
    other_params['privatePort'] = private_port
    other_params['privateEndPort'] = private_port
    other_params['publicPort'] = public_port
    other_params['publicEndPort'] = public_port
    other_params['virtualMachineId'] = server_id

    Chef::Log.debug("Creating port Forwarding Rule for router ip 
      #{ip_address['ipaddress']} with protocol: #{protocol}, public port: #{public_port}")
  elsif 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



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
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/chef/knife/cosmic_forwardrule_create.rb', line 48

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', 'name' => hostname }
  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

  if locate_config_value(:vrip)
    Chef::Log.debug("Forwarding rule for VPC.")
    server['nic'].each do |nic|
      params['vmguestip'] = nic['ipaddress']
    end
    ip_address = {}
    ip_address['ipaddress'] = config[:vrip]
  else
    Chef::Log.debug("Forwarding rule for public IP on server")
    server_address = connection.get_server_public_ip(server)
    ip_address = connection.get_public_ip_address(server_address)
  
    if ip_address.nil? || ip_address['id'].nil?
      ui.error "Cannot find public ip address for hostname: #{hostname}."
      exit 1
    end
  end

  @name_args.each do |rule|
    create_port_forwarding_rule(ip_address, server, rule, connection, params)
  end
end