Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add functionality for managing vlans #66

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
162 changes: 162 additions & 0 deletions tests/unit_test/network/test_ut_network_fabricvlan.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
import pytest
from ucsm_apis.network import fabricvlan
from ucsmsdk.ucshandle import UcsHandle

handle = UcsHandle("10.10.10.10", "username", "password")


def test_vlan_create_success(mocker):
mock_login = mocker.patch('ucsmsdk.ucshandle.UcsHandle.login',
autospec=True)
mock_login.return_value = True
mock_query_dn = mocker.patch('ucsmsdk.ucshandle.UcsHandle.query_dn',
autospec=True)
mock_query_dn.return_value = "fabric/lan"
mock_commit = mocker.patch('ucsmsdk.ucshandle.UcsHandle.commit',
autospec=True)
mock_commit.return_value = None
mock_fabric_vlan = mocker.patch('ucsm_apis.network.'
'fabricvlan.FabricVlan')
mock_add_mo = mocker.patch('ucsmsdk.ucshandle.UcsHandle.add_mo',
autospec=True)
mock_add_mo.return_value = None

fabricvlan.vlan_create(handle, name="test",
id="100")

mock_query_dn.assert_called_with(handle, "fabric/lan")
mock_fabric_vlan.assert_called_with(parent_mo_or_dn="fabric/lan",
id="100",
name="test",
sharing=None)


def test_vlan_create_fail_org_not_exist(mocker):
mock_login = mocker.patch.object(UcsHandle, 'login', autospec=True)
mock_login.return_value = True
mock_query_dn = mocker.patch.object(UcsHandle, 'query_dn',
autospec=True)
mock_query_dn.return_value = None

with pytest.raises(fabricvlan.UcsOperationError):
fabricvlan.vlan_create(handle, name="test",
id="100")


def test_vlan_get_success(mocker):
mock_login = mocker.patch.object(UcsHandle, 'login', autospec=True)
mock_login.return_value = True
mock_query_dn = mocker.patch.object(UcsHandle, 'query_dn',
autospec=True)
mock_query_dn.return_value = "fabric/lan/net-test"

fabricvlan.vlan_get(handle, name="test")

mock_query_dn.assert_called_with(handle, "fabric/lan/net-test")


def test_vlan_get_fail_vlan_does_not_exist(mocker):
mock_login = mocker.patch.object(UcsHandle, 'login', autospec=True)
mock_login.return_value = True
mock_query_dn = mocker.patch.object(UcsHandle, 'query_dn',
autospec=True)
mock_query_dn.return_value = None

with pytest.raises(fabricvlan.UcsOperationError):
fabricvlan.vlan_get(handle, name="noaqui")


def test_vlan_exists_success(mocker):
mock_login = mocker.patch.object(UcsHandle, 'login', autospec=True)
mock_login.return_value = True
mock_query_dn = mocker.patch.object(UcsHandle, 'query_dn',
autospec=True)
mock_query_dn.return_value = "fabric/lan/net-test"
mock_check_prop = mocker.patch.object(fabricvlan.FabricVlan,
'check_prop_match', autospec=True)
mock_check_prop.return_value = True
mock_get = mocker.patch.object(fabricvlan, 'vlan_get', autospec=True)

fabricvlan.vlan_exists(handle, name="test")

mock_get.assert_called_with(handle=handle, name="test",
caller='vlan_exists',
org_dn="fabric/lan")


def test_vlan_exists_fail_org_does_not_exist(mocker):
mock_login = mocker.patch.object(UcsHandle, 'login', autospec=True)
mock_login.return_value = True
mock_get = mocker.patch.object(fabricvlan, 'vlan_get', autospec=True)
mock_get.side_effect = fabricvlan.UcsOperationError("query_dn",
"vlan does not exist")

result = fabricvlan.vlan_exists(handle, name="no_aqui")

assert result == (False, None)


def test_vlan_modify_success(mocker):
mock_login = mocker.patch.object(UcsHandle, 'login', autospec=True)
mock_login.return_value = True
mock_get = mocker.patch.object(fabricvlan, 'vlan_get', autospec=True)
mo_mock = mocker.Mock()
mo_mock.set_prop_multiple.return_value = True
mock_get.return_value = mo_mock
mock_set_mo = mocker.patch.object(UcsHandle, 'set_mo', autospec=True)
mock_set_mo.return_value = None
mock_commit = mocker.patch.object(UcsHandle, 'commit', autospec=True)
mock_commit.return_value = None

fabricvlan.vlan_modify(handle, name="test",
sharing="isolated")

mock_get.assert_called_with(handle=handle, name="test",
org_dn="fabric/lan",
caller="vlan_modify")
mo_mock.set_prop_multiple.assert_called_with(sharing="isolated")


def test_vlan_modify_failure_pool_nonexistent(mocker):
mock_login = mocker.patch.object(UcsHandle, 'login', autospec=True)
mock_login.return_value = True
mock_get = mocker.patch.object(fabricvlan, 'vlan_get', autospec=True)
mock_get.side_effect = fabricvlan.UcsOperationError("query_dn",
"vlan does not exist")

with pytest.raises(fabricvlan.UcsOperationError):
fabricvlan.vlan_modify(handle, name="noaqui",
sharing="isolated")


