/
OnmsServer.ts
109 lines (98 loc) · 3.2 KB
/
OnmsServer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/** @hidden */
// tslint:disable-next-line
const URI = require('urijs');
import {OnmsAuthConfig} from '../api/OnmsAuthConfig';
import {ServerMetadata} from './ServerMetadata';
import {ServerTypes} from './ServerType';
import {UUID} from '../internal/UUID';
/**
* Represents a remote OpenNMS server.
* @module OnmsServer
*/
export class OnmsServer {
/** A unique identifier for this server. */
public id: string;
/** An optional name associated with this server. */
public name?: string;
/** The base URL to the server. */
public url: string;
/** The authorization configuration associated with the server. */
public auth: OnmsAuthConfig;
/** The capabilities of the server */
public metadata: ServerMetadata;
/**
* Construct a new OnmsServer object representing a remote server.
* @example
* <caption>provide a pre-existing [[OnmsAuthConfig]] for auth</caption>
* ```javascript
* const server = new OnmsServer('Test', 'https://myserver/opennms/', auth);
* ```
* @example
* <caption>provide a username and password for auth</caption>
* ```javascript
* const server = new OnmsServer('Test', 'https://myserver/opennms/', 'admin', 'admin');
* ```
* @constructor
* @param name - A name for the server suitable for display.
* @param url - The URL to the server.
* @param auth - An [[OnmsAuthConfig]], or the username to authorize as.
* @param password - The password to authorize with if a username was
* supplied to the `auth` parameter.
*/
constructor(name?: string, url?: string, auth?: OnmsAuthConfig | string, password?: string) {
this.id = UUID.generate();
this.name = name;
this.url = url;
if (auth instanceof OnmsAuthConfig) {
this.auth = auth;
} else {
this.auth = new OnmsAuthConfig(auth, password);
}
}
/**
* Given a relative URL fragment, construct a URL for that fragment on the server.
* @param forFragment - The URL fragment to append to the server URL.
* @returns A complete URL.
*/
public resolveURL(forFragment?: string) {
if (!this.url) {
return undefined;
}
if (forFragment === undefined) {
return this.url;
}
if (forFragment.indexOf('/') === 0 || forFragment.indexOf('http') === 0) {
return forFragment;
}
return URI(this.url).segment(forFragment).toString();
}
/**
* Create a new server object from this existing one.
*/
public clone() {
const auth = (this.auth ? this.auth.clone() : undefined);
const ret = new OnmsServer(this.name, this.url, auth);
ret.metadata = (this.metadata ? this.metadata.clone() : undefined);
return ret;
}
/**
* Get the hostname portion of the URL associated with this server.
*/
get host() {
if (!this.url) {
return undefined;
}
return URI(this.url).hostname();
}
/** A string representation of this server suitable for display. */
public toString() {
if (this.metadata) {
return 'OpenNMS '
+ (this.metadata.type === ServerTypes.MERIDIAN ? 'Meridian' : 'Horizon')
+ ' ' + this.metadata.version.displayVersion
+ ' at ' + (this.host || this.url);
} else {
return 'OpenNMS at ' + (this.host || this.url);
}
}
}