Skip to content

Commit

Permalink
feat(vms): add filtering (#31)
Browse files Browse the repository at this point in the history
Closes #25
  • Loading branch information
Stanley288 authored and vivanov1410 committed Jul 5, 2016
1 parent 161447b commit 83f4865
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 18 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "solarwinds",
"version": "0.3.0",
"version": "0.3.1",
"description": "The Node.js library and CLI for the Solarwinds API",
"main": "./lib/index.js",
"bin": {
Expand Down
18 changes: 14 additions & 4 deletions playground.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,18 @@ const solarwinds = new SolarWinds()

async function app() {
try {
const vms = await solarwinds.virtualMachines.query()
const find = await solarwinds.virtualMachines.find(vms[144]['virtualMachineID'])
// const vms = await solarwinds.virtualMachines.query()
// const find = await solarwinds.virtualMachines.find(vms[144]['virtualMachineID'])
// const vms = await solarwinds.virtualMachines.filterIp('1')
// 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()
// })
// const node = await solarwinds.nodes.create({
// name: 'DevDocker03',
// ip: '10.0.8.162',
Expand All @@ -15,13 +25,13 @@ async function app() {
// const nodes = await solarwinds.nodes.query()
// const appTemplates = await solarwinds.applicationTemplates.query()
// const credentials = await solarwinds.credentials.query()
const credential = await solarwinds.credentials.findByName('wb2')
// const credential = await solarwinds.credentials.findByName('wb2')
// const appTemplate = await solarwinds.applicationTemplates.find(94)
// const appTemplate = await solarwinds.applicationTemplates.findByName('COS Linux Basic Processes')
// const nodes = await solarwinds.nodes.remanage(403)
// const node = await solarwinds.nodes.find(403)
// node.unmanage(1)
console.log(credential)
// console.log(table.toString())
} catch (err) {
console.error(err)
}
Expand Down
20 changes: 15 additions & 5 deletions src/solarwinds-vm.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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

import { print, error } from './utils'
import SolarWinds from './'
Expand All @@ -11,12 +12,19 @@ const solarwinds = new SolarWinds()
program
.command('list')
.alias('ls')
.option('--filter <filter>', 'Filter by key, for example Name, IP or ID')
.description('List all available virtual machines')
.action(async () => {
.action(async (options) => {
try {
const vms = await solarwinds.virtualMachines.query()
const table = new Table()
let filter
if (options.filter) {
const [key, value] = options.filter.split('=')
filter = { [key]: value }
}

const vms = await solarwinds.virtualMachines.query(filter)

const table = new Table()
vms.forEach(vm => {
table.cell('ID', vm.virtualMachineID)
table.cell('NAME', vm.name)
Expand All @@ -25,7 +33,7 @@ program
table.newRow()
})

console.log(table.toString())
console.log(table.toString()) // eslint-disable-line
} catch (err) {
error(err)
process.exit(1)
Expand All @@ -37,7 +45,9 @@ program
.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)
const vm = is.number(id)
? await solarwinds.virtualMachines.find(id)
: await solarwinds.virtualMachines.findByName(id)
print(vm)
} catch (err) {
error(err)
Expand Down
35 changes: 27 additions & 8 deletions src/virtualMachines.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,35 @@
import VirtualMachine from './models/virtualMachine'


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

constructor(client) {
this.client = client
}

async query() {
const vms = await this.client.query(`
SELECT ${this.props}
FROM ${this.table}
ORDER BY VirtualMachineID
`)
async query(filter) {
let vms
if (filter) {
if (filter.hasOwnProperty('name')) {
vms = await this.client.query(this.getQuery('name', filter.name.toLowerCase()))
} else if (filter.hasOwnProperty('ip')) {
vms = await this.client.query(this.getQuery('iPAddress', filter.ip))
} else {
vms = await this.client.query(this.getQuery('virtualMachineID', filter.id))
}
} else {
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}`)

Expand All @@ -35,6 +46,14 @@ class VirtualMachines {
return new VirtualMachine(res[0])
}

getQuery(key, value) {
return `
SELECT ${this.props}
FROM ${this.table}
WHERE ${key} LIKE '%${value}%'
ORDER BY ${key}
`
}
}

export default VirtualMachines

0 comments on commit 83f4865

Please sign in to comment.