Skip to content
This repository has been archived by the owner on Jan 26, 2022. It is now read-only.

Commit

Permalink
cf-runtime version 0.0.1
Browse files Browse the repository at this point in the history
Change-Id: Ic7d57a8c9f916b7f362a37fefce5e7600281ba58
  • Loading branch information
mariash committed May 3, 2012
0 parents commit f6d5f90
Show file tree
Hide file tree
Showing 25 changed files with 1,469 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cf-runtime/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
tests/services.conf
node_modules/
2 changes: 2 additions & 0 deletions cf-runtime/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
tests/
node_modules/
369 changes: 369 additions & 0 deletions cf-runtime/LICENSE.txt

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions cf-runtime/NOTICE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
vcap-node
Copyright (c) 2012 VMware, Inc. All Rights Reserved.

This product is licensed to you under the Apache License, Version 2.0 (the "License").
You may not use this product except in compliance with the License.

This product includes a number of subcomponents with
separate copyright notices and license terms. Your use of these
subcomponents is subject to the terms and conditions of the
subcomponent's license, as noted in the LICENSE file.
180 changes: 180 additions & 0 deletions cf-runtime/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
# cf-runtime

cf-runtime is a Node.js module that provides API to Cloud Foundry platform. It provides easy access to Cloud Foundry application properties and services.

## Installation

```bash
npm install cf-runtime
```

If cloned from github install dependencies:

```bash
npm install -d
```

### Tests

Run basic tests:

```bash
npm test
```

To run integration tests copy services.conf.template to services.conf with specified services connection properties and run:

```bash
TESTS=integration npm test
```

## Usage

```js
var cf = require('cf-runtime')
var app = cf.CloudApp

// Check if application is running on Cloud Foundry

app.runningInCloud

// Get application properties

app.host
app.port

// Get the list of application service names

app.serviceNames

// Obtain connection properties for single service of type Redis

app.serviceProps.redis

// Obtain connection properties for service named 'redis-service-name'

app.serviceProps['redis-service-name']

// Obtain the list of service names of specific type

app.serviceNamesOfType.redis

// Check if service of the given type is available

cf.RedisClient !== undefined

// Connect to a single service of type Redis

var redisClient = cf.RedisClient.create()

// Connect to mysql service named 'redis-service-name'

var redisClient = cf.RedisClient.createFromSvc('redis-service-name')

```

### Service properties

* name
* label (service type)
* version
* host
* port
* username
* password

#### Additional properties

Rabbitmq:

* url
* vhost

MongoDB:

* db
* url

Postgresql, Mysql, Redis:

* database

### Service clients

This is the list of Node.js modules that are used to provide connection to Cloud Foundry services:

#### AMQP client

