Skip to content

Commit

Permalink
feat(vms): query and find vms (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
Stanley288 authored and vivanov1410 committed Jun 30, 2016
1 parent 7c0c287 commit 7eb66d7
Show file tree
Hide file tree
Showing 9 changed files with 146 additions and 13 deletions.
48 changes: 45 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ $ npm install -g solarwinds

Commands:

node Nodes monitored by SolarWinds. `NODE` can be a **node id** or a **hostname**.
app-template Application templates. `APPTEMPLATE` can be an app template **id** or a **name**.
node Nodes monitored by SolarWinds. `NODE` can be a node id or a hostname.
vm Virtual machines accessible by SolarWinds. `VM` can be a vm id or a vm name.
app-template Application templates. `APPTEMPLATE` can be an app template id or a name.
help [cmd] display help for [cmd]

Options:
Expand Down Expand Up @@ -182,6 +183,47 @@ Application templates. `APPTEMPLATE` can be a application template **id** or a *
-h, --help output usage information
```

### Virtual Machines

Virtual machines accessible by SolarWinds. `VM` can be a vm **id** or a vm **name**.

```console
Usage: solarwinds vm [options] [command]

Commands:

list|ls List all available virtual machines
inspect <VM> Display detailed information about a virtual machine

Options:

-h, --help output usage information
```

#### vm list

```console
Usage: solarwinds vm list|ls [options]

List all available virtual machines

Options:

-h, --help output usage information
```

#### vm inspect

```console
Usage: solarwinds vm inspect [options] <VM>

Display detailed information about a virtual machine

Options:

-h, --help output usage information
```

## Library Usage

This library by default uses the environment variables specified above that can be overwritten:
Expand All @@ -196,4 +238,4 @@ const nodes = await solarwinds.nodes.query()

## Resources

- [SolarWinds API Schema](http://solarwinds.github.io/OrionSDK/schema/index.html)
- [SolarWinds API Schema](http://solarwinds.github.io/OrionSDK/schema/index.html)
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "solarwinds",
"version": "0.1.0",
"version": "0.2.0",
"description": "The Node.js library and CLI for the Solarwinds API",
"main": "./lib/index.js",
"bin": {
Expand Down Expand Up @@ -31,6 +31,7 @@
"babel-polyfill": "^6.9.1",
"camelcase-keys": "^3.0.0",
"commander": "^2.9.0",
"easy-table": "^1.0.0",
"is_js": "^0.8.0",
"moment": "^2.13.0",
"parse-duration": "^0.1.1",
Expand Down
4 changes: 3 additions & 1 deletion playground.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import SolarWinds from './src'
import Table from 'easy-table'

const solarwinds = new SolarWinds()

async function app() {
try {
// const vms = await solarwinds.virtualMachines.query()
const vms = await solarwinds.virtualMachines.query()
const find = await solarwinds.virtualMachines.find(vms[144]['virtualMachineID'])
// const node = await solarwinds.nodes.create({
// name: 'DevDocker03',
// ip: '10.0.8.162',
Expand Down
6 changes: 6 additions & 0 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class Client {
qs: { query: q },
}, (error, response, body) => {
if (error) return reject(error)
if (response.statusCode >= 300) return reject(body)
if (is.undefined(body)) return reject(new Error('Invalid response from SolarWinds API'))

resolve(body.results.map(x => camelize(x)))
Expand All @@ -46,6 +47,7 @@ class Client {
body: data,
}, (error, response, body) => {
if (error) return reject(error)
if (response.statusCode >= 300) return reject(body)

resolve(body)
})
Expand All @@ -70,6 +72,8 @@ class Client {
return new Promise((resolve, reject) => {
this.request.get(`${this.longUrl}/${route}`, (error, response, body) => {
if (error) return reject(error)
if (response.statusCode >= 300) return reject(body)

resolve(camelize(body))
})
})
Expand All @@ -79,6 +83,8 @@ class Client {
return new Promise((resolve, reject) => {
this.request.delete(`${this.longUrl}/${route}`, (error, response, body) => {
if (error) return reject(error)
if (response.statusCode >= 300) return reject(body)

resolve(body)
})
})
Expand Down
13 changes: 13 additions & 0 deletions src/models/virtualMachine.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
class VirtualMachine {
static props = ['virtualMachineID', 'managedObjectID', 'uuid', 'hostID', 'nodeID', 'resourcePoolID', 'vMConfigFile', 'memoryConfigured', 'memoryShares', 'cPUShares', 'guestState', 'iPAddress', 'logDirectory', 'guestVmWareToolsVersion', 'guestVmWareToolsStatus', 'name', 'guestName', 'guestFamily', 'guestDnsName', 'nicCount', 'vDisksCount', 'processorCount', 'powerState', 'bootTime', 'configStatus', 'overallStatus', 'nodeStatus', 'networkUsageRate', 'networkTransmitRate', 'networkReceiveRate', 'cpuLoad', 'cpuUsageMHz', 'memUsage', 'memUsageMB', 'isLicensed', 'platformID', 'pollingSource', 'relativePath', 'datastoreIdentifier', 'cpuReady', 'swappedMemoryUtilization', 'swappedMemoryUtilizationPercent', 'balloonMemload', 'balloonMemloadPercent', 'iOPSTotal', 'iOPSRead', 'iOPSWrite', 'latencyTotal', 'latencyRead', 'latencyWrite', 'snapshotStorageSize', 'consumedMemLoad', 'consumedPercentMemLoad', 'lastActivityDate', 'totalStorageSize', 'totalStorageSizeUsed', 'volumeSummaryCapacity', 'volumeSummaryFreeSpace', 'volumeSummaryFreeSpacePercent', 'volumeSummaryCapacityDepletionDate', 'triggeredAlarmDescription', 'orionIdPrefix', 'orionIdColumn', 'runTime', 'cpuCostop', 'dynamicMemoryEnabled', 'snapshotSummaryCount', 'dateCreated', 'oldestSnapshotDate', 'heartBeat', 'snapshotDateModified', 'memoryAllocationLimit', 'virtualDiskDateModified', 'detailsUrl', 'status', 'statusDescription', 'statusLED', 'unManaged', 'unManageFrom', 'unManageUntil', 'image', 'ancestorDisplayNames', 'ancestorDetailsUrls', 'statusIconHint', 'displayName', 'description', 'instanceType', 'uri']

constructor(vm) {
Object.keys(vm).forEach(key => {
Object.assign(this, { [key]: vm[key] })
})
}

os() {
switch (this.guestFamily) {
case 'windowsGuest':
return 'Windows'
case 'linuxGuest':
return 'Linux'
default:
return 'Unknown'
}
}
}

export default VirtualMachine
1 change: 0 additions & 1 deletion src/solarwinds-app-template.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#!/usr/bin/env node

import program from 'commander'
import is from 'is_js'

import { print, error } from './utils'
import SolarWinds from './'
Expand Down
48 changes: 48 additions & 0 deletions src/solarwinds-vm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/usr/bin/env node

import program from 'commander'
import Table from 'easy-table'

import { print, error } from './utils'
import SolarWinds from './'

const solarwinds = new SolarWinds()

program
.command('list')
.alias('ls')
.description('List all available virtual machines')
.action(async () => {
try {
const vms = await solarwinds.virtualMachines.query()
const table = new Table()

vms.forEach(vm => {
table.cell('ID', vm.virtualMachineID)
table.cell('NAME', vm.name)
table.cell('IP', vm.iPAddress)
table.cell('OS', vm.os())
table.newRow()
})

console.log(table.toString())
} catch (err) {
error(err)
process.exit(1)
}
})

program
.command('inspect <VM>')
.description('Display detailed information about a virtual machine')
.action(async (id) => {
try {
const vm = isNaN(id) ? await solarwinds.virtualMachines.findByName(id) : await solarwinds.virtualMachines.find(id)
print(vm)
} catch (err) {
error(err)
process.exit(1)
}
})

program.parse(process.argv)
6 changes: 4 additions & 2 deletions src/solarwinds.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import { version } from '../package.json'

program
.version(version)
.command('node', 'Nodes monitored by SolarWinds. `NODE` can be a **node id** or a **hostname**.')
.command('app-template', 'Application templates. `APPTEMPLATE` can be an app template **id** or a **name**.')
.command('node', 'Nodes monitored by SolarWinds. `NODE` can be a node id or a hostname.')
.command('vm', 'Virtual machines accessible by SolarWinds. `VM` can be a vm id or a vm name.')
.command('app-template', 'Application templates. `APPTEMPLATE` can be an app template id or a name.')

.parse(process.argv)

if (!program.args.length) program.help()
30 changes: 25 additions & 5 deletions src/virtualMachines.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,40 @@
import VirtualMachine from './models/virtualMachine'

const props = [
'VirtualMachineID AS id', 'Name', 'IPAddress', 'NodeID',
'StatusDescription',
].join(', ')

class VirtualMachines {
props = VirtualMachine.props.join()
table = 'Orion.VIM.VirtualMachines'

constructor(client) {
this.client = client
}

async query() {
const vms = await this.client.query(`SELECT ${props} FROM Orion.VIM.VirtualMachines`)
const vms = await this.client.query(`
SELECT ${this.props}
FROM ${this.table}
ORDER BY VirtualMachineID
`)

return vms.map(x => new VirtualMachine(x))
}

async find(id) {
const res = await this.client.read(`Orion/${this.table}/VirtualMachineID=${id}`)

return new VirtualMachine(res)
}

async findByName(name) {
const res = await this.client.query(`
SELECT TOP 1 ${this.props}
FROM ${this.table}
WHERE name LIKE '%${name.toLowerCase()}%'
`)

return new VirtualMachine(res[0])
}

}

export default VirtualMachines

0 comments on commit 7eb66d7

Please sign in to comment.