Name
sn_agent.ACCConfigurationFileAPI
Description
No description available
Script
var ACCConfigurationFileAPI = Class.create();
ACCConfigurationFileAPI.prototype = {
initialize: function() {},
/**
* Creates or updates the specified ACC Configuration File (sn_agent_configuration_file) record.
* Searches for an existing ACC Configuration File with the specified name and domain combination.
* If a record is found, then the record will be updated. Otherwise, a new record will be created.
*
* This function guarantees that there will be exactly 1 attachment on the record and attachment
* will contain the desired file content.
*
* Successful update or creation will trigger the ACC Configuration File to sync down to the
* MID servers and then to the agents.
*
* Parameters:
* 1. name - mandatory, the name that will be used for the following:
* the record's name field,
* the name of the attachment on the record
* the name of file when it syncs down to the agent
*
* 2. domain - mandatory, the domain that the configuration file record belongs to
* the ACC Configuration File will only be synced to agents that belong to this domain
*
* 3. content - mandatory, the file content that will be written to the ACC Configuration File
*
* Return: true if the configuration file was succesfully created or updated, false otherwise
*
*/
createOrUpdateConfigurationFile: function(name, domain, content) {
if (!name) {
gs.error("No file name provided for creating the ACC Configuration File");
return false;
}
if (!content) {
gs.error("No file content provided for creating the ACC Configuration File");
return false;
}
if (!domain) {
gs.error("No domain provided for creating the ACC Configuration File");
return false;
}
var configurationFileUtils = new ACCConfigurationFileUtils();
if (!configurationFileUtils.isValidName(name)) {
gs.error("The file name provided must only contain alphanumeric characters and . , - , _");
return false;
}
var configFileGr = new GlideRecord("sn_agent_configuration_file");
configFileGr.addQuery("name", name);
configFileGr.addQuery("sys_domain", domain);
configFileGr.query();
// insert new Configuration File record if one is not found with same name and domain
var newRecord = false;
if (!configFileGr.next()) {
configFileGr.setValue("name", name);
configFileGr.setValue("sys_domain", domain);
configFileGr.setWorkflow(false);
if (!configFileGr.insert()) {
gs.Error("Failed to create ACC Configuration File " + name);
return false;
}
newRecord = true;
}
// to ensure there is exactly 1 attachment on the record, and that this attacment contains the
// desired content:
// 1. delete any existing attachments on the record that do not match the content
// 2. if multiple existing attachments have the desired content, remove all but one copy of it
// 3. if no existing attachments contain the desired content, create a new attachment
// loop through the attachments on the Configuration File Record
var glideSysAttachment = new GlideSysAttachment();
var attachmentGr = glideSysAttachment.getAttachments("sn_agent_configuration_file", configFileGr.sys_id);
var found = false;
while (attachmentGr.next()) {
// we already found an attachment with the desired content, so delete this copy
if (found) {
gs.debug("Attachment with desired content already found, will delete this attachment" + attachmentGr.sys_id);
glideSysAttachment.deleteAttachment(attachmentGr.sys_id);
continue;
}
// delete the existing attachment if the content does not match the passed content
var currentContent = glideSysAttachment.getContent(attachmentGr);
if (currentContent != content) {
gs.debug("Attachment does not have matching content, will delete " + attachmentGr.sys_id);
glideSysAttachment.deleteAttachment(attachmentGr.sys_id);
continue;
}
// found an existing attachment that has the specified content in it already
found = true;
}
if (found) {
gs.debug("Attachment with matching content found, will not write new attachment for ACC Configuration File " + name);
return true;
}
gs.debug("Writing new attachment for ACC Configuration File " + name);
var sysId = glideSysAttachment.write(configFileGr, name, "text/plain", content);
if (!sysId) {
gs.error("Failed to create attachment for ACC Configuration File " + name);
return false;
}
// for the delete and update cases, the attachment sync is triggered elsewhere
// but for some reason, the attachment sync is not triggered when creating the new attachment
// so notify the MID servers for this case
if (newRecord) {
gs.debug("Notifying MID Servers, new ACC Configuration File created " + name);
new MIDNotificationHandler().notifyMIDServers("FileChange", "sn_agent_configuration_file");
}
return true;
},
/**
* Deletes the specified ACC Configuration File
* When the ACC Configuration File is deleted, it will be removed from the agents as well
*
* Parameters:
* 1. name - mandatory, the name of the configuration file that should be deleted
*
* 2. domain - mandatory, the domain of the configuration file that should be deleted
*
* Return: true if the configuration file does not exist or has been deleted
* false if the name or domain parameters are empty
*
*/
deleteConfigurationFile: function(name, domain) {
if (!name) {
gs.error("No file name provided for deleting the ACC Configuration File");
return false;
}
if (!domain) {
gs.error("No domain provided for deleting the ACC Configuration File");
return false;
}
var configFileGr = new GlideRecord("sn_agent_configuration_file");
configFileGr.addQuery("name", name);
configFileGr.addQuery("sys_domain", domain);
configFileGr.query();
if (configFileGr.next())
configFileGr.deleteRecord();
return true;
},
type: 'ACCConfigurationFileAPI'
};
Sys ID
d420dbcb771d9110f1acee1b8d5a999a