def add_vm_network(vmId, network, config={})
      params = {
        'method' => :get,
        'command' => "/vApp/vm-#{vmId}/networkConnectionSection"
      }

      netconfig_response, headers = send_request(params)

      parent_section = netconfig_response.css('NetworkConnectionSection').first

      # For some reasons these elements must be removed
      netconfig_response.css("Link").each {|n| n.remove}

      # Delete placeholder network if present (since vcloud 5.5 has been removed)
      none_network = netconfig_response.css('NetworkConnection').find{|n| n.attribute('network').text == 'none'}
      none_network.remove if none_network

      networks_count = netconfig_response.css('NetworkConnection').count

      primary_index_node = netconfig_response.css('PrimaryNetworkConnectionIndex').first
      unless primary_index_node
        primary_index_node = Nokogiri::XML::Node.new "PrimaryNetworkConnectionIndex", parent_section
        parent_section.add_child(primary_index_node)
      end
      primary_index_node.content = config[:primary_index] || 0

      new_network = Nokogiri::XML::Node.new "NetworkConnection", parent_section
      new_network["network"] = network[:name]
      new_network["needsCustomization"] = true

      idx_node = Nokogiri::XML::Node.new "NetworkConnectionIndex", new_network
      idx_node.content = config[:network_index] || networks_count
      new_network.add_child(idx_node)

      if config[:ip]
        ip_node = Nokogiri::XML::Node.new "IpAddress", new_network
        ip_node.content = config[:ip]
        new_network.add_child(ip_node)
      end

      is_connected_node = Nokogiri::XML::Node.new "IsConnected", new_network
      is_connected_node.content = config[:is_connected] || true
      new_network.add_child(is_connected_node)

      allocation_node = Nokogiri::XML::Node.new "IpAddressAllocationMode", new_network
      allocation_node.content = config[:ip_allocation_mode] || "POOL"
      new_network.add_child(allocation_node)

      parent_section.add_child(new_network)

      params = {
        'method' => :put,
        'command' => "/vApp/vm-#{vmId}/networkConnectionSection"
      }

      put_response, headers = send_request(params, netconfig_response.to_xml, "application/vnd.vmware.vcloud.networkConnectionSection+xml")

      task_id = headers[:location].gsub(/.*\/task\//, "")
      task_id
    end
def add_vm_network(vmId, network, config={})
      params = {
        'method' => :get,
        'command' => "/vApp/vm-#{vmId}/networkConnectionSection"
      }

      netconfig_response, headers = send_request(params)

      parent_section = netconfig_response.css('NetworkConnectionSection').first

      # For some reasons these elements must be removed
      netconfig_response.css("Link").each {|n| n.remove}

      # Delete placeholder network if present (since vcloud 5.5 has been removed)
      none_network = netconfig_response.css('NetworkConnection').find{|n| n.attribute('network').text == 'none'}
      none_network.remove if none_network

      networks_count = netconfig_response.css('NetworkConnection').count

      primary_index_node = netconfig_response.css('PrimaryNetworkConnectionIndex').first
      unless primary_index_node
        primary_index_node = Nokogiri::XML::Node.new "PrimaryNetworkConnectionIndex", parent_section
        parent_section.add_child(primary_index_node)
      end
      primary_index_node.content = config[:primary_index] || 0

      new_network = Nokogiri::XML::Node.new "NetworkConnection", parent_section
      new_network["network"] = network[:name]
      new_network["needsCustomization"] = true

      idx_node = Nokogiri::XML::Node.new "NetworkConnectionIndex", new_network
      idx_node.content = config[:network_index] || networks_count
      new_network.add_child(idx_node)

      if config[:ip]
        ip_node = Nokogiri::XML::Node.new "IpAddress", new_network
        ip_node.content = config[:ip]
        new_network.add_child(ip_node)
      end

      is_connected_node = Nokogiri::XML::Node.new "IsConnected", new_network
      is_connected_node.content = config[:is_connected] || true
      new_network.add_child(is_connected_node)

      allocation_node = Nokogiri::XML::Node.new "IpAddressAllocationMode", new_network
      allocation_node.content = config[:ip_allocation_mode] || "POOL"
      new_network.add_child(allocation_node)

      parent_section.add_child(new_network)

      params = {
        'method' => :put,
        'command' => "/vApp/vm-#{vmId}/networkConnectionSection"
      }

      put_response, headers = send_request(params, netconfig_response.to_xml, "application/vnd.vmware.vcloud.networkConnectionSection+xml")

      task_id = headers[:location].gsub(/.*\/task\//, "")
      task_id
    end
Add a new network to a VM