-
Notifications
You must be signed in to change notification settings - Fork 8
/
OnmsServer.ts
223 lines (197 loc) · 5.78 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// tslint:disable:max-classes-per-file
/** @hidden */
// tslint:disable-next-line
const URI = require('urijs');
import {OnmsAuthConfig} from '../api/OnmsAuthConfig';
import {ServerMetadata} from './ServerMetadata';
import {ServerTypes} from './ServerType';
import {MD5} from 'object-hash';
/**
* A builder for [[OnmsServer]]. Create a new one with [[OnmsServer.newBuilder]].
* @category Rest
*/
// tslint:disable:completed-docs variable-name whitespace
export class OnmsServerBuilder {
/** @hidden */
private _name?: string;
/** @hidden */
private _url?: string;
/** @hidden */
private _auth?: OnmsAuthConfig;
/** @hidden */
private _metadata?: ServerMetadata;
/**
* Construct a new builder from an existing options object, if provided.
*/
public constructor(url?: string) {
this._url = url;
}
/** Build the [[OnmsServer]] object. */
public build(): OnmsServer {
return new OnmsServer(this);
}
/**
* The display name of the server.
*
* If `undefined` is passed, the name will be unset.
* @param name the server name
*/
public setName(name?: string) {
this._name = name;
return this;
}
/**
* The URL of the server.
*
* If `undefined` is passed, the URL will be unset.
* @param url the server's URL
*/
public setUrl(url?: string) {
this._url = url;
return this;
}
/**
* The authentication config to use when connecting.
*
* If `undefined` is passed, the default authentication settings will be used.
* @param auth the authentication config
*/
public setAuth(auth?: OnmsAuthConfig) {
this._auth = auth;
return this;
}
/**
* The server metadata to associate with the server.
*
* If `undefined` is passed, no metadata will be used.
* @param metadata the metadata
*/
public setMetadata(metadata?: ServerMetadata) {
this._metadata = metadata;
return this;
}
public get name() {
return this._name;
}
public get url() {
return this._url;
}
public get auth() {
return this._auth;
}
public get metadata() {
return this._metadata;
}
}
// tslint:enable:completed-docs variable-name whitespace
/**
* Represents a remote OpenNMS server.
* @category Rest
*/
export class OnmsServer {
/**
* Create a new builder for an [[OnmsServer]] object.
* @param server if an existing server object is passed, the builder will be pre-populated
*/
public static newBuilder(url?: string) {
return new OnmsServerBuilder(url);
}
/** A unique identifier for this server. */
public readonly id: string;
/** An optional name associated with this server. */
public readonly name?: string;
/** The base URL to the server. */
public readonly url: string;
/** The authorization configuration associated with the server. */
public readonly auth: OnmsAuthConfig | null;
/** The capabilities of the server */
public readonly metadata: ServerMetadata | null;
/**
* 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);
* ```
*
* @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.
*/
public constructor(serverBuilder: OnmsServerBuilder) {
if (!serverBuilder.url) {
throw new TypeError('URL is a required field!');
}
this.name = serverBuilder.name;
this.url = serverBuilder.url;
this.auth = serverBuilder.auth || null;
this.metadata = serverBuilder.metadata || null;
this.id = MD5(JSON.stringify([this.name, this.url, this.auth, this.metadata]));
}
/**
* 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.
* @parm withQuery - Query parameters to be appended to the URL.
* @returns A complete URL.
*/
public resolveURL(forFragment?: string, withQuery?: any) {
if (!this.url) {
return undefined;
}
if (forFragment === undefined) {
return this.url;
}
let uri = URI(this.url);
if (forFragment.indexOf('/') === 0 || forFragment.indexOf('http') === 0) {
uri = URI(forFragment);
} else {
uri = uri.segment(forFragment);
}
if (withQuery !== undefined) {
uri = uri.addQuery(withQuery);
}
return uri.toString();
}
/**
* Check whether the provided server has the same settings as this one.
*/
public equals(that?: OnmsServer | null) {
return that
&& this.id === that.id;
}
/**
* Create a new server object from this existing one, with the same ID.
*/
public clone() {
const auth = this.auth ? this.auth.clone() : undefined;
const metadata = this.metadata ? this.metadata.clone() : undefined;
return new OnmsServerBuilder(this.url)
.setName(this.name)
.setAuth(this.auth || undefined)
.setMetadata(this.metadata || undefined)
.build();
}
/**
* 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);
}
}
}