LoginSignup
6
5

More than 5 years have passed since last update.

Node-REDのfunctionでVCAP_SERVICESを取得する

Last updated at Posted at 2016-05-29

BluemixのNode-REDでVCAP_SERVICESの値を取得する方法。
パレットにないサービスをバインドして使うときに知っていると便利。
IOT Starterの構成ファイル(bluemix-settings.js)を編集して再デプロイしておく点がミソ。

参照: Creating apps with Node-RED Starter

手順

  1. IOT Starter作成
  2. Gitリポジトリを作成
  3. bluemix-settings.jsを編集
  4. 編集を反映してNode-REDを再起動
  5. Node-REDでVCAP_SERVICES取得

1. IOT Starter作成

Node-RED実行環境として、ボイラープレートのInternet of Things Platform Starterを作成する。作成手順はあちこちで紹介されていると思うのでここでは省略。
Qiita0528_01.jpg

2. Gitリポジトリを作成

IOT Starterの構成ファイルを取得して編集するため、上で作成したランタイムをBluemixのダッシュボードで開き、「Gitの追加」をクリックしてGitリポジトリを作成。
Qiita0528_2.jpg

(2016/12/28補足)「Gitの追加」のリンクの場所が、最近は下図のようにダッシュボードの「概要」の一番下に移動しています。
Screen Shot 2016-12-28 at 9.11.01.png

Gitリポジトリが出来上がると、「Gitの追加」と表示されていたところがGitリポジトリのURLリンクに変わるので、リンクをクリックしてリポジトリに移動。
DevOpsサービスの画面に切り替わるので、ここで「EDIT CODE」をクリックしてエディタに移動。
Qiita0528_03.jpg

3. bluemix-settings.jsを編集

エディタ画面でbluemix-settings.jsを選択。編集箇所は以下のとおり。

bluemix-settings.js
    functionGlobalContext: {
        VCAP_SERVICES: JSON.parse(process.env.VCAP_SERVICES)}, 

編集前:
Qiita0528_04.jpg

編集後:
Qiita0528_05.jpg

シンタックスエラーの赤丸バッテンは無視してもよいが、気になる場合は以下のコメント文を追加(node.js環境で稼働することをeslintに明示指定)。

bluemix-settings.js
/* eslint-env node */

編集したら「ファイル」→「保存」で保存
Qiita0529_06.jpg

4. 編集を反映してNode-REDを再起動

以下の手順に従って、Gitの画面で編集をコミット

  1. 左端列のGitアイコンをクリックしてGit画面に切り替え
  2. 適当にコメントを記入
  3. コミット (git commit -m コメント)
  4. プッシュ(git push origin master)

Qiita0529_07.jpg

プッシュをトリガーにしてNode-REDが再デプロイ&再起動される。進み具合が気になる人は「BUILD&DEPLOY」の画面へ。

Qiita0529_08.jpg

Qiita0529_09.jpg

5. Node-REDでVCAP_SERVICES取得

下図のフローのfunctionノードでVCAP_SERVICESを取得し、debug画面に値を出力。
Qiita0529_10.jpg

functionの実装は以下のとおり。この例ではIOT StarterランタイムにバインドされているCloudantNoSQLDBのcredentials情報を取得。
Qiita0529_11.jpg

VCAP_SERVICES取得
var services = context.global.VCAP_SERVICES;
var credentials = services['cloudantNoSQLDB'][0].credentials;
msg.username = credentials.username;
msg.password = credentials.password;
msg.host = credentials.host;
msg.port = credentials.port;
msg.url = credentials.url;

return msg;

実行結果は以下のとおり。
Qiita0529_12.jpg

最後に、Node-REDのフロー定義をEXPORTしたものを添付しておく。

Node-REDフロー定義
[{"id":"2127569f.f5c4da","type":"inject","z":"1c37fc7a.bda2a4","name":"トリガー","topic":"","payload":"","payloadType":"str","repeat":"","crontab":"","once":false,"x":93.5,"y":73,"wires":[["4fc639f9.5b5408"]]},{"id":"4fc639f9.5b5408","type":"function","z":"1c37fc7a.bda2a4","name":"VCAP_SERVICES取得","func":"var services = context.global.VCAP_SERVICES;\nvar credentials = services['cloudantNoSQLDB'][0].credentials;\nmsg.username = credentials.username;\nmsg.password = credentials.password;\nmsg.host = credentials.host;\nmsg.port = credentials.port;\nmsg.url = credentials.url;\n\nreturn msg;","outputs":1,"noerr":0,"x":163.5,"y":134,"wires":[["919a369f.115828","3a489d92.fcad72","dfb4dc69.4fb28","d7507d21.4537b","c37d9c34.0993a"]]},{"id":"919a369f.115828","type":"debug","z":"1c37fc7a.bda2a4","name":"username","active":true,"console":"false","complete":"username","x":370.5,"y":53,"wires":[]},{"id":"3a489d92.fcad72","type":"debug","z":"1c37fc7a.bda2a4","name":"password","active":true,"console":"false","complete":"password","x":370,"y":99,"wires":[]},{"id":"dfb4dc69.4fb28","type":"debug","z":"1c37fc7a.bda2a4","name":"host","active":true,"console":"false","complete":"host","x":361,"y":144,"wires":[]},{"id":"d7507d21.4537b","type":"debug","z":"1c37fc7a.bda2a4","name":"port","active":true,"console":"false","complete":"port","x":362,"y":190,"wires":[]},{"id":"c37d9c34.0993a","type":"debug","z":"1c37fc7a.bda2a4","name":"url","active":true,"console":"false","complete":"url","x":362,"y":239,"wires":[]}]

以上。

6
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
5