UCSVlans.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #
  2. # Copyright (c) 2017-2018 Joe Clarke <jclarke@cisco.com>
  3. # All rights reserved.
  4. #
  5. # Redistribution and use in source and binary forms, with or without
  6. # modification, are permitted provided that the following conditions
  7. # are met:
  8. # 1. Redistributions of source code must retain the above copyright
  9. # notice, this list of conditions and the following disclaimer.
  10. # 2. Redistributions in binary form must reproduce the above copyright
  11. # notice, this list of conditions and the following disclaimer in the
  12. # documentation and/or other materials provided with the distribution.
  13. #
  14. # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  15. # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  17. # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  18. # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  19. # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  20. # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  21. # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  22. # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  23. # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  24. # SUCH DAMAGE.
  25. from ucsmsdk.ucshandle import UcsHandle
  26. from ucsmsdk.mometa.fabric.FabricVlan import FabricVlan
  27. from ucsmsdk.mometa.vnic.VnicEtherIf import VnicEtherIf
  28. import os
  29. import pprint
  30. class UCSVlans():
  31. _handle = None
  32. def _login(self):
  33. if self._handle is not None:
  34. return self._handle
  35. handle = UcsHandle(self.device, self.username,
  36. os.environ['NXOS_ADMIN_PW'])
  37. handle.login()
  38. self._handle = handle
  39. return self._handle
  40. def deploy_fabric_vlan(self):
  41. try:
  42. handle = self._login()
  43. v = FabricVlan(parent_mo_or_dn='fabric/lan',
  44. name=self.vname, id=str(self.vid))
  45. handle.add_mo(v)
  46. handle.commit()
  47. except Exception as e:
  48. print('Error adding VLAN {} to fabric on {}: {}'.format(
  49. self.vid, self.device, e))
  50. return False
  51. return True
  52. def delete_fabric_vlan(self):
  53. try:
  54. handle = self._login()
  55. filter_str = '(id, "{}")'.format(self.vid)
  56. v = handle.query_classid(
  57. class_id='FabricVlan', filter_str=filter_str)
  58. if len(v) != 1:
  59. raise Exception(
  60. 'Got {} elements with VLAN ID {}'.format(len(v), self.vid))
  61. handle.remove_mo(v[0])
  62. handle.commit()
  63. except Exception as e:
  64. print('Error deleting VLAN {} on {}: {}'.format(
  65. self.vid, self.device, e))
  66. return False
  67. return True
  68. def deploy_lan_policy(self):
  69. try:
  70. handle = self._login()
  71. mos = []
  72. for vnic in (self.vnic_a, self.vnic_b):
  73. try:
  74. v = VnicEtherIf(
  75. parent_mo_or_dn='org-root/lan-conn-pol-{}/ether-{}'.format(self.policy, vnic), name=self.vname)
  76. handle.add_mo(v)
  77. handle.commit()
  78. mos.append(v)
  79. except Exception as e:
  80. print('Error adding VLAN {} to LAN connectivity policy {}/{} on {}: {}'.format(
  81. self.vname, self.policy, vnic, self.device, e))
  82. if len(mos) > 0:
  83. try:
  84. handle.delete_mo(v)
  85. handle.commit()
  86. except Exception as ie:
  87. print('Error removing VLAN {} from LAN connectivity policy {} on {}: {}'.format(
  88. self.vname, self.policy, self.device, ie))
  89. return False
  90. except Exception as le:
  91. print('Error logging into {}: {}'.format(self.device, le))
  92. return False
  93. return True
  94. def logout(self):
  95. if self._handle is None:
  96. return
  97. try:
  98. self._handle.logout()
  99. except:
  100. pass