Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Firebase CLI Supports Firestore Provisioning API #5616

Merged
merged 55 commits into from
Apr 17, 2023
Merged

Conversation

VicVer09
Copy link
Contributor

@VicVer09 VicVer09 commented Mar 20, 2023

b/267473272

Add support for Firestore Provisioning API.

This feature is experimental and may not yet be enabled on all projects. It introduces 6 new commands to the Firebase CLI:

List Locations (firestore:locations)

  • necessary for creating a firestore database, lists all available location ids in ASCII table format
  • optional flag --json to show full JSON response

Testing:

$ firebase firestore:locations
┌────────────────────┬──────────────────────────────┐
│ Display Name       │ LocationId                   │
├────────────────────┼──────────────────────────────┤
│ Taiwan             │ asia-east1                   │
├────────────────────┼──────────────────────────────┤
│ Hong Kong          │ asia-east2                   │
├────────────────────┼──────────────────────────────┤
│ Tokyo              │ asia-northeast1              │
├────────────────────┼──────────────────────────────┤
│ Osaka              │ asia-northeast2              │
├────────────────────┼──────────────────────────────┤
│ Seoul              │ asia-northeast3              │
├────────────────────┼──────────────────────────────┤
│ Mumbai             │ asia-south1                  │
├────────────────────┼──────────────────────────────┤
│ Singapore          │ asia-southeast1              │
├────────────────────┼──────────────────────────────┤
│ Jakarta            │ asia-southeast2              │
├────────────────────┼──────────────────────────────┤
│ Sydney             │ australia-southeast1         │
├────────────────────┼──────────────────────────────┤
│ Melbourne          │ australia-southeast2         │
├────────────────────┼──────────────────────────────┤
│ Europe             │ eur3                         │
├────────────────────┼──────────────────────────────┤
│ Warsaw             │ europe-central2              │
├────────────────────┼──────────────────────────────┤
│ London             │ europe-west2                 │
├────────────────────┼──────────────────────────────┤
│ Frankfurt          │ europe-west3                 │
├────────────────────┼──────────────────────────────┤
│ Netherlands        │ europe-west4                 │
├────────────────────┼──────────────────────────────┤
│ Zurich             │ europe-west6                 │
├────────────────────┼──────────────────────────────┤
│ United States      │ nam5                         │
├────────────────────┼──────────────────────────────┤
│ Montréal           │ northamerica-northeast1      │
├────────────────────┼──────────────────────────────┤
│ São Paulo          │ southamerica-east1           │
├────────────────────┼──────────────────────────────┤
│ South Carolina     │ us-east1                     │
├────────────────────┼──────────────────────────────┤
│ Northern Virginia  │ us-east4                     │
├────────────────────┼──────────────────────────────┤
│ Oregon             │ us-west1                     │
├────────────────────┼──────────────────────────────┤
│ Los Angeles        │ us-west2                     │
├────────────────────┼──────────────────────────────┤
│ Salt Lake City     │ us-west3                     │
├────────────────────┼──────────────────────────────┤
│ Las Vegas          │ us-west4                     │
└────────────────────┴──────────────────────────────┘

Create Database (firestore:databases:create)

  • creates a firestore database
  • mandatory positional argument for database ID
  • mandatory flag --location (location id)
  • This is a single value since multi region is not supported
  • Since Datastore Mode is not exposed/accessible in the Firebase console today, database type is hardcoded to be FIRESTORE_NATIVE.
  • optional flag –delete-protection ENABLED or DISABLED (default)
  • Newly created databases will have closed rules by default, at the end of command user is prompted to create a firestore rules configuration
  • Command will wait until the operation is complete, then displays the database details

Testing:

$ firebase firestore:databases:create demo --location nam5 --delete-protection ENABLED
Successfully created projects/firebase-cli-database-id-dev/databases/demo

List Databases (firestore:databases:list)

  • lists all existing databases
  • optional flag --json to show full JSON response

Testing:

$ firebase firestore:databases:list
┌─────────────────────────────────────────────────────────────────────────────┐
│ Database Name                                                               │
├─────────────────────────────────────────────────────────────────────────────┤
│ projects/firebase-cli-database-id-dev/databases/(default)                   │
├─────────────────────────────────────────────────────────────────────────────┤
│ projects/firebase-cli-database-id-dev/databases/named                       │
├─────────────────────────────────────────────────────────────────────────────┤
│ projects/firebase-cli-database-id-dev/databases/protected                   │
├─────────────────────────────────────────────────────────────────────────────┤
│ projects/firebase-cli-database-id-dev/databases/verylooooooooooooooongdb    │
└─────────────────────────────────────────────────────────────────────────────┘

Get Database (firestore:databases:get)

  • shows config for database
  • optional database positional argument, (default) if none provided
  • optional --json flag to display raw api response

Testing:

$ firebase firestore:databases:get demo       
┌─────────────────────────┬──────────────────────────────────────────────────────────────┐
│ Field                   │ Value                                                        │
├─────────────────────────┼──────────────────────────────────────────────────────────────┤
│ Name                    │ projects/firebase-cli-database-id-dev/databases/demo         │
├─────────────────────────┼──────────────────────────────────────────────────────────────┤
│ Create Time             │ 2023-02-24T20:14:48.306235Z                                  │
├─────────────────────────┼──────────────────────────────────────────────────────────────┤
│ Last Update Time        │ 2023-02-24T20:14:48.306235Z                                  │
├─────────────────────────┼──────────────────────────────────────────────────────────────┤
│ Type                    │ FIRESTORE_NATIVE                                             │
├─────────────────────────┼──────────────────────────────────────────────────────────────┤
│ Location                │ nam5                                                         │
├─────────────────────────┼──────────────────────────────────────────────────────────────┤
│ Delete Protection State │ DELETE_PROTECTION_DISABLED                                   │
└─────────────────────────┴──────────────────────────────────────────────────────────────┘

