|
| 1 | +The code examples use the following: |
| 2 | + |
| 3 | +```python |
| 4 | +import upcloud |
| 5 | +from upcloud import FirewallRule |
| 6 | + |
| 7 | +manager = upcloud.CloudManager("username", "password") |
| 8 | +``` |
| 9 | + |
| 10 | +# About |
| 11 | + |
| 12 | +Firewall is configured with FirewallRule objects that are specific to each server. |
| 13 | +Please note that a servers firewall rules are ignored if firewall is turned off |
| 14 | +(see [Server](/server) and [API documentation](https://www.upcloud.com/api/7-servers/#modify-server)). |
| 15 | + |
| 16 | +If a server is removed, its firewall and thus its firewall rules are removed too. |
| 17 | + |
| 18 | +Please refer to the [API documentation](https://www.upcloud.com/api/10-firewall/#create-firewall-rule) |
| 19 | +for more info on the attributes of FirewallRule. |
| 20 | + |
| 21 | +## List / Get |
| 22 | + |
| 23 | +```python |
| 24 | +server = manager.get_servers()[0] |
| 25 | + |
| 26 | +# all firewall rules |
| 27 | +firewall_rules = server.get_firewall_rules() |
| 28 | +``` |
| 29 | + |
| 30 | +## Create |
| 31 | + |
| 32 | +```python |
| 33 | +server = manager.get_servers()[0] |
| 34 | + |
| 35 | +rule = server.add_firewall_rule( |
| 36 | + FirewallRule( |
| 37 | + position = "1", |
| 38 | + direction = "in", |
| 39 | + family = "IPv4", |
| 40 | + protocol = "tcp", |
| 41 | + source_address_start = "192.168.1.1", |
| 42 | + source_address_end = "192.168.1.255", |
| 43 | + destination_port_start = "22", |
| 44 | + destination_port_end = "22", |
| 45 | + action = "accept" |
| 46 | + ) |
| 47 | +) |
| 48 | +``` |
| 49 | + |
| 50 | +### Configure Firewall |
| 51 | + |
| 52 | +Server provides a helper function to add several firewall rules in series. |
| 53 | +Please note that the function does not know about pre-existing rules |
| 54 | +(UpCloud servers are created without any firewall rules by default). |
| 55 | + |
| 56 | +```python |
| 57 | +server = manager.get_servers()[0] |
| 58 | + |
| 59 | +rules = server.configure_firewall( |
| 60 | + [ |
| 61 | + FirewallRule( |
| 62 | + position = "1", |
| 63 | + direction = "in", |
| 64 | + family = "IPv4", |
| 65 | + protocol = "tcp", |
| 66 | + source_address_start = "192.168.1.1", |
| 67 | + source_address_end = "192.168.1.255", |
| 68 | + destination_port_start = "22", |
| 69 | + destination_port_end = "22", |
| 70 | + action = "accept" |
| 71 | + ), |
| 72 | + FirewallRule( |
| 73 | + position = "2", |
| 74 | + direction = "in", |
| 75 | + family = "IPv4", |
| 76 | + protocol = "tcp", |
| 77 | + source_address_start = "192.168.1.1", |
| 78 | + source_address_end = "192.168.1.255", |
| 79 | + destination_port_start = "21", |
| 80 | + destination_port_end = "21", |
| 81 | + action = "accept" |
| 82 | + ) |
| 83 | + ] |
| 84 | +) |
| 85 | +``` |
| 86 | + |
| 87 | +## Destroy |
| 88 | + |
| 89 | +```python |
| 90 | +server = manager.get_servers()[0] |
| 91 | +server.get_firewall_rules()[0].destroy() |
| 92 | +``` |
| 93 | + |
| 94 | +### Destroying all firewall rules |
| 95 | + |
| 96 | +Due to how the API handles positions, the following will NOT work: |
| 97 | + |
| 98 | +```python |
| 99 | +# does NOT work |
| 100 | +for rule in server.get_firewall_rules(): |
| 101 | + rule.destroy() |
| 102 | +``` |
| 103 | + |
| 104 | +This is because rules are based on position and the positions are always so |
| 105 | +that they start from 1 and are increment by one for each consecutive rule. |
| 106 | + |
| 107 | +A better approach would be to use CloudManager/FirewallManager directly |
| 108 | +(CloudManager and its mixins provide API functionality to Server, Storage, FirewallRule, etc. objects) |
| 109 | + |
| 110 | +```python |
| 111 | +for rule in server.get_firewall_rules(): |
| 112 | + manager.delete_firewall_rule(server.uuid, 1) |
| 113 | +``` |
| 114 | + |
| 115 | + |
| 116 | + |
| 117 | + |
0 commit comments