Node module: [amqp](https://github.com/postwait/node-amqp)

Functions:

* cf.AMQPClient.create([implOptions]) - creates and returns an amqp client instance connected to a single rabbitmq service
* cf.AMQPClient.createFromSvc(name, [implOptions]) - creates and returns an amqp client instance connected to a rabbitmq service with the specified name

Parameters:

implOptions - optional {object} non-connection related implementation options

Returns: AMQP client instance

#### Mongodb client

Node module: [mongodb](https://github.com/christkv/node-mongodb-native)

Functions:

* cf.MongoClient.create([options], callback) - creates a mongodb client instance connected to a single mongodb service and executes provided callback
* cf.MongoClient.createFromSvc(name, [options], callback) - creates a mongodb client instance connected to a mongodb service with the specified name and executes provided callback

Parameters:

options - optional {object} non-connection related options
callback - {function} connection callback

Returns: null

#### Mysql client

Node module: [mysql](https://github.com/felixge/node-mysql)

Functions:

* cf.MysqlClient.create([options]) - creates and returns a mysql client instance connected to a single mysql service
* cf.MysqlClient.createFromSvc(name, [options]) - creates and returns a mysql client instance connected to a mysql service with the specified name

Parameters:

options - optional {object} non-connection related options

Returns: Mysql client instance

#### Postgresql client

Node module: [pg](https://github.com/brianc/node-postgres)

Functions:

* cf.PGClient.create(callback) - creates a postgresql client instance connected to a single postgresql service and executes provided callback
* cf.PGClient.createFromSvc(name, callback) - creates a postgresql client instance connected to a postgresql service with the specified name and executes provided callback

Parameters:

callback - {function} connection callback

Returns: {boolean}

#### Redis client

Node module: [redis](https://github.com/mranney/node_redis)

Functions:

* cf.RedisClient.create([options]) - creates and returns a redis client instance connected to a single redis service
* cf.RedisClient.createFromSvc(name, [options]) - creates and returns a redis client instance connected to a redis service with the specified name

Parameters:

options - optional {object} non-connection related options

Returns: Redis client instance
44 changes: 44 additions & 0 deletions cf-runtime/lib/amqp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Cloud Foundry API module
* AMQP client to rabbitmq service
*
* create([implOptions]) - creates and returns an amqp client instance
* connected to a single rabbitmq service
*
* createFromSvc(name, [implOptions]) - creates and returns an amqp client
* instance connected to a rabbitmq service with the specified name
*
* implOptions - non-connection related implementation options
*/

var sc = require("./service")
, util = require("util");

function AMQPClient () {
this.type = "rabbitmq";
}

util.inherits(AMQPClient, sc.ServiceClient);

AMQPClient.prototype._create = function (props) {
var amqp = require("amqp");

var config = { "login" : props.username
, "password" : props.password
, "host" : props.host
, "port" : props.port
, "vhost" : props.vhost
};

return amqp.createConnection(config, arguments[1]);
};

exports.create = function () {
var client = new AMQPClient();
return client.create.apply(client, arguments);
};

exports.createFromSvc = function (name) {
var client = new AMQPClient();
return client.createFromSvc.apply(client, arguments);
};
41 changes: 41 additions & 0 deletions cf-runtime/lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Cloud Foundry API module
*
* CloudApp - application data
* MysqlClient - mysql service connection
* PGClient - postgresql service connection
* MongoDBClient - mongodb service connection
* RedisClient - redis service connection
* AMQPClient - rabbitmq service connection
*/

(function () {

module.exports = exports;

var CloudApp = exports.CloudApp = require("./properties");

if (CloudApp.runningInCloud) {

if (CloudApp.serviceNamesOfType.mongodb !== undefined) {
exports.MongoClient = require("./mongodb");
}

if (CloudApp.serviceNamesOfType.redis !== undefined) {
exports.RedisClient = require("./redis");
}

if (CloudApp.serviceNamesOfType.mysql !== undefined) {
exports.MysqlClient = require("./mysql");
}

if (CloudApp.serviceNamesOfType.postgresql !== undefined) {
exports.PGClient = require("./postgresql");
}

if (CloudApp.serviceNamesOfType.rabbitmq !== undefined) {
exports.AMQPClient = require("./amqp");
}
}

})();
41 changes: 41 additions & 0 deletions cf-runtime/lib/mongodb.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Cloud Foundry API module
* MongoDB client
*
* create([options], callback) - creates a mongodb client instance
* connected to a single mongodb service and executes provided callback
*
* createFromSvc(name, [options], callback) - creates a mongodb client
* instance connected to a mongodb service with the specified name
* and executes provided callback
*/

var sc = require("./service")
, util = require("util");

function MongoClient () {
this.type = "mongodb";
}

util.inherits(MongoClient, sc.ServiceClient);

MongoClient.prototype._create = function (props) {
var mongodb = require("mongodb").Db;

if (arguments.length > 2) {
return mongodb.connect(props.url, arguments[1], arguments[2]);
}
else {
return mongodb.connect(props.url, arguments[1]);
}
};

exports.create = function () {
var client = new MongoClient();
return client.create.apply(client, arguments);
};

exports.createFromSvc = function (name) {
var client = new MongoClient();
return client.createFromSvc.apply(client, arguments);
};
44 changes: 44 additions & 0 deletions cf-runtime/lib/mysql.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Cloud Foundry API module
* MySQL client
*
* create([options]) - creates and returns a mysql client instance
* connected to a single mysql service
*
* createFromSvc(name, [options]) - creates and returns a mysql client
* instance connected to a mysql service with the specified name
*/

var sc = require("./service")
, util = require("util");

function MysqlClient () {
this.type = "mysql";
}

util.inherits(MysqlClient, sc.ServiceClient);

MysqlClient.prototype._create = function (props) {
var mysql = require("mysql");

var options = {};
if (arguments[1] !== undefined) options = arguments[1];

options.host = props.host;
options.port = props.port;
options.user = props.username;
options.password = props.password;
options.database = props.database;

return mysql.createClient(options);
};

exports.create = function () {
var client = new MysqlClient();
return client.create.apply(client, arguments);
};

exports.createFromSvc = function (name) {
var client = new MysqlClient();
return client.createFromSvc.apply(client, arguments);
};
Loading

0 comments on commit f6d5f90

Please sign in to comment.