VMware {code} Community
Clarencehj
Contributor
Contributor

Problems setting the EnterLockdownMode Method with SOAP / PHP

My question is hos to I use PHP / SOAP calls to  enable Lockdown mode?  The ESXi host is controlled via a  vCenter Server and I'm able to connect to the system and perform other tasks.

Hers a quick snippet:

$soapmsg['this'] = new soapval('_this','SearchIndex','SearchIndex');
$soapmsg['dnsName'] = "esxi1";
$soapmsg['vmSearch'] = "false";
//  $result2 = $client->call('FindByIp',$soapmsg,$namespace);
$result2 = $client->call('FindByDnsName',$soapmsg,$namespace);

This works fine.  Just to test my code I run the following.

unset($soapmsg);
     $soapmsg['this'] = new soapval('_this','HostSystem',$result2);

$soapmsg['timeout'] = 10;

        $result3 = $client->call('ExitMaintenanceMode_Task',$soapmsg,$namespace);

-----------------

Now when I runs the next piece of code it fails.

$soapmsg['task'] = $result3;

$result4 = $client->call('EnterLockdownMode',$soapmsg,$namespace)

-----------------

This is the error I receive when I execute the query.

host-78 <=  This is good .. It found the ESXi host

----------

task-81060   <= This is good as well . It tried to take the ESXiout of Maintenance mode.

----------

Array

(

    [faultcode] => ServerFaultCode

    [faultstring] => Unable to resolve WSDL method name EnterLockdownMode for namespace name urn:vim25

while parsing SOAP body

at line 1, column 346

while parsing SOAP envelope

at line 1, column 43

while parsing HTTP request before method was determined

at line 1, column 0

    [detail] => Array

        (

            [InvalidRequestFault] =>

        )

)

-----

Has anyone had any luck with using the EnterLockdownMode method???

0 Kudos
1 Reply
CoolC1
Contributor
Contributor

I was able to get this to work on my own...

If you want to connect to VMware vcenter server or ESXi hosts (5.x0 then do the following;

1 )  Download the NuSoap files sourceforge.

Here's a sample of how to connect and "do something:.   You will have to use the SDK to create your own XML blocks of code to do what you want.

---------

// login example via serviceinstance
    require_once('assets/lib/nusoap.php');
    $wsdl = "https://$vcenter/sdk/vimService.wsdl";

    # SOAP settings
     ini_set("soap.wsdl_cache_enabled", "0");

    # SOAP object
    $client = new \SoapClient($wsdl,
     array(
              'trace'=> 1,
              'exceptions' => 0,
              'encoding' => 'UTF-8',
       'location' => "https://$vcenter/sdk" ,
              'passphrase' => ''
          )
    );
 

    // --------------------------------------------------------------------- \\
    $soapmsg['_this'] = new \soapvar('ServiceInstance', XSD_STRING,'ServiceInstance');
    $result = $client->RetrieveServiceContent($soapmsg);
    $service_instance = $result->returnval;
    //print "<textarea cols=120 rows=20>";
    // print_r($result);
    //print "</textarea>";
    // --------------------------------------------------------------------- \\


    // Login to the vCenter Server
    // --------------------------------------------------------------------- \\
    $soapmsg1["_this"] = $service_instance->sessionManager;
    $soapmsg1["userName"] = "$admin_username";
    $soapmsg1["password"] = "$admin_password";
    $result = $client->Login($soapmsg1);
    if ( isset($result->faultstring) ) {
     $err=$result->faultstring;
     return ("Login Failed. " . $err );
    };
    $soap_session = $result->returnval;
    //print "<textarea cols=120 rows=20>";
    // print_r($soap_session);
    //print "</textarea>";
    // --------------------------------------------------------------------- \\


    // Seacrh for and retirn the ESXi Host Object
    // --------------------------------------------------------------------- \\
    $soapmsg2["_this"] = $service_instance->searchIndex;
    $soapmsg2["dnsName"]="$esxi_hostname";
    $soapmsg2["vmSearch"]='0';
    $result = $client->FindByDnsName($soapmsg2);
    if ( isset($result->faultstring) ) {
     $err=$result->faultstring;
     return ("Search for Hostname Failed. " . $err );
    };
    $host_entity = $result->returnval;
    // print "<textarea cols=120 rows=20>";
    //  print_r($host_entity);
    // print "</textarea>";
    // --------------------------------------------------------------------- \\


    // Disable the lockdownMode
    // --------------------------------------------------------------------- \\
    $soapmsg3['_this'] = $host_entity;
    $result = $client->ExitLockdownMode($soapmsg3);
    if ( isset($result->faultstring) ) {
     $err=$result->faultstring;
     //return ("ExitLockDownMode Failed. " . $err );
    };
    //print "<textarea cols=120 rows=5>";
    //  print_r($result);
    //print "</textarea>";
    // --------------------------------------

That's it... for  connecting to the vcenter server and executing a method.

Now if you want to do queries you have to add blocks of XML code.   I did the following against the ESXi host.  But I guess it should also work with the vcenter server.

----------------------------------

function get_path($client,$objectid,$type,$path)

  {

      $xml = "

             <specSet>

               <propSet>

                 <type>$type</type>

                 <pathSet>$path</pathSet>

               </propSet>

               <objectSet>

                 <obj type='$type'>".$objectid."</obj>

               </objectSet>

             </specSet>";

        $soapmsg = NULL; // Reset the $soapmsg array..

     $soapmsg["_this"] = array( "_" => 'propertyCollector', "type" => "PropertyCollector");

        $soapmsg["specSet"] =  new \SoapVar($xml,XSD_ANYXML);

 

        return $client->RetrieveProperties($soapmsg);

  }

$rtr=get_path($client,$service_instance->rootFolder->_ ,'Folder','childEntity');

  $datacenter_ = $rtr->returnval->propSet->val->ManagedObjectReference->_; // This returns the DATACENTER NAME

$rtr=get_path($client,$datacenter_,'Datacenter','hostFolder');

  $hostFolder_ = $rtr->returnval->propSet->val->_; // This returns the hostFolder that holds the CLUSTER NAME

  $rtr=get_path($client,$hostFolder_,'Folder','childEntity');

  $cluster_ = $rtr->returnval->propSet->val->ManagedObjectReference->_; // This returns CLUSTER NAME

--------------------------------------

It took me forever to find out how to connect to VMware using PHP.  This information should be more readily available since PHP has been around for A-bazillion years.

Good luck!

0 Kudos