Skip to content

Commit

Permalink
feat: improve error handling and credentials check (#14)
Browse files Browse the repository at this point in the history
Improve error handling and credentials check

Closes #5
  • Loading branch information
vivanov1410 authored and Stanley288 committed Jun 29, 2016
1 parent a3a7794 commit edba388
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 21 deletions.
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.0.6",
"version": "0.0.7",
"description": "The Node.js library and CLI for the Solarwinds API",
"main": "./lib/solarwinds",
"bin": {
Expand Down Expand Up @@ -31,6 +31,7 @@
"babel-polyfill": "^6.9.1",
"camelcase-keys": "^3.0.0",
"commander": "^2.9.0",
"is_js": "^0.8.0",
"moment": "^2.13.0",
"parse-duration": "^0.1.1",
"request": "^2.72.0"
Expand Down
59 changes: 40 additions & 19 deletions src/cli-node.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#!/usr/bin/env node

import program from 'commander'
import { print, error } from './utils'
import is from 'is_js'

import { print } from './utils'
import SolarWinds from './solarwinds'

const solarwinds = new SolarWinds()
Expand All @@ -19,8 +21,8 @@ program
ip,
})
print(node)
} catch (e) {
error(e)
} catch (err) {
throw new Error(err)
}
})

Expand All @@ -29,16 +31,24 @@ program
.alias('ls')
.description('List all available nodes')
.action(async () => {
const nodes = await solarwinds.nodes.query()
print(nodes)
try {
const nodes = await solarwinds.nodes.query()
print(nodes)
} catch (err) {
throw new Error(err)
}
})

program
.command('inspect <NODE>')
.description('Display detailed information about a node')
.action(async (id) => {
const node = isNaN(id) ? await solarwinds.nodes.findByName(id) : await solarwinds.nodes.find(id)
print(node)
try {
const node = isNaN(id) ? await solarwinds.nodes.findByName(id) : await solarwinds.nodes.find(id)
print(node)
} catch (err) {
throw new Error(err)
}
})

program
Expand All @@ -47,33 +57,44 @@ program
.option('-d, --duration <value>', 'Duration, for example 15s, 30m, 3h or 1d')
.action(async (id, options) => {
const { duration } = options
if (typeof duration === 'undefined') {
console.error('duration is required')
process.exit(1)
if (is.undefined(duration)) {
throw new Error('Provide duration option')
}

const node = isNaN(id) ? await solarwinds.nodes.findByName(id) : await solarwinds.nodes.find(id)
const result = await solarwinds.nodes.unmanage(node.id, duration)
print(result)
try {
const node = isNaN(id) ? await solarwinds.nodes.findByName(id) : await solarwinds.nodes.find(id)
const result = await solarwinds.nodes.unmanage(node.id, duration)
print(result)
} catch (err) {
throw new Error(err)
}
})

program
.command('remanage <NODE>')
.description('Remanage node')
.action(async (id) => {
const node = isNaN(id) ? await solarwinds.nodes.findByName(id) : await solarwinds.nodes.find(id)
const result = await solarwinds.nodes.remanage(node.id)
print(result)
try {
const node = isNaN(id) ? await solarwinds.nodes.findByName(id) : await solarwinds.nodes.find(id)
const result = await solarwinds.nodes.remanage(node.id)
print(result)
} catch (err) {
throw new Error(err)
}
})

program
.command('remove <NODE>')
.alias('rm')
.description('Remove node')
.action(async (id) => {
const node = isNaN(id) ? await solarwinds.nodes.findByName(id) : await solarwinds.nodes.find(id)
await solarwinds.nodes.remove(node.id)
print(node.id)
try {
const node = isNaN(id) ? await solarwinds.nodes.findByName(id) : await solarwinds.nodes.find(id)
await solarwinds.nodes.remove(node.id)
print(node.id)
} catch (err) {
throw new Error(err)
}
})

program.parse(process.argv)
17 changes: 16 additions & 1 deletion src/client.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
import request from 'request'
import is from 'is_js'

import { camelize } from './utils'

class Client {
constructor(username, password, hostname, port) {
this.shortUrl = `https://${hostname}:${port}/SolarWinds/InformationService/V3/Json`
this.longUrl = `${this.shortUrl}/swis://${hostname}`
this.request = request.defaults({
// baseUrl: `${url}/SolarWinds/InformationService/V3/Json`,
auth: { username, password },
json: true,
strictSSL: false,
})

this.checkCredentials()
}

checkCredentials() {
this.request.get({
url: `${this.shortUrl}/Query`,
qs: { query: 'SELECT TOP 1 NodeID FROM Orion.Nodes' },
}, (error, response, body) => {
if (error || is.undefined(body)) throw new Error('Invalid SolarWinds username or password')
})
}

query(q) {
Expand All @@ -20,6 +32,8 @@ class Client {
qs: { query: q },
}, (error, response, body) => {
if (error) return reject(error)
if (is.undefined(body)) return reject(new Error('Invalid response from SolarWinds API'))

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

resolve(body)
})
})
Expand Down
8 changes: 8 additions & 0 deletions src/solarwinds.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import is from 'is_js'

import Client from './client'
import Nodes from './nodes'
import VirtualMachines from './virtualMachines'
Expand All @@ -18,7 +20,13 @@ class Landscape {
username = SOLARWINDS_USERNAME, password = SOLARWINDS_PASSWORD,
hostname = SOLARWINDS_HOSTNAME, port = SOLARWINDS_PORT
) {
if (is.undefined(username) || is.empty(username)) throw new Error('Provide username for SolarWinds or env variable SOLARWINDS_USERNAME')
if (is.undefined(password) || is.empty(password)) throw new Error('Provide password for SolarWinds or env variable SOLARWINDS_PASSWORD')
if (is.undefined(hostname) || is.empty(hostname)) throw new Error('Provide hostname of SolarWinds API or env variable SOLARWINDS_HOSTNAME')
if (is.undefined(port) || is.empty(port)) throw new Error('Provide port of SolarWinds API or env variable SOLARWINDS_PORT')

this.client = new Client(username, password, hostname, port)

this.nodes = new Nodes(this.client)
this.virtualMachines = new VirtualMachines(this.client)
}
Expand Down

0 comments on commit edba388

Please sign in to comment.