function getTenants()

in core/routemgmt/common/utils.js [90:147]


function getTenants(gwInfo, ns, tenantInstance) {
  var qsNsOnly = { 'filter[where][namespace]' : ns };
  var qsNsAndInstance = { 'filter[where][namespace]' : ns,
                          'filter[where][instance]'  : tenantInstance };
  var qs = qsNsOnly;
  if (tenantInstance) qs = qsNsAndInstance;
  var options = {
    followAllRedirects: true,
    url: gwInfo.gwUrl+'/tenants',
    qs: qs,
    headers: {
      'Accept': 'application/json'
    },
  };
  if (gwInfo.gwAuth) {
    options.headers.Authorization = 'Basic ' + gwInfo.gwAuth;
  }
  console.log('getTenants: request: '+JSON.stringify(options));

  return new Promise(function(resolve, reject) {
    request.get(options, function(error, response, body) {
      var statusCode = response ? response.statusCode : undefined;
      console.log('getTenants: response status: '+ statusCode);
      if (error) console.error('Warning: getTenant request failed: '+utils2.makeJsonString(error));
      if (body) console.log('getTenants: response body: '+utils2.makeJsonString(body));
      if (error) {
        console.error('getTenants: Unable to obtain tenant from the API Gateway');
        reject('Unable to obtain Tenant from the API Gateway: '+utils2.makeJsonString(error));
      } else if (statusCode != 200) {
        if (body) {
          var errMsg = JSON.stringify(body);
          if (body.error && body.error.message) errMsg = body.error.message;
          reject('API Gateway failure (status code '+statusCode+'): '+ errMsg);
        } else {
          reject('Unable to configure the API Gateway: Response failure code: '+statusCode);
        }
      } else {
        if (body) {
          try {
            var bodyJson = JSON.parse(body);
            if (Array.isArray(bodyJson)) {
              resolve(bodyJson);
            } else {
              console.error('getTenants: Invalid API GW response body; a JSON array was not returned');
              resolve( [] );
            }
          } catch(e) {
            console.error('getTenants: Invalid API GW response body; JSON.parse() failure: '+e);
            reject('Internal error. Invalid API Gateway response: '+e);
          }
        } else {
          console.log('getTenants: No tenants found');
          resolve( [] );
        }
      }
    });
  });
}