-
Notifications
You must be signed in to change notification settings - Fork 916
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
817 additions
and
0 deletions.
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 |
---|---|---|
@@ -0,0 +1,173 @@ | ||
# Firestore | ||
|
||
This module allows to crete a firestore datatabase, fields, indexes and documents. | ||
|
||
## Examples | ||
|
||
### New database | ||
|
||
```hcl | ||
module "firestore" { | ||
source = "./fabric/modules/firestore" | ||
project_id = "my-project" | ||
database = { | ||
name = "my-database" | ||
location_id = "nam5" | ||
type = "FIRESTORE_NATIVE" | ||
} | ||
} | ||
# tftest modules=1 resources=1 inventory=new-database.yaml | ||
``` | ||
|
||
### New database with weekly backup | ||
|
||
```hcl | ||
module "firestore" { | ||
source = "./fabric/modules/firestore" | ||
project_id = "my-project" | ||
database = { | ||
name = "my-database" | ||
location_id = "nam5" | ||
type = "FIRESTORE_NATIVE" | ||
} | ||
backup_schedule = { | ||
retention = "86400s" | ||
weekly_recurrence = "MONDAY" | ||
} | ||
} | ||
# tftest modules=1 resources=2 inventory=new-database-with-weekly-backup.yaml | ||
``` | ||
|
||
### New database with document | ||
|
||
```hcl | ||
module "firestore" { | ||
source = "./fabric/modules/firestore" | ||
project_id = "my-project" | ||
database = { | ||
name = "my-database" | ||
location_id = "nam5" | ||
type = "FIRESTORE_NATIVE" | ||
} | ||
documents = { | ||
my-doc-1 = { | ||
collection = "my-coll" | ||
document_id = "d3db1c14-e56d-4597-af1c-f95c2d2290c1" | ||
fields = { | ||
field1 = "value1" | ||
field2 = "value2" | ||
} | ||
} | ||
} | ||
} | ||
# tftest modules=1 resources=2 inventory=new-database-with-document.yaml | ||
``` | ||
|
||
### Existing database with document | ||
|
||
```hcl | ||
module "firestore" { | ||
source = "./fabric/modules/firestore" | ||
project_id = "my-project" | ||
database = { | ||
name = "my-database" | ||
} | ||
database_create = false | ||
documents = { | ||
my-doc-1 = { | ||
collection = "my-coll" | ||
document_id = "d3db1c14-e56d-4597-af1c-f95c2d2290c1" | ||
fields = { | ||
field1 = "value1" | ||
field2 = "value2" | ||
} | ||
} | ||
} | ||
} | ||
# tftest modules=1 resources=1 inventory=existing-database-with-document.yaml | ||
``` | ||
|
||
### New database with field | ||
|
||
```hcl | ||
module "firestore" { | ||
source = "./fabric/modules/firestore" | ||
project_id = "my-project" | ||
database = { | ||
name = "my-database" | ||
location_id = "name5" | ||
type = "FIRESTORE_NATIVE" | ||
} | ||
fields = { | ||
my-field-in-my-coll = { | ||
collection = "my-coll" | ||
field = "my-field" | ||
indexes = [ | ||
{ | ||
order = "ASCENDING" | ||
query_scope = "COLLECTION_GROUP" | ||
}, | ||
{ | ||
array_config = "CONTAINS" | ||
} | ||
] | ||
} | ||
} | ||
} | ||
# tftest modules=1 resources=2 inventory=new-database-with-field.yaml | ||
``` | ||
|
||
### New database with index | ||
|
||
```hcl | ||
module "firestore" { | ||
source = "./fabric/modules/firestore" | ||
project_id = "my-project" | ||
database = { | ||
name = "my-database" | ||
location_id = "name5" | ||
type = "FIRESTORE_NATIVE" | ||
} | ||
indexes = { | ||
my-index = { | ||
collection = "my-coll" | ||
fields = [ | ||
{ | ||
field_path = "name" | ||
order = "ASCENDING" | ||
}, | ||
{ | ||
field_path = "description" | ||
order = "DESCENDING" | ||
} | ||
] | ||
} | ||
} | ||
} | ||
# tftest modules=1 resources=2 inventory=new-database-with-index.yaml | ||
``` | ||
<!-- BEGIN TFDOC --> | ||
## Variables | ||
|
||
| name | description | type | required | default | | ||
|---|---|:---:|:---:|:---:| | ||
| [database](variables.tf#L41) | Database attributes. | <code title="object({ app_engine_integration_mode = optional(string) concurrency_mode = optional(string) deletion_policy = optional(string) delete_protection_state = optional(string) kms_key_name = optional(string) location_id = optional(string) name = string point_in_time_recovery_enablement = optional(string) type = optional(string) })">object({…})</code> | ✓ | | | ||
| [project_id](variables.tf#L228) | Project id. | <code>string</code> | ✓ | | | ||
| [backup_schedule](variables.tf#L17) | Backup schedule. | <code title="object({ retention = string daily_recurrence = optional(bool, false) weekly_recurrence = optional(string) })">object({…})</code> | | <code>null</code> | | ||
| [database_create](variables.tf#L95) | Flag indicating whether the database should be created of not. | <code>string</code> | | <code>"true"</code> | | ||
| [documents](variables.tf#L101) | Documents. | <code title="map(object({ collection = string document_id = string fields = any }))">map(object({…}))</code> | | <code>{}</code> | | ||
| [fields](variables.tf#L112) | Fields. | <code title="map(object({ collection = string field = string indexes = optional(list(object({ query_scope = optional(string) order = optional(string) array_config = optional(string) }))) ttl_config = optional(bool, false) }))">map(object({…}))</code> | | <code>{}</code> | | ||
| [indexes](variables.tf#L164) | Indexes. | <code title="map(object({ api_scope = optional(string) collection = string fields = list(object({ field_path = optional(string) order = optional(string) array_config = optional(string) vector_config = optional(object({ dimension = optional(number) flat = optional(bool) })) })) query_scope = optional(string) }))">map(object({…}))</code> | | <code>{}</code> | | ||
|
||
## Outputs | ||
|
||
| name | description | sensitive | | ||
|---|---|:---:| | ||
| [firestore_database](outputs.tf#L17) | Firestore database. | | | ||
| [firestore_document_ids](outputs.tf#L22) | Firestore document ids. | | | ||
| [firestore_documents](outputs.tf#L26) | Firestore documents. | | | ||
| [firestore_field_ids](outputs.tf#L31) | Firestore field ids. | | | ||
| [firestore_fields](outputs.tf#L36) | Firestore fields. | | | ||
| [firestore_index_ids](outputs.tf#L41) | Firestore index ids. | | | ||
| [firestore_indexes](outputs.tf#L46) | Firestore indexes. | | | ||
<!-- END TFDOC --> |
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,135 @@ | ||
/** | ||
* Copyright 2024 Google LLC | ||
* | ||
* Licensed 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. | ||
*/ | ||
|
||
locals { | ||
firestore_database_name = var.database_create ? google_firestore_database.firestore_database[0].name : var.database.name | ||
} | ||
|
||
resource "google_firestore_database" "firestore_database" { | ||
count = var.database_create ? 1 : 0 | ||
provider = google-beta | ||
project = var.project_id | ||
name = var.database.name | ||
location_id = var.database.location_id | ||
type = var.database.type | ||
concurrency_mode = var.database.concurrency_mode | ||
app_engine_integration_mode = var.database.app_engine_integration_mode | ||
point_in_time_recovery_enablement = var.database.point_in_time_recovery_enablement | ||
delete_protection_state = var.database.delete_protection_state | ||
deletion_policy = var.database.deletion_policy | ||
|
||
dynamic "cmek_config" { | ||
for_each = var.database.kms_key_name == null ? [] : [""] | ||
content { | ||
kms_key_name = var.database.kms_key_name | ||
} | ||
} | ||
lifecycle { | ||
precondition { | ||
condition = var.database.type != null && contains(["DATASTORE_MODE", "FIRESTORE_NATIVE"], var.database.type) | ||
error_message = "Invalid type. Possible values: DATASTORE_MODE, FIRESTORE_NATIVE" | ||
} | ||
precondition { | ||
condition = var.database.location_id != null | ||
error_message = "location_id must be set." | ||
} | ||
} | ||
} | ||
|
||
resource "google_firestore_backup_schedule" "firestore_backup_schedule" { | ||
count = var.backup_schedule == null ? 0 : 1 | ||
project = var.project_id | ||
database = local.firestore_database_name | ||
retention = var.backup_schedule.retention | ||
|
||
dynamic "daily_recurrence" { | ||
for_each = var.backup_schedule.daily_recurrence ? [""] : [] | ||
content { | ||
|
||
} | ||
} | ||
|
||
dynamic "weekly_recurrence" { | ||
for_each = var.backup_schedule.weekly_recurrence == null ? [] : [""] | ||
content { | ||
day = var.backup_schedule.weekly_recurrence | ||
} | ||
} | ||
} | ||
|
||
resource "google_firestore_field" "firestore_fields" { | ||
for_each = var.fields | ||
project = var.project_id | ||
database = local.firestore_database_name | ||
collection = each.value.collection | ||
field = each.value.field | ||
|
||
dynamic "index_config" { | ||
for_each = each.value.indexes == null ? [] : [""] | ||
content { | ||
dynamic "indexes" { | ||
for_each = each.value.indexes | ||
content { | ||
query_scope = indexes.value.query_scope | ||
order = indexes.value.order | ||
array_config = indexes.value.array_config | ||
} | ||
} | ||
} | ||
} | ||
dynamic "ttl_config" { | ||
for_each = each.value.ttl_config ? [""] : [] | ||
content { | ||
|
||
} | ||
} | ||
} | ||
|
||
resource "google_firestore_document" "firestore_documents" { | ||
for_each = var.documents | ||
project = var.project_id | ||
database = local.firestore_database_name | ||
collection = each.value.collection | ||
document_id = each.value.document_id | ||
fields = jsonencode(each.value.fields) | ||
} | ||
|
||
resource "google_firestore_index" "firestore_indexes" { | ||
for_each = var.indexes | ||
project = var.project_id | ||
database = local.firestore_database_name | ||
collection = each.value.collection | ||
dynamic "fields" { | ||
for_each = each.value.fields | ||
content { | ||
field_path = fields.value.field_path | ||
order = fields.value.order | ||
array_config = fields.value.array_config | ||
dynamic "vector_config" { | ||
for_each = fields.value.vector_config == null ? [] : [""] | ||
content { | ||
dimension = fields.value.vector_config.dimension | ||
dynamic "flat" { | ||
for_each = fields.value.vector_config.flat ? [""] : [] | ||
content { | ||
|
||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
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,50 @@ | ||
/** | ||
* Copyright 2024 Google LLC | ||
* | ||
* Licensed 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. | ||
*/ | ||
|
||
output "firestore_database" { | ||
description = "Firestore database." | ||
value = var.database_create ? google_firestore_database.firestore_database[0] : null | ||
} | ||
|
||
output "firestore_document_ids" { | ||
description = "Firestore document ids." | ||
value = [for v in google_firestore_document.firestore_documents : v.id] | ||
} | ||
output "firestore_documents" { | ||
description = "Firestore documents." | ||
value = google_firestore_document.firestore_documents | ||
} | ||
|
||
output "firestore_field_ids" { | ||
description = "Firestore field ids." | ||
value = [for v in google_firestore_field.firestore_fields : v.id] | ||
} | ||
|
||
output "firestore_fields" { | ||
description = "Firestore fields." | ||
value = google_firestore_field.firestore_fields | ||
} | ||
|
||
output "firestore_index_ids" { | ||
description = "Firestore index ids." | ||
value = { for k, v in google_firestore_index.firestore_indexes : k => v.id } | ||
} | ||
|
||
output "firestore_indexes" { | ||
description = "Firestore indexes." | ||
value = google_firestore_index.firestore_indexes | ||
} | ||
|
Oops, something went wrong.