Пример #1
0
def searchEvent(token, tsimEventIdentifier, tsimEventValue):
    tsim_event_id = ""
    # Authenticate first.
    # https://docs.bmc.com/docs/TSInfrastructure/113/listing-events-774797826.html
    authToken = token
    url = tsim_url + 'Event/search?routingId=' + tsim_cell + '&routingType=' + tsim_routing
    headers = {
        'content-type': "application/json",
        'cache-control': "no-cache",
        'Authorization': 'authtoken ' + authToken,
    }

    # Payload is the complete event definition in JSON format
    payload = {
        "sortCriteria": [{
            "sortOrder": 1,
            "attributeName": "date_reception"
        }],
        "criteria": {
            "value": tsimEventValue,
            "identifier": tsimEventIdentifier,
            "operator": "EQUALS"
        }
    }

    # Make the call to the API
    if _localDebug:
        logger.debug('HTTP API Url: %s', url)
        logger.debug('HTTP Headers: %s', headers)
        logger.debug('HTTP Payload: %s', payload)

    try:
        response = requests.post(url,
                                 json=payload,
                                 headers=headers,
                                 verify=False)
    except requests.RequestException as e:
        logger.error('HTTP Response Error: %s', e)

    rsc = response.status_code
    if rsc == 501:
        logger.error('HTTP Response Status: %s', rsc)
    elif rsc != 200:
        logger.error('HTTP Response Status: %s', rsc)
    elif rsc == 200:
        rst = response.text
        if _localDebug:
            logger.debug('HTTP Response Text: %s', rst)
        if w3rkstatt.jsonValidator(rst):
            tsim_data = rst
            tsim_event_id = tsimGetEventID(tsim_data)
            if _localDebug:
                logger.debug('TSIM: event search: %s', tsim_event_id)
    else:
        logger.error('TSIM: failed to search the event: %s', response)
    return tsim_event_id
Пример #2
0
def writeInfoFile(file, content):
    fileStatus = False
    fileContent = json.loads(content)
    fileJsonStatus = w3rkstatt.jsonValidator(data=content)

    if fileJsonStatus:
        fileName = file
        filePath = w3rkstatt.concatPath(path=data_folder, folder=fileName)
        fileRsp = w3rkstatt.writeJsonFile(file=filePath, content=fileContent)
        fileStatus = w3rkstatt.getFileStatus(path=filePath)
    else:
        filePath = ""

        if _localQA:
            logger.info('Info File: "%s" ', filePath)

    return filePath
Пример #3
0
def createEvent(token, event_data):
    tsim_event_id = ""
    authToken = token
    url = tsim_url + 'Event/create?routingId=' + tsim_cell + '&routingType=' + tsim_routing
    headers = {
        'content-type': "application/json",
        'cache-control': "no-cache",
        'Authorization': 'authtoken ' + authToken,
    }

    # Payload is the complete event definition in JSON format
    payload = event_data

    # Make the call to the API
    if _localDebug:
        logger.debug('HTTP API Url: %s', url)
        logger.debug('HTTP Headers: %s', headers)
        logger.debug('HTTP Payload: %s', payload)

    try:
        response = requests.post(url,
                                 data=payload,
                                 headers=headers,
                                 verify=False)
    except requests.RequestException as e:
        logger.error('HTTP Response Error: %s', e)

    rsc = response.status_code
    if rsc == 501:
        logger.error('HTTP Response Status: %s', rsc)
    elif rsc != 200:
        logger.error('HTTP Response Status: %s', rsc)
    elif rsc == 200:
        rst = response.text
        if _localDebug:
            logger.debug('HTTP Response Text: %s', rst)
        if w3rkstatt.jsonValidator(rst):
            tsim_data = rst
            tsim_event_id = getEventID(tsim_data)
            if _localDebug:
                logger.info('TSIM: event created: %s', tsim_event_id)
    else:
        logger.error('TSIM: failed to create the event: %s', response)
    return tsim_event_id
Пример #4
0
def getCtmFolder(ctmApiClient, data):
    ctmData = data
    ctmFolderID = w3rkstatt.getJsonValue(path="$.folder_id", data=ctmData)
    ctmFolder = w3rkstatt.getJsonValue(path="$.folder", data=ctmData)
    ctmServer = w3rkstatt.getJsonValue(path="$.ctm", data=ctmData)
    if _localDebugAdv:
        logger.info('CTM Get Job Folder: "%s @ %s"', ctmFolder, ctmServer)
    value = ctm.getCtmDeployedFolder(ctmApiClient=ctmApiClient,
                                     ctmServer=ctmServer,
                                     ctmFolder=ctmFolder)

    if w3rkstatt.jsonValidator(data=value):
        ctmfolderInfo = w3rkstatt.dTranslate4Json(data=value)

    # adjust if CTM API access failed
    sJobLogStatus = True
    # Failed to get
    if "Failed to get" in str(value):
        sJobLogStatus = False

    i = 0
    if "." in value:
        xTemp = value.split(".")
        for xLine in xTemp:
            zValue = xLine.strip()
            # construct json string
            if i == 0:
                sEntry = '"entry-' + str(i).zfill(4) + '":"' + zValue + '"'
            else:
                sEntry = sEntry + ',"entry-' + str(i).zfill(
                    4) + '":"' + zValue + '"'
            i += 1
    else:
        sEntry = '"entry-0000": "' + value + '"'

    jData = '{"count":' + str(i) + ',"status":' + str(
        sJobLogStatus) + ',"entries":[{' + str(sEntry) + '}]}'
    sData = w3rkstatt.dTranslate4Json(data=jData)

    return sData
