This example provides a common pattern for filtering Lookup Service registration data. This example is based on the code from the LookupService.pm module in the vSphere SDK for Perl.

This example uses the steps that are described in the Retrieve Service Endpoints on vCenter Server Instances procedure.

use LWP::UserAgent;
use HTTP::Request;
use HTTP::Headers;
use XML::LibXML;

sub lookup_service_infos
{
  # Uses global $my_ls_url.
  # Accepts a node_id string or '*' to search all nodes.
  my $prod = shift;
  my $svc_type = shift;
  my $proto = shift;
  my $ep_type = shift;
  my $node_id = shift || '*';

  # Format SOAP XML for List request.
  my $node_element = ($node_id == '*' ? '' : "<nodeId>$node_id</nodeId>");
  my $soap_message = "
     <S:Envelope xlns:S='http://schemas.xmlsoap.org/soap/envelope/'>
       <S:Body>
         <List xmlns='urn:lookup'>
           <_this type='LookupServiceRegistration'>ServiceRegistration</_this>
           <filterCriteria>
             $node_element
             <serviceType>
               <product>$prod</product>
               <type>$stype</type>
             </serviceType>
             <endpointType>
               <protocol>$proto</protocol>
               <type>$etype</type>
             </endpointType>
           </filterCriteria>
         </List>
       </S:Body>
     </S:Envelope>
  ";

  # Use this to set HTTP header info.
  sub byte_length
  {
    my ($string) = @_;
    use bytes;
    length($string);
  }

  # Send HTTP request.
  my $user_agent = LWP::UserAgent->new(
                        agent    => 'viperl',
                        ssl_opts => {verify_hostname=>0});
  my $http_header = HTTP::Headers->new(
                        Content_type   => 'text/xml',
                        SOAPAction     => 'urn:lookup/2.0',
                        Content_Length => byte_length($soap_message));
  my $http:request = HTTP::Request->new('POST',
                                        $my_ls_url,
                                        $http_header,
                                        $soap_message);
  my $response = $user_agent->request($http_request);

  # Parse results.
  my $xml_parser = XML::LibXML->new;
  my $result;
  eval { $parsed = $xml_parser->parse_string($response->content) };
  if ($@) { die 'SOAP request error.' };
  my $body = $parsed->documentElement()->
                      getChildrenByTagName('soapenv:Body')->shift;
  my $list_response = $body->
                      getChildrenByTagName('ListResponse')->shift;
  my $return_val = $list_response->
                      getChildrenByTagName('returnval')->shift;
  my @endpoints = $return_val->
                      getChildrenByTagName('serviceEndpoints');
  return @endpoints;
}  # lookup_service_infos()