Skip to content

Commit 9443fb2

Browse files
maasthaandreaangiolillothemantissaZuhairahmedzach-carr
authored
INTMDB-802: Create new TF Data Federation Query Limit resource and data sources (#1173)
* INTMDB-802: Add data_source and resource for Federated Database Query Limit * INTMDB-802: update provider.go * INTMDB-802: Add AtlasDataFederation Query Limit resource * INTMDB-802: Add AtlasDataFederation Query Limit resource * Update data_source_mongodbatlas_federated_query_limit.go * Update resource_mongodbatlas_federated_query_limit.go * INTMDB-802: temp * INTMDB-802: Add example * INTMDB-802: Add example * INTMDB-802: minor * INTMDB-802: example fix * Acceptance tests * Acceptance tests * Fix docs * Update main.tf * Update mongodbatlas/resource_mongodbatlas_federated_query_limit.go Co-authored-by: Andrea Angiolillo <[email protected]> * Update mongodbatlas/resource_mongodbatlas_federated_query_limit.go Co-authored-by: Andrea Angiolillo <[email protected]> * Update mongodbatlas/data_source_mongodbatlas_federated_query_limit.go Co-authored-by: Andrea Angiolillo <[email protected]> * Update mongodbatlas/resource_mongodbatlas_federated_query_limit.go Co-authored-by: Andrea Angiolillo <[email protected]> * Update mongodbatlas/resource_mongodbatlas_federated_query_limit.go Co-authored-by: Andrea Angiolillo <[email protected]> * Update website/docs/d/federated_query_limit.html.markdown Co-authored-by: Melissa Plunkett <[email protected]> * Update website/docs/d/federated_query_limit.html.markdown Co-authored-by: Melissa Plunkett <[email protected]> * Update website/docs/r/federated_query_limit.html.markdown Co-authored-by: Melissa Plunkett <[email protected]> * Update website/docs/r/federated_query_limit.html.markdown Co-authored-by: Melissa Plunkett <[email protected]> * Update website/docs/r/federated_query_limit.html.markdown Co-authored-by: Melissa Plunkett <[email protected]> * Update website/docs/d/federated_query_limits.html.markdown Co-authored-by: Melissa Plunkett <[email protected]> * Update website/docs/r/federated_query_limit.html.markdown Co-authored-by: Melissa Plunkett <[email protected]> * Update federated_query_limit.html.markdown * Update federated_query_limit.html.markdown * Address PR comments * Address PR comments * Update website/docs/d/federated_query_limit.html.markdown Co-authored-by: Zuhair Ahmed <[email protected]> * Update website/docs/d/federated_query_limits.html.markdown Co-authored-by: Zuhair Ahmed <[email protected]> * Update examples/federated-database-query-limit/README.md Co-authored-by: Zuhair Ahmed <[email protected]> * Update examples/federated-database-query-limit/README.md Co-authored-by: Zuhair Ahmed <[email protected]> * Update examples/federated-database-query-limit/README.md Co-authored-by: Zuhair Ahmed <[email protected]> * Update federated_query_limit.html.markdown * Update federated_query_limits.html.markdown * Update README.md * Address PR comments * Address PR comments * Apply suggestions from code review Co-authored-by: zach-carr <[email protected]> * INTMDB-805: [Terraform] Create new TF Data Lake Pipelines Run data sources only (not resource) (#1177) * INTMDB-803: [Terraform] Create a new Private Endpoint resource and data sources which supports Federated Database Instance and Online Archive (#1182) * Fix AWS provider version and example * updated federated instance * Addressed Comments * Update mongodbatlas/data_source_mongodbatlas_federated_query_limits.go Co-authored-by: Andrea Angiolillo <[email protected]> * Update data_source_mongodbatlas_federated_query_limits.go --------- Co-authored-by: Andrea Angiolillo <[email protected]> Co-authored-by: Melissa Plunkett <[email protected]> Co-authored-by: Zuhair Ahmed <[email protected]> Co-authored-by: zach-carr <[email protected]>
1 parent dc5ebe3 commit 9443fb2

16 files changed

+1456
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Example - MongoDB Atlas Federated Database Query Limit with Atlas clusters
2+
3+
This project aims to provide an example of using [MongoDB Atlas Federated Database Query Limit](https://www.mongodb.com/docs/atlas/data-federation/overview/).
4+
5+
6+
## Dependencies
7+
8+
* Terraform MongoDB Atlas Provider v1.10.0
9+
* A MongoDB Atlas account
10+
11+
```
12+
Terraform v1.4.6
13+
+ provider registry.terraform.io/terraform-providers/mongodbatlas v1.10.0
14+
```
15+
16+
## Usage
17+
**1\. Create a .tfvars file**
18+
19+
This example requires an Atlas Project to already exist. Once a project is created create the terraform.tfvars file and enter the values for all the required variables, including the project, and make sure **not to commit it**.
20+
21+
**2\. Review the Terraform plan**
22+
23+
Execute the below command and ensure you are happy with the plan. The `terraform plan` command lets you to preview the actions Terraform would take to modify your infrastructure, or save a speculative plan which you can apply later.
24+
25+
``` bash
26+
$ terraform plan
27+
```
28+
This project currently supports the below deployments:
29+
30+
- Two MongoDB Atlas clusters in the specified project
31+
- MongoDB Atlas Federated Database Instance based on Atlas clusters
32+
- MongoDB Atlas Federated Database Query Limit
33+
34+
**3\. Execute the Terraform apply.**
35+
36+
Now execute the plan to provision the Federated settings resources. The `terraform apply` command performs a plan just like `terraform plan` does, but then actually carries out the planned changes to each resource using the relevant infrastructure provider's API. It asks for confirmation from the user before making any changes, unless it was explicitly told to skip approval.
37+
38+
``` bash
39+
$ terraform apply
40+
```
41+
42+
**4\. Destroy the resources.**
43+
44+
Once you are finished your testing, ensure you destroy the resources to avoid unnecessary Atlas charges. Calling the `terraform destroy` command will instruct Terraform to terminate / destroy all the resources managed. This will enable you to completely tear down and remove all resources defined in the Terraform State that have previously been deployed.
45+
46+
``` bash
47+
$ terraform destroy
48+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
resource "mongodbatlas_advanced_cluster" "atlas_cluster_1" {
2+
project_id = var.project_id
3+
name = var.atlas_cluster_name_1
4+
cluster_type = "REPLICASET"
5+
6+
replication_specs {
7+
region_configs {
8+
electable_specs {
9+
instance_size = var.provider_instance_size_name
10+
}
11+
provider_name = var.provider_name
12+
backing_provider_name = var.backing_provider_name
13+
region_name = var.provider_region_name
14+
priority = 7
15+
}
16+
}
17+
}
18+
19+
resource "mongodbatlas_advanced_cluster" "atlas_cluster_2" {
20+
project_id = var.project_id
21+
name = var.atlas_cluster_name_2
22+
cluster_type = "REPLICASET"
23+
24+
replication_specs {
25+
region_configs {
26+
electable_specs {
27+
instance_size = var.provider_instance_size_name
28+
}
29+
provider_name = var.provider_name
30+
backing_provider_name = var.backing_provider_name
31+
region_name = var.provider_region_name
32+
priority = 7
33+
}
34+
}
35+
}
36+
37+
resource "mongodbatlas_federated_database_instance" "test-instance" {
38+
project_id = var.project_id
39+
name = var.federated_instance_name
40+
storage_databases {
41+
name = "VirtualDatabase0"
42+
collections {
43+
name = "VirtualCollection0"
44+
data_sources {
45+
collection = var.collection_1
46+
database = var.database_1
47+
store_name = mongodbatlas_advanced_cluster.atlas_cluster_1.name
48+
}
49+
data_sources {
50+
collection = var.collection_2
51+
database = var.database_2
52+
store_name = mongodbatlas_advanced_cluster.atlas_cluster_2.name
53+
}
54+
}
55+
}
56+
57+
storage_stores {
58+
name = mongodbatlas_advanced_cluster.atlas_cluster_1.name
59+
cluster_name = mongodbatlas_advanced_cluster.atlas_cluster_1.name
60+
project_id = var.project_id
61+
provider = "atlas"
62+
read_preference {
63+
mode = "secondary"
64+
}
65+
}
66+
67+
storage_stores {
68+
name = mongodbatlas_advanced_cluster.atlas_cluster_2.name
69+
cluster_name = mongodbatlas_advanced_cluster.atlas_cluster_2.name
70+
project_id = var.project_id
71+
provider = "atlas"
72+
read_preference {
73+
mode = "secondary"
74+
}
75+
}
76+
}
77+
78+
resource "mongodbatlas_federated_query_limit" "query_limit" {
79+
project_id = var.project_id
80+
tenant_name = mongodbatlas_federated_database_instance.test-instance.name
81+
limit_name = var.federated_query_limit
82+
overrun_policy = var.overrun_policy
83+
value = var.limit_value
84+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
output "mongodbatlas_federated_query_limit_name" {
2+
value = mongodbatlas_federated_query_limit.query_limit.limit_name
3+
}
4+
5+
output "mongodbatlas_federated_query_limit_default_limit" {
6+
value = mongodbatlas_federated_query_limit.query_limit.default_limit
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
provider "mongodbatlas" {
2+
public_key = var.public_key
3+
private_key = var.private_key
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
variable "public_key" {
2+
type = string
3+
description = "Public Programmatic API key to authenticate to Atlas"
4+
}
5+
variable "private_key" {
6+
type = string
7+
description = "Private Programmatic API key to authenticate to Atlas"
8+
}
9+
variable "project_id" {
10+
type = string
11+
description = "MongoDB Project ID"
12+
}
13+
14+
variable "collection_1" {
15+
type = string
16+
description = "Human-readable label that identifies the collection in the database in first cluster"
17+
}
18+
19+
variable "database_1" {
20+
type = string
21+
description = "Human-readable label that identifies the database, which contains the collection in the first cluster"
22+
}
23+
24+
variable "collection_2" {
25+
type = string
26+
description = "Human-readable label that identifies the collection in the database in second cluster"
27+
}
28+
29+
variable "database_2" {
30+
type = string
31+
description = "Human-readable label that identifies the database, which contains the collection in the second cluster"
32+
}
33+
34+
variable "federated_instance_name" {
35+
type = string
36+
description = "MongoDB Federated Database Instance Name."
37+
default = "FederatedDatabaseInstance0"
38+
}
39+
40+
variable "atlas_cluster_name_1" {
41+
type = string
42+
description = "First Atlas Cluster Name."
43+
default = "ClusterFederatedTest1"
44+
}
45+
46+
variable "atlas_cluster_name_2" {
47+
type = string
48+
description = "Second Atlas Cluster Name."
49+
default = "ClusterFederatedTest2"
50+
}
51+
52+
variable "provider_region_name" {
53+
type = string
54+
description = "Physical location where MongoDB Cloud deploys your AWS-hosted MongoDB cluster nodes."
55+
default = "US_EAST_1"
56+
}
57+
58+
variable "provider_instance_size_name" {
59+
type = string
60+
description = "Cluster tier. Default is M10"
61+
default = "M10"
62+
}
63+
64+
variable "backing_provider_name" {
65+
type = string
66+
default = "AWS"
67+
}
68+
69+
variable "provider_name" {
70+
type = string
71+
default = "AWS"
72+
}
73+
74+
variable "federated_query_limit" {
75+
type = string
76+
description = "Human-readable label that identifies the user-managed limit to modify."
77+
default = "bytesProcessed.monthly"
78+
}
79+
80+
variable "overrun_policy" {
81+
type = string
82+
description = "Action to take when the usage limit is exceeded."
83+
default = "BLOCK"
84+
}
85+
86+
variable "limit_value" {
87+
type = number
88+
description = "Amount to set the federated query limit to."
89+
default = 5147483648
90+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
terraform {
2+
required_providers {
3+
mongodbatlas = {
4+
source = "mongodb/mongodbatlas"
5+
}
6+
}
7+
required_version = ">= 0.13"
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package mongodbatlas
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9+
)
10+
11+
func dataSourceMongoDBAtlasFederatedDatabaseQueryLimit() *schema.Resource {
12+
return &schema.Resource{
13+
ReadContext: dataSourceMongoDBAtlasFederatedDatabaseQueryLimitRead,
14+
Schema: schemaMongoDBAtlasFederatedDatabaseQueryLimitDataSource(),
15+
}
16+
}
17+
18+
func schemaMongoDBAtlasFederatedDatabaseQueryLimitDataSource() map[string]*schema.Schema {
19+
return map[string]*schema.Schema{
20+
"project_id": {
21+
Type: schema.TypeString,
22+
Required: true,
23+
},
24+
"limit_name": {
25+
Type: schema.TypeString,
26+
Required: true,
27+
},
28+
"tenant_name": {
29+
Type: schema.TypeString,
30+
Required: true,
31+
},
32+
"overrun_policy": {
33+
Type: schema.TypeString,
34+
Computed: true,
35+
},
36+
"value": {
37+
Type: schema.TypeInt,
38+
Computed: true,
39+
},
40+
"current_usage": {
41+
Type: schema.TypeInt,
42+
Computed: true,
43+
},
44+
"default_limit": {
45+
Type: schema.TypeInt,
46+
Computed: true,
47+
},
48+
"last_modified_date": {
49+
Type: schema.TypeString,
50+
Computed: true,
51+
},
52+
"maximum_limit": {
53+
Type: schema.TypeInt,
54+
Computed: true,
55+
},
56+
}
57+
}
58+
59+
func dataSourceMongoDBAtlasFederatedDatabaseQueryLimitRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
60+
conn := meta.(*MongoDBClient).Atlas
61+
62+
projectID := d.Get("project_id").(string)
63+
tenantName := d.Get("tenant_name").(string)
64+
limitName := d.Get("limit_name").(string)
65+
66+
queryLimit, _, err := conn.DataFederation.GetQueryLimit(ctx, projectID, tenantName, limitName)
67+
68+
if err != nil {
69+
return diag.FromErr(fmt.Errorf("couldn't import federated database query limit(%s) for project (%s), tenant (%s), error: %s", limitName, projectID, tenantName, err))
70+
}
71+
72+
if err = setResourceFieldsFromFederatedDatabaseQueryLimit(d, projectID, queryLimit); err != nil {
73+
return diag.FromErr(err)
74+
}
75+
76+
d.SetId(encodeStateID(map[string]string{
77+
"project_id": projectID,
78+
"tenant_name": queryLimit.TenantName,
79+
"limit_name": queryLimit.Name,
80+
}))
81+
82+
return nil
83+
}

0 commit comments

Comments
 (0)