forked from membase/membase-cli
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuckets.py
120 lines (99 loc) · 3.04 KB
/
buckets.py
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
listservers class
This class implements methods that will list servers within a
membase cluster
"""
import pprint
from membase_info import usage
import restclient
rest_cmds = {
'bucket-list': '/pools/default/buckets',
'bucket-flush': '/pools/default/buckets/',
'bucket-delete': '/pools/default/buckets/',
'bucket-create': '/pools/default/buckets/',
}
methods = {
'bucket-list': 'GET',
'bucket-delete': 'DELETE',
'bucket-create': 'POST',
'bucket-flush': 'POST',
'bucket-stats': 'GET',
}
class Buckets:
def __init__(self):
"""
constructor
"""
# defaults
self.debug = False
self.verbose = False
self.rest_cmd = rest_cmds['bucket-list']
self.method = 'GET'
def runCmd(
self,
cmd,
server,
port,
user,
password,
opts,
):
# defaults
bucketname = ''
cachesize = ''
standard_result = ''
self.user = user
self.password = password
# set standard opts
output= 'default'
for (o, a) in opts:
if o == '-b' or o == '--buckets':
bucketname = a
if o == '-d' or o == '--debug':
self.debug = True
if o in ('-o', '--output'):
output = a
if o == '-s' or o == '--size':
cachesize = a
if o == '-v' or o == '--verbose':
self.verbose = True
self.rest_cmd = rest_cmds[cmd]
# instantiate a rest client
rest = restclient.RestClient(server,
port,
{'debug':self.debug})
# get the parameters straight
if cmd in ('bucket-delete', 'bucket-create', 'bucket-flush'):
if len(bucketname):
rest.setParam('name', bucketname)
if len(cachesize):
rest.setParam('cacheSize', cachesize)
self.rest_cmd = self.rest_cmd + bucketname
if cmd == 'bucket-flush':
self.rest_cmd = self.rest_cmd + '/controller/doFlush'
opts = {'error_msg':"Unable to obtain bucket list"}
data = rest.restCmd(methods[cmd],
self.rest_cmd,
self.user,
self.password,
opts)
if methods[cmd] == 'GET':
if output == 'json':
print data
else:
json = rest.getJson(data)
if self.verbose:
print 'List of buckets within the server %s:%s' \
% (server, port)
for bucket in json:
print '%s' % bucket['name']
else:
if output == 'json':
print rest.jsonMessage(data)
else:
print data
# debug
# pp = pprint.PrettyPrinter(indent=4)
# pp.pprint(json)