Completed
Push — master ( 85147e...d9066e )
by Tomaz
03:35
created

CreateBalancerAction   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 10
c 0
b 0
f 0
wmc 2
1
from libcloud.loadbalancer.base import Algorithm
2
3
from lib.actions import BaseAction
4
5
__all__ = [
6
    'CreateBalancerAction'
7
]
8
9
10
class CreateBalancerAction(BaseAction):
11
12
    def run(self, region, network_domain_id, name, port, protocol,
13
            algorithm=Algorithm.ROUND_ROBIN):
14
        driver = self._get_lb_driver(region)
15
16
        # Use a local lookup - these maps are protected fields of each driver
17
        _VALUE_TO_ALGORITHM_MAP = {
18
            'ROUND_ROBIN': Algorithm.ROUND_ROBIN,
19
            'LEAST_CONNECTIONS': Algorithm.LEAST_CONNECTIONS,
20
            'SHORTEST_RESPONSE': Algorithm.SHORTEST_RESPONSE,
21
            'PERSISTENT_IP': Algorithm.PERSISTENT_IP
22
        }
23
24
        if algorithm is not Algorithm.ROUND_ROBIN:
25
            algorithm = _VALUE_TO_ALGORITHM_MAP[algorithm]
26
        driver.network_domain_id = network_domain_id
27
        record = driver.create_balancer(name=name,
28
                                        port=port,
29
                                        protocol=protocol,
30
                                        algorithm=algorithm,
31
                                        members=None)
32
33
        return self.resultsets.formatter(record)
34