forked from couchbaselabs/devguide-examples
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add bucket management operations: create/remove buckets
- Loading branch information
Showing
5 changed files
with
101 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,3 +16,4 @@ connecting-ssl | |
subdoc-updating | ||
subdoc-retrieving | ||
fts-basic | ||
create-remove-bucket |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#include <libcouchbase/couchbase.h> | ||
#include <cassert> | ||
#include <cstdlib> | ||
#include <string> | ||
|
||
static void | ||
http_callback(lcb_t, int, const lcb_RESPHTTP *resp) | ||
{ | ||
printf("Operation completed with HTTP code: %d\n", resp->htstatus); | ||
printf("Payload: %.*s\n", (int)resp->nbody, resp->body); | ||
} | ||
|
||
int main(int, char **) | ||
{ | ||
lcb_create_st crst; | ||
lcb_t instance; | ||
memset(&crst, 0, sizeof crst); | ||
crst.version = 3; | ||
crst.v.v3.connstr = "couchbase://localhost/default"; | ||
|
||
lcb_create(&instance, &crst); | ||
lcb_connect(instance); | ||
lcb_wait(instance); | ||
assert(lcb_get_bootstrap_status(instance) == LCB_SUCCESS); | ||
lcb_install_callback3(instance, LCB_CALLBACK_HTTP, (lcb_RESPCALLBACK)http_callback); | ||
|
||
// Create the required parameters according to the Couchbase REST API | ||
std::string path("/pools/default/buckets"); | ||
|
||
std::string params; | ||
params += "name=newBucket&"; | ||
params += "type=couchbase&"; | ||
|
||
// authType should always be SASL. You can leave the saslPassword field | ||
// empty if you don't want to protect this bucket. | ||
params += "authType=sasl&saslPassword=&"; | ||
params += "ramQuotaMB=256"; | ||
printf("Using %s\n", params.c_str()); | ||
|
||
lcb_CMDHTTP htcmd = { 0 }; | ||
LCB_CMD_SET_KEY(&htcmd, path.c_str(), path.size()); | ||
htcmd.body = params.c_str(); | ||
htcmd.nbody = params.size(); | ||
htcmd.content_type = "application/x-www-form-urlencoded"; | ||
htcmd.method = LCB_HTTP_METHOD_POST; | ||
htcmd.type = LCB_HTTP_TYPE_MANAGEMENT; | ||
htcmd.username = "Administrator"; | ||
htcmd.password = "123456"; | ||
lcb_http3(instance, NULL, &htcmd); | ||
lcb_wait(instance); | ||
|
||
// now remove the bucket | ||
memset(&htcmd, 0, sizeof htcmd); | ||
path = "/pools/default/buckets/newBucket"; | ||
LCB_CMD_SET_KEY(&htcmd, path.c_str(), path.size()); | ||
htcmd.method = LCB_HTTP_METHOD_DELETE; | ||
htcmd.type = LCB_HTTP_TYPE_MANAGEMENT; | ||
htcmd.username = "Administrator"; | ||
htcmd.password = "123456"; | ||
lcb_http3(instance, NULL, &htcmd); | ||
lcb_wait(instance); | ||
|
||
lcb_destroy(instance); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#!/usr/bin/env python | ||
|
||
from couchbase.admin import Admin | ||
from couchbase.bucket import Bucket | ||
|
||
adm = Admin('Administrator', '123456', host='localhost', port=8091) | ||
adm.bucket_create('new-bucket', | ||
bucket_type='couchbase', | ||
bucket_password='s3cr3t') | ||
|
||
# Wait for bucket to become ready | ||
adm.wait_ready('new-bucket', timeout=30) | ||
|
||
bucket = Bucket('couchbase://localhost/new-bucket', password='s3cr3t') | ||
bucket.upsert('foo', 'bar') | ||
|
||
adm.bucket_remove('new-bucket') |