<commit_before>var osutils = require('./os-utils');


//constructor - should be the only export
function Neutron(endpoint_url, auth_token)
{
  //we need to overwrite this for unit testing and it allows us to use a custom request object that includes graphite logging
  this.request = require('request');

  //this is an optional lib that we override to normalfy the openstack responses - leave as is for no munging
  this.mangler = require('./mangler');
  this.mangleObject = this.mangler.mangleObject;

  //endpoint_url should come from the keystone projectInfo call - also yank all the trailing slashes in case folks get clever
  this.url = endpoint_url.replace(/\/$/, "");

  //auth_token should be the scoped token from the projectInfo call
  this.token = auth_token;

  //default the timeout to false - this forces the static value to be used
  this.timeout = false;
}


//allows for simple global timeouts if desired
Neutron.timeout = 9000;


//allows for instance timeouts if desired (see getRequestOptions)
Neutron.prototype.setTimeout = function(request_timeout)
{
  this.timeout = request_timeout;
};



//lets us override the existing request lib for this instance (useful for a bunch of things)
Neutron.prototype.setRequest = function(request_lib)
{
  this.request = request_lib;
};



//lets us mangle/sanitize/make sane the various responses from openstack
//any replacement must simply support a static mangleObject that supports the following types [ie mangleObject(type, object)]
//SecurityGroup, SecurityRule, etc..
Neutron.prototype.setMangler = function(mangle_lib)
{
  this.mangler = mangle_lib;
  this.mangleObject = this.mangler.mangleObject;
}



//returns an formatted options object - just makes the code below a little less repetitious
//path should begin with a "/"
//json_value should be almost certainly be true if you don't have an actual object you want to send over
Neutron.prototype.getRequestOptions = function(path, json_value)
{
  //start w/the instance timeout
  var request_timeout = this.timeout;
  if(!request_timeout)
  {
    //override with the static value if no instance value was given
    request_timeout = Neutron.timeout;
  }
  var return_object = {
    uri: this.url + path,
    headers:{'X-Auth-Token': this.token},
    json: json_value,
    timeout: request_timeout
  };

  return return_object;
};



//gets a list of all networks for the given project/tenant
//calls back with cb(error, network_array)
Neutron.prototype.listNetworks = function(cb){
  var self = this;
  var network_array = [];
  var n = 0;
  var request_options = this.getRequestOptions('/networks', true);
  request_options.logPath = 'api-calls.neutron.network-list';

  this.request.get(request_options, function(error, response, body){
    if(osutils.isError(error, response))
    {
      cb(osutils.getError('Neutron.listNetworks', error, response, body));
      return;
    }

    //can't tell at this point if an empty result would come back with a blank array or just no ports at all so...
    if(body.networks && body.networks.length)
    {
      for(n = 0; n < body.networks.length; n++)
      {
        network_array[n] = self.mangleObject('Network', body.networks[n]);
      }
    }
    else
    {
      //leave an empty array as the result and log an issue.... that might not be an issue... or might be...
      console.error('No networks found for this project - returning blank array');
    }

    cb(null, network_array);
  });
};



//gets a network by id (within the current project/tenant)
//calls back with cb(error, network_obj)
Neutron.prototype.getNetwork = function(network_id, cb){
  var self = this;
  var request_options = this.getRequestOptions('/networks/' + network_id, true);
  request_options.logPath = 'api-calls.neutron.network-get';

  this.request.get(request_options, function(error, response, body){
    if(osutils.isError(error, response) || !body.network)
    {
      cb(osutils.getError('Neutron.getNetwork', error, response, body));
      return;
    }
    //else

    cb(null, self.mangleObject('Network', body.network));
  });
};



