Name

sn_cmdb_int_util.BaseRemoveSoftwareUtil

Description

No description available

Script

var BaseRemoveSoftwareUtil = Class.create();
BaseRemoveSoftwareUtil.prototype = {

  /**
   * This reusable utility script include class for implementing remove software transform
   **/

  IMPORT_SET_TABLE_NAME: "",
  IMPORT_SET_SYS_ID: "",
  IMPORT_SET_DATA_SOURCE: "",
  LOG: "",
  COMMENTS_FIELD_SIZE: 1000,
  COMMENTS_POSTFIX_LENGTH: 4,
  fSWDeleted: 0,
  DISCOVERY_SOURCE: "",
  TARGET_TABLE: "",
  IS_SAM: "",

  initialize: function(import_set, log) {
      this.IMPORT_SET_TABLE_NAME = import_set.table_name;
      this.IMPORT_SET_SYS_ID = import_set.sys_id;
      this.IMPORT_SET_DATA_SOURCE = import_set.data_source;
      this.LOG = log;
      this.IS_SAM = GlidePluginManager.isActive("com.snc.software_asset_management") || GlidePluginManager.isActive("com.snc.sams");
      if (this.IS_SAM)
          this.TARGET_TABLE = 'cmdb_sam_sw_install';
      else
          this.TARGET_TABLE = 'cmdb_software_instance';
      this.init();
  },

  /**
   * Implement in extension script
   **/
  init: function() {},

  /**
   * Implement in extension script, this method should implement logic to find and delete
   * software instance.
   **/
  findAndDeleteSoftwareInstance: function(importSetGr) {},

  /**
   * This method invoked for trasform script to start processing remove software
   **/
  processSW: function() {
      var ga = new GlideRecord(this.IMPORT_SET_TABLE_NAME);
      ga.addQuery("sys_import_set", this.IMPORT_SET_SYS_ID);
      ga.query();

      while (ga.next())
          this.findAndDeleteSoftwareInstance(ga);
      // Manually update the total number of records processed
      this._updateTransformHistory();
  },

  /**
   * This method returns the sys_id for Computer CIs for given SNK
   **/
  findComputer: function(computerSNK) {
      var grs = new GlideRecord("sys_object_source");
      grs.addQuery("name", this.DISCOVERY_SOURCE);
      grs.addQuery("id", computerSNK);
      grs.setLimit(1);
      grs.query();
      if (grs.next())
          return grs.target_sys_id;
      return null;
  },

  /**
   * This method logs total number software instance/install deleted
   **/
  _updateTransformHistory: function() {
      var total = 0;
      var grr = new GlideRecord(this.IMPORT_SET_TABLE_NAME);
      grr.addQuery("sys_import_set", this.IMPORT_SET_SYS_ID);
      grr.query();
      if (grr.next())
          total = grr.getRowCount();

      this.LOG.info("Total import records: " + total + "; Software Deleted: " + this.fSWDeleted);
  },

  /**
   * This method update comments of import set rows with information about software instance/install deleted
   **/
  _getComments: function(deletedSoftwareInstance) {
      var comment = "Deleted Software:" + deletedSoftwareInstance.length + " [" + deletedSoftwareInstance;
      if (this.COMMENTS_FIELD_SIZE >= comment.length + 1)
          comment = comment + "]";
      else
          comment = comment.substring(0, this.COMMENTS_FIELD_SIZE - this.COMMENTS_POSTFIX_LENGTH) + "...]";
      return comment;
  },

  /**
   * This method returns the list of software instance/install sys_ids for given list
   * of SNKs
   **/
  getImportSetSoftwareInstanceSysIds: function(sourceNativeKeys) {
      var softwareInstanceSysIds = {};
      var gr = new GlideRecord('sys_object_source');
      gr.addQuery("name", this.DISCOVERY_SOURCE);
      gr.addQuery("id", sourceNativeKeys);
      gr.query();
      while (gr.next()) {
          var softwareInstanceSysId = gr.getValue("target_sys_id");
          softwareInstanceSysIds[softwareInstanceSysId] = true;
      }
      return softwareInstanceSysIds;
  },

  /**
   * This method deletes software instance/install for given list
   * sys_ids for given computer
   **/
  deleteSoftwareInstances: function(importSetGr, deletedSoftwareInstanceSysIds, ciSysId) {
      var deletedSoftwareInstance = [];
      if (deletedSoftwareInstanceSysIds && deletedSoftwareInstanceSysIds.length > 0) {

          var softwareToDelete = new GlideRecord(this.TARGET_TABLE);

          softwareToDelete.addQuery("installed_on", ciSysId);
          softwareToDelete.addQuery("sys_id", deletedSoftwareInstanceSysIds);
          softwareToDelete.query();

          while (softwareToDelete.next()) {
              if (!softwareToDelete.deleteRecord())
                  continue;
              this.fSWDeleted++;
              var softwareName = "";
              if (!this.IS_SAM) {
                  softwareName = softwareToDelete.name + "";
                  this.LOG.info("Deleted: " + softwareName +
                      " (" + softwareToDelete.sys_id + ")" +
                      " from Installed Software (cmdb_software_instance) for computer " +
                      softwareToDelete.installed_on.name);
              } else {
                  softwareName = softwareToDelete.display_name + "";
                  this.LOG.info("Deleted: " + softwareName +
                      " " + softwareToDelete.version + " (" + softwareToDelete.sys_id + ")" +
                      " from Software Installations (cmdb_sam_sw_install) for computer " +
                      softwareToDelete.installed_on.name);
              }
              deletedSoftwareInstance.push(softwareName);
          }
      }
      return deletedSoftwareInstance;
  },

  type: 'BaseRemoveSoftwareUtil'
};

Sys ID

b0363d5553233010d7ffddeeff7b125d

Offical Documentation

Official Docs: