1 /* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /*
3  *     Copyright 2010-2020 Couchbase, Inc.
4  *
5  *   Licensed under the Apache License, Version 2.0 (the "License");
6  *   you may not use this file except in compliance with the License.
7  *   You may obtain a copy of the License at
8  *
9  *       http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *   Unless required by applicable law or agreed to in writing, software
12  *   distributed under the License is distributed on an "AS IS" BASIS,
13  *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *   See the License for the specific language governing permissions and
15  *   limitations under the License.
16  */
17 #ifndef TESTS_SERVER_PARAMS_H
18 #define TESTS_SERVER_PARAMS_H 1
19 
20 #include "config.h"
21 #include <string>
22 #include <string.h>
23 #include <libcouchbase/couchbase.h>
24 
25 class ServerParams
26 {
27   public:
ServerParams()28     ServerParams() {}
ServerParams(const char *h, const char *b, const char *u, const char *p)29     ServerParams(const char *h, const char *b, const char *u, const char *p)
30     {
31         loadParam(host, h);
32         loadParam(bucket, b);
33         loadParam(user, u);
34         loadParam(pass, p);
35     }
36 
makeConnectParams(lcb_CREATEOPTS *&crst, lcb_io_opt_t io, lcb_INSTANCE_TYPE type = LCB_TYPE_BUCKET)37     void makeConnectParams(lcb_CREATEOPTS *&crst, lcb_io_opt_t io, lcb_INSTANCE_TYPE type = LCB_TYPE_BUCKET)
38     {
39         lcb_createopts_create(&crst, type);
40         if (host.find("couchbase://") == 0) {
41             connstr = host;
42             // deactivate dnssrv and compression, use cccp bootstrap
43             connstr += "?dnssrv=off&bootstrap_on=cccp&compression=off";
44         } else {
45             if (mcNodes.empty() || type == LCB_TYPE_CLUSTER) {
46                 connstr = "couchbase://" + host + "=http";
47             } else {
48                 connstr = "couchbase+explicit://" + host + "=http;" + mcNodes;
49             }
50         }
51 
52         lcb_createopts_connstr(crst, connstr.c_str(), connstr.size());
53         lcb_createopts_credentials(crst, user.c_str(), user.size(), pass.c_str(), pass.size());
54         if (type == LCB_TYPE_BUCKET) {
55             lcb_createopts_bucket(crst, bucket.c_str(), bucket.size());
56         }
57         lcb_createopts_io(crst, io);
58     }
59 
getUsername()60     std::string getUsername()
61     {
62         return user;
63     }
64 
getPassword()65     std::string getPassword()
66     {
67         return pass;
68     }
69 
getBucket()70     std::string getBucket()
71     {
72         return bucket;
73     }
74 
getMcPorts() const75     const std::string &getMcPorts() const
76     {
77         return mcNodes;
78     }
79 
setMcPorts(const std::vector<int> &portlist)80     void setMcPorts(const std::vector<int> &portlist)
81     {
82         std::stringstream ss;
83         for (std::vector<int>::const_iterator ii = portlist.begin(); ii != portlist.end(); ii++) {
84             ss << "localhost";
85             ss << ":";
86             ss << std::dec << *ii;
87             ss << "=mcd";
88             ss << ";";
89         }
90         mcNodes = ss.str();
91     }
92 
93   protected:
94     std::string host;
95     std::string user;
96     std::string pass;
97     std::string bucket;
98     std::string mcNodes;
99     std::string connstr;
100 
101   private:
loadParam(std::string &d, const char *s)102     void loadParam(std::string &d, const char *s)
103     {
104         if (s) {
105             d.assign(s);
106         }
107     }
108 };
109 
110 #endif
111