Completed
Pull Request — master (#485)
by Anthony
02:44
created

CreateFirewallRuleAction.run()   B

Complexity

Conditions 3

Size

Total Lines 82

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
c 1
b 0
f 0
dl 0
loc 82
rs 8.7769

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
                address_list_id=None,
35
                port_list_id=None,
36
            )
37
        else:
38
            source = DimensionDataFirewallAddress(
39
                any_ip=True,
40
                ip_address=kwargs['source_ip'],
41
                port_begin=kwargs['source_port_begin'],
42
                port_end=['source_port_end'],
43
                ip_prefix_size=['source_ip_prefix_size'],
44
                address_list_id=None,
45
                port_list_id=None,
46
            )
47
        if any_destination:
48
            destination = DimensionDataFirewallAddress(
49
                any_ip=True,
50
                ip_address=None,
51
                port_begin=None,
52
                port_end=None,
53
                ip_prefix_size=None,
54
                address_list_id=None,
55
                port_list_id=None,
56
            )
57
        else:
58
            destination = DimensionDataFirewallAddress(
59
                any_ip=True,
60
                ip_address=kwargs['destination_ip'],
61
                port_begin=kwargs['destination_port_begin'],
62
                port_end=['destination_port_end'],
63
                ip_prefix_size=['destination_ip_prefix_size'],
64
                address_list_id=None,
65
                port_list_id=None,
66
            )
67
        # setup the rule
68
        rule = DimensionDataFirewallRule(
69
            id=None,
70
            location=network_domain.location,
71
            status=None,
72
            network_domain=network_domain,
73
            enabled=True,
74
            source=source,
75
            destination=destination,
76
            protocol=kwargs['protocol'],
77
            name=kwargs['name'],
78
            action=kwargs['fw_action'],
79
            ip_version=kwargs['ip_version']
80
        )
81
        kwargs['rule'] = rule
82
        del kwargs['name']
83
        del kwargs['fw_action']
84
        del kwargs['ip_version']
85
        del kwargs['protocol']
86
        del kwargs['source_ip']
87
        del kwargs['source_port_begin']
88
        del kwargs['source_port_end']
89
        del kwargs['source_ip_prefix_size']
90
        del kwargs['destination_ip']
91
        del kwargs['destination_port_begin']
92
        del kwargs['destination_port_end']
93
        del kwargs['destination_ip_prefix_size']
94
        return self._do_function(driver, action, **kwargs)
95