Name

sn_agent.AbstractTestCheckBuilder

Description

A Base Class for Check Defintion and Instance Test Builders.

Script

var AbstractTestCheckBuilder = Class.create();
AbstractTestCheckBuilder.prototype = {
  // Set the sys_id of the check to test.
  withCheckId: function(checkId) {
      this.checkId = checkId;
      return this;
  },

  // Set the CI sys_id to test the check against.
  withCiId: function(ciId) {
      this.ciId = ciId;
      return this;
  },

  // Set the credentials sys_id to use. This removes any previous assignment done by calling withCredentialsName,
  // withCredentialsAliasId or withCredentialsAliasName.
  withCredentialsId: function(credentialsId) {
      this.credId = credentialsId;
      this.credName = null;
      this.credAliasId = null;
      this.credAliasName = null;
      return this;
  },

  // Set the credentials name to use. If the given value is the name of several credentials records, then one of them is choosen randomly.
  // This removes any previous assignment done by calling withCredentialsId, withCredentialsAliasId or withCredentialsAliasName.
  withCredentialsName: function(credentialsName) {
      this.credName = credentialsName;
      this.credId = null;
      this.credAliasId = null;
      this.credAliasName = null;
      return this;
  },

  // Set the credentials alias sys_id to use. This removes any previous assignment done by calling withCredentialsId,
  // withCredentialsName or withCredentialsAliasName.
  withCredentialsAliasId: function(credentialsAliasId) {
      this.credAliasId = credentialsAliasId;
      this.credId = null;
      this.credName = null;
      this.credAliasName = null;
      return this;
  },

  // Set the credentials alias name to use. If the given value is the name of several credentials aliases records, then one of them is choosen randomly.
  // This removes any previous assignment done by calling withCredentialsId, withCredentialsName or withCredentialsAliasId.
  withCredentialsAliasName: function(credentialsAliasName) {
      this.credAliasName = credentialsAliasName;
      this.credId = null;
      this.credName = null;
      this.credAliasId = null;
      return this;
  },

  // Build the test check request with the arguments set. Returns a JSON with 2 attributes: the test result record id
  // and an error. Both are strings. In case the build was successful, test result id is not nil and error is nil.
  // In case an error happened during build, test result id is nil and error is not nil.
  build: function() {
      // _build is implemented in the sub classes...
      return this._build();
  },

  getCheckGr: function(checkTable) {
      var retJson = {
          checkGr: null,
          error: null
      };
      var checkGr = new GlideRecord(checkTable);
      if (!checkGr.get(this.checkId))
          retJson.error = this.CHECK_NOT_FOUND_ERR;
      else
          retJson.checkGr = checkGr;
      return retJson;
  },

  validateCreds: function(isDef, policyGr) {
      var credIdSpecified = !gs.nil(this.credId);
      var credNameSpecified = !gs.nil(this.credName);
      var credAliasIdSpecified = !gs.nil(this.credAliasId);
      var credAliasNameSpecified = !gs.nil(this.credAliasName);
      if (this.monSync.isCheckUsingSecureParam(this.checkId, isDef)) {
          if (!credIdSpecified && !credNameSpecified && !credAliasIdSpecified && !credAliasNameSpecified) {
              if (isDef)
                  return this.CREDENTIALS_NOT_SPECIFIED_ERR;
              // if this is a check instance, try taking creds from policy
              this.credName = policyGr.getValue("cred_alias");
              credNameSpecified = !gs.nil(this.credName);
              this.credAliasId = policyGr.getValue("credential_alias");
              credAliasIdSpecified = !gs.nil(this.credAliasId);
              if (!credNameSpecified && !credAliasIdSpecified)
                  return this.CREDENTIALS_NOT_SPECIFIED_ERR;
          }
          if (credNameSpecified) {
              var credGr = new GlideRecord(this.CREDENTIALS);
              credGr.addQuery("name", this.credName);
              credGr.query();
              if (credGr.next())
                  this.credId = credGr.getUniqueValue();
              else
                  return this.CREDENTIALS_NAME_NOT_FOUND_ERR;
          } else if (credAliasNameSpecified) {
              var credAliasGr = new GlideRecord(this.ALIASES);
              credAliasGr.addQuery("type", "credential");
              credAliasGr.addQuery("name", this.credAliasName);
              credAliasGr.query();
              if (credAliasGr.next())
                  this.credAliasId = credAliasGr.getUniqueValue();
              else
                  return this.CREDENTIALS_ALIAS_NAME_NOT_FOUND_ERR;
          } else if (credIdSpecified) {
              var cred = new GlideRecord(this.CREDENTIALS);
              if (!cred.get(this.credId))
                  return this.CREDENTIALS_ID_NOT_FOUND_ERR;
          } else if (credAliasIdSpecified) {
              var credAlias = new GlideRecord(this.ALIASES);
              credAlias.addQuery("type", "credential");
              credAlias.addQuery("sys_id", this.credAliasId);
              credAlias.query();
              if (credAlias.getRowCount() < 1)
                  return this.CREDENTIALS_ALIAS_ID_NOT_FOUND_ERR;
          }
      } else if (credIdSpecified || credNameSpecified || credAliasIdSpecified || credAliasNameSpecified)
          return this.CREDENTIALS_SPECIFIED_ERR;
      return null;
  },

  checkId: null,
  ciId: null,
  credId: null,
  credName: null,
  credAliasId: null,
  credAliasName: null,

  monSync: new MonitoringSync(),

  CHECK_NOT_FOUND_ERR: "given check sys_id is nil or non existent",
  CHECK_BACKGROUND_ERR: "test check is not supported for background checks",
  CI_NO_AGENT_ERR: "given ci doesn't have an associated agent which is up",
  CREDENTIALS_NOT_SPECIFIED_ERR: "credentials/credentials alias are not specified by name/sys_id although the given check requires them",
  CREDENTIALS_SPECIFIED_ERR: "credentials/credentials alias are specified by name/sys_id although the given check doesn't require them",
  CREDENTIALS_NAME_NOT_FOUND_ERR: "given credentials name is nil or non existent",
  CREDENTIALS_ALIAS_NAME_NOT_FOUND_ERR: "given credentials alias name is nil or non existent",
  CREDENTIALS_ID_NOT_FOUND_ERR: "given credentials sys_id is non existent",
  CREDENTIALS_ALIAS_ID_NOT_FOUND_ERR: "given credentials alias sys_id is non existent",
  ERR: "error creating test check request",

  CREDENTIALS: "discovery_credentials",
  ALIASES: "sys_alias",

  initialize: function() {},

  type: 'AbstractTestCheckBuilder'
};

Sys ID

2fe3d25d53f8301062d1ddeeff7b122f

Offical Documentation

Official Docs: