/
management-bucket-create.cc
120 lines (104 loc) · 3.8 KB
/
management-bucket-create.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <string>
#include <iostream>
#include <libcouchbase/couchbase.h>
static void
check(lcb_STATUS err, const char *msg)
{
if (err != LCB_SUCCESS) {
std::cerr << "[ERROR] " << msg << ": " << lcb_strerror_short(err) << "\n";
exit(EXIT_FAILURE);
}
}
void
http_callback(lcb_INSTANCE *, int, const lcb_RESPHTTP *resp)
{
check(lcb_resphttp_status(resp), "HTTP operation status in the callback");
uint16_t status;
lcb_resphttp_http_status(resp, &status);
std::cout << "HTTP status: " << status << "\n";
const char *buf;
size_t buf_len = 0;
lcb_resphttp_body(resp, &buf, &buf_len);
if (buf_len > 0) {
std::cout << std::string(buf, buf_len) << "\n";
}
}
int
main()
{
std::string username{"some-user"};
std::string password{"some-password"};
std::string connection_string{"couchbase://localhost"};
lcb_CREATEOPTS *create_options = nullptr;
check(lcb_createopts_create(&create_options, LCB_TYPE_CLUSTER),
"build options object for lcb_create in CLUSTER mode");
check(lcb_createopts_credentials(create_options, username.c_str(), username.size(),
password.c_str(),
password.size()),
"assign credentials");
check(lcb_createopts_connstr(create_options, connection_string.c_str(),
connection_string.size()),
"assign connection string");
lcb_INSTANCE *instance = nullptr;
check(lcb_create(&instance, create_options), "create lcb_INSTANCE");
check(lcb_createopts_destroy(create_options), "destroy options object");
check(lcb_connect(instance), "schedule connection");
check(lcb_wait(instance, LCB_WAIT_DEFAULT), "wait for connection");
check(lcb_get_bootstrap_status(instance), "check bootstrap status");
/**
* More on bucket create REST API
* https://docs.couchbase.com/server/current/rest-api/rest-bucket-create.html
*/
std::string bucket_name{"mybucket"};
std::size_t bucket_ram_quota_in_mb{200};
// The settings must be encoded as URL form
std::string content_type{"application/x-www-form-urlencoded"};
std::string bucket_settings{
"name=" + bucket_name + "&ramQuotaMB=" + std::to_string(bucket_ram_quota_in_mb)};
std::string create_path{"/pools/default/buckets"};
lcb_install_callback(instance, LCB_CALLBACK_HTTP,
reinterpret_cast<lcb_RESPCALLBACK>(http_callback));
lcb_CMDHTTP *cmd = nullptr;
check(lcb_cmdhttp_create(&cmd, LCB_HTTP_TYPE_MANAGEMENT),
"create HTTP command object of MANAGEMENT type");
check(lcb_cmdhttp_method(cmd, LCB_HTTP_METHOD_POST), "set HTTP method");
check(lcb_cmdhttp_path(cmd, create_path.c_str(), create_path.size()), "set HTTP path");
check(lcb_cmdhttp_content_type(cmd, content_type.c_str(), content_type.size()),
"set HTTP content type");
check(lcb_cmdhttp_body(cmd, bucket_settings.c_str(), bucket_settings.size()), "set HTTP body");
check(lcb_http(instance, nullptr, cmd), "schedule HTTP command");
check(lcb_cmdhttp_destroy(cmd), "destroy command object");
check(lcb_wait(instance, LCB_WAIT_DEFAULT), "wait for completion");
lcb_destroy(instance);
return 0;
}
// OUTPUT (success)
//
// HTTP status: 202
//
// OUTPUT (already exists)
//
// HTTP status: 400
// {
// "errors": {
// "name": "Bucket with given name already exists"
// },
// "summaries": {
// "ramSummary": {
// "total": 536870912,
// "otherBuckets": 209715200,
// "nodesCount": 1,
// "perNodeMegs": 200,
// "thisAlloc": 209715200,
// "thisUsed": 0,
// "free": 117440512
// },
// "hddSummary": {
// "total": 228026265600,
// "otherData": 221181213938,
// "otherBuckets": 4263694,
// "thisUsed": 0,
// "free": 6840787968
// }
// }
// }