Joes Momma
Joes Momma

Reputation: 11

Connect vCenter using PHP and SOAP

I am working on a small project that needs to connect to vCenter using the wsdl. I am very new to soap. I have tried using set headers but still same issue.

header('content-type: json');

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

ini_set('soap.wsdl_cache_enabled', 0);
ini_set('soap.wsdl_cache_ttl', 0);

// Vcenter URL
$vcenter_url = "https://vcenter.labs.local/sdk";

// WSDL File location
$wsdl = './vmware/vimService.wsdl';

// Login Creds
$username = "[email protected]";
$password = "S0m3P@ssw0rd123!";

// Connect to vCenter using SOAP
$context = stream_context_create(
  array(
    'ssl' => array(
      'verify_peer' => false,
      'verify_peer_name' => false,
      'allow_self_signed' => true,
    )
  )
);

$client = new SoapClient(
  $wsdl,
  array(
    'trace' => true,
    'exceptions' => true,
    "location" => $vcenter_url,
    'stream_context' => $context
  )
);

$soap_message["_this"] = new SoapVar("ServiceInstance", XSD_STRING, "ServiceInstance");
$result = $client->RetrieveServiceContent($soap_message);
$service_instance = $result->returnval;

// Login to vCenter
$login = $client->Login(
  array(
    '_this' => $service_instance->sessionManager,
    'userName' => $username,
    'password' => $password
  )
);

// Show Login
print_r($login);

/* I have removed the below to try another method with getting hosts */
// Get all datacenters
//$datacenters = $client->retrieveProperties(array(
//  '_this' => $login->returnval, 'specSet' => array(
//    'propSet' => array('type' => 'Datacenter'),
//    'objectSet' => array('obj' => $login->returnval)
  )
//));

//Retrieve the list of hosts
$hosts = $client->RetrieveProperties(
  array(
    'authentication' => $login,
    '_this' => 'PropertyCollector',
    'specSet' => array(
      'propSet' => array(
        'type' => 'HostSystem',
        'all' => false,
        'pathSet' => array('name')
      ),
      'objectSet' => array(
        'obj' => 'Datacenter',
        'skip' => false,
        'selectSet' => array(
          array(
            'name' => 'get_entities',
            'type' => 'TraversalSpec'
          )
        )
      )
    )
  )
);

When I run this, I am getting the login successful. However when it try's to collect the datacenter information it produces the following error:

Fatal error: Uncaught SoapFault exception: [ServerFaultCode] The object 'vmodl.ManagedObject:' has already been deleted or has not been completely created in /app/paloalto/public/test.php:58 Stack trace: #0 /app/paloalto/public/test.php(58): SoapClient->__call() #1 {main} thrown in /app/paloalto/public/test.php on line 58

'objectSet' => array('obj' => $login->returnval)

Here is the output from the Login

stdClass Object
(
    [returnval] => stdClass Object
        (
            [key] => 524c3cf4-ef5e-3624-afdb-2739a3a628d6
            [userName] => VSPHERE.LOCAL\Administrator
            [fullName] => Administrator vsphere.local
            [loginTime] => 2023-01-23T09:40:16.663361Z
            [lastActiveTime] => 2023-01-23T09:40:16.663361Z
            [locale] => en
            [messageLocale] => en
            [extensionSession] => 
            [ipAddress] => 192.168.0.205
            [userAgent] => PHP-SOAP/8.1.2-1ubuntu2.9
            [callCount] => 0
        )

)

as I keep digging I see the sessions working. Here is a response using the __getLastResponseHeaders.

HTTP/1.1 200 OK
cache-control: no-cache
content-type: text/xml; charset=utf-8
date: Mon, 23 Jan 2023 12:28:57 GMT
set-cookie: vmware_soap_session="86de10e372ed0165a8832d7be32938cd889f50f1"; Path=/; HttpOnly; Secure;
x-envoy-upstream-service-time: 32
server: envoy
transfer-encoding: chunked

I cannot seem to figure out why the session is not being passed after login is successful in the client.

Upvotes: 0

Views: 629

Answers (1)

Benbb96
Benbb96

Reputation: 2283

It might be late to answer but here's the changes that I'll do:

//Retrieve the list of hosts
$hosts = $client->RetrieveProperties(
  array(
    '_this' => $service_instance->propertyCollector,
    'specSet' => array(
      'propSet' => array(
        'type' => 'HostSystem',
        'all' => false,
        'pathSet' => array('name')
      ),
      'objectSet' => array(
        'obj' => $service_instance->rootFolder,
        'skip' => false,
        'selectSet' => array(
          array(
            'name' => 'get_entities',
            'type' => 'TraversalSpec'
          )
        )
      )
    )
  )
);

I removed authentication key and reused $service_instance to access propertyCollector and rootFolder.

Upvotes: 0

Related Questions