Add batch api.

1. Add batch switches api.
2. Add batch switch_machines api.
3. update unittest and api.raml.

Change-Id: I5dd06de12f11eb4fa580a6cfacf1fb917ba6b4ea
This commit is contained in:
Lei Lei 2015-05-11 16:57:15 -07:00
parent 3ff01aa51a
commit bf093b03b1
5 changed files with 3813 additions and 3319 deletions

View File

@ -657,6 +657,21 @@ def add_switch():
)
@app.route("/switchesbatch", methods=['POST'])
@log_user_action
@login_required
@update_user_token
def add_switches():
"""add switches."""
data = _get_request_data()
return utils.make_json_response(
200,
switch_api.add_switches(
data=data, user=current_user
)
)
@app.route("/switches/<int:switch_id>", methods=['PUT'])
@log_user_action
@login_required
@ -872,6 +887,20 @@ def add_switch_machine(switch_id):
)
@app.route("/switches/machines", methods=['POST'])
@log_user_action
@login_required
@update_user_token
def add_switch_machines():
"""add switch machines."""
data = _get_request_data_as_list()
return utils.make_json_response(
200, switch_api.add_switch_machines(
data=data, user=current_user
)
)
@app.route(
'/switches/<int:switch_id>/machines/<int:machine_id>',
methods=['GET']
@ -2389,7 +2418,7 @@ def update_host_networks():
data = _get_request_data_as_list()
return utils.make_json_response(
200, host_api.add_host_networks(
data, user=current_user,)
data=data, user=current_user,)
)

File diff suppressed because it is too large Load Diff

View File

