Remove duplicated instance check

Deduplicate instance existence check for requests in master.
This commit is contained in:
Hornwitser committed 2022-03-28 20:42:08 +02:00
1 parent 4edc7a2949
commit 2e1c1fd3c8
2 files changed
+22 -33

No files matched your search

+6 -28
View File
@@ -159,10 +159,7 @@ class ControlConnection extends BaseConnection {
async getInstanceRequestHandler(message) { async getInstanceRequestHandler(message) {
let id = message.data.id; let id = message.data.id;
let instance = this._master.instances.get(id); let instance = this._master.getRequestInstance(id);
if (!instance) {
throw new libErrors.RequestError(`Instance with ID ${id} does not exist`);
}
return { return {
id, id,
@@ -249,11 +246,7 @@ class ControlConnection extends BaseConnection {
} }
async deleteInstanceRequestHandler(message, request) { async deleteInstanceRequestHandler(message, request) {
let instance = this._master.instances.get(message.data.instance_id); let instance = this._master.getRequestInstance(message.data.instance_id);
if (!instance) {
throw new libErrors.RequestError(`Instance with ID ${message.data.instance_id} does not exist`);
}
if (instance.config.get("instance.assigned_slave") !== null) { if (instance.config.get("instance.assigned_slave") !== null) {
await this.forwardRequestToInstance(message, request); await this.forwardRequestToInstance(message, request);
} }
@@ -266,11 +259,7 @@ class ControlConnection extends BaseConnection {
} }
async getInstanceConfigRequestHandler(message) { async getInstanceConfigRequestHandler(message) {
let instance = this._master.instances.get(message.data.instance_id); let instance = this._master.getRequestInstance(message.data.instance_id);
if (!instance) {
throw new libErrors.RequestError(`Instance with ID ${message.data.instance_id} does not exist`);
}
return { return {
serialized_config: instance.config.serialize("control"), serialized_config: instance.config.serialize("control"),
}; };
@@ -290,11 +279,7 @@ class ControlConnection extends BaseConnection {
} }
async setInstanceConfigFieldRequestHandler(message) { async setInstanceConfigFieldRequestHandler(message) {
let instance = this._master.instances.get(message.data.instance_id); let instance = this._master.getRequestInstance(message.data.instance_id);
if (!instance) {
throw new libErrors.RequestError(`Instance with ID ${message.data.instance_id} does not exist`);
}
if (message.data.field === "instance.assigned_slave") { if (message.data.field === "instance.assigned_slave") {
throw new libErrors.RequestError("instance.assigned_slave must be set through the assign-slave interface"); throw new libErrors.RequestError("instance.assigned_slave must be set through the assign-slave interface");
} }
@@ -309,11 +294,7 @@ class ControlConnection extends BaseConnection {
} }
async setInstanceConfigPropRequestHandler(message) { async setInstanceConfigPropRequestHandler(message) {
let instance = this._master.instances.get(message.data.instance_id); let instance = this._master.getRequestInstance(message.data.instance_id);
if (!instance) {
throw new libErrors.RequestError(`Instance with ID ${message.data.instance_id} does not exist`);
}
let { field, prop, value } = message.data; let { field, prop, value } = message.data;
instance.config.setProp(field, prop, value, "control"); instance.config.setProp(field, prop, value, "control");
await this.updateInstanceConfig(instance); await this.updateInstanceConfig(instance);
@@ -321,10 +302,7 @@ class ControlConnection extends BaseConnection {
async assignInstanceCommandRequestHandler(message, request) { async assignInstanceCommandRequestHandler(message, request) {
let { slave_id, instance_id } = message.data; let { slave_id, instance_id } = message.data;
let instance = this._master.instances.get(instance_id); let instance = this._master.getRequestInstance(instance_id);
if (!instance) {
throw new libErrors.RequestError(`Instance with ID ${instance_id} does not exist`);
}
// Check if target slave is connected // Check if target slave is connected
let newSlaveConnection; let newSlaveConnection;
+16 -5
View File
@@ -424,6 +424,21 @@ class Master {
} }
} }
/**
* Get instance by ID for a request
*
* @param {number} instanceId - ID of instance to get.
* @returns {object} instance
* @throws {module:lib/errors.RequestError} if the instance does not exist.
*/
getRequestInstance(instanceId) {
let instance = this.instances.get(instanceId);
if (!instance) {
throw new libErrors.RequestError(`Instance with ID ${instanceId} does not exist`);
}
return instance;
}
addInstanceHooks(instance) { addInstanceHooks(instance) {
instance.config.on("fieldChanged", (group, field, prev) => { instance.config.on("fieldChanged", (group, field, prev) => {
if (group.name === "instance" && field === "name") { if (group.name === "instance" && field === "name") {
@@ -608,11 +623,7 @@ class Master {
* @returns {object} data returned from the request. * @returns {object} data returned from the request.
*/ */
async forwardRequestToInstance(request, data) { async forwardRequestToInstance(request, data) {
let instance = this.instances.get(data.instance_id); let instance = this.getRequestInstance(data.instance_id);
if (!instance) {
throw new libErrors.RequestError(`Instance with ID ${data.instance_id} does not exist`);
}
let slaveId = instance.config.get("instance.assigned_slave"); let slaveId = instance.config.get("instance.assigned_slave");
if (slaveId === null) { if (slaveId === null) {
throw new libErrors.RequestError("Instance is not assigned to a slave"); throw new libErrors.RequestError("Instance is not assigned to a slave");