This is an example of how to use the Memset Firewalling API with Python and the standard module xmlrpclib for XML-RPC client access.
Substitute API_KEY_HEX with a valid API key and SERVER_NAME with a valid server name and it can be run with a Python 2 interpreter.
#!/usr/bin/env python
"""
Memset firewalling API example with Python.
"""
uri = "https://API_KEY_HEX:@api.memset.com/v1/xmlrpc/"
server_name = 'SERVER_NAME'
from xmlrpclib import ServerProxy
from pprint import pprint
def main():
s = ServerProxy(uri)
# create a rule group with some rules
rule_group_nickname = "test-rule-group"
rules = [
{'action': 'ACCEPT',
'dest_ports': '80,8000,8080',
'protocols': 'tcp',
'ordering': 1,
'comment': 'Extended HTTP traffic',
},
{'action': 'REJECT',
'ip_version': 'ipv4',
'source_ips': '222.111.111.10/24',
'protocols': 'tcp,udp',
'ordering': 2,
'comment': 'Dossers',
},
]
r = s.firewalling.rule_group_create(dict(nickname=rule_group_nickname, notes="Standard firewall rules for HTTP traffic", rules=rules))
rule_group_name = r['name']
pprint(r)
# Apply this firewall rule group
r = s.firewalling.update(dict(name=server_name, rule_group_name=rule_group_name))
pprint(r)
# add another rule to the rule group
rule = {'action': 'ACCEPT',
'dest_ports': '9999',
'protocols': 'udp',
'ordering': 3,
'comment': 'Video conferencing',
}
r = s.firewalling.rule_create(dict(rule_group_name=rule_group_name, **rule))
rule_id = r['rule_id']
pprint(r)
# update this firewall rule
r = s.firewalling.rule_update(dict(rule_id=rule_id, ip_version='ipv4', dest_ports='9000,9001,9999', comment='IPV4 Video Conferencing'))
pprint(r)
# switch back to one of the default public rules
r = s.firewalling.update(dict(name=server_name, rule_group_name="managed-linux"))
pprint(r)
# clean up
r = s.firewalling.rule_group_delete(dict(rule_group_name=rule_group_name))
pprint(r)
# 'clone' the managed-linux public group
r = s.firewalling.rule_group_info(dict(rule_group_name='managed-linux'))
cloned_rules = []
for rule_id, rule_def in r['rules'].items():
del(rule_def['rule_group_name'])
cloned_rules.append(rule_def)
cloned_group_nickname = 'cloned-group'
r = s.firewalling.rule_group_create(dict(nickname=cloned_group_nickname, notes='Test to clone a group', rules=cloned_rules))
cloned_group_name = r['name']
pprint(r)
# clean up
r = s.firewalling.rule_group_delete(dict(rule_group_name=cloned_group_name))
pprint(r)
if __name__ == "__main__":
main()