Provision a Drop Node via Terraform

Provision a Drop Node via Terraform

This section provides a clear, step-by-step guide to automating the deletion (drop) of a MongoDB node on 123cluster using Terraform and the Mastercard/restapi provider. You’ll see how to convert a REST API command into a parameterized Terraform workflow, making your removal process safe, auditable, and CI/CD-read

Step 1: Copy the curl Command from the UI
On the Remove Node page for your MongoDB node, click { REST API }. You’ll copy a command such as:

curl -v \
  -H "Authorization: Bearer <YOUR_JWT_TOKEN>" \
  -H "Content-Type: application/json" \
  -H "Accept: application/json" \
  -d '{
    "node_id":                 <NODE_ID>,
    "type":                    "MONGO",
    "is_kubernetes":           false,
    "drop_namespace":          false,
    "drop_controller_manager": false,
    "final_backup":            true,
    "rest_api":                true
  }' \
  <API_BASE_URL>/drop_mongo_node/

Step 2: Parse the curl Command

  • Authorization header:
    Extract the JWT token after Bearer.
  • Content-Type & Accept:
    Both must be application/json.
  • Payload fields:
    • node_id (integer or string, unique node identifier)
    • type (string, "MONGO")
    • is_kubernetes (boolean, is node in Kubernetes)
    • drop_namespace (boolean, drop Kubernetes namespace)
    • drop_controller_manager (boolean, drop controller manager)
    • final_backup (boolean, perform a final backup before deletion)
    • rest_api (boolean)
  • Endpoint:
    • Base URI: <API_BASE_URL>
    • Resource path: /drop_mongo_node/

Step 3: Translate into Terraform

  • Create directory & file:
integration/
└── terraform/
    └── export_database/
        └── main.tf
  • Provider block
// Terraform configuration for automating MongoDB node export (dump) on 123cluster
terraform {
  required_providers {
    restapi = {
      source  = "Mastercard/restapi"
      version = "1.19.1"
    }
  }
}

/*
  REST API provider configuration:
  - uri: Base API endpoint for 123cluster.
  - headers: JWT and content type.
  - write_returns_object: Output as object.
  - debug: Enables logging for troubleshooting.
  - HTTP methods: POST for all actions.
*/
provider "restapi" {
  uri                  = "<API_BASE_URL>"
  write_returns_object = true
  debug                = true
  headers = {
    Authorization = "Bearer <YOUR_JWT_TOKEN>" // Use a valid, secure JWT token
    Content-Type  = "application/json"
    Accept        = "application/json"
  }
  create_method  = "POST"
  update_method  = "POST"
  destroy_method = "POST"
}
  • Variable declarations
/*
  Use variables for all environment- and user-specific values.
*/

variable "mongo_host_address" {
  description = "Address (IP or FQDN) of the MongoDB host"
  type        = string
  default     = "<MONGO_HOST_ADDRESS>"
}

variable "mongo_host_port" {
  description = "Port on which MongoDB is listening"
  type        = string
  default     = "<MONGO_HOST_PORT>"
}

variable "cluster_id" {
  description = "Cluster identifier for the MongoDB cluster"
  type        = string
  default     = "<CLUSTER_ID>"
}

variable "export_path" {
  description = "Directory on server to store export (dump) files"
  type        = string
  default     = "/backups/mongo/exports"
}

variable "schedule" {
  description = "Whether to schedule the export"
  type        = bool
  default     = false
}

variable "compress" {
  description = "Whether to compress the export file"
  type        = bool
  default     = false
}

variable "include_data" {
  description = "Include actual database data in export (not just schema)"
  type        = bool
  default     = true
}

variable "keep_days" {
  description = "Number of days to keep the exported files"
  type        = number
  default     = 2
}
  • Resource definition
/*
  Resource for triggering MongoDB node export (dump) via REST API.
  All fields are parameterized for portability and automation.
*/
resource "restapi_object" "export_database" {
  path = "/dump_mongo_node/"
  data = jsonencode({
    node_id      = "${var.mongo_host_address}:${var.mongo_host_port}"
    type         = "MONGO"
    cluster_id   = var.cluster_id
    schedule     = var.schedule
    compress     = var.compress
    include_data = var.include_data
    export_path  = var.export_path
    keep_days    = var.keep_days
    rest_api     = true
  })
}
  • Output block
/*
  Output the full API JSON response after triggering the export.
  Use for logs, CI/CD visibility, or follow-up automation.
*/
output "export_response" {
  description = "Raw JSON response from dump_mongo_node"
  value       = restapi_object.export_database.data
}

Step 4: Initialize & Apply

cd integration/terraform/export_database

# Initialize the Terraform working directory and download necessary providers
terraform init

# Apply the configuration, review the planned actions, and confirm execution
terraform apply

# Output the API response for logging or integration with other tools
terraform output export_response

Additional Guidance & Best Practices

  • Parameterization: Always use variables for node IDs and all flags to keep the workflow modular, reusable, and safe for automation.
  • Security: Protect API tokens and sensitive data using environment variables, secret managers, or the sensitive attribute in Terraform.
  • CI/CD Integration: Incorporate these Terraform steps into your CI/CD pipeline for consistent, auditable, and automated node removal processes.
  • Final Backup: Enabling final_backup before node deletion helps prevent accidental data loss.
  • API Versioning: Check for updates to the 123cluster API documentation to keep payloads and endpoints compatible.
  • Logging & Outputs: Use outputs to track deletion results for compliance, audits, or triggers for downstream cleanup.
  • Error Handling: Validate API responses in your automation to handle failures gracefully and alert if deletion is unsuccessful.
  • Safety: Consider manual confirmation or double-checks for production environments to avoid accidental deletions.