You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@openwhisk.apache.org by GitBox <gi...@apache.org> on 2018/11/20 05:24:42 UTC

[GitHub] jsambit commented on a change in pull request #163: Added support for Azure cosmosDB for alarms package

jsambit commented on a change in pull request #163: Added support for Azure cosmosDB for alarms package
URL: https://github.com/apache/incubator-openwhisk-package-alarms/pull/163#discussion_r234872463
 
 

 ##########
 File path: provider/lib/cosmosdb.js
 ##########
 @@ -0,0 +1,223 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+module.exports = function(endpoint, masterKey) {
+    var DocumentClient = require('documentdb').DocumentClient;
+
+    var client = new DocumentClient(endpoint, {masterKey: masterKey});
+
+    this.client = client;
+    var utilsDB = this;
+
+    this.init = function(databaseName) {
+        let querySpec = {
+            query: 'SELECT * FROM root r WHERE r.id = @id',
+            parameters: [{ name: '@id', value: databaseName }]
+        };
+        return new Promise((resolve, reject) => {
+        client.queryDatabases(querySpec).toArray((err, results) => {
+            if(err){
+                reject(err);
+            }
+
+            console.log("cosmosdb client initialized successfully");
+            utilsDB.dbLink = results[0]._self;
+            resolve();
+        });
+        });
+    };
+
+    //get or create collection in cosmosdb terminology
+    this.createDatabase = function(log, collectionName) {
+        var method = 'createDatabase';
+        let logger = log;
+        logger.info(method, 'creating the trigger database');
+        let querySpec = {
+            query: 'SELECT * FROM root r WHERE r.id=@id',
+            parameters: [{ name: '@id', value: collectionName }]
+        };
+        return new Promise((resolve, reject) => {
+            client.queryCollections(utilsDB.dbLink, querySpec).toArray((err, results) => {
+            if (err) {
+                reject(err);
+            }
+            if (results.length === 0) {
+                logger.info(method, "No Trigger database found. Creating one.");
+                createDB(collectionName)
+                    .then((col) => {
+                        logger.info(method, "created trigger database.");
+                        utilsDB.collectionLink = col._self;
+                        resolve(utilsDB);
+                    })
+                    .catch((err) => reject(err));
+            } else {
+                utilsDB.collectionLink = results[0]._self;
+                utilsDB.prefix = results[0].id;
+                resolve(utilsDB);
+            }
+            });
+        });
+    };
+
+       //create collection in cosmosdb terminology
+    function createDB (collectionName) {
+        var collectionDefinition = { id: collectionName };
+        return new Promise((resolve, reject) => {
+            client.createCollection(utilsDB.dbLink, collectionDefinition, function(err, collection) {
+                if(err) reject(err);
+
+                console.log("Created collection");
+                utilsDB.collectionLink = collection._self;
+                resolve(collection);
+            });
+        });
+    }
+
+
+    this.createTrigger = function(triggerID, newTrigger) {
+        if(!newTrigger.id) {
+            console.log("add id to doc");
+            newTrigger.id = triggerID;
+        }
+
+        return new Promise(function(resolve, reject) {
+
+            client.createDocument(utilsDB.collectionLink, newTrigger, function(err, document) {
+            if(err) reject(err);
+
+            console.log("created trigger " + triggerID);
+            resolve();
+        });
+        });
+    };
+
+    this.getTrigger = function(triggerID) {
 
 Review comment:
   Need to support retry for DB calls.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services