Skip to content

Commit 9f970f3

Browse files
author
Elias Nygren
committed
fix firewallrule defaults and required fields according to API documentation
1 parent b3406b8 commit 9f970f3

File tree

1 file changed

+24
-21
lines changed

1 file changed

+24
-21
lines changed

upcloud/firewall.py

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,24 @@ class FirewallRule(object):
55
Object representation of the FirewallRule in UpCloud's API.
66
"""
77

8-
attributes = set([
9-
'action',
10-
'destination_address_end',
11-
'destination_address_start',
12-
'destination_port_end',
13-
'destination_port_start',
14-
'direction',
15-
'family',
16-
'icmp_type',
17-
'position',
18-
'protocol',
19-
'source_address_end',
20-
'source_address_start',
21-
'source_port_end',
22-
'source_port_start',
23-
])
8+
attributes = {
9+
# attribute: default_value
10+
# if none -> not sent to API unless given as param
11+
'action': 'drop',
12+
'direction': 'in',
13+
'family': 'IPv4',
14+
'destination_address_end': None,
15+
'destination_address_start': None,
16+
'destination_port_end': None,
17+
'destination_port_start': None,
18+
'icmp_type': None,
19+
'position': None,
20+
'protocol': None,
21+
'source_address_end': None,
22+
'source_address_start': None,
23+
'source_port_end': None,
24+
'source_port_start': None,
25+
}
2426

2527
def __init__(self, **kwargs):
2628
"""
@@ -36,10 +38,10 @@ def __init__(self, **kwargs):
3638

3739
setattr(self, key, kwargs[key])
3840

39-
# set attributes that were not given as empty strings ("defaults")
41+
# set defaults (if need be) where the default is not None
4042
for attr in self.attributes:
41-
if not hasattr(self, attr):
42-
setattr(self, attr, '')
43+
if not hasattr(self, attr) and self.attributes[attr] != None:
44+
setattr(self, attr, self.attributes[attr])
4345

4446
def prepare_post_body(self):
4547
"""
@@ -48,7 +50,8 @@ def prepare_post_body(self):
4850

4951
body = {}
5052
for attr in self.attributes:
51-
body[attr] = getattr(self, attr)
53+
if hasattr(self, attr):
54+
body[attr] = getattr(self, attr)
5255
return body
5356

5457
def destroy(self):
@@ -76,7 +79,7 @@ def _invalid_key_err(self, key):
7679
"""
7780
Raise exception on invalid parameters given to __init__.
7881
"""
79-
attr_list = list(self.attributes)
82+
attr_list = list(self.attributes.keys())
8083
raise Exception(
8184
"invalid parameter to FirewallRule, '{key}' is not in {attributes}"
8285
.format(key=key, attributes=attr_list)

0 commit comments

Comments
 (0)