ucs_add_delete_vlan.py 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #!/usr/bin/env python2
  2. #
  3. # Copyright (c) 2017-2018 Joe Clarke <jclarke@cisco.com>
  4. # All rights reserved.
  5. #
  6. # Redistribution and use in source and binary forms, with or without
  7. # modification, are permitted provided that the following conditions
  8. # are met:
  9. # 1. Redistributions of source code must retain the above copyright
  10. # notice, this list of conditions and the following disclaimer.
  11. # 2. Redistributions in binary form must reproduce the above copyright
  12. # notice, this list of conditions and the following disclaimer in the
  13. # documentation and/or other materials provided with the distribution.
  14. #
  15. # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  16. # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  19. # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  21. # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  22. # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  23. # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  24. # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  25. # SUCH DAMAGE.
  26. import argparse
  27. import os
  28. import sys
  29. from UCSVlans import UCSVlans
  30. if __name__ == '__main__':
  31. uv = UCSVlans()
  32. parser = argparse.ArgumentParser(
  33. prog=sys.argv[0], description='Add a new VLAN to a UCS fabric')
  34. parser.add_argument('--device', '-d', dest='device', metavar='<HOSTNAME|IP>',
  35. help='UCS chassis to which to add VLAN', required=True)
  36. parser.add_argument('--vlan', '-v', dest='vid',
  37. metavar='<VLAN_ID>', help='VLAN ID', type=int, required=True)
  38. parser.add_argument('--name', '-n', dest='vname',
  39. metavar='<VLAN_NAME>', help='VLAN Name')
  40. parser.add_argument('--username', '-u', dest='username',
  41. metavar='<USERNAME>', help='Device username', required=True)
  42. parser.add_argument('--policy', '-p', dest='policy', metavar='<POLICY>',
  43. help='Name of LAN Connectivity Policy')
  44. parser.add_argument('--vnic-a', '-a', dest='vnic_a', metavar='<VNIC_A_NAME>',
  45. help='Name of vNIC in LAN connectivity policy for Fabric-A')
  46. parser.add_argument('--vnic-b', '-b', dest='vnic_b', metavar='<VNIC_B_NAME>',
  47. help='Name of vNIC in LAN connectivity policy for Fabric-B')
  48. parser.add_argument('--delete', '-D', dest='delete',
  49. action='store_true', help='Delete the specified VLAN')
  50. parser.set_defaults(delete=False)
  51. parser.parse_args(namespace=uv)
  52. if 'UCS_ADMIN_PW' not in os.environ:
  53. print('The environment variable "UCS_ADMIN_PW" must be set with the password for {}'.format(
  54. uv.username))
  55. sys.exit(1)
  56. if not uv.delete and not uv.vname:
  57. parser.error('VLAN name must be specified')
  58. if not uv.delete and not uv.policy:
  59. parser.error('LAN connectivity policy name must be specified')
  60. if not uv.delete and not uv.vnic_a:
  61. parser.error('vNIC name for Fabric-A must be specified')
  62. if not uv.delete and not uv.vnic_b:
  63. parser.error('vNIC name for Fabric-B muct be specified')
  64. if uv.delete:
  65. res = uv.delete_fabric_vlan()
  66. if not res:
  67. print('Error deleting VLAN {} from {}'.format(uv.vid, uv.device))
  68. uv.logout()
  69. sys.exit(1)
  70. else:
  71. res = uv.deploy_fabric_vlan()
  72. if not res:
  73. uv.logout()
  74. sys.exit(1)
  75. res = uv.deploy_lan_policy()
  76. if not res:
  77. print('Error deploying LAN Policy changes to {}; removing VLAN from fabric'.format(
  78. uv.device))
  79. uv.delete_fabric_vlan()
  80. uv.logout()
  81. sys.exit(1)
  82. uv.logout()