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

CreateFirewallRuleAction.run()   A

Complexity

Conditions 3

Size

Total Lines 74

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 3
dl 0
loc 74
rs 9.0335

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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