Example #1
0
function get_api_database (req, res) {
  if (req.suffix.length > 1) { 
    actions.resultBad(req, res, arangodb.ERROR_HTTP_BAD_PARAMETER);
    return;
  }
  
  var result;
  if (req.suffix.length === 0) {
    // list of all databases
    result = arangodb.db._listDatabases();
  }
  else {
    if (req.suffix[0] === 'user') {
      // return all databases for current user
      var username = '', password = '', auth = '';

      if (req.headers.hasOwnProperty('authorization')) {
        auth = req.headers.authorization;
        var header = req.headers.authorization.replace(/^Basic\s+/i, '');
        var decoded = require("internal").base64Decode(header);
        var pos = decoded.indexOf(':');

        if (pos >= 0) {
          username = decoded.substr(0, pos);
          password = decoded.substr(pos + 1, decoded.length - pos - 1);
        }
      }

      result = arangodb.db._listDatabases(username, password, auth);
    }
    else if (req.suffix[0] === 'current') {
      if (cluster.isCoordinator()) {
        // fetch database information from Agency
        var values = ArangoAgency.get("Plan/Databases/" + encodeURIComponent(req.database), false);
        var dbEntry = values["Plan/Databases/" + encodeURIComponent(req.database)];
        result = {
          name: dbEntry.name,
          id: dbEntry.id,
          path: "none",
          isSystem: (dbEntry.name.substr(0, 1) === '_')
        };
      }
      else {
        // information about the current database
        result = {
          name: arangodb.db._name(),
          id: arangodb.db._id(),
          path: arangodb.db._path(),
          isSystem: arangodb.db._isSystem()
        };
      }
    }
    else {
      actions.resultBad(req, res, arangodb.ERROR_HTTP_BAD_PARAMETER);
      return;
    }
  }

  actions.resultOk(req, res, actions.HTTP_OK, { result : result });
}
Example #2
0
function get_api_database (req, res) {
  if (req.suffix.length > 1) { 
    actions.resultBad(req, res, arangodb.ERROR_HTTP_BAD_PARAMETER);
    return;
  }
  
  if (req.suffix.length > 1) {
    actions.resultBad(req, res, arangodb.ERROR_HTTP_BAD_PARAMETER);
    return;
  }

  var result;
  if (req.suffix.length === 0) {
    // list of all databases
    result = arangodb.db._listDatabases();
  }
  else {
    if (req.suffix[0] === 'user') {
      // return all databases for current user
      var username = '', password = '';

      if (req.headers.hasOwnProperty('authorization')) {
        var header = req.headers.authorization.replace(/^Basic\s+/i, '');
        var decoded = require("internal").base64Decode(header);
        var pos = decoded.indexOf(':');

        if (pos >= 0) {
          username = decoded.substr(0, pos);
          password = decoded.substr(pos + 1, decoded.length - pos - 1);
        }
      }

      result = arangodb.db._listDatabases(username, password);
    }
    else if (req.suffix[0] === 'current') {
      // information about the current database
      result = {
        name: arangodb.db._name(),
        id: arangodb.db._id(),
        path: arangodb.db._path(),
        isSystem: arangodb.db._isSystem()
      };
    }
    else {
      actions.resultBad(req, res, arangodb.ERROR_HTTP_BAD_PARAMETER);
      return;
    }
  }

  actions.resultOk(req, res, actions.HTTP_OK, { result : result });
}