mongodb / terraform-provider-mongodbatlas

Terraform MongoDB Atlas Provider: Deploy, update, and manage MongoDB Atlas infrastructure as code through HashiCorp Terraform
https://registry.terraform.io/providers/mongodb/mongodbatlas
Mozilla Public License 2.0
246 stars 179 forks source link

Auto Deferring MongoDB Atlas Maintenance Window Fails #694

Closed nleite closed 2 years ago

nleite commented 2 years ago

Atlas terraform provider throwing back a 400 while creating a project maintenance window with auto_defer option set. Seems like there is an out-of-order (or something similar) API call that tries to set the autoDefer before the maintenance window is set or gets completed.

Terraform CLI and Terraform MongoDB Atlas Provider Version

    mongodbatlas = {
      source  = "mongodb/mongodbatlas"
      version = "1.2.0"
    }

Terraform Config

...
resource "mongodbatlas_maintenance_window" "cluster_maintenance_window" {
    project_id = mongodbatlas_project.atlas_project.id
    auto_defer = var.maintenance_auto_defer
    day_of_week = var.maintenance_day_of_week
    hour_of_day = var.maintenance_hour_of_day
}
...

Steps to Reproduce

terraform init terraform plan terraform apply

Expected Behavior

Should create the maintenance window for the project without any issues

Actual Behavior

│ Error: error auto deferring the MongoDB Atlas Maintenance Window (XXXXXXXX): POST https://cloud.mongodb.com/api/atlas/v1.0/groups/XXXXXXXXXX/maintenanceWindow/autoDefer: 400 (request "ATLAS_MAINTENANCE_WINDOW_NOT_DEFINED") No maintenance window defined for project. This action is only supported for projects with user defined maintenance windows.
│ 
│   with module.test-webtask.mongodbatlas_maintenance_window.cluster_maintenance_window,
│   on .terraform/modules/test-webtask/cluster/atlas.tf line 213, in resource "mongodbatlas_maintenance_window" "cluster_maintenance_window":
│  213: resource "mongodbatlas_maintenance_window" "cluster_maintenance_window" {
themantissa commented 2 years ago

@nleite we'll have the team take a look and get back to you.

nikhil-mongo commented 2 years ago

@nleite The terraform resource is designed correctly. In order to set the number of auto-deferrals, it is important for the maintenance window to exist.

Therefore, initially the resource should be as below -

resource "mongodbatlas_maintenance_window" "cluster_maintenance_window" {
    project_id = mongodbatlas_project.atlas_project.id
   // auto_defer = var.maintenance_auto_defer
    day_of_week = var.maintenance_day_of_week
    hour_of_day = var.maintenance_hour_of_day
}

- Run `terraform apply`

Once completed, alter the resource as below - 

```terraform
resource "mongodbatlas_maintenance_window" "cluster_maintenance_window" {
    project_id = mongodbatlas_project.atlas_project.id
    auto_defer = var.maintenance_auto_defer
    day_of_week = var.maintenance_day_of_week
    hour_of_day = var.maintenance_hour_of_day
}

The above difference is because there are two different API calls required to accomplish this.

The create maintenance window call is -

PATCH /api/atlas/v1.0/groups/{GROUPID}/maintenanceWindow

While when the auto_defer is set it calls for the below API -

POST /api/atlas/v1.0/groups/{GROUPID}/maintenanceWindow/autoDefer

Since these two API calls cannot be combined, and when terraform finds auto_defer as mentioned in resource it straightaway hits the second API call which fails with 400 error as there is no maintenance window to autodefer.

I hope I have been able to answer your question.

Thanks

themantissa commented 2 years ago

Internal ticket INTMDB-305 to investigate solutions.

martinstibbe commented 2 years ago

@nleite This fix will be in version 1.3.1 pre release for testing soon

themantissa commented 2 years ago

@nleite Pre-release with the fix is out if you'd like to test it. We'll likely release Monday. https://github.com/mongodb/terraform-provider-mongodbatlas/releases/tag/untagged-bcc6453f1acaa220dd7e

themantissa commented 2 years ago

1.3.1 is released