Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw an error if wsdl class map caching is enabled but cache is not writable #2

Merged
merged 18 commits into from
Sep 19, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
40150a5
Require TypeDefinitions.inc in the WsdlClassMapper. This seems like t…
May 14, 2013
c661f33
Removed require_once call for TypeDefinitions.inc because a proper co…
May 16, 2013
90e3baa
Removing xsi: suffix was making it impossibel for vSphere to determin…
Jul 18, 2013
98adb8e
Added a getId method on VirtualMachine that returns its ID in vcenter.
Jul 18, 2013
ee2192a
Allow the PropertyCollector to return results even if there is no cla…
Jul 31, 2013
dfdc0ae
Fixed formatting
Jul 31, 2013
4955f7a
Merged upstream
Aug 8, 2013
53aded0
Added more detailed error messaging for SoapFaults. It's hacked in ki…
Aug 14, 2013
bee1f99
Added Folder class extending ManagedObject with methods for finding c…
Aug 14, 2013
c55d354
Fixed bug where Exception was not using the global namespace
Aug 15, 2013
48044a8
Added create option to Folder->getChild and Folder->getFolderByPath s…
Aug 26, 2013
b4f20df
isMethodAPropertyRetrieval was too greedy, preventing any ManagedObje…
Aug 27, 2013
7cf5d20
Removed getId method from VirtualMachine because it is more suited fo…
Aug 27, 2013
e6fe7b8
Added clone session functionality. Instead of authenticating every ti…
Aug 27, 2013
0117eb5
Added getHardware method to the VirtualMachine extension which return…
Sep 11, 2013
d550867
Added faultstring to SoapFault handling.
Sep 11, 2013
6885dfa
Added getConfig and getGuestInfo methods to VirtualMachine class.
Sep 11, 2013
e11f150
Had to remove the getConfig method of VirtualMachine because it confl…
Sep 12, 2013
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions library/Vmwarephp/Extensions/Folder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php
namespace Vmwarephp\Extensions;

