Skip to content

Commit

Permalink
feat(Topology): Add fetch target clusters API
Browse files Browse the repository at this point in the history
Include "IsDefault" if available.

Signed-off-by: Gordon Smith <gordonjsmith@gmail.com>
  • Loading branch information
GordonSmith committed May 15, 2019
1 parent 86339a2 commit 4035bdc
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 3 deletions.
50 changes: 47 additions & 3 deletions packages/comms/src/ecl/targetCluster.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Cache, StateObject } from "@hpcc-js/util";
import { IConnection, IOptions } from "../connection";
import { GetTargetClusterInfo, GetTargetClusterUsageEx, MachineService } from "../services/wsMachine";
import { TopologyService, TpTargetClusterQuery } from "../services/wsTopology";
import { TopologyService, TpListTargetClusters, TpTargetClusterQuery } from "../services/wsTopology";
import { Machine } from "./machine";

export class TargetClusterCache extends Cache<{ BaseUrl: string, Name: string }, TargetCluster> {
Expand All @@ -17,8 +17,8 @@ export interface TpTargetClusterEx {
MachineInfoEx: GetTargetClusterInfo.MachineInfoEx[];
}

export type UTargetClusterState = TpTargetClusterQuery.TpTargetCluster & TpTargetClusterEx;
export type ITargetClusterState = TpTargetClusterQuery.TpTargetCluster | TpTargetClusterEx;
export type UTargetClusterState = TpTargetClusterQuery.TpTargetCluster & TpListTargetClusters.TpClusterNameType & TpTargetClusterEx;
export type ITargetClusterState = TpTargetClusterQuery.TpTargetCluster | TpListTargetClusters.TpClusterNameType | TpTargetClusterEx;
export class TargetCluster extends StateObject<UTargetClusterState, ITargetClusterState> implements UTargetClusterState {
protected connection: TopologyService;
protected machineConnection: MachineService;
Expand All @@ -27,6 +27,7 @@ export class TargetCluster extends StateObject<UTargetClusterState, ITargetClust
get Name(): string { return this.get("Name"); }
get Prefix(): string { return this.get("Prefix"); }
get Type(): string { return this.get("Type"); }
get IsDefault(): boolean { return this.get("IsDefault"); }
get TpClusters(): TpTargetClusterQuery.TpClusters { return this.get("TpClusters"); }
get TpEclCCServers(): TpTargetClusterQuery.TpEclCCServers { return this.get("TpEclCCServers"); }
get TpEclServers(): TpTargetClusterQuery.TpEclServers { return this.get("TpEclServers"); }
Expand Down Expand Up @@ -105,3 +106,46 @@ export class TargetCluster extends StateObject<UTargetClusterState, ITargetClust
return this.machineConnection.GetTargetClusterUsageEx([this.Name]);
}
}

export function targetClusters(optsConnection: IOptions | IConnection | TopologyService): Promise<TargetCluster[]> {
let connection: TopologyService;
if (optsConnection instanceof TopologyService) {
connection = optsConnection;
} else {
connection = new TopologyService(optsConnection);
}
return connection.TpListTargetClusters({}).then(response => {
return response.TargetClusters.TpClusterNameType.map(item => TargetCluster.attach(optsConnection, item.Name, item));
});
}

const _defaultTargetCluster: { [baseUrl: string]: Promise<TargetCluster> } = {};
export function defaultTargetCluster(optsConnection: IOptions | IConnection | TopologyService): Promise<TargetCluster> {
if (!_defaultTargetCluster[optsConnection.baseUrl]) {
let connection: TopologyService;
if (optsConnection instanceof TopologyService) {
connection = optsConnection;
} else {
connection = new TopologyService(optsConnection);
}
_defaultTargetCluster[optsConnection.baseUrl] = connection.TpListTargetClusters({}).then(response => {
let firstItem: TpListTargetClusters.TpClusterNameType;
let defaultItem: TpListTargetClusters.TpClusterNameType;
let hthorItem: TpListTargetClusters.TpClusterNameType;
response.TargetClusters.TpClusterNameType.forEach(item => {
if (!firstItem) {
firstItem = item;
}
if (!defaultItem && item.IsDefault === true) {
defaultItem = item;
}
if (!hthorItem && item.Type === "hthor") {
hthorItem = item;
}
});
const defItem = defaultItem || hthorItem || firstItem;
return TargetCluster.attach(optsConnection, defItem.Name, defItem);
});
}
return _defaultTargetCluster[optsConnection.baseUrl];
}
38 changes: 38 additions & 0 deletions packages/comms/src/services/wsTopology.ts
Original file line number Diff line number Diff line change
Expand Up @@ -762,6 +762,40 @@ export namespace TpTargetClusterQuery {
}
}

export namespace TpListTargetClusters {

export interface Request {
}

export interface Exception {
Code: string;
Audience: string;
Source: string;
Message: string;
}

export interface Exceptions {
Source: string;
Exception: Exception[];
}

export interface TpClusterNameType {
Name: string;
Type: string;
IsDefault: boolean;
}

export interface TargetClusters {
TpClusterNameType: TpClusterNameType[];
}

export interface Response {
Exceptions: Exceptions;
TargetClusters: TargetClusters;
}

}

export class TopologyService extends Service {

constructor(optsConnection: IOptions | IConnection) {
Expand Down Expand Up @@ -815,4 +849,8 @@ export class TopologyService extends Service {
TpTargetClusterQuery(request: TpTargetClusterQuery.Request): Promise<TpTargetClusterQuery.Response> {
return this._connection.send("TpTargetClusterQuery", request);
}

TpListTargetClusters(request: TpListTargetClusters.Request): Promise<TpListTargetClusters.Response> {
return this._connection.send("TpListTargetClusters", request);
}
}

0 comments on commit 4035bdc

Please sign in to comment.