Giter Club home page Giter Club logo

vsphere-connect's Introduction

vsphere-connect

A modern vSphere Client

Currently undergoing a major re-write see development branch for current progress

Project Page

Summary

vSphere connect is a modern vSphere client that has been re-designed to mimic RethinkDB's ReQL and provide a way to construct powerful and complex requests easily. vsphere-connect also provides pseudo-realtime changefeeds that take advantage of the vSphere WaitForUpdatesEx to provided change events through an RxJS Observable

Example

Return a list of VirtualMachines with 2 CPUs

import VConnect from 'vsphere-connect'
let v = VConnect('vcenter.mydomain.com')
  .login('[email protected]', 'vmware100')
  
v.type('vm')
  .pluck({
    config: {
      hardware: {
        numCPU: true
      }
    }
  })
  .filter(vm => {
    return v.expr(vm)('config')('hardware')('numCPU')
      .default(0)
      .eq(2)
  })('name')
  .then(console.log)

Example

Subscribe to changes on 2 VMs

import VConnect from 'vsphere-connect'
let v = VConnect('vcenter.mydomain.com')
  .login('[email protected]', 'vmware100')

v.type('vm')
  .allData()
  .get('vm-10', 'vm-54')
  .changes()
  .subscribe(
    change => {
      console.log(change)
    },
    error => {
      console.error(error)
    },
    () => {
      console.log('complete')
    }
  )

vsphere-connect's People

Contributors

bhoriuchi avatar dependabot[bot] avatar unimea avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

vsphere-connect's Issues

nsp alerts for version 1.1.2

Branden,

The current release of has two nsp alerts for [email protected], but the master branch looks like it has updated these along with a bunch of other fixes.

Are you planning on creating a new release that includes the updates in master and updates this reference to soap?

When a newer vSphere version is encountered, an error is thrown

When attempting to use vsphere-connect against e.g. ESXi 6.5.0a, it throws an error.

I would expect it to instead use the latest API version it understands (6.0) when it encounters a version which is too new.

Steps to reproduce against ESXi 6.5.0a:

> var connect = require('vsphere-connect');
> connect.createClient({host: 'esxi.example.com'})
Unhandled rejection TypeError: env.versions.join is not a function
    at Object.getSchema (/path/to/vsphere-schema/lib/utils/index.js:59:95)
    at /path/to/vsphere-connect/lib/client/index.js:156:54
...

Forcing the "API Version" to be '6.0' instead of assuming it can use whatever the server returns appears to work around this problem:

> var connect = require('vsphere-connect');
> Object.defineProperty(connect.Client.prototype, 'apiVersion', {get: function() {return '6.0'}})
> connect.createClient({host: 'esxi.example.com'})

The problematic code appears to be in lib/client/index.js where it calls RetrieveServiceContent then assumes that it can understand whatever API version the server returns in 'about.apiVersion', without checking whether this version is understood by the vsphere-schema library.

ReconfigVM_Task disk edit

Hello,

