Class: VagrantPlugins::VCloud::Action::ForwardPorts

Inherits:
Object
  • Object
show all
Includes:
Util::CompileForwardedPorts
Defined in:
lib/vagrant-vcloud/action/forward_ports.rb

Instance Method Summary collapse

Methods included from Util::CompileForwardedPorts

#compile_forwarded_ports

Constructor Details

#initialize(app, env) ⇒ ForwardPorts

Returns a new instance of ForwardPorts.



7
8
9
10
# File 'lib/vagrant-vcloud/action/forward_ports.rb', line 7

def initialize(app, env)
  @app = app
  @logger = Log4r::Logger.new('vagrant_vcloud::action::forward_ports')
end

Instance Method Details

#call(env) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/vagrant-vcloud/action/forward_ports.rb', line 12

def call(env)
  @env = env

  # Get the ports we are forwarding
  env[:forwarded_ports] ||= compile_forwarded_ports(
    env[:machine].config
  )

  forward_ports

  @app.call(env)
end

#forward_portsObject



25
26
27
28
29
30
31
32
33
34
35
36
37
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/vagrant-vcloud/action/forward_ports.rb', line 25

def forward_ports
  ports = []

  cfg = @env[:machine].provider_config
  cnx = cfg.vcloud_cnx.driver
  vapp_id = @env[:machine].get_vapp_id
  vm_name = @env[:machine].name

  # FIXME: why are we overriding this here ?
  #        It's already been taken care during the initial
  #        InventoryCheck. (tsugliani)
  #
  # cfg.org = cnx.get_organization_by_name(cfg.org_name)
  # cfg.vdc_network_id = cfg.org[:networks][cfg.vdc_network_name]

  @logger.debug('Getting VM info...')
  vm = cnx.get_vapp(vapp_id)
  vm_info = vm[:vms_hash][vm_name.to_sym]

  @env[:forwarded_ports].each do |fp|
    # FIXME: Useless variable assignement 'message_attributes'
    # (tsugliani)
    message_attributes = {
      :guest_port   => fp.guest_port,
      :host_port    => fp.host_port
    }

    @env[:ui].info(
      "Forwarding Ports: VM port #{fp.guest_port} -> " +
      "vShield Edge port #{fp.host_port}"
    )

    # Add the options to the ports array to send to the driver later
    ports << {
      :guestip                => fp.guest_ip,
      :nat_internal_port      => fp.guest_port,
      :hostip                 => fp.host_ip,
      :nat_external_port      => fp.host_port,
      :name                   => fp.id,
      :nat_protocol           => fp.protocol.upcase,
      :vapp_scoped_local_id   => vm_info[:vapp_scoped_local_id]
    }
  end

  if !ports.empty?
    # We only need to forward ports if there are any to forward
    @logger.debug("Port object to be passed: #{ports.inspect}")
    @logger.debug("Current network id #{cfg.vdc_network_id}")

    ### Here we apply the nat_rules to the vApp we just built
    add_ports = cnx.add_vapp_port_forwarding_rules(
      vapp_id,
      'Vagrant-vApp-Net',
      {
        :fence_mode       => 'natRouted',
        :parent_network   => cfg.vdc_network_id,
        :nat_policy_type  => 'allowTraffic',
        :nat_rules        => ports
      }
    )

    wait = cnx.wait_task_completion(add_ports)

    if !wait[:errormsg].nil?
      raise Errors::ComposeVAppError, :message => wait[:errormsg]
    end

  end

end