|
| 1 | +#!/usr/bin/env python |
| 2 | +"""Cloudflare API code - example""" |
| 3 | + |
| 4 | +import os |
| 5 | +import sys |
| 6 | +import time |
| 7 | + |
| 8 | +# sys.path.insert(0, os.path.abspath('..')) |
| 9 | +import CloudFlare |
| 10 | + |
| 11 | +def main(): |
| 12 | + |
| 13 | + # Code up ... |
| 14 | + # cli4 /accounts/::00000000000000000000000000000000/rules/lists/::00000000000000000000000000000000/items |
| 15 | + |
| 16 | + try: |
| 17 | + account_id = sys.argv[1] |
| 18 | + list_id = sys.argv[2] |
| 19 | + except IndexError: |
| 20 | + exit('usage: example_account_rules_lists_items.py account_id list_id') |
| 21 | + |
| 22 | + with CloudFlare.CloudFlare() as cf: |
| 23 | + |
| 24 | + # |
| 25 | + # Print existing list - showing GET function |
| 26 | + # |
| 27 | + print('EXISTING LIST LOOKS LIKE:') |
| 28 | + items = cf.accounts.rules.lists.items(account_id, list_id) |
| 29 | + for item in items: |
| 30 | + print('%s %s %s %-30s ; %s' % (item['id'], item['created_on'], item['modified_on'], item['ip'], item['comment'])) |
| 31 | + print('') |
| 32 | + |
| 33 | + |
| 34 | + # |
| 35 | + # Add an element to list - showing POST function |
| 36 | + # |
| 37 | + new_ip_address = '4.4.4.4' |
| 38 | + new_ip_comment = 'all the fours!' |
| 39 | + new_ip_id = None |
| 40 | + |
| 41 | + print('ADD TO LIST:') |
| 42 | + new_r = cf.accounts.rules.lists.items.post(account_id, list_id, data=[{'ip':new_ip_address,'comment':new_ip_comment}]) |
| 43 | + print('new_r = %s' % (new_r)) |
| 44 | + print('') |
| 45 | + |
| 46 | + # |
| 47 | + # So it seems that it takes a while for the database to update; this is delay is a hack |
| 48 | + # |
| 49 | + time.sleep(1) |
| 50 | + |
| 51 | + |
| 52 | + # |
| 53 | + # Print the full list again - to show POST worked |
| 54 | + # |
| 55 | + print('NEW LIST LOOKS LIKE:') |
| 56 | + items = cf.accounts.rules.lists.items(account_id, list_id) |
| 57 | + for item in items: |
| 58 | + print('%s %s %s %-30s ; %s' % (item['id'], item['created_on'], item['modified_on'], item['ip'], item['comment'])) |
| 59 | + if item['ip'] == new_ip_address: |
| 60 | + new_ip_id = item['id'] |
| 61 | + print('') |
| 62 | + |
| 63 | + |
| 64 | + # |
| 65 | + # Now remove that element - to show DELETE function (note the use of new_ip_id value |
| 66 | + # |
| 67 | + print('DELETE FROM LIST:') |
| 68 | + if new_ip_id is None: |
| 69 | + exit(' --- NOTHING TO DELETE') |
| 70 | + del_r = cf.accounts.rules.lists.items.delete(account_id, list_id, data={'items':[{'id':new_ip_id}]}) |
| 71 | + print('del_r = %s' % (del_r)) |
| 72 | + print('') |
| 73 | + |
| 74 | + # |
| 75 | + # So it seems that it takes a while for the database to update; this is delay is a hack |
| 76 | + # |
| 77 | + time.sleep(1) |
| 78 | + |
| 79 | + # |
| 80 | + # Print the full list again - to show DELETE worked |
| 81 | + # |
| 82 | + print('FINAL LIST LOOKS LIKE:') |
| 83 | + items = cf.accounts.rules.lists.items(account_id, list_id) |
| 84 | + for item in items: |
| 85 | + print('%s %s %s %-30s ; %s' % (item['id'], item['created_on'], item['modified_on'], item['ip'], item['comment'])) |
| 86 | + print('') |
| 87 | + |
| 88 | + exit(0) |
| 89 | + |
| 90 | +if __name__ == '__main__': |
| 91 | + main() |
| 92 | + |
0 commit comments