//gets a list of all routers for the given project/tenant
//calls back with cb(error, router_array)
Neutron.prototype.listRouters = function(cb){
  var self = this;
  var router_array = [];
  var n = 0;<commit_msg>added subnet calls<commit_after>var osutils = require('./os-utils');


//constructor - should be the only export
function Neutron(endpoint_url, auth_token)
{
  //we need to overwrite this for unit testing and it allows us to use a custom request object that includes graphite logging
  this.request = require('request');

  //this is an optional lib that we override to normalfy the openstack responses - leave as is for no munging
  this.mangler = require('./mangler');
  this.mangleObject = this.mangler.mangleObject;

  //endpoint_url should come from the keystone projectInfo call - also yank all the trailing slashes in case folks get clever
  this.url = endpoint_url.replace(/\/$/, "");

  //auth_token should be the scoped token from the projectInfo call
  this.token = auth_token;

  //default the timeout to false - this forces the static value to be used
  this.timeout = false;
}


//allows for simple global timeouts if desired
Neutron.timeout = 9000;


//allows for instance timeouts if desired (see getRequestOptions)
Neutron.prototype.setTimeout = function(request_timeout)
{
  this.timeout = request_timeout;
};



//lets us override the existing request lib for this instance (useful for a bunch of things)
Neutron.prototype.setRequest = function(request_lib)
{
  this.request = request_lib;
};



//lets us mangle/sanitize/make sane the various responses from openstack
//any replacement must simply support a static mangleObject that supports the following types [ie mangleObject(type, object)]
//SecurityGroup, SecurityRule, etc..
Neutron.prototype.setMangler = function(mangle_lib)
{
  this.mangler = mangle_lib;
  this.mangleObject = this.mangler.mangleObject;
}



//returns an formatted options object - just makes the code below a little less repetitious
//path should begin with a "/"
//json_value should be almost certainly be true if you don't have an actual object you want to send over
Neutron.prototype.getRequestOptions = function(path, json_value)
{
  //start w/the instance timeout
  var request_timeout = this.timeout;
  if(!request_timeout)
  {
    //override with the static value if no instance value was given
    request_timeout = Neutron.timeout;
  }
  var return_object = {
    uri: this.url + path,
    headers:{'X-Auth-Token': this.token},
    json: json_value,
    timeout: request_timeout
  };

  return return_object;
};



//gets a list of all networks for the given project/tenant
//calls back with cb(error, network_array)
Neutron.prototype.listNetworks = function(cb){
  var self = this;
  var network_array = [];
  var n = 0;
  var request_options = this.getRequestOptions('/networks', true);
  request_options.logPath = 'api-calls.neutron.network-list';

  this.request.get(request_options, function(error, response, body){
    if(osutils.isError(error, response))
    {
      cb(osutils.getError('Neutron.listNetworks', error, response, body));
      return;
    }

    //can't tell at this point if an empty result would come back with a blank array or just no ports at all so...
    if(body.networks && body.networks.length)
    {
      for(n = 0; n < body.networks.length; n++)
      {
        network_array[n] = self.mangleObject('Network', body.networks[n]);
      }
    }
    else
    {
      //leave an empty array as the result and log an issue.... that might not be an issue... or might be...
      console.error('No networks found for this project - returning blank array');
    }

    cb(null, network_array);
  });
};



//gets a network by id (within the current project/tenant)
//calls back with cb(error, network_obj)
Neutron.prototype.getNetwork = function(network_id, cb){
  var self = this;
  var request_options = this.getRequestOptions('/networks/' + network_id, true);
  request_options.logPath = 'api-calls.neutron.network-get';

  this.request.get(request_options, function(error, response, body){
    if(osutils.isError(error, response) || !body.network)
    {
      cb(osutils.getError('Neutron.getNetwork', error, response, body));
      return;
    }
    //else

    cb(null, self.mangleObject('Network', body.network));
  });
};




//gets a list of all networks for the given project/tenant
//calls back with cb(error, network_array)
Neutron.prototype.listSubnets = function(cb){
  var self = this;
  var subnet_array = [];
  var n = 0;
  var request_options = this.getRequestOptions('/subnets', true);
  request_options.logPath = 'api-calls.neutron.subnet-list';

  this.request.get(request_options, function(error, response, body){
    if(osutils.isError(error, response))
    {
      cb(osutils.getError('Neutron.listSubnets', error, response, body));
      return;
    }

    //can't tell at this point if an empty result would come back with a blank array or just no ports at all so...
    if(body.subnets && body.subnets.length)
    {
      for(n = 0; n < body.subnets.length; n++)
      {
        subnet_array[n] = self.mangleObject('Subnet', body.subnets[n]);
      }
    }
    else
    {
      //leave an empty array as the result and log an issue.... that might not be an issue... or might be...
      console.error('No subnets found for this project - returning blank array');
    }

    cb(null, subnet_array);
  });
};



//gets a subnet by id (within the current project/tenant)
//calls back with cb(error, subnet_obj)
Neutron.prototype.getSubnet = function(subnet_id, cb){
  var self = this;
  var request_options = this.getRequestOptions('/subnets/' + subnet_id, true);
  request_options.logPath = 'api-calls.neutron.subnet-get';

  this.request.get(request_options, function(error, response, body){
    if(osutils.isError(error, response) || !body.subnet)
    {
      cb(osutils.getError('Neutron.getSubnet', error, response, body));
      return;
    }
    //else

    cb(null, self.mangleObject('Subnet', body.network));
  });
};


//gets a list of all routers for the given project/tenant
//calls back with cb(error, router_array)
Neutron.prototype.listRouters = function(cb){
  var self = this;
  var router_array = [];
  var n = 0;
{'fraction_non_alphanumeric': 0.1096936310041558, 'fraction_numerical': 0.001546341934860346, 'mean_word_length': 3.546572934973638, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6763632', 'n_tokens_mistral': 3187, 'n_tokens_neox': 3101, 'n_words': 1015}