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

      netconfig_response, headers = send_request(params)

      if config[:primary_index]
        node = netconfig_response.css('PrimaryNetworkConnectionIndex').first
        node.content = config[:primary_index]
      end

      picked_network = netconfig_response.css("NetworkConnection").select do |net|
        net.attribute('network').text == network[:name]
      end.first

      raise WrongItemIDError, "Network named #{network[:name]} not found." unless picked_network

      if config[:ip_allocation_mode]
        node = picked_network.css('IpAddressAllocationMode').first
        node.content = config[:ip_allocation_mode]
      end

      if config[:network_index]
        node = picked_network.css('NetworkConnectionIndex').first
        node.content = config[:network_index]
      end

      if config[:is_connected]
        node = picked_network.css('IsConnected').first
        node.content = config[:is_connected]
      end

      if config[:ip]
        node = picked_network.css('IpAddress').first
        node.content = config[:ip]
      end

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

      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 edit_vm_network(vmId, network, config={})
      params = {
        'method' => :get,
        'command' => "/vApp/vm-#{vmId}/networkConnectionSection"
      }

      netconfig_response, headers = send_request(params)

      if config[:primary_index]
        node = netconfig_response.css('PrimaryNetworkConnectionIndex').first
        node.content = config[:primary_index]
      end

      picked_network = netconfig_response.css("NetworkConnection").select do |net|
        net.attribute('network').text == network[:name]
      end.first

      raise WrongItemIDError, "Network named #{network[:name]} not found." unless picked_network

      if config[:ip_allocation_mode]
        node = picked_network.css('IpAddressAllocationMode').first
        node.content = config[:ip_allocation_mode]
      end

      if config[:network_index]
        node = picked_network.css('NetworkConnectionIndex').first
        node.content = config[:network_index]
      end

      if config[:is_connected]
        node = picked_network.css('IsConnected').first
        node.content = config[:is_connected]
      end

      if config[:ip]
        node = picked_network.css('IpAddress').first
        node.content = config[:ip]
      end

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

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

      task_id = headers[:location].gsub(/.*\/task\//, "")
      task_id
    end
Edit VM Network Config

 Retrieve the existing network config section and edit it
 to ensure settings are not lost