“One of my most productive days was throwing away 1,000 lines of code.”
— Ken Thompson
Last time, we wrapped up the code to refactor the application publishing process. Now we need to do the same thing with the application distribution process, the process that runs on the Host instance to share recently published application versions to all of the other Client instances in the community. Currently, this involves three separate functions, publishNewVersion, publishVersionRecord, and publishVersionAttachment.
publishNewVersion: function(newVersion, targetInstance, attachmentId) {
var targetGR = new GlideRecord('x_11556_col_store_member_organization');
if (targetGR.get('instance', targetInstance)) {
var token = targetGR.getDisplayValue('token');
var versionGR = new GlideRecord('x_11556_col_store_member_application_version');
if (versionGR.get(newVersion)) {
var canContinue = true;
var targetAppId = '';
var mbrAppGR = versionGR.member_application.getRefRecord();
var request = new sn_ws.RESTMessageV2();
request.setHttpMethod('get');
request.setBasicAuth(this.WORKER_ROOT + targetInstance, token);
request.setRequestHeader("Accept", "application/json");
request.setEndpoint('https://' + targetInstance + '.service-now.com/api/now/table/x_11556_col_store_member_application?sysparm_fields=sys_id&sysparm_query=provider.instance%3D' + mbrAppGR.getDisplayValue('provider.instance') + '%5Ename%3D' + encodeURIComponent(mbrAppGR.getDisplayValue('name')));
var response = request.execute();
if (response.haveError()) {
gs.error('CollaborationStoreUtils.publishNewVersion: Error returned from Target instance ' + targetInstance + ': ' + response.getErrorCode() + ' - ' + response.getErrorMessage());
canContinue = false;
} else if (response.getStatusCode() == '200') {
var jsonString = response.getBody();
var jsonObject = {};
try {
jsonObject = JSON.parse(jsonString);
} catch (e) {
gs.error('CollaborationStoreUtils.publishNewVersion: Unparsable JSON string returned from Target instance ' + targetInstance + ': ' + jsonString);
canContinue = false;
}
if (canContinue) {
var payload = {};
payload.name = mbrAppGR.getDisplayValue('name');
payload.scope = mbrAppGR.getDisplayValue('scope');
payload.description = mbrAppGR.getDisplayValue('description');
payload.current_version = mbrAppGR.getDisplayValue('current_version');
payload.active = 'true';
request = new sn_ws.RESTMessageV2();
request.setBasicAuth(this.WORKER_ROOT + targetInstance, token);
request.setRequestHeader("Accept", "application/json");
if (jsonObject.result && jsonObject.result.length > 0) {
targetAppId = jsonObject.result[0].sys_id;
request.setHttpMethod('put');
request.setEndpoint('https://' + targetInstance + '.service-now.com/api/now/table/x_11556_col_store_member_application/' + targetAppId);
} else {
request.setHttpMethod('post');
request.setEndpoint('https://' + targetInstance + '.service-now.com/api/now/table/x_11556_col_store_member_application');
payload.provider = mbrAppGR.getDisplayValue('provider.instance');
}
request.setRequestBody(JSON.stringify(payload, null, '\t'));
response = request.execute();
if (response.haveError()) {
gs.error('CollaborationStoreUtils.publishNewVersion: Error returned from Target instance ' + targetInstance + ': ' + response.getErrorCode() + ' - ' + response.getErrorMessage());
canContinue = false;
} else if (response.getStatusCode() != 200 && response.getStatusCode() != 201) {
gs.error('CollaborationStoreUtils.publishNewVersion: Invalid HTTP Response Code returned from Target instance ' + targetInstance + ': ' + response.getStatusCode());
canContinue = false;
} else {
jsonString = response.getBody();
jsonObject = {};
try {
jsonObject = JSON.parse(jsonString);
} catch (e) {
gs.error('CollaborationStoreUtils.publishNewVersion: Unparsable JSON string returned from Target instance ' + targetInstance + ': ' + jsonString);
canContinue = false;
}
if (canContinue) {
targetAppId = jsonObject.result.sys_id;
}
}
}
} else {
gs.error('CollaborationStoreUtils.publishNewVersion: Invalid HTTP Response Code returned from Target instance ' + targetInstance + ': ' + response.getStatusCode());
}
if (canContinue) {
this.publishVersionRecord(targetInstance, token, versionGR, targetAppId, attachmentId);
}
} else {
gs.error('CollaborationStoreUtils.publishNewVersion: Version record not found: ' + newVersion);
}
} else {
gs.error('CollaborationStoreUtils.publishNewVersion: Target instance record not found: ' + targetInstance);
}
},
publishVersionRecord: function(targetInstance, token, versionGR, targetAppId, attachmentId) {
var canContinue = true;
var payload = {};
payload.member_application = targetAppId;
payload.version = versionGR.getDisplayValue('version');
payload.built_on = versionGR.getDisplayValue('built_on');
var request = new sn_ws.RESTMessageV2();
request.setBasicAuth(this.WORKER_ROOT + targetInstance, token);
request.setRequestHeader("Accept", "application/json");
request.setHttpMethod('post');
request.setEndpoint('https://' + targetInstance + '.service-now.com/api/now/table/x_11556_col_store_member_application_version');
request.setRequestBody(JSON.stringify(payload, null, '\t'));
response = request.execute();
if (response.haveError()) {
gs.error('CollaborationStoreUtils.publishVersionRecord: Error returned from Target instance ' + targetInstance + ': ' + response.getErrorCode() + ' - ' + response.getErrorMessage());
canContinue = false;
} else if (response.getStatusCode() != 201) {
gs.error('CollaborationStoreUtils.publishVersionRecord: Invalid HTTP Response Code returned from Target instance ' + targetInstance + ': ' + response.getStatusCode());
canContinue = false;
} else {
jsonString = response.getBody();
jsonObject = {};
try {
jsonObject = JSON.parse(jsonString);
} catch (e) {
gs.error('CollaborationStoreUtils.publishVersionRecord: Unparsable JSON string returned from Target instance ' + targetInstance + ': ' + jsonString);
canContinue = false;
}
if (canContinue) {
targetVerId = jsonObject.result.sys_id;
this.publishVersionAttachment(targetInstance, token, targetVerId, attachmentId);
}
}
},
publishVersionAttachment: function(targetInstance, token, targetVerId, attachmentId) {
var gsa = new GlideSysAttachment();
var sysAttGR = new GlideRecord('sys_attachment');
if (sysAttGR.get(attachmentId)) {
var url = 'https://';
url += targetInstance;
url += '.service-now.com/api/now/attachment/file?table_name=x_11556_col_store_member_application_version&table_sys_id=';
url += targetVerId;
url += '&file_name=';
url += sysAttGR.getDisplayValue('file_name');
var request = new sn_ws.RESTMessageV2();
request.setBasicAuth(this.WORKER_ROOT + targetInstance, token);
request.setRequestHeader('Content-Type', sysAttGR.getDisplayValue('content_type'));
request.setRequestHeader('Accept', 'application/json');
request.setHttpMethod('post');
request.setEndpoint(url);
request.setRequestBody(gsa.getContent(sysAttGR));
response = request.execute();
if (response.haveError()) {
gs.error('CollaborationStoreUtils.publishVersionAttachment: Error returned from Target instance ' + targetInstance + ': ' + response.getErrorCode() + ' - ' + response.getErrorMessage());
} else if (response.getStatusCode() != 201) {
gs.error('CollaborationStoreUtils.publishVersionAttachment: Invalid HTTP Response Code returned from Target instance ' + targetInstance + ': ' + response.getStatusCode());
}
} else {
gs.error('CollaborationStoreUtils.publishVersionAttachment: Invalid attachment record sys_id: ' + attachmentId);
}
}
The corresponding shared functions are pushApplication, pushVersion, and pushAttachment in the CollaborationStoreUtils Script Include. The pushApplication takes the application GlideRecord, the target instance GlideRecord, and the sys_id of the application providing instance on the target instance as arguments. The current function already has code to fetch the target instance GlideRecord and the version GlideRecord, and from the version GlideRecord, we can obtain the application GlideRecord. We also built a function earlier that fetched the sys_id of an instance record on the target instance, but that function assumed that you were looking for the local instance on the target instance. In this case, we are looking for the instance that provided the application, so if we want to leverage that function, we will have to alter it to accept the instance name as one of the arguments. Here is the start of the function as it stands right now.
getRemoteInstanceSysId: function(targetGR) {
var sysId = '';
var result = {};
result.url = 'https://' + targetGR.getDisplayValue('instance') + '.service-now.com/api/now/table/x_11556_col_store_member_organization?sysparm_fields=sys_id&sysparm_query=instance%3D' + gs.getProperty('instance_name');
...
}
To add the ability to pass in the instance name, we can change that to this:
getRemoteInstanceSysId: function(targetGR, instanceName) {
var sysId = '';
var result = {};
result.url = 'https://' + targetGR.getDisplayValue('instance') + '.service-now.com/api/now/table/x_11556_col_store_member_organization?sysparm_fields=sys_id&sysparm_query=instance%3D' + instanceName;
...
}
Then we just have to go back into the code and find where this function is called and add the appropriate instance name to the argument list. With that little modification out of the way, we should be able to gather up all of the required arguments to call the pushApplication function. That will make our replacement code look like this:
publishNewVersion: function(newVersion, targetInstance, attachmentId) {
var targetGR = new GlideRecord('x_11556_col_store_member_organization');
if (targetGR.get('instance', targetInstance)) {
var versionGR = new GlideRecord('x_11556_col_store_member_application_version');
if (versionGR.get(newVersion)) {
var applicationGR = versionGR.member_application.getRefRecord();
var remoteSysId = this.getRemoteInstanceSysId(targetGR, applicationGR.getDisplayValue('provider.instance'));
var result = this.pushApplication(applicationGR, targetGR, remoteSysId);
if (result.error) {
gs.error('CollaborationStoreUtils.publishNewVersion: Error returned from Target instance ' + targetInstance + ': ' + result.error_code + ' - ' + result.error_message);
} else if (result.parse_error) {
gs.error('CollaborationStoreUtils.publishNewVersion: Unparsable JSON string returned from Target instance ' + targetInstance + ': ' + result.body);
} else if (result.status != 200 && result.status != 201) {
gs.error('CollaborationStoreUtils.publishNewVersion: Invalid HTTP Response Code returned from Target instance ' + targetInstance + ': ' + result.status);
} else {
var remoteAppId = result.obj.result.sys_id;
}
} else {
gs.error('CollaborationStoreUtils.publishNewVersion: Version record not found: ' + newVersion);
}
} else {
gs.error('CollaborationStoreUtils.publishNewVersion: Target instance record not found: ' + targetInstance);
}
}
If all goes well, we will be able to obtain the sys_id of the transferred application record on the target system from the result object, which is the only other piece of data that we need in order to call the pushVersion function. In fact, we can make that call on the very next line and check the results right there.
var remoteAppId = result.obj.result.sys_id;
result = this.pushVersion(versionGR, targetGR, remoteAppId);
if (result.error) {
gs.error('CollaborationStoreUtils.publishNewVersion: Error returned from Target instance ' + targetInstance + ': ' + result.error_code + ' - ' + result.error_message);
} else if (result.parse_error) {
gs.error('CollaborationStoreUtils.publishNewVersion: Unparsable JSON string returned from Target instance ' + targetInstance + ': ' + result.body);
} else if (result.status != 200 && result.status != 201) {
gs.error('CollaborationStoreUtils.publishNewVersion: Invalid HTTP Response Code returned from Target instance ' + targetInstance + ': ' + result.status);
} else {
var remoteVerId = result.obj.result.sys_id;
}
And again, if all goes well, we will be able to obtain the sys_id of the transferred version record on the target system from the result object, which is the other piece of data that we need in order to call the pushAttachment function. And once again, we can make that call on the very next line and check the results right there.
var remoteVerId = result.obj.result.sys_id;
result = this.pushAttachment(attachmentGR, targetGR, remoteVerId);
if (result.error) {
gs.error('CollaborationStoreUtils.publishNewVersion: Error returned from Target instance ' + targetInstance + ': ' + result.error_code + ' - ' + result.error_message);
} else if (result.parse_error) {
gs.error('CollaborationStoreUtils.publishNewVersion: Unparsable JSON string returned from Target instance ' + targetInstance + ': ' + result.body);
} else if (result.status != 200 && result.status != 201) {
gs.error('CollaborationStoreUtils.publishNewVersion: Invalid HTTP Response Code returned from Target instance ' + targetInstance + ': ' + result.status);
}
So putting it all together in this way, we can replace the original three functions with a single function that makes three calls to our shared functions. Once again, we have simplified things quite a bit and at the same time added functionality with the inclusion of the logo images and the REST API call logging. Here is the whole thing all put together:
publishNewVersion: function(newVersion, targetInstance, attachmentId) {
var targetGR = new GlideRecord('x_11556_col_store_member_organization');
if (targetGR.get('instance', targetInstance)) {
var versionGR = new GlideRecord('x_11556_col_store_member_application_version');
if (versionGR.get(newVersion)) {
var applicationGR = versionGR.member_application.getRefRecord();
var remoteSysId = this.getRemoteInstanceSysId(targetGR, applicationGR.getDisplayValue('provider.instance'));
var result = this.pushApplication(applicationGR, targetGR, remoteSysId);
if (result.error) {
gs.error('CollaborationStoreUtils.publishNewVersion: Error returned from Target instance ' + targetInstance + ': ' + result.error_code + ' - ' + result.error_message);
} else if (result.parse_error) {
gs.error('CollaborationStoreUtils.publishNewVersion: Unparsable JSON string returned from Target instance ' + targetInstance + ': ' + result.body);
} else if (result.status != 200 && result.status != 201) {
gs.error('CollaborationStoreUtils.publishNewVersion: Invalid HTTP Response Code returned from Target instance ' + targetInstance + ': ' + result.status);
} else {
var remoteAppId = result.obj.result.sys_id;
result = this.pushVersion(versionGR, targetGR, remoteAppId);
if (result.error) {
gs.error('CollaborationStoreUtils.publishNewVersion: Error returned from Target instance ' + targetInstance + ': ' + result.error_code + ' - ' + result.error_message);
} else if (result.parse_error) {
gs.error('CollaborationStoreUtils.publishNewVersion: Unparsable JSON string returned from Target instance ' + targetInstance + ': ' + result.body);
} else if (result.status != 200 && result.status != 201) {
gs.error('CollaborationStoreUtils.publishNewVersion: Invalid HTTP Response Code returned from Target instance ' + targetInstance + ': ' + result.status);
} else {
var remoteVerId = result.obj.result.sys_id;
result = this.pushAttachment(attachmentGR, targetGR, remoteVerId);
if (result.error) {
gs.error('CollaborationStoreUtils.publishNewVersion: Error returned from Target instance ' + targetInstance + ': ' + result.error_code + ' - ' + result.error_message);
} else if (result.parse_error) {
gs.error('CollaborationStoreUtils.publishNewVersion: Unparsable JSON string returned from Target instance ' + targetInstance + ': ' + result.body);
} else if (result.status != 200 && result.status != 201) {
gs.error('CollaborationStoreUtils.publishNewVersion: Invalid HTTP Response Code returned from Target instance ' + targetInstance + ': ' + result.status);
}
}
}
} else {
gs.error('CollaborationStoreUtils.publishNewVersion: Version record not found: ' + newVersion);
}
} else {
gs.error('CollaborationStoreUtils.publishNewVersion: Target instance record not found: ' + targetInstance);
}
}
That should cover the application distribution process, which should complete the list of features that should be relying on the shared functions. The only thing left at this point would be to hunt down all of the other REST API calls and throw in a call to the new shared logging function. Maybe we will take a look at that next time out.