Skip to content

Commit

Permalink
Add bucket management operations: create/remove buckets
Browse files Browse the repository at this point in the history
  • Loading branch information
mnunberg committed Jun 3, 2016
1 parent a03845a commit c62e483
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 1 deletion.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -282,3 +282,20 @@ Go |
node.js |
Java |
PHP

## Cluster Management

### Create/Remove buckets

This should show a basic example of creating and removing a bucket. It might
also be good to show how to wait until a newly created bucket becomes ready
as well.

[C](c/create-remove-bucket.cc) |
[Python](python/create-remove-bucket.py) |
.NET |
Go |
node.js |
Java |
PHP

1 change: 1 addition & 0 deletions c/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ connecting-ssl
subdoc-updating
subdoc-retrieving
fts-basic
create-remove-bucket
3 changes: 2 additions & 1 deletion c/Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
PROGS=connecting updating retrieving query-create-index query-criteria \
query-placeholders counter expiration \
query-consistency cas durability bulk-get bulk-store \
connecting-ssl subdoc-retrieving subdoc-updating fts-basic
connecting-ssl subdoc-retrieving subdoc-updating fts-basic \
create-remove-bucket

all: $(PROGS)

Expand Down
64 changes: 64 additions & 0 deletions c/create-remove-bucket.cc
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);
}
17 changes: 17 additions & 0 deletions python/create-remove-bucket.py
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')

0 comments on commit c62e483

Please sign in to comment.