File: example_account_rules_lists_items.py

package info (click to toggle)
python-cloudflare 2.20.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,048 kB
  • sloc: python: 6,932; makefile: 138; sh: 76
file content (88 lines) | stat: -rwxr-xr-x 2,780 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/env python3
"""Cloudflare API code - example"""

import os
import sys
import time

# sys.path.insert(0, os.path.abspath('..'))
import CloudFlare

def main():

    # Code up ...
    # cli4 /accounts/::00000000000000000000000000000000/rules/lists/::00000000000000000000000000000000/items

    try:
        account_id = sys.argv[1]
        list_id = sys.argv[2]
    except IndexError:
        exit('usage: example_account_rules_lists_items.py account_id list_id')

    with CloudFlare.CloudFlare() as cf:

        #
        # Print existing list - showing GET function
        #
        print('EXISTING LIST LOOKS LIKE:')
        items = cf.accounts.rules.lists.items(account_id, list_id)
        for item in items:
            print('%s %s %s %-30s ; %s' % (item['id'], item['created_on'], item['modified_on'], item['ip'], item['comment']))
        print('')

        #
        # Add an element to list - showing POST function
        #
        new_ip_address = '4.4.4.4'
        new_ip_comment = 'all the fours!'
        new_ip_id = None

        print('ADD TO LIST:')
        new_r = cf.accounts.rules.lists.items.post(account_id, list_id, data=[{'ip':new_ip_address,'comment':new_ip_comment}])
        print('new_r = %s' % (new_r))
        print('')

        #
        # So it seems that it takes a while for the database to update; this is delay is a hack
        #
        time.sleep(1)

        #
        # Print the full list again - to show POST worked
        #
        print('NEW LIST LOOKS LIKE:')
        items = cf.accounts.rules.lists.items(account_id, list_id)
        for item in items:
            print('%s %s %s %-30s ; %s' % (item['id'], item['created_on'], item['modified_on'], item['ip'], item['comment']))
            if item['ip'] == new_ip_address:
                new_ip_id = item['id']
        print('')

        #
        # Now remove that element - to show DELETE function (note the use of new_ip_id value
        #
        print('DELETE FROM LIST:')
        if new_ip_id is None:
            exit('    --- NOTHING TO DELETE')
        del_r = cf.accounts.rules.lists.items.delete(account_id, list_id, data={'items':[{'id':new_ip_id}]})
        print('del_r = %s' % (del_r))
        print('')

        #
        # So it seems that it takes a while for the database to update; this is delay is a hack
        #
        time.sleep(1)

        #
        # Print the full list again - to show DELETE worked
        #
        print('FINAL LIST LOOKS LIKE:')
        items = cf.accounts.rules.lists.items(account_id, list_id)
        for item in items:
            print('%s %s %s %-30s ; %s' % (item['id'], item['created_on'], item['modified_on'], item['ip'], item['comment']))
        print('')

    exit(0)

if __name__ == '__main__':
    main()