Chilkat Online Tools

PureBasic / MongoDB Atlas / Configure Auditing for a Project

Back to Collection Items

IncludeFile "CkJsonObject.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkHttp.pb"
IncludeFile "CkHttpResponse.pb"

Procedure ChilkatExample()

    ; This example assumes the Chilkat API to have been previously unlocked.
    ; See Global Unlock Sample for sample code.

    http.i = CkHttp::ckCreate()
    If http.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success.i

    CkHttp::setCkDigestAuth(http, 1)
    CkHttp::setCkLogin(http, "username")
    CkHttp::setCkPassword(http, "password")

    ; Use this online tool to generate code from sample JSON: Generate Code to Create JSON

    ; The following JSON is sent in the request body.

    ; {
    ;   "auditAuthorizationSuccess": false,
    ;   "auditFilter": "{\"atype\" : \"authenticate\", \"param\" : {\"user\" : \"auditReadOnly\", \"db\" : \"admin\", \"mechanism\" : \"SCRAM-SHA-1\"} }",
    ;   "enabled": true
    ; }

    json.i = CkJsonObject::ckCreate()
    If json.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckUpdateBool(json,"auditAuthorizationSuccess",0)
    CkJsonObject::ckUpdateString(json,"auditFilter","{" + Chr(34) + "atype" + Chr(34) + " : " + Chr(34) + "authenticate" + Chr(34) + ", " + Chr(34) + "param" + Chr(34) + " : {" + Chr(34) + "user" + Chr(34) + " : " + Chr(34) + "auditReadOnly" + Chr(34) + ", " + Chr(34) + "db" + Chr(34) + " : " + Chr(34) + "admin" + Chr(34) + ", " + Chr(34) + "mechanism" + Chr(34) + " : " + Chr(34) + "SCRAM-SHA-1" + Chr(34) + "} }")
    CkJsonObject::ckUpdateBool(json,"enabled",1)

    sbRequestBody.i = CkStringBuilder::ckCreate()
    If sbRequestBody.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckEmitSb(json,sbRequestBody)

    resp.i = CkHttp::ckPTextSb(http,"PATCH","https://domain.com/api/atlas/{{version}}/groups/{{ProjectID}}/auditLog",sbRequestBody,"utf-8","application/json",0,0)
    If CkHttp::ckLastMethodSuccess(http) = 0
        Debug CkHttp::ckLastErrorText(http)
        CkHttp::ckDispose(http)
        CkJsonObject::ckDispose(json)
        CkStringBuilder::ckDispose(sbRequestBody)
        ProcedureReturn
    EndIf

    Debug Str(CkHttpResponse::ckStatusCode(resp))
    Debug CkHttpResponse::ckBodyStr(resp)
    CkHttpResponse::ckDispose(resp)



    CkHttp::ckDispose(http)
    CkJsonObject::ckDispose(json)
    CkStringBuilder::ckDispose(sbRequestBody)


    ProcedureReturn
EndProcedure

Curl Command

curl -X PATCH
	--digest -u 'username:password'
	-d '{
    "auditAuthorizationSuccess": false,
    "auditFilter": "{\"atype\" : \"authenticate\", \"param\" : {\"user\" : \"auditReadOnly\", \"db\" : \"admin\", \"mechanism\" : \"SCRAM-SHA-1\"} }",
    "enabled": true
}'
https://domain.com/api/atlas/{{version}}/groups/{{ProjectID}}/auditLog

Postman Collection Item JSON

{
  "name": "Configure Auditing for a Project",
  "request": {
    "method": "PATCH",
    "header": [
    ],
    "body": {
      "mode": "raw",
      "raw": "{\n    \"auditAuthorizationSuccess\": false,\n    \"auditFilter\": \"{\\\"atype\\\" : \\\"authenticate\\\", \\\"param\\\" : {\\\"user\\\" : \\\"auditReadOnly\\\", \\\"db\\\" : \\\"admin\\\", \\\"mechanism\\\" : \\\"SCRAM-SHA-1\\\"} }\",\n    \"enabled\": true\n}",
      "options": {
        "raw": {
          "language": "json"
        }
      }
    },
    "url": {
      "raw": "{{base_url}}/api/atlas/{{version}}/groups/{{ProjectID}}/auditLog",
      "host": [
        "{{base_url}}"
      ],
      "path": [
        "api",
        "atlas",
        "{{version}}",
        "groups",
        "{{ProjectID}}",
        "auditLog"
      ]
    },
    "description": "https://docs.atlas.mongodb.com/reference/api/auditing-set-auditLog/"
  },
  "response": [
  ]
}