class Folder extends \Vmwarephp\ManagedObject
{
/**
* @param $type The type of ManagedObject to find
* @param $name The name of the ManagedObject to find
* @param $create Whether or not to create a folder with the name $name if it doesn't exist.
*/
public function getChild($type = '', $name = '', $create = false)
{
if (!$type || !$name) {
throw new \Exception('Folder::getChild requires $type and $child arguments');
}

foreach ($this->childEntity as $child) {
if (!is_object($child)) {
continue;
}
if ($child->getReferenceType() === $type) {
if ($child->name === $name) {
return $child;
}
}
}

if ($create && $type === 'Folder') {
return $this->createFolder(array('name' => $name));
}

return false;
}

/**
* @param $path A folder path delimited with '/' like Folder1/Folder2/etc which would attempt to find Folder1 as a child of this Folder, Folder2 as a child of Folder1, etc as a child of Folder2.
* @param $create Whether or not to create the folders described by the path if they don't exist.
*/
public function getFolderByPath($path = '', $create = false)
{
if (!$path) {
throw new \Exception('Folder::getFolderByPath requires a $path argument');
}
$folderNames = explode('/', $path);
$name = $path;
$newPath = '';

if (count($folderNames) > 1) {
$name = $folderNames[0];
$newPath = str_replace($name.'/', '', $path);
}

$childFolder = $this->getChild('Folder', $name);
if (!$childFolder) {
if ($create) {
$childFolder = $this->createFolder(array('name' => $name));
} else {
return false;
}
}
if (!$newPath) {
return $childFolder;
}

return $childFolder->getFolderByPath($newPath, $create);
}
}
5 changes: 2 additions & 3 deletions library/Vmwarephp/Extensions/PropertyCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,8 @@ private function appendTraversedPropertiesToRequestedObject($collectionResult, $

private function findRequestedObjectInCollectionResult($collectionResult, $managedObjectType) {
if (is_object($collectionResult)) return $collectionResult;
foreach ($collectionResult as $managedObject) {
if ($managedObject->getReferenceType() == $managedObjectType) return $managedObject;
}
foreach ($collectionResult as $managedObject)
if (strpos(get_class($managedObject), $managedObjectType) !== false || $managedObject->reference->type === $managedObjectType) return $managedObject;
throw new \Exception('Cannot find the object we requested to collect the properties for in servers response!');
}

Expand Down
8 changes: 8 additions & 0 deletions library/Vmwarephp/Extensions/VirtualMachine.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,12 @@ function getUsedSpace() {
function getProvisionedSpace() {
return $this->summary->storage->committed + $this->summary->storage->uncommitted;
}

function getHardware() {
return $this->config->hardware;
}

function getGuestInfo() {
return $this->guest;
}
}
41 changes: 38 additions & 3 deletions library/Vmwarephp/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ class Service {
private $serviceContent;
private $session;
private $clientFactory;
private $cloneTicketCache = 'cloneTicket.cache';

function __construct(Vhost $vhost, \Vmwarephp\Factory\SoapClient $soapClientFactory = null) {
$this->vhost = $vhost;
$this->clientFactory = $soapClientFactory ? : new \Vmwarephp\Factory\SoapClient();
$this->soapClient = $this->clientFactory->make($this->vhost);
$this->typeConverter = new TypeConverter($this);
$this->cloneTicketCache = dirname(__FILE__).'/'.$this->cloneTicketCache;
}

function __call($method, $arguments) {
Expand Down Expand Up @@ -46,10 +48,27 @@ function findManagedObjectByName($objectType, $name, $propertiesToCollect = arra
function connect() {
if ($this->session) return $this->session;
$sessionManager = $this->getSessionManager();
$this->session = $sessionManager->Login(array('userName' => $this->vhost->username, 'password' => $this->vhost->password, 'locale' => null));
$this->session = $this->connectWithCloneTicket();
if (!$this->session)
$this->session = $sessionManager->Login(array('userName' => $this->vhost->username, 'password' => $this->vhost->password, 'locale' => null));
$cloneTicket = $sessionManager->AcquireCloneTicket();
if (!file_put_contents($this->cloneTicketCache, $cloneTicket))
throw new \Exception('There was an error writing to the clone ticket path. Check the permissions of the cache directory.');
return $this->session;
}

private function connectWithCloneTicket() {
$cloneTicket = file_get_contents($this->cloneTicketCache);
if (!$cloneTicket) {
return false;
}
try {
return $this->getSessionManager()->CloneSession(array('cloneTicket' => $cloneTicket));
} catch (\Exception $e) {
return false;
}
}

function getServiceContent() {
if (!$this->serviceContent)
$this->serviceContent = $this->makeSoapCall('RetrieveServiceContent', \Vmwarephp\Factory\SoapMessage::makeForServiceInstance());
Expand All @@ -65,7 +84,23 @@ protected function convertResponse($response) {

private function makeSoapCall($method, $soapMessage) {
$this->soapClient->_classmap = $this->clientFactory->getClientClassMap();
$result = $this->soapClient->$method($soapMessage);
try {
$result = $this->soapClient->$method($soapMessage);
} catch (\SoapFault $sf) {
$this->soapClient->_classmap = null;
$faults = array();
foreach ($sf->detail as $fault) {
$faults[] = "{$fault->enc_stype}: ".print_r($fault->enc_value, true);
}
$message = "{$sf->faultcode}: {$sf->faultstring}. ";
if ($sf->string) {
$message .= "{$sf->string} ";
}
if (count($faults)) {
$message .= implode(', ', $faults);
}
throw new \Exception($message);
}
$this->soapClient->_classmap = null;
return $this->convertResponse($result);
}
Expand All @@ -81,7 +116,7 @@ private function getQueriedProperty($method, $arguments) {
}

private function isMethodAPropertyRetrieval($calledMethod) {
return preg_match('/^get/', strtolower($calledMethod));
return preg_match('/^get/', $calledMethod);
}

private function generateNameForThePropertyToRetrieve($calledMethod) {
Expand Down
3 changes: 1 addition & 2 deletions library/Vmwarephp/SoapClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ function __doRequest($request, $location, $action, $version, $one_way = 0) {
* php bug #45404
* */
private function appendXsiTypeForExtendedDatastructures($request) {
$request = str_replace("xsi:", "", $request);
return str_replace(array("type=\"ns1:TraversalSpec\"", '<ns1:selectSet />'), array("xsi:type=\"ns1:TraversalSpec\"", ''), $request);
return $request = str_replace(array("xsi:type=\"ns1:TraversalSpec\"", '<ns1:selectSet />'), array("xsi:type=\"ns1:TraversalSpec\"", ''), $request);
}
}
4 changes: 3 additions & 1 deletion library/Vmwarephp/WsdlClassMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ private function readClassDefinitions() {

private function cacheClassMap($classMap) {
if (!$this->useClassMapCaching) return;
file_put_contents($this->makeCacheFilePath(), serialize($classMap));
if (!file_put_contents($this->makeCacheFilePath(), serialize($classMap))) {
throw new \Exception('\\Vmwarephp\\WsdlClassMapper is configured to cache the class map but was not able to. Check the permissions on the cache directory.');
}
}

private function makeCacheFilePath() {
Expand Down