terraform-provider-mongodbatlas icon indicating copy to clipboard operation
terraform-provider-mongodbatlas copied to clipboard

Schema error when creating event_trigger referring to Atlas App Services function

Open willmorgan opened this issue 1 year ago • 3 comments

Terraform CLI and Terraform MongoDB Atlas Provider Version

1.2.6

Terraform Configuration File

resource "mongodbatlas_event_trigger" "ticket_claimed" {
  name                        = "${var.name}-trigger"
  type                        = "DATABASE"
  project_id                  = var.mongodbatlas_project_id
  app_id                      = "6xxxxxxxxxxxxxxxxxxxxxx0"
  function_id                 = "my_trigger"
  config_service_id           = mongodbatlas_advanced_cluster.atlas.cluster_id
  config_operation_types      = ["UPDATE"]
  config_database             = mongodbatlas_advanced_cluster.atlas.name
  config_collection           = "tickets"
  config_match                = "{\"updateDescription.updatedFields\":{\"some_field\":{\"$exists\":true}}}"
  config_full_document        = false
  config_full_document_before = false
  disabled                    = false
  unordered                   = false
}

Steps to Reproduce

tf apply

Expected Behavior

The trigger is created referring to the given function name.

Actual Behavior

│ Error: error creating MongoDB EventTriggers (<groupId>): POST https://realm.mongodb.com/api/admin/v3.0/groups/<groupId>/apps/<appId>/triggers: 400 (request "") cannot unmarshal into an ObjectID, the length must be 24 but it is 9

Where 9 is the character length of "my_trigger" in the resource definition above.

Debug Output

---[ REQUEST ]---------------------------------------
POST /api/admin/v3.0/groups/<groupId>/apps/<appId>/triggers HTTP/1.1
Host: realm.mongodb.com
User-Agent: terraform-provider-mongodbatlas/1.4.6 go-realm
Content-Length: 293
Accept: application/json
Content-Type: application/json
Accept-Encoding: gzip

{
 "name": "<name>",
 "type": "DATABASE",
 "function_id": "my_trigger",
 "config": {
  "operation_types": [
   "UPDATE"
  ],
  "database": "dev-stargate",
  "collection": "tickets",
  "service_id": "<clusterId>",
  "match": {
   "updateDescription.updatedFields": {
    "some_field": {
     "$exists": true
    }
   }
  }
 }
}

Additional Context

It looks like the schema that the Terraform provider sends differs from the OpenAPI description of the endpoint here: https://www.mongodb.com/docs/atlas/app-services/admin/api/v3/#operation/adminListTriggers

As an aside, it took me a lot of time to narrow this down to a suspected provider issue because of the inconsistent naming of fields when finding all the data the resource has to ingest.

For example:

  • groupId/projectId are the same
  • ~~serviceId/clusterId appear to be the same~~ (no, you've got to link the cluster)
  • appId differs from the client app ID mentioned in the console
  • It isn't possible to map the client app ID into an app ObjectId, which is used by this endpoint
  • The function_id appears in the REST API docs to actually be a function name, not an ObjectID.

willmorgan avatar Oct 03 '22 10:10 willmorgan

@willmorgan I am have replicated your issue v 1.2.6 I think 1.4.6 was version further down the details ... Using latest version I get error as well │ Error: error creating MongoDB EventTriggers (621952567c87684fd69b0101): POST https://realm.mongodb.com/api/admin/v3.0/groups/621952grpid9b0101/apps/633b89e4ecappid28af8f/triggers: 400 (request "") cannot unmarshal into an ObjectID, the length must be 24 but it is 6 Let me check with engineering on realm library API to see where this constraint comes from

martinstibbe avatar Oct 04 '22 00:10 martinstibbe

@willmorgan I used Function id from URL in the portal I get past that 24 digit ID requirement I still get an error Error: error creating MongoDB EventTriggers (621952567c8grpd69b0101): POST https://realm.mongodb.com/api/admin/v3.0/groups/621952567grpb0101/apps/633b89efuncid03be328af8f/triggers: 400 (request "") a database trigger requires an associated Atlas cluster service

martinstibbe avatar Oct 04 '22 02:10 martinstibbe

moving ticket to internal team to review BAAS-17291 and thank you @willmorgan. we will keep you updated as we dive deeper here

Zuhairahmed avatar Oct 07 '22 17:10 Zuhairahmed