(redata, jdata, r_server, logger, failback=False)
return True
Edit DNS Records
Edit DNS Records
Edit DNS Records
def edit(redata, jdata, r_server, logger, failback=False):
    ''' Edit DNS Records '''
    usrdata = {'z': redata['data']['domain'],
               'tkn': redata['data']['apikey'],
               'email': redata['data']['email']}
    key = 'cf-replace:' + jdata['cid'] + ':' + redata['id']
    runcount = 0
    if failback is True:
        records = r_server.smembers(key + ":deleted")
        for rec in records:
            rstring = r_server.get(key + ":" + rec)
            if rstring is not None:
                data = json.loads(rstring)
                data['id'] = rec
                result = editRecord(data)
                if result:
                    dkey = key + ":" + rec
                    r_server.delete(dkey)
                    skey = key + ":deleted"
                    r_server.srem(skey, rec)
                    line = "cloudflare-ip-replace: Record %s changed to IP %s" % (
                        data['id'], data['content'])
                    logger.debug(line)
                else:
                    line = "cloudflare-ip-replace: False to change record %s" % data['id']
                    logger.debug(line)
            else:
                skey = key + ":deleted"
                r_server.srem(skey, rec)
                line = "cloudflare-ip-replace: False to change record %s - Not found in Redis, removed from record list" % rec
                logger.debug(line)
            runcount = runcount + 1
    else:
        falsedata, failcount = checkZone(redata['data']['ip'], usrdata)
        line = "cloudflare-ip-replace: Found %d false records" % failcount
        logger.debug(line)
        for rec in falsedata.keys():
            data = usrdata
            for item in falsedata[rec].keys():
                data[item] = falsedata[rec][item]
            rstring = json.dumps(data)
            data['content'] = redata['data']['replaceip']
            data['id'] = rec
            result = editRecord(data)
            if result:
                if redata['data']['failback'] == "automatic":
                    r_server.set(key + ":" + rec, rstring)
                    r_server.sadd(key + ":deleted", rec)
                line = "cloudflare-ip-replace: Record %s changed to IP %s" % (
                    data['id'], data['content'])
                logger.debug(line)
            else:
                line = "cloudflare-ip-replace: False to edit record %s" % data[
                    'id']
                logger.debug(line)
            runcount = runcount + 1
    line = "cloudflare-ip-replace: Actioned %d records" % runcount
    logger.debug(line)
    return True
def edit ( redata , jdata , r_server , logger , failback = False ) : usrdata = { 'z' : redata [ 'data' ] [ 'domain' ] , 'tkn' : redata [ 'data' ] [ 'apikey' ] , 'email' : redata [ 'data' ] [ 'email' ] } key = 'cf-replace:' + jdata [ 'cid' ] + ':' + redata [ 'id' ] runcount = 0 if failback is True : records = r_server . smembers ( key + ":deleted" ) for rec in records : rstring = r_server . get ( key + ":" + rec ) if rstring is not None : data = json . loads ( rstring ) data [ 'id' ] = rec result = editRecord ( data ) if result : dkey = key + ":" + rec r_server . delete ( dkey ) skey = key + ":deleted" r_server . srem ( skey , rec ) line = "cloudflare-ip-replace: Record %s changed to IP %s" % ( data [ 'id' ] , data [ 'content' ] ) logger . debug ( line ) else : line = "cloudflare-ip-replace: False to change record %s" % data [ 'id' ] logger . debug ( line ) else : skey = key + ":deleted" r_server . srem ( skey , rec ) line = "cloudflare-ip-replace: False to change record %s - Not found in Redis, removed from record list" % rec logger . debug ( line ) runcount = runcount + 1 else : falsedata , failcount = checkZone ( redata [ 'data' ] [ 'ip' ] , usrdata ) line = "cloudflare-ip-replace: Found %d false records" % failcount logger . debug ( line ) for rec in falsedata . keys ( ) : data = usrdata for item in falsedata [ rec ] . keys ( ) : data [ item ] = falsedata [ rec ] [ item ] rstring = json . dumps ( data ) data [ 'content' ] = redata [ 'data' ] [ 'replaceip' ] data [ 'id' ] = rec result = editRecord ( data ) if result : if redata [ 'data' ] [ 'failback' ] == "automatic" : r_server . set ( key + ":" + rec , rstring ) r_server . sadd ( key + ":deleted" , rec ) line = "cloudflare-ip-replace: Record %s changed to IP %s" % ( data [ 'id' ] , data [ 'content' ] ) logger . debug ( line ) else : line = "cloudflare-ip-replace: False to edit record %s" % data [ 'id' ] logger . debug ( line ) runcount = runcount + 1 line = "cloudflare-ip-replace: Actioned %d records" % runcount logger . debug ( line ) return True