#!/usr/bin/env python

import clitool

def connect(mc, bucket, username, password):
    if username:
        mc.sasl_auth_plain(username, password)
    mc.bucket_select(bucket)

def listvb(mc, bucket, username=None, password=""):
    connect(mc, bucket, username, password)
    vbs = mc.stats('vbucket')
    for (vb, state) in sorted(list(vbs.items())):
        print "vbucket", vb[3:], state

def setvb(mc, bucket, vbid, vbstate, username=None, password=""):
    connect(mc, bucket, username, password)
    mc.set_vbucket_state(int(vbid), vbstate)

def rmvb(mc, bucket, vbid, username=None, password=""):
    connect(mc, bucket, username, password)
    mc.delete_vbucket(int(vbid))

if __name__ == '__main__':

    c = clitool.CliTool()

    c.addCommand('list', listvb, 'list <bucket> [username password]')
    c.addCommand('set', setvb, 'set <bucket> <vbid> active|replica|pending|dead [username password]')
    c.addCommand('rm', rmvb, 'rm <bucket> <vbid> [username password]')

    c.execute()
