Name

sn_agent.PolicyUpdateDomainSeparation

Description

No description available

Script

var PolicyUpdateDomainSeparation = Class.create();
PolicyUpdateDomainSeparation.prototype = {
  initialize: function() {
      this.policyStatusUpdates = {};
      this.isDomainSeparationPluginActive = (new DomainInfo()).isDomainSeparationPluginActive();
  },

  /**
   * Updates the policy status map based on the passed policy record and publish status.
   * If the {domain, policy} combination already exists in the map, this function will replace the existing
   * status value, unless the existing value takes precedence over the new value.
   * 
   * Parameters:
   *     policyGr - a GlideRecord pointing to a record in sn_agent_policy
   *     status - the new publish status of the policy
   * 
   * Return:
   *     none
   */
  updatePolicyStatusMap: function(policyGr, status) {
      if (!this.isDomainSeparationPluginActive) {
          gs.warn("Cannot call PolicyUpdateDomainSeparation functions when domain separation is not active");
          return;
      }

      var domainId = policyGr.getValue('sys_domain');
      var policyId = policyGr.getUniqueValue();
      this.updatePolicyStatusMapFromId(domainId, policyId, status);
  },

  /**
   * Updates the policy status map based on the passed domain sys_id, policy sys_id, and publish status.
   * If the {domain, policy} combination already exists in the map, this function will replace the existing
   * status value, unless the existing value takes precedence over the new value.
   * 
   * Parameters:
   *     domainId - the sys_id of the domain of the policy
   *     policyId - the sys_id of the policy
   *     status - the new publish status of the policy
   * 
   * Return:
   *     none
   */
  updatePolicyStatusMapFromId: function(domainId, policyId, status) {
      if (!this.isDomainSeparationPluginActive) {
          gs.warn("Cannot call PolicyUpdateDomainSeparation functions when domain separation is not active");
          return;
      }

      // create an entry for the domain if it does not exist
      this.policyStatusUpdates[domainId] = this.policyStatusUpdates.hasOwnProperty(domainId) ? this.policyStatusUpdates[domainId] : {};
      // create an entry for the policy if it does not exist
      this.policyStatusUpdates[domainId][policyId] = this.policyStatusUpdates[domainId].hasOwnProperty(policyId) ? this.policyStatusUpdates[domainId][policyId] : '';

      var currentEntryStatus = this.policyStatusUpdates[domainId][policyId];
      // if the entry for the policy is "Error", then do not update the entry, since "Error" has the highest precedence
      if (currentEntryStatus == '6')
          return;

      this.policyStatusUpdates[domainId][policyId] = status;
  },

  /**
   * Updates the policies in the policy status map with their new statuses
   */
  updatePolicies: function() {
      if (!this.isDomainSeparationPluginActive) {
          gs.warn("Cannot call PolicyUpdateDomainSeparation functions when domain separation is not active");
          return;
      }

      var initialDomainSysId = (new DomainInfo()).getCurrentDomain();
      try {
          for (var domainId in this.policyStatusUpdates) {
              this.changeSessionDomain(domainId);

              for (var policyId in this.policyStatusUpdates[domainId]) {
                  var newStatus = this.policyStatusUpdates[domainId][policyId];
                  var policyGr = new GlideRecord('sn_agent_policy');
                  policyGr.get(policyId);
                  var currentStatus = policyGr.getValue('publish_status');
                  if (currentStatus == newStatus)
                      continue;
                  policyGr.setValue('publish_status', newStatus);
                  policyGr.update();
              }
          }
      } finally {
          // go back to original domain
          gs.debug("switching back to original domain: " + initialDomainSysId);
          this.changeSessionDomain(initialDomainSysId);
      }
  },

  /**
   * Changes the current session to the specified domain.
   * This uses the CTIEvaluator, which is a hack to execute Javascript in global scope from the scoped app.
   * If you need to call this, take care to reset the session domain if needed.
   * 
   * Parameters:
   *     domainSysId - the sys_id of the domain
   */
  changeSessionDomain: function(domainSysId) {
      if (!this.isDomainSeparationPluginActive) {
          gs.warn("Cannot call PolicyUpdateDomainSeparation functions when domain separation is not active");
          return;
      }

      var changeDomainScript = "GlideSession.get().setDomainID('" + domainSysId + "');";
      var e = new global.CTIEvaluator(changeDomainScript);
      e.evaluate();
  },

  /**
   * Gets domains from the sn_agent_ci_extended_info table
   * 
   * Return:
   *     an array containing domain sys_ids
   */
  getAgentLeafDomains: function() {
      if (!this.isDomainSeparationPluginActive)
          return ['global'];

      var extendedInfoAggregate = new GlideAggregate('sn_agent_ci_extended_info');
      extendedInfoAggregate.groupBy('sys_domain');
      extendedInfoAggregate.query();

      if (!extendedInfoAggregate.hasNext())
          return [];

      var domainSysIds = [];
      while (extendedInfoAggregate.next())
          domainSysIds.push(extendedInfoAggregate.getValue('sys_domain'));

      return domainSysIds;
  },

  type: 'PolicyUpdateDomainSeparation'
};

Sys ID

c5896a0f77ede1107c09ee1b8d5a99dd

Offical Documentation

Official Docs: