More work on client
* Add status function * Fix tests * Fix options parser
This commit is contained in:
parent
615b24a532
commit
c7cb4d67a9
@ -26,4 +26,7 @@ def main():
|
|||||||
if args.command == 'list':
|
if args.command == 'list':
|
||||||
api.list_lb()
|
api.list_lb()
|
||||||
|
|
||||||
|
if args.command == 'status':
|
||||||
|
api.get_lb(args.lbid)
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
|
@ -60,9 +60,10 @@ class ClientOptions(object):
|
|||||||
subparsers.add_parser(
|
subparsers.add_parser(
|
||||||
'modify', help='modify a load balancer'
|
'modify', help='modify a load balancer'
|
||||||
)
|
)
|
||||||
subparsers.add_parser(
|
sp = subparsers.add_parser(
|
||||||
'status', help='get status of a load balancer'
|
'status', help='get status of a load balancer'
|
||||||
)
|
)
|
||||||
|
sp.add_argument('lbid', help='Load Balancer ID')
|
||||||
subparsers.add_parser(
|
subparsers.add_parser(
|
||||||
'node-list', help='list nodes in a load balancer'
|
'node-list', help='list nodes in a load balancer'
|
||||||
)
|
)
|
||||||
@ -80,4 +81,5 @@ class ClientOptions(object):
|
|||||||
)
|
)
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
|
self._generate()
|
||||||
return self.options.parse_args()
|
return self.options.parse_args()
|
||||||
|
@ -29,16 +29,37 @@ class LibraAPI(object):
|
|||||||
|
|
||||||
def list_lb(self):
|
def list_lb(self):
|
||||||
resp, body = self._get('/loadbalaners')
|
resp, body = self._get('/loadbalaners')
|
||||||
columns = ['Name', 'ID', 'Protocol', 'Port', 'Algorithm', 'Status',
|
column_names = ['Name', 'ID', 'Protocol', 'Port', 'Algorithm',
|
||||||
'Created', 'Updated']
|
'Status', 'Created', 'Updated']
|
||||||
self._render(columns, body['loadBalancers'])
|
columns = ['name', 'id', 'protocol', 'port', 'algorithm', 'status',
|
||||||
|
'created', 'updated']
|
||||||
|
self._render_list(column_names, columns, body['loadBalancers'])
|
||||||
|
|
||||||
def _render(self, columns, data):
|
def get_lb(self, lbid):
|
||||||
table = prettytable.PrettyTable(columns)
|
resp, body = self._get('/loadbalancers/{0}'.format(lbid))
|
||||||
|
column_names = ['ID', 'Name', 'Protocol', 'Port', 'Algorithm',
|
||||||
|
'Status', 'Created', 'Updated', 'IPs', 'Nodes',
|
||||||
|
'Persistence Type', 'Connection Throttle']
|
||||||
|
columns = ['id', 'name', 'protocol', 'port', 'algorithm', 'status',
|
||||||
|
'created', 'updated', 'virtualIps', 'nodes',
|
||||||
|
'sessionPersistence', 'connectionThrottle']
|
||||||
|
self._render_dict(column_names, columns, body)
|
||||||
|
|
||||||
|
def _render_list(self, column_names, columns, data):
|
||||||
|
table = prettytable.PrettyTable(column_names)
|
||||||
for item in data:
|
for item in data:
|
||||||
row = []
|
row = []
|
||||||
for column in columns:
|
for column in columns:
|
||||||
rdata = item[column.lower()]
|
rdata = item[column]
|
||||||
|
row.append(rdata)
|
||||||
|
table.add_row(row)
|
||||||
|
print table
|
||||||
|
|
||||||
|
def _render_dict(self, column_names, columns, data):
|
||||||
|
table = prettytable.PrettyTable(column_names)
|
||||||
|
row = []
|
||||||
|
for column in columns:
|
||||||
|
rdata = data[column]
|
||||||
row.append(rdata)
|
row.append(rdata)
|
||||||
table.add_row(row)
|
table.add_row(row)
|
||||||
print table
|
print table
|
||||||
|
@ -6,8 +6,19 @@ import sys
|
|||||||
from StringIO import StringIO
|
from StringIO import StringIO
|
||||||
from libra.client.libraapi import LibraAPI
|
from libra.client.libraapi import LibraAPI
|
||||||
|
|
||||||
fake_response = httplib2.Response({"status": '200'})
|
|
||||||
fake_body = json.dumps({
|
class TestLBaaSClientLibraAPI(unittest.TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
self.api = LibraAPI('username', 'password', 'tenant', 'auth_test', 'region')
|
||||||
|
self.api.nova.management_url = "http://example.com"
|
||||||
|
self.api.nova.auth_token = "token"
|
||||||
|
|
||||||
|
def tearDown(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def testListLb(self):
|
||||||
|
fake_response = httplib2.Response({"status": '200'})
|
||||||
|
fake_body = json.dumps({
|
||||||
"loadBalancers":[
|
"loadBalancers":[
|
||||||
{
|
{
|
||||||
"name":"lb-site1",
|
"name":"lb-site1",
|
||||||
@ -30,21 +41,9 @@ fake_body = json.dumps({
|
|||||||
"updated":"2010-11-30T03:23:44Z"
|
"updated":"2010-11-30T03:23:44Z"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
})
|
})
|
||||||
|
mock_request = mock.Mock(return_value=(fake_response, fake_body))
|
||||||
|
|
||||||
mock_request = mock.Mock(return_value=(fake_response, fake_body))
|
|
||||||
|
|
||||||
|
|
||||||
class TestLBaaSClientLibraAPI(unittest.TestCase):
|
|
||||||
def setUp(self):
|
|
||||||
self.api = LibraAPI('username', 'password', 'tenant', 'auth_test', 'region')
|
|
||||||
self.api.nova.management_url = "http://example.com"
|
|
||||||
self.api.nova.auth_token = "token"
|
|
||||||
|
|
||||||
def tearDown(self):
|
|
||||||
pass
|
|
||||||
|
|
||||||
def testListLb(self):
|
|
||||||
with mock.patch.object(httplib2.Http, "request", mock_request):
|
with mock.patch.object(httplib2.Http, "request", mock_request):
|
||||||
with mock.patch('time.time', mock.Mock(return_value=1234)):
|
with mock.patch('time.time', mock.Mock(return_value=1234)):
|
||||||
orig = sys.stdout
|
orig = sys.stdout
|
||||||
@ -53,6 +52,68 @@ class TestLBaaSClientLibraAPI(unittest.TestCase):
|
|||||||
sys.stdout = out
|
sys.stdout = out
|
||||||
self.api.list_lb()
|
self.api.list_lb()
|
||||||
output = out.getvalue().strip()
|
output = out.getvalue().strip()
|
||||||
|
self.assertRegexpMatches(output, 'lb-site1')
|
||||||
|
self.assertRegexpMatches(output, '71')
|
||||||
|
self.assertRegexpMatches(output, 'HTTP')
|
||||||
|
self.assertRegexpMatches(output, '80')
|
||||||
self.assertRegexpMatches(output, 'LEAST_CONNECTIONS')
|
self.assertRegexpMatches(output, 'LEAST_CONNECTIONS')
|
||||||
|
self.assertRegexpMatches(output, 'ACTIVE')
|
||||||
|
self.assertRegexpMatches(output, '2010-11-30T03:23:42Z')
|
||||||
|
self.assertRegexpMatches(output, '2010-11-30T03:23:44Z')
|
||||||
finally:
|
finally:
|
||||||
sys.stdout = orig
|
sys.stdout = orig
|
||||||
|
|
||||||
|
def testGetLb(self):
|
||||||
|
fake_response = httplib2.Response({"status": '200'})
|
||||||
|
fake_body = json.dumps({
|
||||||
|
"id": "2000",
|
||||||
|
"name":"sample-loadbalancer",
|
||||||
|
"protocol":"HTTP",
|
||||||
|
"port": "80",
|
||||||
|
"algorithm":"ROUND_ROBIN",
|
||||||
|
"status":"ACTIVE",
|
||||||
|
"created":"2010-11-30T03:23:42Z",
|
||||||
|
"updated":"2010-11-30T03:23:44Z",
|
||||||
|
"virtualIps":[
|
||||||
|
{
|
||||||
|
"id": "1000",
|
||||||
|
"address":"2001:cdba:0000:0000:0000:0000:3257:9652",
|
||||||
|
"type":"PUBLIC",
|
||||||
|
"ipVersion":"IPV6"
|
||||||
|
}],
|
||||||
|
"nodes": [
|
||||||
|
{
|
||||||
|
"id": "1041",
|
||||||
|
"address":"10.1.1.1",
|
||||||
|
"port": "80",
|
||||||
|
"condition":"ENABLED",
|
||||||
|
"status":"ONLINE"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "1411",
|
||||||
|
"address":"10.1.1.2",
|
||||||
|
"port": "80",
|
||||||
|
"condition":"ENABLED",
|
||||||
|
"status":"ONLINE"
|
||||||
|
}],
|
||||||
|
"sessionPersistence":{
|
||||||
|
"persistenceType":"HTTP_COOKIE"
|
||||||
|
},
|
||||||
|
"connectionThrottle":{
|
||||||
|
"maxRequestRate": "50",
|
||||||
|
"rateInterval": "60"
|
||||||
|
}
|
||||||
|
})
|
||||||
|
mock_request = mock.Mock(return_value=(fake_response, fake_body))
|
||||||
|
with mock.patch.object(httplib2.Http, "request", mock_request):
|
||||||
|
with mock.patch('time.time', mock.Mock(return_value=1234)):
|
||||||
|
orig = sys.stdout
|
||||||
|
try:
|
||||||
|
out = StringIO()
|
||||||
|
sys.stdout = out
|
||||||
|
self.api.get_lb('2000')
|
||||||
|
output = out.getvalue().strip()
|
||||||
|
self.assertRegexpMatches(output, 'HTTP_COOKIE')
|
||||||
|
finally:
|
||||||
|
sys.stdout = orig
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user