def test_vlan_delete_success(mocker):
mock_login = mocker.patch('ucsmsdk.ucshandle.UcsHandle.login',
autospec=True)
mock_login.return_value = True
mock_query_dn = mocker.patch('ucsmsdk.ucshandle.UcsHandle.query_dn',
autospec=True)
mock_query_dn.return_value = "fabric/lan/net-test"
mock_commit = mocker.patch('ucsmsdk.ucshandle.'
'UcsHandle.commit',
autospec=True)
mock_commit.return_value = None
mock_remove_mo = mocker.patch('ucsmsdk.ucshandle.UcsHandle.remove_mo',
autospec=True)
mock_remove_mo.return_value = None

fabricvlan.vlan_delete(handle, name="test")

mock_query_dn.assert_called_with(handle, "fabric/lan/net-test")
assert mock_remove_mo.call_count == 1
assert mock_commit.call_count == 1


def test_vlan_delete_failure_org_nonexistent(mocker):
mock_login = mocker.patch.object(UcsHandle, 'login', autospec=True)
mock_login.return_value = True
mock_query_dn = mocker.patch.object(UcsHandle, 'query_dn',
autospec=True)
mock_query_dn.return_value = None

with pytest.raises(fabricvlan.UcsOperationError):
fabricvlan.vlan_delete(handle, name="noaqui")
198 changes: 198 additions & 0 deletions ucsm_apis/network/fabricvlan.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
# Copyright 2017 Cisco Systems, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
This module performs operations related to Fabric Ethernet Lan Ports.
"""

from ucsmsdk.mometa.fabric.FabricVlan import FabricVlan
from ucsmsdk.ucsexception import UcsOperationError


def vlan_create(handle, org_dn="fabric/lan",
name=None, id=None,
sharing=None, **kwargs):

"""
Creates vlans

Args:
handle (UCSHandle)
org_dn (string): org dn
name (string): vlan name
id (string): vlan id number
sharing(string): sharing type ["community",
"isolated",
"none",
"primary"]
**kwargs: Any additional key-value pair of managed object(MO)'s
property and value, which are not part of regular args.
This should be used for future version compatibility.

Returns:
FabricVlan: managed object

Raises:
UcsOperationError: if Org_dn is not present

Example:
vlan_create(handle,
name="vmotion",
id="400")
"""

obj = handle.query_dn(org_dn)
if not obj:
raise UcsOperationError("vlan_create", "Org {} \
does not exist".format(org_dn))

mo = FabricVlan(parent_mo_or_dn=obj, id=id,
name=name, sharing=sharing)
mo.set_prop_multiple(**kwargs)
handle.add_mo(mo, modify_present=True)
handle.commit()
return mo


def vlan_get(handle, name=None, org_dn="fabric/lan",
caller="vlan_get"):

"""
Gets vlan

Args:
handle (UCSHandle)
org_dn (string): org dn
name (string): vlan name
caller (string): caller method name

Returns:
FabricVlan: managed object

Raises:
UcsOperationError: if FabricVlan is not present

Example:
vlan_get(handle,
name="test")
"""

dn = org_dn + "/net-" + name
mo = handle.query_dn(dn)
if not mo:
raise UcsOperationError(caller, "vlan {} "
"does not exist".format(dn))
return mo


def vlan_exists(handle, name=None, org_dn="fabric/lan",
**kwargs):

"""
checks if vlan exists

Args:
handle (UCSHandle)
org_dn (string): org dn
name (string): vlan name
**kwargs: key-value pair of managed object(MO) property and value, Use
'print(ucscoreutils.get_meta_info(<classid>).config_props)'
to get all configurable properties of class

Returns:
(True/False, FabricVlan MO/None)

Raises:
None

Example:
vlan_exists:(handle,
name="test")
"""

try:
mo = vlan_get(handle=handle, name=name,
org_dn=org_dn, caller="vlan_exists")
except UcsOperationError:
return (False, None)
mo_exists = mo.check_prop_match(**kwargs)
return (mo_exists, mo if mo_exists else None)


def vlan_modify(handle, name=None,
org_dn="fabric/lan", **kwargs):

"""
modifies vlan

Args:
handle (UCSHandle)
org_dn (string): org dn
name (string): vlan name
**kwargs: key-value pair of managed object(MO) property and value, Use
'print(ucscoreutils.get_meta_info(<classid>).config_props)'
to get all configurable properties of class

Returns:
FabricVlan: managed object

Raises:
UcsOperationError: if FabricVlan is not present

Example:
vlan_modify(handle,
name="test",
sharing="community")
"""

mo = vlan_get(handle=handle, name=name,
org_dn=org_dn, caller="vlan_modify")
mo.set_prop_multiple(**kwargs)
handle.set_mo(mo)
handle.commit()
return mo


def vlan_delete(handle, name=None,
org_dn="fabric/lan", **kwargs):

"""
Deletes vlan

Args:
handle (UCSHandle)
org_dn (string): org dn
name (string): vlan name
**kwargs: Any additional key-value pair of managed object(MO)'s
property and value, which are not part of regular args.
This should be used for future version compatibility.

Returns:
None

Raises:
UcsOperationError: if if FabricVlan is not present

Example:
vlan_delete(handle,
name="test")
"""

dn = org_dn + "/net-" + name
mo = handle.query_dn(dn)
if not mo:
raise UcsOperationError("vlan_delete", "vlan {} "
"does not exist".format(dn))

handle.remove_mo(mo)
handle.commit()