Apparently i am doing something wrong with the reconfigvm task but i can't figure out what the prblem in there.

          'ReconfigVM_Task',
          {
            _this: util.moRef('VirtualMachine', virtualMachine.moref),
            spec: {
              deviceChange: {
                device: {
                  key: disk.key,
                  capacityInKB: disk.capacityInKB,
                  backing: {
                    fileName: disk.backing.fileName,
                    diskMode: disk.backing.diskMode,
                  },
                },
                operation: 'edit',
              },
            },
          },```
{ errorCode: 400,
  message: 'Required property key is missing from data object of type VirtualDevice\n\nwhile parsing serialized DataObject of type vim.vm.device.VirtualDevice\nat line 1, column 388\n\nwhile parsing property "device" of static type VirtualDevice\n\nwhile parsing serialized DataObject of type vim.vm.device.VirtualDeviceSpec\nat line 1, column 347\n\nwhile parsing property "deviceChange" of static type ArrayOfVirtualDeviceConfigSpec\n\nwhile parsing serialized DataObject of type vim.vm.ConfigSpec\nat line 1, column 341\n\nwhile parsing call information for method ReconfigVM_Task\nat line 1, column 281\n\nwhile parsing SOAP body\nat line 1, column 270\n\nwhile parsing SOAP envelope\nat line 1, column 38\n\nwhile parsing HTTP request for method reconfigure\non object of type vim.VirtualMachine\nat line 1, column 0' }

Wait method (Task) to finish?

Seems that on old API there was client.monitor.task to track Task to finish (i.e. CreateSnapshot_Task).
Is there some similar way to track Task status with new API?

Support for 6.7.1

Could you please update this library to support for vsphere 6.7.1?

I am so appreciate your work. Thank you so much.

Can't pass parameters to method

Looks like method invocation is not slotting arguments into SOAP document properly.

Example:

// list all files in a given datastore
async function listFiles( client, datastore ) {
    let flags = {
        fileSize: true,
        fileType:  true,
        modification: true
    };
    let spec = {
        details: flags
    }
    let moref = await vconnect.util.moRef( 'HostDatastoreBrowser', datastore.browser );
    return await client.method( 'SearchDatastore_Task', {
        _this: moref,
        dataStorePath: '/',
        searchSpec: spec
    }).then(function(result) {
        console.log( "Found " + results.length + " files on " + datastore.name );
        return results;
    }).caught(function(err) {
        console.error( "Can't list files: " err );
        throw(err);
    });
}

Returns this:

Can't list files: {"errorCode":400,"message":"Got <dataStorePath> tag when expecting <_this> tag\n\nwhile parsing call information for method SearchDatastore_Task\nat line 1, column 281\n\nwhile parsing SOAP body\nat line 1, column 270\n\nwhile parsing SOAP envelope\nat line 1, column 38\n\nwhile parsing HTTP request for method search\non object of type vim.host.DatastoreBrowser\nat line 1, column 0"}

Failed to import ova

I trying to import ova using ova file and the script fails with the below error

2017-11-24T10:24:27.388Z error vpxd[7F996F9F3700] [Originator@6876 sub=SoapAdapter]
--> Required property id is missing from data object of type OvfConsumerOstNode
-->
--> while parsing serialized DataObject of type vim.OvfConsumer.OstNode
--> at line 11, column 8
-->
--> while parsing property "instantiationOst" of static type OvfConsumerOstNode
-->
--> while parsing serialized DataObject of type vim.ImportSpec
--> at line 7, column 6
-->
--> while parsing call information for method ImportVApp
--> at line 5, column 4
-->
--> while parsing SOAP body
--> at line 4, column 2
-->
--> while parsing SOAP envelope
--> at line 2, column 0
-->
--> while parsing HTTP request for method importVApp
--> on object of type vim.ResourcePool
--> at line 1, column 0
2017-11-24T10:24:31.053Z info vpxd[7F996F8F1700] [Originator@6876 sub=vpxLro opID=2dc64d86] [VpxLRO] -- BEGIN task-internal-18777 -- SessionManager -- vim.SessionManager.sessionIsActive -- 520abc27-ebc2-2ccc-42bf-864790e61a93(52ef17e8-51fb-0da9-5fca-d490400cd24d)

Below is the code

"use strict";

let username = '<user>';
let password = '<password>';
let host = '<vCenter host>';
const VConnect = require('vsphere-connect');
const fs = require('fs');

let v = VConnect(host, {ignoreSSL: true})
v.createClient().then(client => {
    return client.login(username, password).then((c) => {
     const vim = client._soapClient.types.vim25;
     return client.retrieve({
       type: 'ClusterComputeResource',
       id: 'domain-c7'
     }).then((clu) => {
       const cluster = clu[0].moRef;
       console.log(cluster);
       return client.retrieve({
         type: 'Network',
         id: 'network-22'
       }).then((net) => {
         const network = net[0].moRef;
         console.log(network);
         return client.retrieve({
           type: 'Datastore',
           id: 'datastore-154'
         }).then((dst) => {
           const ds = dst[0].moRef;
           console.log(ds);
           return client.retrieve({
             type: 'Folder',
             id: 'group-v3'
           }).then((fl) => {
             const folder = fl[0].moRef;
             console.log(folder);
             return client.retrieve({
               type: 'ResourcePool',
               id: 'resgroup-8'
             }).then((rpool) => {
               const rp = rpool[0].moRef;
               console.log(rp);
               // download https://communities.vmware.com/servlet/JiveServlet/downloadBody/21621-102-3-28798/Tiny%20Linux%20VM.ova and untar
               const ovfDescriptor1 = fs.readFileSync('~/Tiny Linux VM.ovf').toString();
               const specParams = getImportSpecParams(vim, network);
               return client.method('CreateImportSpec', {
                 _this: client.serviceContent.ovfManager,
                  ovfDescriptor: ovfDescriptor1,
                  resourcePool: rp,
                  datastore: ds,
                  cisp: { locale: 'US', deploymentOption: '', entityName: 'testvmw' }
               }).then((imspec) => {
                 console.log('imspec', imspec.importSpec);
                 console.log(imspec.importSpec)
                 return client.method('ImportVApp', {
                   _this: rp,
                   spec: imspec.importSpec,
                   folder: folder
                 }).then((vapp) => {
                   console.log('vapp', vapp);
                 })
               }).catch((err1) =>  console.log('err1', err1))
             })
             })
           })
         })
       })
     })
}).catch((err) =>  console.log('err', err))

function getImportSpecParams(vim, network) {
  const propertyMappings = [];
  const ovfPropertyKeys = {
    host_ip: host,
    host_username: username,
    host_password: password,
    proxy: '172.18.192.3',
    static_ip: '172.18.200.186',
    static_netmask: '255.255.255.0',
    static_gateway: '172.18.192.20',
    static_dns: '172.18.192.5'
  };

  for (const key in ovfPropertyKeys) {
    const param = new vim.KeyValue();
    param.key = key;
    param.value = ovfPropertyKeys[key];
    propertyMappings.push(param);
  }

  const networkMapping = new vim.OvfNetworkMapping();
  networkMapping.name = 'Network';
  networkMapping.network = network;

  const importSpecParams = new vim.OvfCreateImportSpecParams();
  importSpecParams.entityName = 'testvm1';
  importSpecParams.diskProvisioning = 'thin';
  importSpecParams.propertyMapping = propertyMappings;
  importSpecParams.ipAllocationPolicy = 'fixedPolicy';
  importSpecParams.deploymentOption = '';

  importSpecParams.locale = 'US';
  importSpecParams.networkMapping = [networkMapping];
  return importSpecParams;
}

Support for vCenter 6.5 schema

It would be great if you can upgrade the package.json to use latest git version of vsphere-schema., which includes 6.5 support

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.