import json
import sys
import certifi
import urllib3
import time
import pymsteams

from datetime import date, timedelta

sys.path.insert(0, './classes/')
import cohesityAPI as cohesity
import serviceNowAPI as serviceNow

debug=0
failureLimit=1
iteration=0
errorCount = 0

itdTeamsMessage = pymsteams.connectorcard("https://ndgov.webhook.office.com/webhookb2/aad89030-8f69-4a4d-a853-c882cbb01a10@2dea0464-da51-4a88-bae2-b3db94bc0c54/IncomingWebhook/d3c07921419c4920810d0c32558c05e3/edcc1502-1ff0-4c3e-9fe2-1ce3f9f7981a")

try: 
    # Create the API object for Cohesity
    mdn = cohesity.API('itdmdndpc01.nd.gov')
    mdnToken = mdn.GetAuthToken()
    mdn.UpdateHeaders(mdnToken['accessToken'])

    # Create the API object for ServiceNow
    ticketGenerator = serviceNow.API('northdakota.service-now.com')

    # Get all SQL resgistered SQL servers from Cohesity
    vms = mdn.GetFilteredRequest("/public/protectionSources/registrationInfo","?environments=kSQL")

    # Loop through all servers and check for health status issues
    for vm in vms['rootNodes']:
        for check in vm['registrationInfo']['registeredAppsInfo'][0]['hostSettingsCheckResults']:
            if check['resultType'] == "kFail":
                errorCount += 1
                # If/Else statement for Debug only, added iteration limit to prevent debug of multiple servers at once
                if (debug == 1) and (iteration < failureLimit):
                    print("Hostname: " + vm['rootNode']['name'])
                    print("NodeID: " + str(vm['rootNode']['id']))
                    print(check['resultType'] + ": " + check['userMessage'])
                    
                    iteration += 1

                    #Try to refresh the source to see if the error goes away
                    print("Attempting to refresh " + vm['rootNode']['name'])
                    mdn.RefreshSource(vm['rootNode']['id'])
                    time.sleep(15)

                    #Pull the source registration information again and output
                    refreshedData = mdn.GetFilteredRequest("/public/protectionSources/registrationInfo","?ids=" + str(vm['rootNode']['id']))

                    print("Refreshed Data")
                    #print(json.dumps(refreshedData,indent=4))

                    for refreshedVM in refreshedData['rootNodes']:
                        persistantError = 0
                        for refreshedCheck in refreshedVM['registrationInfo']['registeredAppsInfo'][0]['hostSettingsCheckResults']:
                            if (refreshedCheck['resultType']=="kFail"):
                                print("Hostname: " + refreshedVM['rootNode']['name'])
                                print("NodeID: " + str(refreshedVM['rootNode']['id']))
                                print(refreshedCheck['resultType'] + ": " + refreshedCheck['userMessage'])
                                persistantError += 1

                                # Open a service now ticket for any server that has a health issue
                                shortDesc = 'Cohesity: SQL Registration Error for ' + vm['rootNode']['name']
                                description = 'Please assign this ticket to the SQL Admins to invesigate the following issue(s)\n' + check['userMessage']
                                print("Opening Ticket")

                        if persistantError == 0:
                            print("Refreshing the source " + vm['rootNode']['name'] + " resolved the registration error.")

                # Already in a 'failure' if statement, else is only for non-debug operation    
                elif (debug == 0):
                    #Try to refresh the source to see if the error goes away
                    mdn.RefreshSource(vm['rootNode']['id'])
                    time.sleep(15)

                    #Pull the source registration information again and output
                    refreshedData = mdn.GetFilteredRequest("/public/protectionSources/registrationInfo","?ids=" + str(vm['rootNode']['id']))

                    for refreshedVM in refreshedData['rootNodes']:
                        persistantError = 0
                        for refreshedCheck in refreshedVM['registrationInfo']['registeredAppsInfo'][0]['hostSettingsCheckResults']:
                            if (refreshedCheck['resultType']=="kFail"):
                                # Open a service now ticket for any server that has a health issue
                                shortDesc = 'Cohesity: SQL Registration Error for ' + vm['rootNode']['name']
                                description = 'Please assign this ticket to the SQL Admins to invesigate the following issue(s)\n' + check['userMessage']
                                snResponse=ticketGenerator.submitTicket(shortDesc, description)

                                itdTeamsMessage.text("SQL Registration error found. A ServiceNow ticket has been opened to investigate " + vm['rootNode']['name'])
                                itdTeamsMessage.send()
                                
                                # Ticket has been opened, break the loop to prevent opening multiple tickets for the same host.
                                break

                        #End refreshed error/message checks loop
                    #End refreshed VM data loop
                #End debug if/elif block
            # End if failure message found
        # End for loop of all error/warning message checks
    # End for loop of all VMs

    itdTeamsMessage.text("Finished checking Cohesity SQL sources for registration errors, found: " + str(errorCount))
    itdTeamsMessage.send()

except OSError as cohesityError:
    print('Cohesity Error: ' + cohesityError)
