(redata, jdata, logger)
Perform Heroku Actions
Perform Heroku Actions
Perform Heroku Actions
def callAction(redata, jdata, logger):
    ''' Perform Heroku Actions '''
    # Ready API Request Data
    # 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'] + "/formation"

    # Perform API Call to get list of formations
    try:
        result = requests.get(url, timeout=timeout, headers=headers, verify=True)
    except:
        # If fail return False to mark reaction false
        return False
        line = "heroku-scale-in: Could not connect to Heroku for reaction %s" % redata['id']
        logger.info(line)
    # Log heroku results for troubleshooting later
    line = "heroku-scale-in: Got status code reply %d from Heroku for reaction %s" % (result.status_code, redata['id'])
    logger.debug(line)

    # if we get a good return
    if result.status_code == 200:
        retdata = json.loads(result.text)
        current_size = None
        current_quantity = None
        for formation in retdata:
            if formation['type'] == redata['data']['dyno_type']:
                current_quantity = int(formation['quantity'])
                # Set current size to a number value for comparison on scale ups
                if formation['size'] == "1X":
                    current_size = 1
                elif formation['size'] == "2X":
                    current_size = 2
                else:
                    current_size = 3

        run = False
        # If the current quantity is greater than min_quantity let's scale it!
        if current_quantity > int(redata['data']['min_quantity']):
            new_quantity = current_quantity - 1
            run = True

        if run == True:
            # Create new payload for scaling and url
            payload = { 'quantity' : new_quantity }
            json_payload = json.dumps(payload)
            url = url + "/" + redata['data']['dyno_type']
            # make request to scale
            try:
                result = requests.patch(url, timeout=timeout, headers=headers, data=json_payload, verify=True)
            except:
                # errors
                return False
                line = "heroku-scale-in: Could not connect to Heroku for reaction %s" % redata['id']
                logger.info(line)
            # Process results
            # Log heroku results for troubleshooting later
            line = "heroku-scale-in: Got status code reply %d from Heroku for reaction %s" % (result.status_code, redata['id'])
            logger.debug(line)
            # Verify we got a 200 and set as success, or set as failure
            if result.status_code == 200:
                return True
            else:
                line = "heroku-scale-in: Reaction %s got %s from Heroku after making a request to %s" % (redata['id'], result.text, url)
                logger.debug(line)
                return False
        else:
            line = "heroku-scale-in: Reaction %s is already scaled %d to the min quantity %d" % (redata['id'], current_quantity, int(redata['data']['min_quantity']))
            logger.debug(line)
            return None
    else:
        line = "heroku-scale-in: Reaction %s got %s from Heroku after making a request to %s" % (redata['id'], result.text, url)
        logger.debug(line)
        return False
def callAction ( redata , jdata , logger ) : # Ready API Request Data # 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' ] + "/formation" # Perform API Call to get list of formations try : result = requests . get ( url , timeout = timeout , headers = headers , verify = True ) except : # If fail return False to mark reaction false return False line = "heroku-scale-in: Could not connect to Heroku for reaction %s" % redata [ 'id' ] logger . info ( line ) # Log heroku results for troubleshooting later line = "heroku-scale-in: Got status code reply %d from Heroku for reaction %s" % ( result . status_code , redata [ 'id' ] ) logger . debug ( line ) # if we get a good return if result . status_code == 200 : retdata = json . loads ( result . text ) current_size = None current_quantity = None for formation in retdata : if formation [ 'type' ] == redata [ 'data' ] [ 'dyno_type' ] : current_quantity = int ( formation [ 'quantity' ] ) # Set current size to a number value for comparison on scale ups if formation [ 'size' ] == "1X" : current_size = 1 elif formation [ 'size' ] == "2X" : current_size = 2 else : current_size = 3 run = False # If the current quantity is greater than min_quantity let's scale it! if current_quantity > int ( redata [ 'data' ] [ 'min_quantity' ] ) : new_quantity = current_quantity - 1 run = True if run == True : # Create new payload for scaling and url payload = { 'quantity' : new_quantity } json_payload = json . dumps ( payload ) url = url + "/" + redata [ 'data' ] [ 'dyno_type' ] # make request to scale try : result = requests . patch ( url , timeout = timeout , headers = headers , data = json_payload , verify = True ) except : # errors return False line = "heroku-scale-in: Could not connect to Heroku for reaction %s" % redata [ 'id' ] logger . info ( line ) # Process results # Log heroku results for troubleshooting later line = "heroku-scale-in: Got status code reply %d from Heroku for reaction %s" % ( result . status_code , redata [ 'id' ] ) logger . debug ( line ) # Verify we got a 200 and set as success, or set as failure if result . status_code == 200 : return True else : line = "heroku-scale-in: Reaction %s got %s from Heroku after making a request to %s" % ( redata [ 'id' ] , result . text , url ) logger . debug ( line ) return False else : line = "heroku-scale-in: Reaction %s is already scaled %d to the min quantity %d" % ( redata [ 'id' ] , current_quantity , int ( redata [ 'data' ] [ 'min_quantity' ] ) ) logger . debug ( line ) return None else : line = "heroku-scale-in: Reaction %s got %s from Heroku after making a request to %s" % ( redata [ 'id' ] , result . text , url ) logger . debug ( line ) return False