@ -111,13 +111,24 @@ def _check_vlans(vlans):
)
@utils.supported_filters(
ADDED_FIELDS,
optional_support_keys=OPTIONAL_ADDED_FIELDS,
ignore_support_keys=IGNORE_FIELDS
)
@utils.input_validates(
ip=utils.check_ip,
credentials=utils.check_switch_credentials,
filters=_check_filters
)
@utils.wrap_to_dict(RESP_FIELDS)
def add_switch_internal(
session, ip_int, exception_when_existing=True,
session, ip, exception_when_existing=True,
filters=setting.SWITCHES_DEFAULT_FILTERS, **kwargs
):
with session.begin(subtransactions=True):
return utils.add_db_object(
session, models.Switch, exception_when_existing, ip_int,
session, models.Switch, exception_when_existing, ip,
filters=filters, **kwargs
)
@ -196,21 +207,10 @@ def del_switch(switch_id, user=None, session=None, **kwargs):
return utils.del_db_object(session, switch)
@utils.supported_filters(
ADDED_FIELDS,
optional_support_keys=OPTIONAL_ADDED_FIELDS,
ignore_support_keys=IGNORE_FIELDS
)
@utils.input_validates(
ip=utils.check_ip,
credentials=utils.check_switch_credentials,
filters=_check_filters
)
@database.run_in_session()
@user_api.check_user_permission_in_session(
permission.PERMISSION_ADD_SWITCH
)
@utils.wrap_to_dict(RESP_FIELDS)
def add_switch(
exception_when_existing=True, ip=None,
user=None, session=None, **kwargs
@ -222,6 +222,42 @@ def add_switch(
)
@database.run_in_session()
@user_api.check_user_permission_in_session(
permission.PERMISSION_ADD_SWITCH
)
def add_switches(
exception_when_existing=False,
data=[], user=None, session=None
):
"""Create switches."""
switches = []
fail_switches = []
for switch_data in data:
switch_ip = long(netaddr.IPAddress(switch_data['ip']))
switch_object = utils.get_db_object(
session, models.Switch, False,
ip_int=switch_ip
)
if switch_object:
logging.error('ip %s exists in switch %s' % (
switch_ip, switch_object.id
))
fail_switches.append(switch_data)
else:
switch_data.pop('ip')
switches.append(
add_switch_internal(
session, switch_ip, exception_when_existing,
**switch_data
)
)
return {
'switches': switches,
'fail_switches': fail_switches
}
def update_switch_internal(session, switch, **kwargs):
"""update switch."""
return utils.update_db_object(
@ -531,16 +567,11 @@ def list_switchmachines_hosts(user=None, session=None, **filters):
ignore_support_keys=IGNORE_FIELDS
)
@utils.input_validates(mac=utils.check_mac, vlans=_check_vlans)
@database.run_in_session()
@user_api.check_user_permission_in_session(
permission.PERMISSION_ADD_SWITCH_MACHINE
)
@utils.wrap_to_dict(RESP_MACHINES_FIELDS)
def add_switch_machine(
switch_id, exception_when_existing=True,
mac=None, user=None, session=None, **kwargs
def _add_switch_machine(
session, user, switch_id, exception_when_existing=True,
mac=None, **kwargs
):
"""Add switch machine."""
switch = utils.get_db_object(
session, models.Switch, id=switch_id)
switch_machine_dict = {}
@ -562,6 +593,113 @@ def add_switch_machine(
)
@database.run_in_session()
@user_api.check_user_permission_in_session(
permission.PERMISSION_ADD_SWITCH_MACHINE
)
def add_switch_machine(
switch_id, exception_when_existing=True,
mac=None, user=None, session=None, **kwargs
):
"""Add switch machine."""
return _add_switch_machine(
session, user, switch_id,
exception_when_existing, mac=mac, **kwargs
)
@database.run_in_session()
@user_api.check_user_permission_in_session(
permission.PERMISSION_ADD_SWITCH_MACHINE
)
def add_switch_machines(
exception_when_existing=False,
data=[], user=None, session=None
):
"""Add switch machines."""
switch_machines = []
duplicate_switch_machines = []
failed_switch_machines = []
switch_ip_list = []
switch_datas = []
for item_data in data:
switch_ip = item_data['switch_ip']
switch_ip_int = long(netaddr.IPAddress(item_data['switch_ip']))
if switch_ip not in switch_ip_list:
switch_object = utils.get_db_object(
session, models.Switch, False,
ip_int=switch_ip_int
)
if switch_object:
switch_ip_list.append(switch_ip)
item_data.pop('switch_ip')
switch_datas.append({
'switch_id': switch_object.id,
'switch_ip': switch_ip,
'machines': [item_data]
})
else:
logging.error(
'switch ip %s is not existed in switch table' % switch_ip
)
item_data.pop('switch_ip')
failed_switch_machines.append(item_data)
else:
for item in switch_datas:
if switch_ip == item['switch_ip']:
item_data.pop('switch_ip')
item['machines'].append(item_data)
for switch_data in switch_datas:
switch_id = switch_data['switch_id']
machines = switch_data['machines']
for machine in machines:
mac = machine['mac']
machine_object = utils.get_db_object(
session, models.Machine, False,
mac=mac
)
if machine_object:
switch_machine_object = utils.get_db_object(
session, models.SwitchMachine, False,
machine_id=machine_object.id
)
if (
switch_machine_object and not(
switch_machine_object.switch_id == switch_id and
switch_machine_object.port == machine['port']
)
):
logging.error('machine %s exists in switch machine %s' % (
machine['mac'], switch_machine_object.switch_machine_id
))
failed_switch_machines.append(machine)
elif (
switch_machine_object and
switch_machine_object.switch_id == switch_id and
switch_machine_object.port == machine['port']
):
logging.error(
'machine %s is dulicate, will not be override' %
machine['mac']
)
duplicate_switch_machines.append(machine)
else:
switch_machines.append(_add_switch_machine(
session, user, switch_id, exception_when_existing,
**machine
))
else:
switch_machines.append(_add_switch_machine(
session, user, switch_id, exception_when_existing,
**machine
))
return {
'switches_machines': switch_machines,
'duplicate_switches_machines': duplicate_switch_machines,
'fail_switches_machines': failed_switch_machines
}
@utils.supported_filters(optional_support_keys=['find_machines'])
@database.run_in_session()
@user_api.check_user_permission_in_session(

View File

@ -644,6 +644,33 @@ class TestSwitchAPI(ApiTestCase):
return_value = self.post(url, data)
self.assertEqual(return_value.status_code, 400)
def test_add_switches(self):
# add switches
url = '/switchesbatch'
data = [
{
'ip': '172.29.8.30',
'vendor': 'Huawei',
'credentials': {
"version": "2c",
"community": "public"
}
}, {
'ip': '172.29.8.40'
}
]
return_value = self.post(url, data)
resp = json.loads(return_value.get_data())
success = []
fail = []
for item in resp['switches']:
success.append(item['ip'])
for item in resp['fail_switches']:
fail.append(item['ip'])
self.assertEqual(return_value.status_code, 200)
self.assertIn('172.29.8.30', success)
self.assertIn('172.29.8.40', fail)
def test_update_switch(self):
# update a swithc successfully
url = '/switches/1'
@ -958,6 +985,83 @@ class TestSwitchMachines(ApiTestCase):
return_value = self.post(url, data)
self.assertEqual(return_value.status_code, 400)
def test_add_switch_machines(self):
# batch switch machines
url = '/switches'
return_value = self.get(url)
url = '/switches/machines'
data = [{
"switch_ip": "0.0.0.0",
"mac": "1a:2b:3c:4d:5e:6f",
"port": "100"
}, {
"switch_ip": "0.0.0.0",
"mac": "a1:b2:c3:d4:e5:f6",
"port": "101"
}, {
"switch_ip": "0.0.0.0",
"mac": "a1:b2:c3:d4:e5:f6",
"port": "101"
}, {
"switch_ip": "0.0.0.0",
"mac": "a1:b2:c3:d4:e5:f6",
"port": "102"
}, {
"switch_ip": "10.10.10.1",
"mac": "b1:b2:c3:d4:e5:f6",
"port": "200"
}, {
"switch_ip": "127.0.0.2",
"mac": "a1:b2:f3:d4:e5:f6",
"port": "100"
}]
return_value = self.post(url, data)
expected = [{
'switch_ip': '0.0.0.0',
'port': '100',
'mac': '1a:2b:3c:4d:5e:6f'
}, {
'switch_ip': '0.0.0.0',
'port': '101',
'mac': 'a1:b2:c3:d4:e5:f6'
}, {
'switch_ip': '10.10.10.1',
'port': '200',
'mac': 'b1:b2:c3:d4:e5:f6'
}]
expect_duplicate = {'mac': 'a1:b2:c3:d4:e5:f6', 'port': '101'}
expect_failed = [
{'mac': 'a1:b2:f3:d4:e5:f6', 'port': '100'},
{'mac': 'a1:b2:c3:d4:e5:f6', 'port': '102'}
]
resp = json.loads(return_value.get_data())
res = []
res_du = []
res_fail = []
for k, v in resp.items():
if k == 'switches_machines':
for item in v:
res.append(item)
if k == 'duplicate_switches_machines':
for item in v:
res_du.append(item)
if k == 'fail_switches_machines':
for item in v:
res_fail.append(item)
for i, v in enumerate(res):
self.assertTrue(
all(item in res[i].items() for item in expected[i].items())
)
for i, v in enumerate(res_fail):
self.assertTrue(
all(item in res_fail[i].items() for
item in expect_failed[i].items())
)
self.assertTrue(
all(item in res_du[0].items() for item in expect_duplicate.items())
)
def test_show_switch_machine(self):
# show a switch_machine successfully
url = '/switches/2/machines/1'

View File

@ -90,6 +90,48 @@ class TestAddSwitch(BaseTest):
self.assertEqual(expected, add_switch['ip'])
class TestAddSwitches(BaseTest):
"""Test add switches."""
def setUp(self):
super(TestAddSwitches, self).setUp()
def tearDown(self):
super(TestAddSwitches, self).tearDown()
def test_add_switches(self):
data = [
{
'ip': '172.29.8.30',
'vendor': 'Huawei',
'credentials': {
"version": "2c",
"community": "public"
}
}, {
'ip': '172.29.8.40'
}, {
'ip': '172.29.8.40'
}
]
switches = switch.add_switches(
data=data,
user=self.user_object
)
ip = []
for item in switches['switches']:
ip.append(item['ip'])
fail_ip = []
for item in switches['fail_switches']:
fail_ip.append(item['ip'])
expected = ['172.29.8.30', '172.29.8.40']
expected_fail = ['172.29.8.40']
for expect in expected:
self.assertIn(expect, ip)
for expect_fail in expected_fail:
self.assertIn(expect_fail, fail_ip)
class TestListSwitches(BaseTest):
"""Test list switch."""
@ -372,6 +414,49 @@ class TestAddSwitchMachine(BaseTest):
self.assertEqual(expected, add_switch_machine['mac'])
class TestAddSwitchMachines(BaseTest):
"""Test add switch machines."""
def setUp(self):
super(TestAddSwitchMachines, self).setUp()
def tearDown(self):
super(TestAddSwitchMachines, self).tearDown()
def test_add_switch_machines(self):
data = [{
'switch_ip': '0.0.0.0',
'mac': '1a:2b:3c:4d:5e:6f',
'port': '100'
}, {
'switch_ip': '0.0.0.0',
'mac': 'a1:b2:c3:d4:e5:f6',
'port': '101'
}, {
'switch_ip': '0.0.0.0',
'mac': 'a1:b2:c3:d4:e5:f6',
'port': '103'
}, {
'switch_ip': '0.0.0.0',
'mac': 'a1:b2:c3:d4:e5:f6',
'port': '101'
}]
add_switch_machines = switch.add_switch_machines(
data=data, user=self.user_object
)
mac = []
failed_mac = []
for switch_machine in add_switch_machines['switches_machines']:
mac.append(switch_machine['mac'])
for failed_switch in add_switch_machines['fail_switches_machines']:
failed_mac.append(failed_switch['mac'])
expect = ['1a:2b:3c:4d:5e:6f', 'a1:b2:c3:d4:e5:f6']
expect_fail = ['a1:b2:c3:d4:e5:f6']
for item in expect:
self.assertIn(item, mac)
for item in expect_fail:
self.assertIn(item, failed_mac)
class TestListSwitchMachines(BaseTest):
"""Test get switch machines."""