Пример #5
0
def writeAlertFile(data, alert, type="job"):
    fileStatus = False
    ctmJobData = data
    if _ctmActiveApi:
        fileType = "ctm-enriched-" + type + "-"
    else:
        fileType = "ctm-basic-" + type + "-"

    fileContent = json.loads(ctmJobData)
    fileJsonStatus = w3rkstatt.jsonValidator(data=ctmJobData)

    if fileJsonStatus:
        fileName = fileType + alert.zfill(8) + "-" + str(epoch).replace(
            ".", "") + ".json"
        filePath = w3rkstatt.concatPath(path=data_folder, folder=fileName)
        fileRsp = w3rkstatt.writeJsonFile(file=filePath, content=fileContent)
        fileStatus = w3rkstatt.getFileStatus(path=filePath)

        if _localDebug:
            logger.info('CTM QA Alert File: "%s" ', filePath)

    return fileStatus
Пример #6
0
def prepareEmail(eml_from, eml_from_name, eml_to, eml_subbject, eml_message, eml_data="",eml_logo_message="",eml_template=""):
    '''
    Prepare e-mail content in HTML format 

    :param str eml_from: e-mail sendder
    :param str eml_from_name: e-mail sendder display name
    :param str eml_to: e-mail recipient
    :param str eml_message: e-mail message
    :param str eml_subbject: e-mail subject line
    :param json eml_data: e-mail data, will be converted to HTML table
    :param str eml_logo_message: message above custom logog
    :param str eml_tempalte: e-mail html template name
    :return: data
    :rtype: dict
    :raises ValueError: N/A
    :raises TypeError: N/A    
    ''' 

    email_from = eml_from
    email_rcpt = eml_to
    email_subject = eml_subbject
    email_message = eml_message
    email_data_text = eml_data

    # transform message data
    if email_data_text is not None:
        email_data_text = str(email_data_text)
        email_data = w3rkstatt.jsonTranslateValuesAdv(data=email_data_text)    
        email_data_status = w3rkstatt.jsonValidator(data=email_data)
        if email_data_status:
            email_data_tbl  = json2html.convert(json = email_data )
        else:
            email_data_tbl  = email_data
    else:
        email_data_tbl  = ""

    # ToDO: format message
    email_msg_tbl = email_message

    # HTML message above logo
    if eml_logo_message is None:
        email_logo_txt = eml_logo_message
    else:
        email_logo_txt = "This message was sent by Orchestrator."

    if eml_template is None:
        email_template = w3rkstatt.readHtmlFile(file=template_file)
    else:
        email_template = eml_template

    if eml_from_name is not None:
        email_from = formataddr((str(Header(eml_from_name, 'utf-8')), email_from))

    if (eml_from_name is None) and (email_from is None):
        email_from = formataddr((str(Header(smtp_user_name, 'utf-8')), smtp_user))

    if (eml_from_name is None) and (email_from is smtp_user):
        email_from = formataddr((str(Header(smtp_user_name, 'utf-8')), smtp_user))

    # smtp_user_name

    # replace HTML content
    email_html_00 = email_template
    email_html_01 = email_html_00.replace("$$EMAIL_LOGO_TEXT$$",email_logo_txt)
    email_html_02 = email_html_01.replace("$$EMAIL_MESSAGE$$",email_msg_tbl)
    email_html_03 = email_html_02.replace("$$EMAIL_DATA$$",email_data_tbl)
    email_html_04 = email_html_03.replace("$$EMAIL_UUID$$",sUuid)
    email_new_html = email_html_04
    
    # For Text E-Mail
    email_format_text = """ Simple E-Mail Body"""

    email_message = MIMEMultipart("alternative")
    email_message["Subject"] = email_subject
    email_message["From"] = email_from
    email_message["To"] = email_rcpt

    email_part_text = MIMEText(email_format_text, "plain")
    email_part_html = MIMEText(email_new_html, "html")

    # Add HTML/plain-text parts to MIMEMultipart message
    # The email client will try to render the last part first
    email_message.attach(email_part_text)
    email_message.attach(email_part_html)

    return email_message