pmcf 1.6.0 → 1.6.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pmcf",
3
- "version": "1.6.0",
3
+ "version": "1.6.2",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
package/src/model.mjs CHANGED
@@ -100,6 +100,10 @@ export class Base {
100
100
  return object;
101
101
  }
102
102
 
103
+ error(...args) {
104
+ console.error(`${this.toString()}:`, ...args);
105
+ }
106
+
103
107
  toString() {
104
108
  return this.typeName + ":" + (this.owner?.name || "") + "/" + this.name;
105
109
  }
@@ -117,6 +121,7 @@ export class Owner extends Base {
117
121
  #hosts = new Map();
118
122
  #networks = new Map();
119
123
  #subnets = new Map();
124
+ #bridges = new Set();
120
125
 
121
126
  async *hosts() {
122
127
  for (const host of this.#hosts.values()) {
@@ -142,6 +147,35 @@ export class Owner extends Base {
142
147
  this.#networks.set(network.name, network);
143
148
  }
144
149
 
150
+ addBridge(network, destinationNetworks) {
151
+ if (destinationNetworks) {
152
+ let bridge;
153
+
154
+ for (bridge of this.#bridges) {
155
+ if (bridge.has(network.name)) {
156
+ bridge.delete(network.name);
157
+ bridge.add(network);
158
+ break;
159
+ }
160
+
161
+ if (bridge.has(network)) {
162
+ break;
163
+ }
164
+ }
165
+
166
+ if (!bridge) {
167
+ bridge = new Set([network]);
168
+ this.#bridges.add(bridge);
169
+ }
170
+
171
+ for (const name of destinationNetworks) {
172
+ bridge.add(this.network(name) || name);
173
+ }
174
+
175
+ return bridge;
176
+ }
177
+ }
178
+
145
179
  addSubnet(subnet) {
146
180
  this.#subnets.set(subnet.name, subnet);
147
181
  }
@@ -159,6 +193,7 @@ export class Owner extends Base {
159
193
  ...super.toJSON(),
160
194
  networks: [...this.#networks.keys()].sort(),
161
195
  subnets: [...this.#subnets.keys()].sort(),
196
+ bridges: [...this.#bridges].map(b => bridgeToJSON(b)),
162
197
  hosts: [...this.#hosts.keys()].sort()
163
198
  };
164
199
  }
@@ -299,22 +334,6 @@ export class Location extends Owner {
299
334
  data.name = name;
300
335
  new Network(this, data);
301
336
  }
302
-
303
- /*
304
- for (const network of this.#networks.values()) {
305
- if (network.bridges) {
306
- network.bridges = new Set(
307
- network.bridges.map(b => {
308
- const n = this.network(b);
309
- if (!n) {
310
- console.error(`No network named ${b}`);
311
- }
312
- return n;
313
- })
314
- );
315
- }
316
- }
317
- */
318
337
  }
319
338
  }
320
339
 
@@ -383,6 +402,7 @@ export class Network extends Owner {
383
402
  metric;
384
403
  ipv4;
385
404
  subnet;
405
+ bridge;
386
406
 
387
407
  static get typeName() {
388
408
  return "network";
@@ -391,6 +411,12 @@ export class Network extends Owner {
391
411
  constructor(owner, data) {
392
412
  super(owner, data);
393
413
 
414
+ let bridges;
415
+ if (data.bridges) {
416
+ bridges = data.bridges;
417
+ delete data.bridges;
418
+ }
419
+
394
420
  Object.assign(this, data);
395
421
 
396
422
  const subnetAddress = this.subnetAddress;
@@ -406,6 +432,8 @@ export class Network extends Owner {
406
432
  }
407
433
 
408
434
  owner.addNetwork(this);
435
+
436
+ this.bridge = owner.addBridge(this, bridges);
409
437
  }
410
438
 
411
439
  get ipv4_netmask() {
@@ -424,7 +452,14 @@ export class Network extends Owner {
424
452
  }
425
453
 
426
454
  get propertyNames() {
427
- return [...super.propertyNames, "kind", "ipv4", "scope", "metric"];
455
+ return [
456
+ ...super.propertyNames,
457
+ "kind",
458
+ "ipv4",
459
+ "scope",
460
+ "metric",
461
+ "bridge"
462
+ ];
428
463
  }
429
464
  }
430
465
 
@@ -513,11 +548,11 @@ export class Host extends Base {
513
548
  if (iface.network) {
514
549
  const network = owner.network(iface.network);
515
550
 
516
- if (!network) {
517
- console.error(`${this.toString()}: Missing network`, iface.network);
518
- } else {
551
+ if (network) {
519
552
  iface.network = network;
520
553
  network.addHost(this);
554
+ } else {
555
+ this.error("Missing network", iface.network);
521
556
  }
522
557
  }
523
558
  }
@@ -805,3 +840,7 @@ function extractFrom(object, propertyNames) {
805
840
  }
806
841
  return json;
807
842
  }
843
+
844
+ function bridgeToJSON(bridge) {
845
+ return [...bridge].map(n => n.name || `(${n})`).sort();
846
+ }
package/types/model.d.mts CHANGED
@@ -18,6 +18,7 @@ export class Base {
18
18
  set directory(directory: any);
19
19
  get directory(): any;
20
20
  expand(object: any): any;
21
+ error(...args: any[]): void;
21
22
  toString(): string;
22
23
  get propertyNames(): string[];
23
24
  toJSON(): {};
@@ -29,12 +30,14 @@ export class Owner extends Base {
29
30
  network(name: any): any;
30
31
  networks(): AsyncGenerator<any, void, unknown>;
31
32
  addNetwork(network: any): void;
33
+ addBridge(network: any, destinationNetworks: any): any;
32
34
  addSubnet(subnet: any): void;
33
35
  subnet(name: any): any;
34
36
  subnets(): MapIterator<any>;
35
37
  toJSON(): {
36
38
  networks: any[];
37
39
  subnets: any[];
40
+ bridges: any[][];
38
41
  hosts: any[];
39
42
  };
40
43
  #private;
@@ -77,6 +80,7 @@ export class Network extends Owner {
77
80
  metric: any;
78
81
  ipv4: any;
79
82
  subnet: any;
83
+ bridge: any;
80
84
  get ipv4_netmask(): any;
81
85
  get subnetAddress(): any;
82
86
  }