-
Notifications
You must be signed in to change notification settings - Fork 903
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add commands to create and list RTDB instances (#1225)
Add commands to create and list RTDB instances
- Loading branch information
1 parent
a9914ea
commit 56f1144
Showing
5 changed files
with
96 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
feature - database:instances:{create,list} now allow viewing and creating Realtime Database instances | ||
fixed - Fixed issue with CORS rejecting some callable functions. | ||
fixed - Functions emulator no longer watches node_modules files. | ||
fixed - Functions emulator fails to route HTTPS functions to user-provided Express app | ||
fixed - Functions emulator fails to provide correct req.path | ||
fixed - Functions emulator fails on various malformed body requests | ||
fixed - Functions emulator fails on various malformed body requests |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import Command = require("../command"); | ||
import logger = require("../logger"); | ||
import requirePermissions = require("../requirePermissions"); | ||
import getProjectNumber = require("../getProjectNumber"); | ||
import firedata = require("../gcp/firedata"); | ||
|
||
export default new Command("database:instances:create <instanceName>") | ||
.description("create a realtime database instance") | ||
.before(requirePermissions, ["firebase.projects.create"]) | ||
.action(async (instanceName: string, options: any) => { | ||
const projectNumber = await getProjectNumber(options); | ||
const instance = await firedata.createDatabaseInstance(projectNumber, instanceName); | ||
logger.info(`created database instance ${instance.instance}`); | ||
return instance; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import Command = require("../command"); | ||
import logger = require("../logger"); | ||
import requirePermissions = require("../requirePermissions"); | ||
import getProjectNumber = require("../getProjectNumber"); | ||
import firedata = require("../gcp/firedata"); | ||
|
||
export default new Command("database:instances:list") | ||
.description("list realtime database instances") | ||
.before(requirePermissions, ["firebase.projects.list"]) | ||
.action(async (options: any) => { | ||
const projectNumber = await getProjectNumber(options); | ||
const instances = await firedata.listDatabaseInstances(projectNumber); | ||
for (const instance of instances) { | ||
logger.info(instance.instance); | ||
} | ||
logger.info(`Project ${options.project} has ${instances.length} database instances`); | ||
return instances; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import * as api from "../api"; | ||
import * as logger from "../logger"; | ||
import * as utils from "../utils"; | ||
|
||
export interface DatabaseInstance { | ||
// The globally unique name of the Database instance. | ||
// Required to be URL safe. ex: 'red-ant' | ||
instance: string; | ||
} | ||
|
||
function _handleErrorResponse(response: any): any { | ||
if (response.body && response.body.error) { | ||
return utils.reject(response.body.error, { code: 2 }); | ||
} | ||
|
||
logger.debug("[firedata] error:", response.status, response.body); | ||
return utils.reject("Unexpected error encountered with FireData.", { | ||
code: 2, | ||
}); | ||
} | ||
|
||
/** | ||
* Create a new Realtime Database instance | ||
* @param projectId Project from which you want to get the ruleset. | ||
* @param instanceName The name for the new Realtime Database instance. | ||
*/ | ||
export async function createDatabaseInstance( | ||
projectNumber: number, | ||
instanceName: string | ||
): Promise<any> { | ||
const response = await api.request("POST", `/v1/projects/${projectNumber}/databases`, { | ||
auth: true, | ||
origin: api.firedataOrigin, | ||
json: { | ||
instance: instanceName, | ||
}, | ||
}); | ||
if (response.status === 200) { | ||
return response.body.instance; | ||
} | ||
return _handleErrorResponse(response); | ||
} | ||
|
||
/** | ||
* Create a new Realtime Database instance | ||
* @param projectId Project from which you want to get the ruleset. | ||
* @param instanceName The name for the new Realtime Database instance. | ||
*/ | ||
export async function listDatabaseInstances(projectNumber: number): Promise<DatabaseInstance[]> { | ||
const response = await api.request("GET", `/v1/projects/${projectNumber}/databases`, { | ||
auth: true, | ||
origin: api.firedataOrigin, | ||
}); | ||
if (response.status === 200) { | ||
return response.body.instance; | ||
} | ||
return _handleErrorResponse(response); | ||
} |