|
|
If you arelucky enough to program against ESX VIM/vSphere SDK before, you can selecteither Java or C#, as the sample codes use in SDK package. As a matter of fact,Java/C# is good at such job. However, you have to use C/C++ if they are bothunavailable. As it is very complicate to use the SDK with C/C++ tool chain,VMware is reluctant to give more samples indeed. Googling result on this topic ischaos at all and all you can do is continuous trying, check to the doc and MOB.More, the codes you get are totally unsalable and fragmentary. Hardcodes and tricksgo everywhere.
Things getchanged.
With thebasis of previous partial completed job, I spent some time recently and finalget rid of nearly all the complex. How can you list all the VMs and its statuson 10.32.230.100, for example? You can
#include <iostream>#include <cassert>#include "vim25VimBindingProxy.h"#include "vim25.h"int main(int argc, char* argv[]){try{using namespace vim25;binding_proxy proxy("10.32.230.100", true);ServiceInstance si(get_service_instance(&proxy));ns2__ServiceContent sc = si.RetrieveServiceContent();SessionManager sm(sc.sessionManager, &proxy);sm.Login("administrator", "unknown:)", NULL);std::string version = sc.about->version;Folder root_folder(sc.rootFolder, &proxy);std::vector<ManagedEntity> vd = root_folder.get_childEntity();for (size_t i = 0; i < vd.size(); ++i){if (vd.is_type_of(Datacenter::type())){Datacenter dc(vd, &proxy);Folder vm_folder = dc.get_vmFolder();std::vector<ManagedEntity> vv = vm_folder.get_childEntity();for (size_t i = 0; i < vv.size(); ++i){if (vv.is_type_of(VirtualMachine::type())){VirtualMachine vm(vv, &proxy);std::cout << "Name: " << vm.get_name() << ", Status:" << vm.get_runtime().powerState << std::endl;}}}}sm.Logout();return 0;}catch (const std::string& err){std::cout << "exception: " << err;return 1;}}
No hardcodeand very straight. More important, it is identical as you do in MOB. All knowledgeyou get from MOB works here. As a comparing, the C# codes with samefunctionality are over 500 LOC.
Under thehood, there are about 10K well formatted lines of C++ codes. If you want tobrowse the whole object library, you need another 20K lines of C++ code (notcompleted). As there is no platform specific code, you can use it anywhere onlyif g++ is ready.
Under thehood of hood, all of those codes are generated by about 1,500 lines of Perlcode. It will first parse all VIM/vSphere help docs and generate a temporary file;then it parses stub header file which is generated by gSOAP tool. At last, itmerges all of that information and gives us running C++ codes.
I'll make it public available soon so that it can serve others also.
版权声明:本文为博主原创文章,未经博主允许不得转载。 |
|