Name

global.CloudDeleteStrategyUtil

Description

No description available

Script

var CloudDeleteStrategyUtil = Class.create();
CloudDeleteStrategyUtil.prototype = {
  isDebug: false,
  debugCounter: 1,

  deleteStrategy: {
      KEEP: '1',
      DELETE: '2',
      ABSENT: '3',
      DELETE_RELATED: '4',
      RETIRED: '5'
  },

  _debug: function(msg, obj) {
      if (this.isDebug) {
          gs.info('@@@DeleteStrategyUtil[{0}]->{1}   data->{2}', this.debugCounter, msg, JSON.stringify(obj, null, 2));
          this.debugCounter++;
      }
  },

  initialize: function() {},

  _printDeleteError: function(errorMsg, configObj, response) {
      var msg =  '@@@DeleteStrategyUtil->' +errorMsg + "  configObj- " + JSON.stringify(configObj, null, 2);

      response.state = 'error';
      response.error_message = msg;

      // hiding errors by default unless debug is set to avoid a massive amount of errors from cloud events
      if (this.isDebug)
          gs.error(msg);
  },

  deleteCi: function(ci_sys_id, chosenStrategy, response) {
      var ciGr = new GlideRecord("cmdb_ci");
      ciGr.addQuery("sys_id", ci_sys_id);
      ciGr.query();

      if (ciGr.next()) {
          if (chosenStrategy == this.deleteStrategy.DELETE_RELATED)
              return SNC.DeleteStrategyHandler.handleDeleteRelatedStrategy(ciGr.sys_class_name, ci_sys_id, this.isDebug);

          if (!SNC.DeleteStrategyHandler.isValidDeleteStrategy(chosenStrategy)) {
              this._printDeleteError("deleteCi -> Unexpected delete strategy. Should be one of the strategies " + JSON.stringify(this.deleteStrategy, null, 2), chosenStrategy, response);

              return false;
          }

          return SNC.DeleteStrategyHandler.handleDeleteStrategy(chosenStrategy, ciGr.sys_class_name, ci_sys_id, this.isDebug);
      } else {
          this._printDeleteError(gs.getMessage('deleteCi -> did not find any CI in cmdb_ci that comply the given sys_id'), ci_sys_id, response);
          return false;
      }
  },

  /**
   *	Find the unique target CI in the relation table(should be from a specific type, have a relation of host on that should be pointed to the LDC)
   *	@Param listOfSydIds - array of suspected Cis which all comply to the same name of the objectId
   *	@Param chosenStrategy - The current chosen delete strategy
   *	return boolean - True if succeeded to delete the CI
   **/
  searchAndDeleteCi: function(configObj, response, listOfSydIds, chosenStrategy) {
      var successFlag = false;
      this._debug("Start searchAndDeleteCi", listOfSydIds);
      //The CI is always dependent on the LDC and hosted on it(so the LDC is the child)
      var relCiGr = new GlideRecord("cmdb_rel_ci");
      relCiGr.addQuery("parent", "IN", listOfSydIds.toString());
      relCiGr.addQuery("child", configObj.ldcSysId);
      relCiGr.addQuery('type', '5f985e0ec0a8010e00a9714f2a172815'); // Hosted-on relation
      relCiGr.query();

      if (relCiGr.next()) {
          var relParent = relCiGr.getValue("parent");
          var relChild = relCiGr.getValue("child");
          var relSysId = relCiGr.getUniqueValue();
          var relMsg = "parent[" + relParent + "], child-[" + relChild + "], rel_ci_sys_id[" + relSysId + "]";
          this._debug("searchAndDeleteCi-> Preparing to delete CI according the relation in cmdb_rel_ci ", relMsg);
          successFlag = this.deleteCi(relParent, chosenStrategy, response);
      } else {
          this._printDeleteError(gs.getMessage('searchAndDeleteCi -> did not find any objectId under a specific LDC'), configObj, response);
          successFlag = false;
      }
      return successFlag;
  },

  handleDelete: function(configObj, response) {
      var successFlag = false;
      this._debug("Start handleDelete", configObj);
      //Retrieve the default pattern delete strategy setting
      var chosenStrategy = gs.getProperty("service.watch.cloud.pattern.invocation.default.delete.strategy", this.deleteStrategy.ABSENT);

      //Try to retrieve a specific delete stratefy if exists for this CI
      var patternCiStrategy = new GlideRecord('sa_ci_to_pattern');
      patternCiStrategy.addQuery("pattern", configObj.patternId);
      patternCiStrategy.addQuery('is_main_ci', true);
      patternCiStrategy.query();

      if (patternCiStrategy.next()) {
          this._debug("handleDelete-> Found a configured delete strategy -", patternCiStrategy.getValue("deletion_strategy"));
          //Direct change to the CMDB set the CI according to the pattern delete strategy (deleted=1/keep=2/absent=3)
          var deletion_strategy = patternCiStrategy.getValue("deletion_strategy");
          chosenStrategy = (deletion_strategy) ? deletion_strategy : chosenStrategy;
      } else {
          this._debug("handleDelete-> no delete strategy was found", {});
      }

      //Serch for a specific unique CI acording to objectID within a specific LDC whith a relation of host on
      var cmdbCloudCi = new GlideRecord(configObj.classType);
      cmdbCloudCi.addQuery("object_id", configObj.inputObjectId);
      cmdbCloudCi.query();
      var listOfSydIds = [];
      while (cmdbCloudCi.next()) {
          listOfSydIds.push(cmdbCloudCi.getUniqueValue());
      }

      this._debug("handleDelete-> Found a list of suspected object Ids - ", listOfSydIds);
      switch (listOfSydIds.length) {
          case 0:
              this._debug('handleDelete-> deleteCi expect to get at least one sysId of a CI that resides within a specific LDC', configObj);
  			response.state = 'skipped';
  			response.error_message = gs.getMessage('This event tried without a success to delete the CI with object_id[{0}] and class type[{1}]', [configObj.inputObjectId,configObj.classType]);
              successFlag = false;
  			break;
          case 1:
              successFlag = this.deleteCi(listOfSydIds[0], chosenStrategy, response); //Only one sys_id, so no need to search
              break;
          default:
              successFlag = this.searchAndDeleteCi(configObj, response, listOfSydIds, chosenStrategy);
      }
  	response.state = (successFlag)? 'processed' : response.state;
      return successFlag;
  },

  type: 'CloudDeleteStrategyUtil'
};

Sys ID

5e3ea30e7703330099808d11681061cb

Offical Documentation

Official Docs: