|
| 1 | +--- |
| 2 | +layout: docs |
| 3 | +menu: |
| 4 | + docsplatform_{{.version}}: |
| 5 | + identifier: database-management-create-api-mongodb |
| 6 | + name: MongoDB |
| 7 | + parent: database-management-create-api |
| 8 | + weight: 10 |
| 9 | +menu_name: docsplatform_{{.version}} |
| 10 | +section_menu_id: guides |
| 11 | +--- |
| 12 | + |
| 13 | +# Creating a MongoDB Database via API |
| 14 | + |
| 15 | +This page walks through the API calls needed to provision a MongoDB database, equivalent |
| 16 | +to using the **Create Database** wizard in the UI at |
| 17 | +`/db/<org>/<cluster>/kubedb.com/v1/mongodbs/create`. See |
| 18 | +[Using the API](../_index.md) for the general 3-call flow and |
| 19 | +[MongoDB](../../mongodb.md) for the Database Mode options this flow configures. |
| 20 | + |
| 21 | +**Example values used throughout**: namespace `demo`, database name `mgc`, MongoDB version |
| 22 | +`8.0.17`, 3-replica replica set, `900m` CPU / `1Gi` memory per node, `2Gi` storage on |
| 23 | +`local-path` storage class. Swap these for your own. |
| 24 | + |
| 25 | +--- |
| 26 | + |
| 27 | +## 1. Get Default Form Values |
| 28 | + |
| 29 | +```bash |
| 30 | +curl -sk "$BASE_URL/helm/packageview/values" \ |
| 31 | + -H "Cookie: $COOKIE" -H "x-csrf-token: $CSRF" \ |
| 32 | + --get \ |
| 33 | + --data-urlencode "name=kubedbcom-mongodb-editor-options" \ |
| 34 | + --data-urlencode "sourceApiGroup=source.toolkit.fluxcd.io" \ |
| 35 | + --data-urlencode "sourceKind=HelmRepository" \ |
| 36 | + --data-urlencode "sourceNamespace=kubeops" \ |
| 37 | + --data-urlencode "sourceName=appscode-wizards-oci" \ |
| 38 | + --data-urlencode "version=v0.35.0" \ |
| 39 | + --data-urlencode "group=kubedb.com" \ |
| 40 | + --data-urlencode "kind=MongoDB" \ |
| 41 | + --data-urlencode "variant=default" \ |
| 42 | + --data-urlencode "namespace=demo" \ |
| 43 | + --data-urlencode "format=json" |
| 44 | +``` |
| 45 | + |
| 46 | +> The `version` query param (`v0.35.0` above) is the wizard chart version — it gets bumped |
| 47 | +> by 1 with each kubedb-platform release. |
| 48 | +
|
| 49 | +**Example response** (trimmed): |
| 50 | + |
| 51 | +```json |
| 52 | +{ |
| 53 | + "form": { |
| 54 | + "alert": { "enabled": "critical", "groups": { "...": "..." } } |
| 55 | + }, |
| 56 | + "metadata": { |
| 57 | + "release": { "name": "", "namespace": "demo" }, |
| 58 | + "resource": { "group": "kubedb.com", "kind": "MongoDB", "version": "v1alpha2" } |
| 59 | + }, |
| 60 | + "spec": { |
| 61 | + "admin": { "databases": { "MongoDB": { "versions": { "default": "", "available": [] } } } }, |
| 62 | + "mode": "Replicaset", |
| 63 | + "podResources": { "machine": "", "resources": { "requests": { "cpu": "900m", "memory": "1Gi" } } }, |
| 64 | + "persistence": { "size": "2Gi" }, |
| 65 | + "storageClasses": null, |
| 66 | + "replicaSet": { "name": "rs0", "replicas": 3 } |
| 67 | + } |
| 68 | +} |
| 69 | +``` |
| 70 | + |
| 71 | +Fields like `spec.admin.databases.MongoDB.versions.default` and `spec.storageClasses` come |
| 72 | +back empty/null — the defaults don't pick a version or storage class for you. Set your |
| 73 | +preferred values in these fields (version, storage class, mode, replicas, resources, |
| 74 | +storage size) before passing the config to call 2. |
| 75 | + |
| 76 | +> `spec.mode` also accepts `Standalone` and `Sharded` — see |
| 77 | +> [MongoDB Database Mode](../../mongodb/#database-mode) for the fields each mode adds |
| 78 | +> (e.g. `Sharded` needs shard/config-server/mongos sections instead of `replicaSet`). |
| 79 | +
|
| 80 | +--- |
| 81 | + |
| 82 | +## 2. Render the Values Model (Dry-Run) |
| 83 | + |
| 84 | +Take the `form` object from call 1, fill in your choices, and submit it here. No database |
| 85 | +is created yet — this only validates and renders the config. |
| 86 | + |
| 87 | +```bash |
| 88 | +curl -sk -X PUT "$BASE_URL/helm/options/model" \ |
| 89 | + -H "Cookie: $COOKIE" -H "x-csrf-token: $CSRF" -H "content-type: application/json" \ |
| 90 | + --data-binary @request-model.json |
| 91 | +``` |
| 92 | + |
| 93 | +Key fields to change per deployment, in the request body: |
| 94 | + |
| 95 | +```json |
| 96 | +{ |
| 97 | + "metadata": { |
| 98 | + "release": { "name": "mgc", "namespace": "demo" } |
| 99 | + }, |
| 100 | + "spec": { |
| 101 | + "mode": "Replicaset", |
| 102 | + "replicaSet": { "name": "rs0", "replicas": 3 }, |
| 103 | + "podResources": { |
| 104 | + "resources": { "requests": { "cpu": "900m", "memory": "1Gi" }, "limits": { "cpu": "900m", "memory": "1Gi" } } |
| 105 | + }, |
| 106 | + "persistence": { "size": "2Gi" } |
| 107 | + } |
| 108 | +} |
| 109 | +``` |
| 110 | + |
| 111 | +`spec.admin.databases.MongoDB.versions.default` in the same body controls the version |
| 112 | +(e.g. `"8.0.17"`). |
| 113 | + |
| 114 | +**Example response** (trimmed): |
| 115 | + |
| 116 | +```json |
| 117 | +{ |
| 118 | + "form": { "...": "echoes back your form, resolved" }, |
| 119 | + "resources": { |
| 120 | + "kubedbComMongoDB": { |
| 121 | + "apiVersion": "kubedb.com/v1", |
| 122 | + "kind": "MongoDB", |
| 123 | + "spec": { |
| 124 | + "replicas": 3, |
| 125 | + "replicaSet": { "name": "rs0" }, |
| 126 | + "storage": { "storageClassName": "local-path", "resources": { "requests": { "storage": "2Gi" } } }, |
| 127 | + "version": "8.0.17" |
| 128 | + } |
| 129 | + }, |
| 130 | + "autoscalingKubedbComMongoDBAutoscaler": { "...": "rendered autoscaler CR" }, |
| 131 | + "catalogAppscodeComMongoDBBinding": { "...": "rendered binding CR" }, |
| 132 | + "coreKubestashComBackupConfiguration": { "...": "rendered backup config CR" } |
| 133 | + } |
| 134 | +} |
| 135 | +``` |
| 136 | + |
| 137 | +This is the key thing to check before submitting: `resources.kubedbComMongoDB` is exactly |
| 138 | +what call 3 will apply. |
| 139 | + |
| 140 | +--- |
| 141 | + |
| 142 | +## 3. Submit — Creates the Database |
| 143 | + |
| 144 | +```bash |
| 145 | +RESPONSE_ID=$(uuidgen) |
| 146 | +curl -sk -X PUT "$BASE_URL/helm/editor?response-id=$RESPONSE_ID" \ |
| 147 | + -H "Cookie: $COOKIE" -H "x-csrf-token: $CSRF" -H "content-type: application/json" \ |
| 148 | + --data-binary @request-editor.json |
| 149 | +``` |
| 150 | + |
| 151 | +`response-id` is any UUID you generate client-side — the server doesn't validate it against |
| 152 | +earlier calls, it's only used to correlate this request with a response/progress stream. |
| 153 | + |
| 154 | +The body is the same `form`/`metadata` as call 2, plus the `resources` map from call 2's |
| 155 | +response — the fully rendered manifests that get applied via Helm: |
| 156 | + |
| 157 | +- `kubedbComMongoDB` — the MongoDB custom resource itself |
| 158 | +- `autoscalingKubedbComMongoDBAutoscaler` — autoscaler config |
| 159 | +- `catalogAppscodeComMongoDBBinding` — service binding for the UI |
| 160 | +- `coreKubestashComBackupConfiguration` — backup schedule/config |
| 161 | + |
| 162 | +A `200` response means the database (and its autoscaler/binding/backup config) has been |
| 163 | +created. Confirm with: |
| 164 | + |
| 165 | +```bash |
| 166 | +curl -sk "$BASE_URL/proxy/kubedb.com/v1alpha2/namespaces/demo/mongodbs/mgc" \ |
| 167 | + -H "Cookie: $COOKIE" -H "x-csrf-token: $CSRF" |
| 168 | +``` |
0 commit comments