Name

global.CloudMidSelectionApi

Description

Cloud management MID selection API. It should be used by all calls for requesting MIDs for cloud.

Script

var CloudMidSelectionApi = Class.create();
CloudMidSelectionApi.prototype = {
  initialize: function() {},

  /*
   * Select a single mid for cloud
   * 
   * @param: {string} app Application calling this API (e.g. ServiceMapping)
   * @param: {string} provider provider. This won't be respected if ldc is passed in. Example here is AWS.
   * @param: {string} ldc Logical data center. 
   * @param: {object} context Extra context that callers needs to pass through that will be availabe at the MID selection override.   
   */
  selectMid: function(app, provider, ldc, context) {
      // initialize
      var midSelector = new SNC.MidSelector();
      var capabilities = [];
      var midServer = '';

      // By default we should have cloud management capability - since this is a cloud call
      capabilities.push({
          'capability': 'Cloud Management'
      });

      // If ldc is not specified - it is by provider (e.g. Ansible/Puppet)	
      // If ldc is specified we can get provider from ldc
      if (gs.nil(ldc)) {
          if (gs.nil(provider))
              throw 'Logical datacenter or provider has to be specified for cloud MID selection';

          // Simple add the provider as capability and all done
          capabilities.push({
              'capability': provider
          });

      } else {
          var glideUtil = new GlideRecordUtil();

          var dcType;
          //Try to figure out dcType from service account
          if (!gs.nil(context)) {
              var contextObj = JSON.parse(context);
              var cloudServiceAccountId = contextObj.service_account_id;

              if (!gs.nil(cloudServiceAccountId)) {
                  var servAcc = glideUtil.getGlideRecordByAttribute('cmdb_ci_cloud_service_account', 'account_id', cloudServiceAccountId);
                  if (servAcc.next())
                      dcType = servAcc.getValue('datacenter_type');
              }
          }

          if (gs.nil(dcType)) {
              // This should not happen - so we should throw an exception rather than simply warn and move on
              var ldcRec = glideUtil.getGlideRecordByAttribute('cmdb_ci_logical_datacenter', 'region', ldc);
              if (!ldcRec.next())
                  throw 'Logical datacenter ' + ldc + ' could not be found!';

              // This should never be null								
              dcType = ldcRec.getValue('sys_class_name');
          }
          // get the provider (cloudType) and add it as a capability
          var capiRec = glideUtil.getGlideRecordByAttribute('sn_capi_provider', 'datacenter_class', dcType);
          if (!capiRec.next())
              throw 'Provider not found for datacenterType ' + dcType + '!';

          var cloudType = capiRec.getValue('name');
          if (gs.nil(cloudType))
              throw 'Provider name is empty for datacenterType ' + dcType + '!';

          capabilities.push({
              'capability': cloudType,
              'value': ldc
          });

      }

      // Now we have everything set up - call the mid selector API to select one mid
      return midSelector.selectMid(app, null, capabilities, context);
  },

  type: 'CloudMidSelectionApi'
};

Sys ID

019292f7132893009f325db12244b04b

Offical Documentation

Official Docs: