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

INTMDB-311: Feature Add: Prometheus and Microsoft Team to the Third Party Integration Settings #706

Merged
merged 14 commits into from
Apr 24, 2022
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions examples/MongoDB-Atlas-Third-Party-Integration/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
iatlaspl.code-workspace
terraform.tfvars
.terraform/
*.tfstate*

108 changes: 108 additions & 0 deletions examples/MongoDB-Atlas-Third-Party-Integration/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
# Example - A basic example to start with the MongoDB Atlas and Terraform
themantissa marked this conversation as resolved.
Show resolved Hide resolved

This project aims to provide a very straight-forward example of setting up Terraform with MongoDB Atlas. This will create the following resources in MongoDB Atlas:

- Atlas Project
- MongoDB Cluster - M10
- Database User
- IP Access List

You can refer to the MongoDB Atlas documentation to know about the region names used in MongoDB Atlas respective to the Cloud Provider's region name.
[Amazon Web Services (AWS)](https://docs.atlas.mongodb.com/reference/amazon-aws/#amazon-aws)
[Google Cloud Platform (GCP)](https://docs.atlas.mongodb.com/reference/google-gcp/#google-gcp)
[Microsoft Azure](https://docs.atlas.mongodb.com/reference/microsoft-azure/#microsoft-azure)

## Dependencies

* Terraform v0.13 or greater
* A MongoDB Atlas account
* provider.mongodbatlas: version = "~> 0.9.1"

## Usage

**1\. Ensure your MongoDB Atlas credentials are set up.**

This can be done using environment variables:

```bash
export MONGODB_ATLAS_PUBLIC_KEY="xxxx"
export MONGODB_ATLAS_PRIVATE_KEY="xxxx"
```

... or follow as in the `variables.tf` file and create **terraform.tfvars** file with all the variable values and make sure **not to commit it**.


> **IMPORTANT** Hard-coding your MongoDB Atlas programmatic API key pair into a Terraform configuration is not recommended. Consider the risks, especially the inadvertent submission of a configuration file containing secrets to a public repository.


**2\. Review the Terraform plan.**

Execute the below command and ensure you are happy with the plan.

``` bash
$ terraform plan
```

This project currently creates the below deployments:

- Atlas Project
- MongoDB cluster - M10
- Database User
- IP Access list

**3\. Execute the Terraform apply.**

Now execute the plan to provision the MongoDB Atlas resources.

``` bash
$ terraform apply
```

**4\. Destroy the resources.**

Once you are finished your testing, ensure you destroy the resources to avoid unnecessary charges.

``` bash
$ terraform destroy
```

**Important Point**

You can fetch the connection string as per the use case by following the MongoDB Atlas documentation on [Connect to your cluster](https://docs.atlas.mongodb.com/tutorial/connect-to-your-cluster/index.html).

Or to fetch the connection string using terraform follow the below steps:

```hcl
output "atlasclusterstring" {
value = mongodbatlas_cluster.cluster.connection_strings
}
```
**Outputs:**
```hcl
atlasclusterstring = [
{
"aws_private_link" = {
"vpce-0ebb76559e8affc96" = "mongodb://pl-0-us-east-1.za3fb.mongodb.net:1024,pl-0-us-east-1.za3fb.mongodb.net:1025,pl-0-us-east-1.za3fb.mongodb.net:1026/?ssl=true&authSource=admin&replicaSet=atlas-d177ke-shard-0"
}
"aws_private_link_srv" = {
"vpce-0ebb76559e8affc96" = "mongodb+srv://mongodb-atlas-pl-0.za3fb.mongodb.net"
}
"private" = ""
"private_srv" = ""
"standard" = "mongodb://mongodb-atlas-shard-00-00.za3fb.mongodb.net:27017,mongodb-atlas-shard-00-01.za3fb.mongodb.net:27017,mongodb-atlas-shard-00-02.za3fb.mongodb.net:27017/?ssl=true&authSource=admin&replicaSet=atlas-d177ke-shard-0"
"standard_srv" = "mongodb+srv://mongodb-atlas.za3fb.mongodb.net"
},
]
```

To fetch a particular connection string, use the **lookup()** function of terraform as below:

```
output "plstring" {
value = lookup(mongodbatlas_cluster.cluster.connection_strings[0].aws_private_link_srv, aws_vpc_endpoint.ptfe_service.id)
}
```
**Output:**
```
plstring = mongodb+srv://cluster-atlas-pl-0.za3fb.mongodb.net
```
24 changes: 24 additions & 0 deletions examples/MongoDB-Atlas-Third-Party-Integration/atlas_cluster.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
resource "mongodbatlas_cluster" "cluster" {
project_id = mongodbatlas_project.project.id
name = var.cluster_name
mongo_db_major_version = var.mongodbversion
cluster_type = "REPLICASET"
replication_specs {
num_shards = 1
regions_config {
region_name = var.region
electable_nodes = 3
priority = 7
read_only_nodes = 0
}
}
# Provider Settings "block"
cloud_backup = true
auto_scaling_disk_gb_enabled = true
provider_name = var.cloud_provider
provider_instance_size_name = "M10"
}
output "connection_strings" {
value = mongodbatlas_cluster.cluster.connection_strings[0].standard_srv
}

19 changes: 19 additions & 0 deletions examples/MongoDB-Atlas-Third-Party-Integration/database_user.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# DATABASE USER [Configure Database Users](https://docs.atlas.mongodb.com/security-add-mongodb-users/)
resource "mongodbatlas_database_user" "user" {
username = var.dbuser
password = var.dbuser_password
project_id = mongodbatlas_project.project.id
auth_database_name = "admin"

roles {
role_name = "readWrite"
database_name = var.database_name # The database name and collection name need not exist in the cluster before creating the user.
}
labels {
key = "Name"
value = "DB User1"
}
}
output "user1" {
value = mongodbatlas_database_user.user.username
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
resource "mongodbatlas_project_ip_access_list" "ip" {
project_id = mongodbatlas_project.project.id
ip_address = var.ip_address
comment = "IP Address for accessing the cluster"
}
output "ipaccesslist" {
value = mongodbatlas_project_ip_access_list.ip.ip_address
}
7 changes: 7 additions & 0 deletions examples/MongoDB-Atlas-Third-Party-Integration/project.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
resource "mongodbatlas_project" "project" {
name = var.project_name
org_id = var.org_id
}
output "project_name" {
value = mongodbatlas_project.project.name
}
4 changes: 4 additions & 0 deletions examples/MongoDB-Atlas-Third-Party-Integration/provider.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
provider "mongodbatlas" {
public_key = var.public_key
private_key = var.private_key
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
resource "mongodbatlas_third_party_integration" "test_msteams" {
project_id = mongodbatlas_project.project.id
type = "MICROSOFT_TEAMS"
microsoft_teams_webhook_url = "https://mongodb0.webhook.office.com/webhookb2/zfd-15e8-47de-9a7a-355183e89a68@thi-841b-4ef9-af16-33548de0c958/IncomingWebhook/xyz"
themantissa marked this conversation as resolved.
Show resolved Hide resolved
}

resource "mongodbatlas_third_party_integration" "test_prometheus" {
project_id = mongodbatlas_project.project.id
type = "PROMETHEUS"
user_name = "prom_user_621952567c87684fd69b0101"
password = "KeQvcbkBhrNeuhVE"
service_discovery = "file"
scheme = "https"
enabled = true
}
51 changes: 51 additions & 0 deletions examples/MongoDB-Atlas-Third-Party-Integration/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
variable "public_key" {
type = string
description = "Public Programmatic API key to authenticate to Atlas"
}
variable "private_key" {
type = string
description = "Private Programmatic API key to authenticate to Atlas"
}
variable "org_id" {
type = string
description = "MongoDB Organization ID"
}
variable "project_name" {
type = string
description = "The MongoDB Atlas Project Name"
}
variable "cluster_name" {
type = string
description = "The MongoDB Atlas Cluster Name"
}
variable "cloud_provider" {
type = string
description = "The cloud provider to use, must be AWS, GCP or AZURE"
}
variable "region" {
type = string
description = "MongoDB Atlas Cluster Region, must be a region for the provider given"
}
variable "mongodbversion" {
type = string
description = "The Major MongoDB Version"
}
variable "dbuser" {
type = string
description = "MongoDB Atlas Database User Name"
}
variable "dbuser_password" {
type = string
description = "MongoDB Atlas Database User Password"
}
variable "database_name" {
type = string
description = "The database in the cluster to limit the database user to, the database does not have to exist yet"
}
variable "ip_address" {
type = string
description = "The IP address that the cluster will be accessed from, can also be a CIDR range or AWS security group"
}



8 changes: 8 additions & 0 deletions examples/MongoDB-Atlas-Third-Party-Integration/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
terraform {
required_providers {
mongodbatlas = {
source = "mongodb/mongodbatlas"
}
}
required_version = ">= 0.13"
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ require (
github.com/mwielbut/pointy v1.1.0
github.com/spf13/cast v1.4.1
github.com/terraform-providers/terraform-provider-aws v1.60.1-0.20210625132053-af2d5c0ad54f
go.mongodb.org/atlas v0.15.1-0.20220215171307-4b760c3c624f
go.mongodb.org/atlas v0.15.1-0.20220403193624-86b34ba344cd
go.mongodb.org/realm v0.1.0
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1221,6 +1221,10 @@ go.etcd.io/etcd/client/v2 v2.305.0/go.mod h1:h9puh54ZTgAKtEbut2oe9P4L/oqKCVB6xsX
go.mongodb.org/atlas v0.12.0/go.mod h1:wVCnHcm/7/IfTjEB6K8K35PLG70yGz8BdkRwX0oK9/M=
go.mongodb.org/atlas v0.15.1-0.20220215171307-4b760c3c624f h1:IvKkFdSSBLC5kqB1X87vn8CRAI7eXoMSK7u2lG+WUg8=
go.mongodb.org/atlas v0.15.1-0.20220215171307-4b760c3c624f/go.mod h1:lQhRHIxc6jQHEK3/q9WLu/SdBkPj2fQYhjLGUF6Z3U8=
go.mongodb.org/atlas v0.15.1-0.20220330015822-18ef33419ce7 h1:nJjUIAkZMJ07WCYDlvqnZPPfOkGUdKJj1m9nPOggibw=
go.mongodb.org/atlas v0.15.1-0.20220330015822-18ef33419ce7/go.mod h1:lQhRHIxc6jQHEK3/q9WLu/SdBkPj2fQYhjLGUF6Z3U8=
go.mongodb.org/atlas v0.15.1-0.20220403193624-86b34ba344cd h1:JzkNgRp8xLbm16DJP28+oGf0P765Nl2Xp8yERZ8O/y0=
go.mongodb.org/atlas v0.15.1-0.20220403193624-86b34ba344cd/go.mod h1:lQhRHIxc6jQHEK3/q9WLu/SdBkPj2fQYhjLGUF6Z3U8=
go.mongodb.org/realm v0.1.0 h1:zJiXyLaZrznQ+Pz947ziSrDKUep39DO4SfA0Fzx8M4M=
go.mongodb.org/realm v0.1.0/go.mod h1:4Vj6iy+Puo1TDERcoh4XZ+pjtwbOzPpzqy3Cwe8ZmDM=
go.mozilla.org/mozlog v0.0.0-20170222151521-4bb13139d403/go.mod h1:jHoPAGnDrCy6kaI2tAze5Prf0Nr0w/oNkROt2lw3n3o=
Expand Down
23 changes: 23 additions & 0 deletions mongodbatlas/data_source_mongodbatlas_third_party_integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,29 @@ func thirdPartyIntegrationSchema() *schema.Resource {
Sensitive: true,
Computed: true,
},
"microsoft_teams_webhook_url": {
Type: schema.TypeString,
Sensitive: true,
Optional: true,
},
"user_name": {
Type: schema.TypeString,
Sensitive: true,
Optional: true,
},
"service_discovery": {
Type: schema.TypeString,
Sensitive: true,
Optional: true,
},
"scheme": {
Type: schema.TypeString,
Optional: true,
},
"enabled": {
Type: schema.TypeBool,
Optional: true,
},
},
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,27 @@ const (
url = "%[4]s"
}
`

MICROSOFTTEAMS = `
resource "mongodbatlas_third_party_integration" "%[1]s" {
project_id = "%[2]s"
type = "%[3]s"
microsoft_teams_webhook_url = "%[4]s"
}
`

PROMETHEUS = `
resource "mongodbatlas_third_party_integration" "%[1]s" {
project_id = "%[2]s"
type = "%[3]s"
user_name = "%[4]s"
password = "%[5]s"
service_discovery = "%[6]s"
scheme = "%[7]s"
enabled = "%[8]s"
}
`

alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
numeric = "0123456789"
alphaNum = alphabet + numeric
Expand Down Expand Up @@ -202,6 +223,24 @@ func testAccMongoDBAtlasThirdPartyIntegrationResourceConfig(config *thirdPartyCo
config.Integration.Type,
config.Integration.URL,
)
case "MICROSOFTTEAMS":
return fmt.Sprintf(WEBHOOK,
config.Name,
config.ProjectID,
config.Integration.Type,
config.Integration.MicrosoftTeamsWebhookURL,
)
case "PROMETHEUS":
return fmt.Sprintf(WEBHOOK,
config.Name,
config.ProjectID,
config.Integration.Type,
config.Integration.UserName,
config.Integration.Password,
config.Integration.ServiceDiscovery,
config.Integration.Scheme,
config.Integration.Enabled,
)
default:
return fmt.Sprintf(Unknown3rdParty,
config.Name,
Expand Down
Loading