Perl Example of Retrieving a Service Endpoint from Multiple vCenter Server instances

This example provides a common pattern for filtering Lookup Service registration data. This example is based on the code from the
external_psc_sso_workflow.pl
sample file.
This example uses the steps that are described in the Retrieve Service Endpoints on vCenter Server Instances procedure.
For a complete and up-to-date version of the sample code, see the vSphere Automation SDK Perl samples at GitHub.
use LWP::UserAgent; use HTTP::Request; use HTTP::Headers; use XML::LibXML; # Use this to set HTTP header info. sub byte_length { my ($string) = @_; use bytes; return length($string); } 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 eq '*' ? '' : "<nodeId>$node_id</nodeId>"); my $soap_message = " <S:Envelope xmlns: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>$svc_type</type> </serviceType> <endpointType> <protocol>$proto</protocol> <type>$ep_type</type> </endpointType> </filterCriteria> </List> </S:Body> </S:Envelope> "; # 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; }