(redata, jdata, logger)
Perform Heroku Actions
Perform Heroku Actions
Perform Heroku Actions
def callAction(redata, jdata, logger):
    ''' Perform Heroku Actions '''
    # Ready API Request
    # Generate Base64 encoded API Key
    basekey = base64.b64encode(":" + redata['data']['apikey'])
    # Create headers for API call
    headers = {
        "Accept" : "application/vnd.heroku+json; version=3",
        "Authorization" : basekey
    }
    # Set Timeout value for API Call
    timeout = 5.00
    # URL to request
    url = "https://api.heroku.com/apps/" + redata['data']['appname'] + "/dynos"
    # Setting payload of user input from /dashboard/reactions/heroku-create-dyno
    payload = {
        'command' : redata['data']['cmd'],
        'size' : redata['data']['size']
    }
    # Heroku accepts boolean but db is string
    if redata['data']['attach'] == "true":
        payload['attach'] = True
    else:
        payload['attach'] = False
    # Convert payload to json
    json_payload = json.dumps(payload)

    # Perform API Call
    try:
        result = requests.post(url, timeout=timeout, headers=headers, data=json_payload, verify=True)
    except:
        # If fail return False to mark reaction false
        return False
        line = "heroku-create-dyno: Could not connect to Heroku for monitor %s" % jdata['cid']
        logger.info(line)

    # Process results
    # Log heroku results for troubleshooting later
    line = "heroku-create-dyno: Got status code reply %d from Heroku for monitor %s" % (result.status_code, jdata['cid'])
    logger.debug(line)
    # Verify we got a 201 and set as success, or set as failure
    if result.status_code == 201:
        return True
    else:
        line = "heroku-create-dyno: Monitor %s got %s form Heroku after making a request to %s" % (jdata['cid'], result.text, url)
        logger.debug(line)
        return False
def callAction ( redata , jdata , logger ) : # Ready API Request # Generate Base64 encoded API Key basekey = base64 . b64encode ( ":" + redata [ 'data' ] [ 'apikey' ] ) # Create headers for API call headers = { "Accept" : "application/vnd.heroku+json; version=3" , "Authorization" : basekey } # Set Timeout value for API Call timeout = 5.00 # URL to request url = "https://api.heroku.com/apps/" + redata [ 'data' ] [ 'appname' ] + "/dynos" # Setting payload of user input from /dashboard/reactions/heroku-create-dyno payload = { 'command' : redata [ 'data' ] [ 'cmd' ] , 'size' : redata [ 'data' ] [ 'size' ] } # Heroku accepts boolean but db is string if redata [ 'data' ] [ 'attach' ] == "true" : payload [ 'attach' ] = True else : payload [ 'attach' ] = False # Convert payload to json json_payload = json . dumps ( payload ) # Perform API Call try : result = requests . post ( url , timeout = timeout , headers = headers , data = json_payload , verify = True ) except : # If fail return False to mark reaction false return False line = "heroku-create-dyno: Could not connect to Heroku for monitor %s" % jdata [ 'cid' ] logger . info ( line ) # Process results # Log heroku results for troubleshooting later line = "heroku-create-dyno: Got status code reply %d from Heroku for monitor %s" % ( result . status_code , jdata [ 'cid' ] ) logger . debug ( line ) # Verify we got a 201 and set as success, or set as failure if result . status_code == 201 : return True else : line = "heroku-create-dyno: Monitor %s got %s form Heroku after making a request to %s" % ( jdata [ 'cid' ] , result . text , url ) logger . debug ( line ) return False