Browse Source

Fully modernize to Python 3.

Joe Clarke 4 years ago
parent
commit
766e06891c

+ 14 - 18
automation/services/clean_reservation.py

@@ -24,6 +24,8 @@
 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 # SUCH DAMAGE.
 
+from __future__ import print_function
+from builtins import input
 import json
 import requests
 from requests.packages.urllib3.exceptions import InsecureRequestWarning
@@ -35,37 +37,31 @@ from netaddr import IPAddress
 import CLEUCreds
 from cleu.config import Config as C
 
-DHCP_BASE = C.DHCP_BASE + 'Reservation'
+DHCP_BASE = C.DHCP_BASE + "Reservation"
 
-HEADERS = {
-    'authorization': CLEUCreds.JCLARKE_BASIC,
-    'accept': 'application/json',
-    'content-type': 'application/json'
-}
+HEADERS = {"authorization": CLEUCreds.JCLARKE_BASIC, "accept": "application/json", "content-type": "application/json"}
 
-if __name__ == '__main__':
+if __name__ == "__main__":
     url = DHCP_BASE
-    ans = input('Are you sure you want to clean reservations (y/N): ')
-    if not re.search(r'^[yY]', ans):
-        print('Exiting...')
+    ans = eval(input("Are you sure you want to clean reservations (y/N): "))
+    if not re.search(r"^[yY]", ans):
+        print("Exiting...")
         sys.exit(0)
     try:
-        response = requests.request('GET', url, headers=HEADERS, verify=False)
+        response = requests.request("GET", url, headers=HEADERS, verify=False)
         response.raise_for_status()
     except Exception as e:
-        sys.stderr.write('Failed to get list of reservations: {}\n'.format(e))
+        sys.stderr.write("Failed to get list of reservations: {}\n".format(e))
         sys.exit(1)
 
     reservations = response.json()
     for lease in reservations:
-        url = '{}/{}'.format(DHCP_BASE, lease['ipaddr'])
+        url = "{}/{}".format(DHCP_BASE, lease["ipaddr"])
         try:
-            response = requests.request(
-                'DELETE', url, headers=HEADERS, verify=False)
+            response = requests.request("DELETE", url, headers=HEADERS, verify=False)
             response.raise_for_status()
         except Exception as e:
-            sys.stderr.write(
-                'Error deleting reservation for {}: {}\n'.format(lease['ipaddr'], e))
+            sys.stderr.write("Error deleting reservation for {}: {}\n".format(lease["ipaddr"], e))
             continue
 
-        print('Deleted reservation for {}.'.format(lease['ipaddr']))
+        print("Deleted reservation for {}.".format(lease["ipaddr"]))

+ 1 - 1
automation/services/dhcp-hook.py

@@ -358,7 +358,7 @@ def print_dnac(spark, what, details, msg):
             hinfo += " (reason: _{}_)".format(ohealth["reason"])
         if len(healths) > 0:
             hinfo += " ["
-            for h, hobj in healths.items():
+            for h, hobj in list(healths.items()):
                 hinfo += "{} health: {} ".format(h, hobj["score"])
                 if hobj["reason"] != "":
                     hinfo += "(reason: {}) ".format(hobj["reason"])

+ 5 - 3
automation/services/remove_scopes.py

@@ -24,6 +24,8 @@
 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 # SUCH DAMAGE.
 
+from __future__ import print_function
+from builtins import input
 import sys
 import json
 from subprocess import Popen, PIPE
@@ -37,10 +39,10 @@ if __name__ == '__main__':
     ans = None
     if len(sys.argv) == 2:
         match = sys.argv[1]
-        ans = input(
-            'Really delete all scopes that match "{}" (y/N): '.format(match))
+        ans = eval(input(
+            'Really delete all scopes that match "{}" (y/N): '.format(match)))
     else:
-        ans = input('Really delete all scopes (y/N): ')
+        ans = eval(input('Really delete all scopes (y/N): '))
 
     if not re.search(r'^[yY]', ans):
         print('Exiting...')

+ 37 - 17
automation/spark/copy_users.py

@@ -1,5 +1,30 @@
 #!/usr/bin/env python3
+#
+# Copyright (c) 2017-2020  Joe Clarke <jclarke@cisco.com>
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+# 1. Redistributions of source code must retain the above copyright
+#    notice, this list of conditions and the following disclaimer.
+# 2. Redistributions in binary form must reproduce the above copyright
+#    notice, this list of conditions and the following disclaimer in the
+#    documentation and/or other materials provided with the distribution.
+#
+# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+# SUCH DAMAGE.
 
+from __future__ import print_function
 import sys
 import json
 from argparse import ArgumentParser
@@ -7,18 +32,13 @@ from sparker import Sparker, ResourceType
 
 
 def main():
-    parser = ArgumentParser(description='Usage: ')
-
-    parser.add_argument('-S', '--source-team', type=str,
-                        help='Name of the source Team of the Room')
-    parser.add_argument('-s', '--source-room', type=str,
-                        help='Name of the source Room')
-    parser.add_argument('-D', '--dest-team', type=str,
-                        help='Name of the destination Team of the Room')
-    parser.add_argument('-d', '--dest-room', type=str,
-                        help='Name of the destination Room')
-    parser.add_argument('-t', '--token', type=str,
-                        help='Webex Teams Token', required=True)
+    parser = ArgumentParser(description="Usage: ")
+
+    parser.add_argument("-S", "--source-team", type=str, help="Name of the source Team of the Room")
+    parser.add_argument("-s", "--source-room", type=str, help="Name of the source Room")
+    parser.add_argument("-D", "--dest-team", type=str, help="Name of the destination Team of the Room")
+    parser.add_argument("-d", "--dest-room", type=str, help="Name of the destination Room")
+    parser.add_argument("-t", "--token", type=str, help="Webex Teams Token", required=True)
     args = parser.parse_args()
 
     spark = Sparker(token=args.token)
@@ -31,12 +51,12 @@ def main():
         resource = args.source_room
         type = ResourceType.ROOM
     else:
-        print('ERROR: Either a source Room or source Team must be specified')
+        print("ERROR: Either a source Room or source Team must be specified")
         sys.exit(1)
 
     members = spark.get_members(resource, type)
     if not members:
-        print('ERROR: Failed to get members')
+        print("ERROR: Failed to get members")
         sys.exit(1)
 
     if args.dest_team:
@@ -46,13 +66,13 @@ def main():
         resource = args.dest_room
         type = ResourceType.ROOM
     else:
-        print('ERROR: Either a destination Room or destination Team must be specified')
+        print("ERROR: Either a destination Room or destination Team must be specified")
         sys.exit(1)
 
     if not spark.add_members(members, resource, type):
-        print('ERROR: Failed to add one or more members')
+        print("ERROR: Failed to add one or more members")
         sys.exit(1)
 
 
-if __name__ == '__main__':
+if __name__ == "__main__":
     main()

+ 2 - 1
automation/spark/sparker.py

@@ -23,6 +23,7 @@
 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 # SUCH DAMAGE.
 
+from builtins import object
 import requests
 from requests_toolbelt import MultipartEncoder
 from io import BytesIO
@@ -45,7 +46,7 @@ class MessageType(Enum):
     NEUTRAL = ""
 
 
-class Sparker:
+class Sparker(object):
     SPARK_API = "https://api.ciscospark.com/v1/"
 
     RETRIES = 5