import sys

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

def main():
    
    # Instantiate the ServiceNow API
    svcNowInterface = serviceNow.API('northdakota.service-now.com')

    # Instantiate Cohesity APIs
    mdnCluster = cohesity.API('itdmdndpc01.nd.gov')

    clusters = [mdnCluster]

    replicationFailures = []
    sqlFailures = []
    backupFailures = []
    undetermined = []

    for cluster in clusters:
        cluster.Authenticate()
        
        alertID = 0
        openAlerts = cluster.GetFilteredRequest("/public/alerts","?alertStateList=kOpen&maxAlerts=1000")
        if len(openAlerts) > 1:
            for alert in openAlerts:
                
                if (alert['severity'] != "kInfo"):
                    # Open a unique ticket for every alert

                    if (alert['alertCategory'] == "kRemoteReplication"):
                        replicationFailures.append(alert)
                        continue

                    if (alert['alertCategory'] == "kBackupRestore"):
                        for item in alert['propertyList']:
                            if (item['value'] == "kSQL"):
                                sqlFailures.append(alert) 
                        continue


                    shortDescription = "Backup Failure on " + str(cluster.GetClusterName()) + ": " + alert['alertDocument']['alertName']
                    longDescription = alert['alertDocument']['alertCause']

                    print("Opening incident for Cohesity alert:" + str(alertID))
                    print("\tShort Description: " + shortDescription)
                    print("\tLong Description: " + longDescription)
                alertID += 1
                #End if alert['severity'] != kInfo
            #End for alert in alerts
        # End if len(openAlerts) > 1
    #End for cluster in clusters

    print("Total replication failures: " + str(len(replicationFailures)))
    print("Total SQL Backup failures: " + str(len(sqlFailures)))
#End main()

# Run the program
main()
