Completed
Pull Request — master (#415)
by Anthony
02:24
created

CreateFirewallRuleAction   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 76
Duplicated Lines 0 %
Metric Value
dl 0
loc 76
rs 10
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A run() 0 74 3
1
from lib import actions
2
from libcloud.compute.drivers.dimensiondata import DimensionDataFirewallRule,\
3
                                                   DimensionDataFirewallAddress
4
5
__all__ = [
6
    'CreateFirewallRuleAction',
7
]
8
9
10
class CreateFirewallRuleAction(actions.BaseAction):
11
12
    def run(self, **kwargs):
13
        network_domain_id = kwargs['network_domain_id']
14
        del kwargs['network_domain_id']
15
        action = kwargs['action']
16
        del kwargs['action']
17
        region = kwargs['region']
18
        del kwargs['region']
19
        driver = self._get_compute_driver(region)
20
        network_domain = driver.ex_get_network_domain(network_domain_id)
21
        kwargs['network_domain'] = network_domain
22
        any_source = kwargs['any_source']
23
        del kwargs['any_source']
24
        any_destination = kwargs['any_destination']
25
        del kwargs['any_destination']
26
        if any_source:
27
            source = DimensionDataFirewallAddress(
28
                any_ip=True,
29
                ip_address=None,
30
                port_begin=None,
31
                port_end=None,
32
                ip_prefix_size=None
33
            )
34
        else:
35
            source = DimensionDataFirewallAddress(
36
                any_ip=True,
37
                ip_address=kwargs['source_ip'],
38
                port_begin=kwargs['source_port_begin'],
39
                port_end=['source_port_end'],
40
                ip_prefix_size=['source_ip_prefix_size']
41
            )
42
        if any_destination:
43
            destination = DimensionDataFirewallAddress(
44
                any_ip=True,
45
                ip_address=None,
46
                port_begin=None,
47
                port_end=None,
48
                ip_prefix_size=None
49
            )
50
        else:
51
            destination = DimensionDataFirewallAddress(
52
                any_ip=True,
53
                ip_address=kwargs['destination_ip'],
54
                port_begin=kwargs['destination_port_begin'],
55
                port_end=['destination_port_end'],
56
                ip_prefix_size=['destination_ip_prefix_size']
57
            )
58
        # setup the rule
59
        rule = DimensionDataFirewallRule(
60
            id=None,
61
            location=network_domain.location,
62
            status=None,
63
            network_domain=network_domain,
64
            enabled=True,
65
            source=source,
66
            destination=destination,
67
            protocol=kwargs['protocol'],
68
            name=kwargs['name'],
69
            action=kwargs['fw_action'],
70
            ip_version=kwargs['ip_version']
71
        )
72
        kwargs['rule'] = rule
73
        del kwargs['name']
74
        del kwargs['fw_action']
75
        del kwargs['ip_version']
76
        del kwargs['protocol']
77
        del kwargs['source_ip']
78
        del kwargs['source_port_begin']
79
        del kwargs['source_port_end']
80
        del kwargs['source_ip_prefix_size']
81
        del kwargs['destination_ip']
82
        del kwargs['destination_port_begin']
83
        del kwargs['destination_port_end']
84
        del kwargs['destination_ip_prefix_size']
85
        return self._do_function(driver, action, **kwargs)
86