2019-07-11 01:30:06 +00:00
|
|
|
import { Context, GET, Path, PathParam, POST, Security, ServiceContext } from "typescript-rest";
|
2018-03-25 19:13:50 +00:00
|
|
|
import { Cache, CACHE_INTEGRATIONS, CACHE_NEB } from "../../MemoryCache";
|
2017-12-29 01:22:50 +00:00
|
|
|
import { NebStore } from "../../db/NebStore";
|
|
|
|
import { NebConfig } from "../../models/neb";
|
|
|
|
import { LogService } from "matrix-js-snippets";
|
|
|
|
import { ApiError } from "../ApiError";
|
2019-07-11 04:10:10 +00:00
|
|
|
import { ROLE_ADMIN, ROLE_USER } from "../security/MatrixSecurity";
|
2017-12-29 01:22:50 +00:00
|
|
|
|
|
|
|
interface CreateWithUpstream {
|
|
|
|
upstreamId: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface CreateWithAppservice {
|
|
|
|
appserviceId: string;
|
|
|
|
adminUrl: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface SetEnabledRequest {
|
|
|
|
enabled: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-30 21:17:39 +00:00
|
|
|
/**
|
|
|
|
* Administrative API for configuring go-neb instances.
|
|
|
|
*/
|
2017-12-29 01:22:50 +00:00
|
|
|
@Path("/api/v1/dimension/admin/neb")
|
2018-03-24 03:36:43 +00:00
|
|
|
export class AdminNebService {
|
2017-12-29 01:22:50 +00:00
|
|
|
|
2019-07-11 01:30:06 +00:00
|
|
|
@Context
|
|
|
|
private context: ServiceContext;
|
|
|
|
|
2017-12-29 01:22:50 +00:00
|
|
|
@GET
|
|
|
|
@Path("all")
|
2019-07-11 04:10:10 +00:00
|
|
|
@Security([ROLE_USER, ROLE_ADMIN])
|
2019-07-11 01:30:06 +00:00
|
|
|
public async getNebConfigs(): Promise<NebConfig[]> {
|
2018-03-24 03:01:56 +00:00
|
|
|
const cachedConfigs = Cache.for(CACHE_NEB).get("configurations");
|
|
|
|
if (cachedConfigs) return cachedConfigs;
|
|
|
|
|
|
|
|
const configs = await NebStore.getAllConfigs();
|
|
|
|
Cache.for(CACHE_NEB).put("configurations", configs);
|
|
|
|
return configs;
|
2017-12-29 01:22:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@GET
|
|
|
|
@Path(":id/config")
|
2019-07-11 04:10:10 +00:00
|
|
|
@Security([ROLE_USER, ROLE_ADMIN])
|
2019-07-11 01:30:06 +00:00
|
|
|
public async getNebConfig(@PathParam("id") nebId: number): Promise<NebConfig> {
|
|
|
|
const configs = await this.getNebConfigs();
|
2018-03-24 03:01:56 +00:00
|
|
|
const firstConfig = configs.filter(c => c.id === nebId)[0];
|
|
|
|
if (!firstConfig) throw new ApiError(404, "Configuration not found");
|
|
|
|
return firstConfig;
|
2017-12-29 01:22:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@POST
|
|
|
|
@Path(":id/integration/:type/enabled")
|
2019-07-11 04:10:10 +00:00
|
|
|
@Security([ROLE_USER, ROLE_ADMIN])
|
2019-07-11 01:30:06 +00:00
|
|
|
public async setIntegrationEnabled(@PathParam("id") nebId: number, @PathParam("type") integrationType: string, request: SetEnabledRequest): Promise<any> {
|
|
|
|
const userId = this.context.request.user.userId;
|
2018-03-30 22:34:39 +00:00
|
|
|
|
2018-03-25 03:17:44 +00:00
|
|
|
await NebStore.setIntegrationEnabled(nebId, integrationType, request.enabled);
|
2018-03-30 22:34:39 +00:00
|
|
|
LogService.info("AdminNebService", userId + " set the " + integrationType + " on NEB " + nebId + " to " + (request.enabled ? "enabled" : "disabled"));
|
|
|
|
|
2018-03-25 03:17:44 +00:00
|
|
|
Cache.for(CACHE_NEB).clear();
|
2018-03-25 19:13:50 +00:00
|
|
|
Cache.for(CACHE_INTEGRATIONS).clear();
|
2018-03-25 03:17:44 +00:00
|
|
|
return {}; // 200 OK
|
2018-03-24 20:54:12 +00:00
|
|
|
}
|
2018-03-24 03:01:56 +00:00
|
|
|
|
2018-03-24 20:54:12 +00:00
|
|
|
@POST
|
|
|
|
@Path(":id/integration/:type/config")
|
2019-07-11 04:10:10 +00:00
|
|
|
@Security([ROLE_USER, ROLE_ADMIN])
|
2019-07-11 01:30:06 +00:00
|
|
|
public async setIntegrationConfig(@PathParam("id") nebId: number, @PathParam("type") integrationType: string, newConfig: any): Promise<any> {
|
|
|
|
const userId = this.context.request.user.userId;
|
2018-03-30 22:34:39 +00:00
|
|
|
|
2018-03-25 03:17:44 +00:00
|
|
|
await NebStore.setIntegrationConfig(nebId, integrationType, newConfig);
|
2018-03-30 22:34:39 +00:00
|
|
|
LogService.info("AdminNebService", userId + " updated the configuration for " + integrationType + " on NEB " + nebId);
|
|
|
|
|
2018-03-25 03:17:44 +00:00
|
|
|
Cache.for(CACHE_NEB).clear();
|
2018-03-25 19:13:50 +00:00
|
|
|
Cache.for(CACHE_INTEGRATIONS).clear();
|
2018-03-25 03:17:44 +00:00
|
|
|
return {}; // 200 OK
|
2018-03-24 20:54:12 +00:00
|
|
|
}
|
2018-03-24 03:01:56 +00:00
|
|
|
|
2018-03-24 20:54:12 +00:00
|
|
|
@GET
|
|
|
|
@Path(":id/integration/:type/config")
|
2019-07-11 04:10:10 +00:00
|
|
|
@Security([ROLE_USER, ROLE_ADMIN])
|
2019-07-11 01:30:06 +00:00
|
|
|
public async getIntegrationConfig(@PathParam("id") nebId: number, @PathParam("type") integrationType: string): Promise<any> {
|
2018-03-24 20:54:12 +00:00
|
|
|
return NebStore.getIntegrationConfig(nebId, integrationType);
|
2017-12-29 01:22:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@POST
|
|
|
|
@Path("new/upstream")
|
2019-07-11 04:10:10 +00:00
|
|
|
@Security([ROLE_USER, ROLE_ADMIN])
|
2019-07-11 01:30:06 +00:00
|
|
|
public async newConfigForUpstream(request: CreateWithUpstream): Promise<NebConfig> {
|
|
|
|
const userId = this.context.request.user.userId;
|
2018-03-24 03:01:56 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
const neb = await NebStore.createForUpstream(request.upstreamId);
|
2018-03-30 22:34:39 +00:00
|
|
|
LogService.info("AdminNebService", userId + " created a new NEB instance from upstream " + request.upstreamId);
|
|
|
|
|
2017-12-29 01:22:50 +00:00
|
|
|
Cache.for(CACHE_NEB).clear();
|
2018-03-25 19:13:50 +00:00
|
|
|
Cache.for(CACHE_INTEGRATIONS).clear();
|
2018-03-24 03:01:56 +00:00
|
|
|
return neb;
|
|
|
|
} catch (err) {
|
|
|
|
LogService.error("DimensionNebAdminService", err);
|
|
|
|
throw new ApiError(500, "Error creating go-neb instance");
|
|
|
|
}
|
2017-12-29 01:22:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@POST
|
|
|
|
@Path("new/appservice")
|
2019-07-11 04:10:10 +00:00
|
|
|
@Security([ROLE_USER, ROLE_ADMIN])
|
2019-07-11 01:30:06 +00:00
|
|
|
public async newConfigForAppservice(request: CreateWithAppservice): Promise<NebConfig> {
|
|
|
|
const userId = this.context.request.user.userId;
|
2018-03-24 03:01:56 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
const neb = await NebStore.createForAppservice(request.appserviceId, request.adminUrl);
|
2018-03-30 22:34:39 +00:00
|
|
|
LogService.info("AdminNebService", userId + " created a new NEB instance from appservice " + request.appserviceId);
|
|
|
|
|
2017-12-29 01:22:50 +00:00
|
|
|
Cache.for(CACHE_NEB).clear();
|
2018-03-25 19:13:50 +00:00
|
|
|
Cache.for(CACHE_INTEGRATIONS).clear();
|
2018-03-24 03:01:56 +00:00
|
|
|
return neb;
|
|
|
|
} catch (err) {
|
|
|
|
LogService.error("DimensionNebAdminService", err);
|
|
|
|
throw new ApiError(500, "Error creating go-neb instance");
|
|
|
|
}
|
2017-12-29 01:22:50 +00:00
|
|
|
}
|
|
|
|
}
|