Update Database (firestore:databases:update)

  • updates delete protection state of given db
  • mandatory positional argument for database ID
  • mandatory delete protection state

Testing

$ firebase firestore:databases:update demo --delete-protection DISABLED
Successfully updated projects/firebase-cli-database-id-dev/databases/demo

Delete Database (firestore:databases:delete)

  • deletes given database
  • mandatory positional argument for database ID
  • confirmation prompt if --force not set
  • (default) database can also be deleted

Testing:

$ firebase firestore:databases:delete demo
? You are about to delete projects/firebase-cli-database-id-dev/databases/demo. Do you wish to continue? Yes
Successfully deleted projects/firebase-cli-database-id-dev/databases/demo

$ firebase firestore:databases:delete protected --force
Error: HTTP Error: 400, Cannot delete the database because delete protection state is set to DELETE_PROTECTION_ENABLED.

Having trouble? Try firebase [command] --help

@VicVer09 VicVer09 requested review from joehan and rwhogg March 31, 2023 17:02
@VicVer09
Copy link
Contributor Author

Addressed comments and after discussion with some of the team added another option to the create command to set delete protection state as well as another command - firestore:databases:update to modify both the database type and delete protection state. major difference is if protection is enabled, then firestore:databases:delete will 400.

Please take a look and pending council approval i'll merge the changes

CHANGELOG.md Outdated
- firestore:databases:get
- firestore:databases:delete
- firestore:locations
- Releases Cloud Firestore emulator v1.16.1, which adds support for read_time in ListCollectionIds.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unsure as to why this line is there?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah yep good catch, another stray changelog from a bad merge

Copy link
Contributor

@joehan joehan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couple small things but code LGTM - will leave any discussion of the API surface to other contexts

return;
}
const deleteProtectionState: types.DatabaseDeleteProtectionState =
options.deleteProtectionState || types.DatabaseDeleteProtectionState.DISABLED;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Prefer ?? over || here - it shouldn't make a functional difference with the current code, but ?? only falls through to the right when the left side is undefined (not just falsy), which is the intent here

CHANGELOG.md Outdated
@@ -1,2 +1,8 @@
- Adds support for the Firestore Provisioning API commands: (#5616)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider rewording this message to be a little clearer to external users - ie "Adds new commands for provisioning and managing Firestore databases:"

Also, include the databases:update command here too

"JSON specification format."
)
.option("--database <databaseId>", "Name of database to be updated. (required)")
.option(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: If I'm reading the code correctly, type can be updated, but its not obvious from this help text. Consider clarifying to something like ie Type to update the database to - with the current wording, it sound like i'm supposed to provide the current type/

appEngineIntegrationMode: string;
keyPrefix: string;
deleteProtectionState: string;
etag: string;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional - I've been partial to string union types instead of JS enums, as they require a little less type casting when used. You could consider them instead:

deleteProtectionState: "DELETE_PROTECTION_ENABLED" || "DELETE_PROTECTION_DISABLED"

@VicVer09 VicVer09 requested a review from joehan April 12, 2023 21:39
@VicVer09
Copy link
Contributor Author

added changes based on comments from firebase api council:

  • database is now a positional arg
  • use --json option instead of --pretty
  • database type always firestore native
  • --deleteProtectionState -> --delete-protection
  • DELETE_PROTECTION_ENABLED/DELETE_PROTECTION_DISABLED -> ENABLED/DISABLED
  • print locations and databases in ascii table format

Copy link
Contributor

@joehan joehan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 last thing to fix, and this should be good to go!


export const command = new Command("firestore:databases:create <database>")
.description("Create a database in your Firebase project.")
.option("--json", "Prints raw json response of the create API call if specified")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Applies for all commands: No need to explicitly declare --json as an option - it is defined as a program option for firebase, not on a per command basis (https://github.com/firebase/firebase-tools/blob/master/src/index.ts#L16)

To support --json, all you need to do is have your command return the JSON that should be shown - in this case, that's databaseResp, so you should be all good.

@VicVer09 VicVer09 merged commit f5786fd into master Apr 17, 2023
@anonimitoraf
Copy link

This looks great. Just confirming, does this allow multiple Firestore DBs in different locations? (e.g. 1 DB in us-east1, another in australia-southeast1)?

@VicVer09
Copy link
Contributor Author

This looks great. Just confirming, does this allow multiple Firestore DBs in different locations? (e.g. 1 DB in us-east1, another in australia-southeast1)?

yes, but for now only for projects that have the multiple databases feature enabled (in private preview).

most projects will not see this capability until later when it is generally available (cannot confirm any exact dates, but keep an eye out for announcements). error messages will say something along the lines of max quota reached.

@ycmjason
Copy link

THIS IS SO EXCITING

ProfHercules pushed a commit to ProfHercules/firebase-tools that referenced this pull request May 5, 2023
b/267473272 - Add support for Firestore Provisioning API. This feature is experimental and may not yet be enabled on all projects. It introduces 6 new commands to the Firebase CLI
tonyjhuang pushed a commit that referenced this pull request May 22, 2023
b/267473272 - Add support for Firestore Provisioning API. This feature is experimental and may not yet be enabled on all projects. It introduces 6 new commands to the Firebase CLI
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants