# Copyright 2019 - Gerard O'Rourke - Purplepi.ie # Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated # documentation files (the "Software"), to deal in the Software without restriction, including without limitation # the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to # permit persons to whom the Software is furnished to do so, subject to the following conditions: # The above copyright notice and this permission notice shall be included in all copies or substantial portions of the # Software. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR # COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR # OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. # Import XML Parser Module import xml.etree.cElementTree as ET # import HTTP Module import requests # Disable SSL warning from requests.packages.urllib3.exceptions import InsecureRequestWarning requests.packages.urllib3.disable_warnings(InsecureRequestWarning) # Default Variables to use to import XML file and to connect to CCE API CCEHost = "ucce-hds-a.lab2.purplepi.ie" user = "admin@lab2.purplepi.ie" password = "myPa$$w0rd" xmlfile = "ReasonCodes_NOT_READY_20191220.xml" def press_y_to_continue(): userinput = input("\nEnter 'y' if you want to continue?") if (userinput != "y"): print("User chose not to continue. Exiting now...") exit() def WrapReasonLoop(): category = "WRAPUP" code = "" for elem in tree.findall(".//WrapUpReason/*"): if (elem.tag == "label"): label = elem.text if (elem.tag == "forAll"): forAll = elem.text CreateReasonLabel(category, code, label, forAll) def ReasonCodeLoop(): for elem in tree.findall(".//ReasonCode/*"): if (elem.tag == "category"): category = elem.text # print("category:", category) if (elem.tag == "code"): code = elem.text # print("code", code) if (elem.tag == "label"): label = elem.text # print("label", elem.text) if (elem.tag == "forAll"): forAll = elem.text # print("forAll", elem.text) CreateReasonLabel(category, code, label, forAll) def CreateReasonLabel(category, code, label, forAll): headers = {'Content-type': 'application/xml'} wrapReasonXML = "0" + code + "" + forAll + "" + label + "" + category + "User-defined " print("XML:", wrapReasonXML) try: response = requests.post(urlReasonLabel, auth=(user, password), verify=False, data=wrapReasonXML, headers=headers) except Exception as e: print(type(e)) print(e) exit() if (response.status_code != 201): print( "ERROR: Unable to create Reason Type: " + category + ", Reason Label: " + label + ". HTTP Status Code: " + str( response.status_code)) print("\nError response Body:") print(response.text) if (response.status_code == 400): press_y_to_continue() if (response.status_code != 400): exit() if (response.status_code == 201): print("Successfully Created Reason Type: " + category + ", Reason Label: " + label) # Main Application print("-----------------------------------------------------------------------") print("CCE Bulk Reason Label Script") print("-----------------------------------------------------------------------") userinput = input("\nEnter the hostname of the CCE Admin Server or press enter to use the default [" + CCEHost + "]?") if (userinput != ""): CCEHost = userinput userinput = input("Enter user of the CCE Admin API or press enter to use the default [" + user + "]?") if (userinput != ""): user = userinput userinput = input("Enter password of the CCE Admin API or press enter to use the default [******]?") if (userinput != ""): password = userinput userinput = input("Enter the XML input filename or press enter to use the default [" + xmlfile + "]?") if (userinput != ""): xmlfile = userinput urlReasonLabel = "https://" + CCEHost + "/unifiedconfig/config/reasoncode" print("-----------------------------------------------------------------------") print("CCE Hostname:", CCEHost) print("CCE user:", user) print("XML File:", xmlfile) print("urlReasonLabel:",urlReasonLabel) print("-----------------------------------------------------------------------") # Import Finesse 11.X Wrap Code XML file # tree = ET.ElementTree(file="C:/Users/gerard/PycharmProjects/ReasonLabels/WrapUpReasons_20191220.xml") try: tree = ET.ElementTree(file=xmlfile) except Exception as e: print(type(e)) print(e) exit() root = tree.getroot() count_wraps = len(tree.findall(".//WrapUpReason/label")) # If input file is a WrapReason File if (count_wraps > 0): print("Input File recognized as a Wrap-up Reasons File!!!") print("Number of Wrap-up Labels:", count_wraps) print("-----------------------------------------------------------------------") print("About to create " + str(count_wraps) + " wrapup reason labels...") press_y_to_continue() WrapReasonLoop() print("Finished!") exit() if (root.tag == "ReasonCodes"): count_reasoncodes = len(tree.findall(".//ReasonCode/label")) if (root.attrib.get('category') == "NOT_READY"): print("Input File recognized as a NOT_READY ReasonCode XML File!!!") if (root.attrib.get('category') == "LOGOUT"): print("Input File recognized as a LOGOUT ReasonCode XML File!!!") if (root.attrib.get('category') == "ALL"): print("Input File recognized as a ALL ReasonCode XML File!!!") if ((root.attrib.get('category') == "NOT_READY") or (root.attrib.get('category') == "LOGOUT") or ( root.attrib.get('category') == "ALL")): print("Number of Reason Codes :", count_reasoncodes) print("-----------------------------------------------------------------------") print("About to create " + str(count_reasoncodes) + " reason codes...") press_y_to_continue() ReasonCodeLoop() print("Finished!") exit() print("Input File NOT recognized! Exiting...") exit() print("Not a valid Input file. Exiting now! ") exit()