def create_balancer(self, name, port, protocol, algorithm, members, ex_members_availability_zones=None): if ex_members_availability_zones is None: ex_members_availability_zones = ["a"] params = { "Action": "CreateLoadBalancer", "LoadBalancerName": name, "Listeners.member.1.InstancePort": str(port), "Listeners.member.1.InstanceProtocol": protocol.upper(), "Listeners.member.1.LoadBalancerPort": str(port), "Listeners.member.1.Protocol": protocol.upper(), } for i, z in enumerate(ex_members_availability_zones): zone = "".join((self.region, z)) params["AvailabilityZones.member.%d" % (i + 1)] = zone data = self.connection.request(ROOT, params=params).object balancer = LoadBalancer( id=name, name=name, state=State.PENDING, ip=findtext(element=data, xpath="DNSName", namespace=NS), port=port, driver=self.connection.driver, ) balancer._members = [] return balancer
def _to_balancer(self, el): name = findtext(element=el, xpath='LoadBalancerName', namespace=NS) id = findtext(element=el, xpath='LoadBalancerArn', namespace=NS) dns_name = findtext(el, xpath='DNSName', namespace=NS) balancer = LoadBalancer( id=id, name=name, state=State.UNKNOWN, ip=dns_name, port=None, driver=self.connection.driver ) extra = { 'listeners': self._ex_get_balancer_listeners(balancer), 'target_groups': self._ex_get_balancer_target_groups(balancer), 'tags': self._ex_get_balancer_tags(balancer) } balancer.extra = extra if len(extra['listeners']) > 0: balancer.port = extra['listeners'][0]['port'] else: balancer.port = None balancer._members = self._ex_get_balancer_memebers(balancer) return balancer
def create_balancer(self, name, port, protocol, algorithm, members, ex_members_availability_zones=None): if ex_members_availability_zones is None: ex_members_availability_zones = ['a'] params = { 'Action': 'CreateLoadBalancer', 'LoadBalancerName': name, 'Listeners.member.1.InstancePort': str(port), 'Listeners.member.1.InstanceProtocol': protocol.upper(), 'Listeners.member.1.LoadBalancerPort': str(port), 'Listeners.member.1.Protocol': protocol.upper(), } for i, z in enumerate(ex_members_availability_zones): zone = ''.join((self.region, z)) params['AvailabilityZones.member.%d' % (i + 1)] = zone data = self.connection.request(ROOT, params=params).object balancer = LoadBalancer(id=name, name=name, state=State.PENDING, ip=findtext(element=data, xpath='DNSName', namespace=NS), port=port, driver=self.connection.driver) balancer._members = [] return balancer
def create_balancer(self, name, port, protocol, algorithm, members, ex_members_availability_zones=None): if ex_members_availability_zones is None: ex_members_availability_zones = ['a'] params = { 'Action': 'CreateLoadBalancer', 'LoadBalancerName': name, 'Listeners.member.1.InstancePort': str(port), 'Listeners.member.1.InstanceProtocol': protocol.upper(), 'Listeners.member.1.LoadBalancerPort': str(port), 'Listeners.member.1.Protocol': protocol.upper(), } for i, z in enumerate(ex_members_availability_zones): zone = ''.join((self.region, z)) params['AvailabilityZones.member.%d' % (i + 1)] = zone data = self.connection.request(ROOT, params=params).object balancer = LoadBalancer( id=name, name=name, state=State.PENDING, ip=findtext(element=data, xpath='DNSName', namespace=NS), port=port, driver=self.connection.driver ) balancer._members = [] return balancer
def test_balancer_detach_member(self): balancer = LoadBalancer(23530, None, None, None, None, self.driver) member = self.driver.balancer_list_members(balancer)[0] ret1 = self.driver.balancer_detach_member(balancer, member) ret2 = balancer.detach_member(member) self.assertTrue(ret1) self.assertTrue(ret2)
def test_balancer_attach_member(self): balancer = LoadBalancer(23530, None, None, None, None, self.driver) member = Member(None, ip="10.0.0.75", port="80") member1 = self.driver.balancer_attach_member(balancer, member=member) member2 = balancer.attach_member(member=member) self.assertEquals(member1.ip, "10.0.0.75") self.assertEquals(member1.port, 80) self.assertEquals(member2.ip, "10.0.0.75") self.assertEquals(member2.port, 80)
def test_balancer_attach_compute_node(self): balancer = LoadBalancer(23530, None, None, None, None, self.driver) node = Node(id="1", name="test", state=None, public_ips=["10.0.0.75"], private_ips=[], driver=DummyNodeDriver) member1 = self.driver.balancer_attach_compute_node(balancer, node) member2 = balancer.attach_compute_node(node) self.assertEquals(member1.ip, "10.0.0.75") self.assertEquals(member1.port, 80) self.assertEquals(member2.ip, "10.0.0.75") self.assertEquals(member2.port, 80)
def test_balancer_attach_member(self): balancer = LoadBalancer(23530, None, None, None, None, self.driver) member = Member(None, ip='10.0.0.75', port='80') member1 = self.driver.balancer_attach_member(balancer, member=member) member2 = balancer.attach_member(member=member) self.assertEqual(member1.ip, '10.0.0.75') self.assertEqual(member1.port, 80) self.assertEqual(member2.ip, '10.0.0.75') self.assertEqual(member2.port, 80)
def _to_balancer(self, obj): balancer = LoadBalancer(id=obj['id'], name=obj['name'], state=obj['state'], ip=obj['publicip'], port=obj['publicport'], driver=self.connection.driver) balancer.ex_private_port = obj['privateport'] balancer.ex_public_ip_id = obj['publicipid'] return balancer
def test_balancer_attach_compute_node(self): balancer = LoadBalancer(23530, None, None, None, None, self.driver) node = Node(id='1', name='test', state=None, public_ips=['10.0.0.75'], private_ips=[], driver=DummyNodeDriver) member1 = self.driver.balancer_attach_compute_node(balancer, node) member2 = balancer.attach_compute_node(node) self.assertEqual(member1.ip, '10.0.0.75') self.assertEqual(member1.port, 80) self.assertEqual(member2.ip, '10.0.0.75') self.assertEqual(member2.port, 80)
def _to_balancer(self, obj): balancer = LoadBalancer( id=obj["id"], name=obj["name"], state=self.LB_STATE_MAP.get(obj["state"], State.UNKNOWN), ip=obj["publicip"], port=obj["publicport"], driver=self.connection.driver, ) balancer.ex_private_port = obj["privateport"] balancer.ex_public_ip_id = obj["publicipid"] return balancer
def _to_balancer(self, obj): balancer = LoadBalancer( id=obj['id'], name=obj['name'], state=obj['state'], ip=obj['publicip'], port=obj['publicport'], driver=self.connection.driver ) balancer.ex_private_port = obj['privateport'] balancer.ex_public_ip_id = obj['publicipid'] return balancer
def _to_balancer(self, element): ipaddress = findtext(element, 'listenerIpAddress', TYPES_URN) name = findtext(element, 'name', TYPES_URN) port = findtext(element, 'port', TYPES_URN) extra = {} pool_element = element.find(fixxpath('pool', TYPES_URN)) if pool_element is None: extra['pool_id'] = None else: extra['pool_id'] = pool_element.get('id') extra['network_domain_id'] = findtext(element, 'networkDomainId', TYPES_URN) balancer = LoadBalancer(id=element.get('id'), name=name, state=self._VALUE_TO_STATE_MAP.get( findtext(element, 'state', TYPES_URN), State.UNKNOWN), ip=ipaddress, port=port, driver=self.connection.driver, extra=extra) return balancer
def test_balancer_attach_member(self): balancer = LoadBalancer(23530, None, None, None, None, None) member = self.driver.balancer_attach_member(balancer, Member(None, ip='10.0.0.75', port='80')) self.assertEquals(member.ip, '10.0.0.75') self.assertEquals(member.port, 80)
def _to_balancer(self, element): ipaddress = findtext(element, "listenerIpAddress", TYPES_URN) name = findtext(element, "name", TYPES_URN) port = findtext(element, "port", TYPES_URN) extra = {} pool_element = element.find(fixxpath("pool", TYPES_URN)) if pool_element is None: extra["pool_id"] = None else: extra["pool_id"] = pool_element.get("id") extra["network_domain_id"] = findtext(element, "networkDomainId", TYPES_URN) balancer = LoadBalancer( id=element.get("id"), name=name, state=self._VALUE_TO_STATE_MAP.get( findtext(element, "state", TYPES_URN), State.UNKNOWN), ip=ipaddress, port=port, driver=self.connection.driver, extra=extra, ) return balancer
def _to_balancer(self, el): _id = findtext(element=el, xpath='LoadBalancerId', namespace=self.namespace) name = findtext(element=el, xpath='LoadBalancerName', namespace=self.namespace) status = findtext(element=el, xpath='LoadBalancerStatus', namespace=self.namespace) state = STATE_MAPPINGS.get(status, State.UNKNOWN) address = findtext(element=el, xpath='Address', namespace=self.namespace) extra = self._get_extra_dict(el, RESOURCE_EXTRA_ATTRIBUTES_MAP['balancer']) balancer = LoadBalancer(id=_id, name=name, state=state, ip=address, port=None, driver=self, extra=extra) return balancer
def _to_balancer(self, el): name = findtext(element=el, xpath="LoadBalancerName", namespace=NS) dns_name = findtext(el, xpath="DNSName", namespace=NS) port = findtext(el, xpath="LoadBalancerPort", namespace=NS) balancer = LoadBalancer( id=name, name=name, state=State.UNKNOWN, ip=dns_name, port=port, driver=self.connection.driver ) xpath = "Instances/member/InstanceId" members = findall(element=el, xpath=xpath, namespace=NS) balancer._members = [] for m in members: balancer._members.append(Member(m.text, None, None, balancer=balancer)) return balancer
def _to_balancer(self, data): return LoadBalancer(id=data['id'], name=data['name'], state=self.LB_STATE_MAP.get( data['status'], State.UNKNOWN), ip=self._public_ip(data), port=data['listeners'][0]['in'], driver=self.connection.driver)
def _to_balancer(self, data): return LoadBalancer( id=data["id"], name=data["name"], state=self.LB_STATE_MAP.get(data["status"], State.UNKNOWN), ip=self._public_ip(data), port=data["listeners"][0]["in"], driver=self.connection.driver, )
def test_update_balancer_port(self): balancer = LoadBalancer(id='3131', name='LB_update', state='PENDING_UPDATE', ip='10.34.4.3', port=80, driver=self.driver) updated_balancer = self.driver.update_balancer(balancer, port=1337) self.assertEqual(1337, updated_balancer.port)
def _to_balancer(self, el): lb = LoadBalancer(id=el.get("id"), name=el["name"], state=self.LB_STATE_MAP.get(el["state"]["name"], State.UNKNOWN), ip=el["virtualip"]["ip"]["ip"], port=el["virtualip"]["port"], driver=self.connection.driver) return lb
def test_update_balancer_protocol(self): balancer = LoadBalancer(id='3130', name='LB_update', state='PENDING_UPDATE', ip='10.34.4.3', port=80, driver=self.driver) updated_balancer = self.driver.update_balancer(balancer, protocol='HTTPS') self.assertEqual('HTTPS', updated_balancer.extra['protocol'])
def test_ex_update_balancer_no_poll_algorithm(self): balancer = LoadBalancer(id='3133', name='LB_update', state='PENDING_UPDATE', ip='10.34.4.3', port=80, driver=self.driver) action_succeeded = self.driver.ex_update_balancer_no_poll( balancer, algorithm=Algorithm.ROUND_ROBIN) self.assertTrue(action_succeeded)
def test_update_balancer_name(self): balancer = LoadBalancer(id='3132', name='LB_update', state='PENDING_UPDATE', ip='10.34.4.3', port=80, driver=self.driver) updated_balancer = self.driver.update_balancer(balancer, name='new_lb_name') self.assertEqual('new_lb_name', updated_balancer.name)
def test_ex_update_balancer_no_poll_port(self): balancer = LoadBalancer(id='3131', name='LB_update', state='PENDING_UPDATE', ip='10.34.4.3', port=80, driver=self.driver) action_succeeded = self.driver.ex_update_balancer_no_poll(balancer, port=1337) self.assertTrue(action_succeeded)
def test_update_balancer_algorithm(self): balancer = LoadBalancer(id='3133', name='LB_update', state='PENDING_UPDATE', ip='10.34.4.3', port=80, driver=self.driver) updated_balancer = self.driver.update_balancer( balancer, algorithm=Algorithm.ROUND_ROBIN) self.assertEqual(Algorithm.ROUND_ROBIN, updated_balancer.extra['algorithm'])
def _to_balancer(self, el): name = findtext(element=el, xpath='LoadBalancerName', namespace=NS) dns_name = findtext(el, xpath='DNSName', namespace=NS) port = findtext(el, xpath='LoadBalancerPort', namespace=NS) balancer = LoadBalancer(id=name, name=name, state=State.UNKNOWN, ip=dns_name, port=port, driver=self.connection.driver) xpath = 'Instances/member/InstanceId' members = findall(element=el, xpath=xpath, namespace=NS) balancer._members = [] for m in members: balancer._members.append( Member(m.text, None, None, balancer=balancer)) return balancer
def _to_balancer(self, el): name = findtext(element=el, xpath='LoadBalancerName', namespace=NS) id = findtext(element=el, xpath='LoadBalancerArn', namespace=NS) dns_name = findtext(el, xpath='DNSName', namespace=NS) balancer = LoadBalancer(id=id, name=name, state=State.UNKNOWN, ip=dns_name, port=None, driver=self.connection.driver) extra = { 'listeners': self._ex_get_balancer_listeners(balancer), 'target_groups': self._ex_get_balancer_target_groups(balancer), 'tags': self._ex_get_balancer_tags(balancer) } balancer.extra = extra if len(extra['listeners']) > 0: balancer.port = extra['listeners'][0]['port'] else: balancer.port = None balancer._members = self._ex_get_balancer_memebers(balancer) return balancer
def _to_balancer(self, el): balancer = LoadBalancer( id=findtext(element=el, xpath="LoadBalancerArn", namespace=NS), name=findtext(element=el, xpath="LoadBalancerName", namespace=NS), state=State.UNKNOWN, ip=findtext(el, xpath="DNSName", namespace=NS), port=None, driver=self.connection.driver, ) balancer.extra = { "listeners": self._ex_get_balancer_listeners(balancer), "tags": self._ex_get_balancer_tags(balancer), "vpc": findtext(el, xpath="VpcId", namespace=NS), } if len(balancer.extra["listeners"]) > 0: balancer.port = balancer.extra["listeners"][0].port else: balancer.port = None return balancer
def test_ex_update_balancer_no_poll_bad_algorithm_exception(self): balancer = LoadBalancer(id='3134', name='LB_update', state='PENDING_UPDATE', ip='10.34.4.3', port=80, driver=self.driver) try: self.driver.update_balancer(balancer, algorithm='HAVE_MERCY_ON_OUR_SERVERS') except LibcloudError: pass else: self.fail('Should have thrown exception with bad algorithm value')
def __init__(self): self._TEST_BALANCER = LoadBalancer( id='test_id', name='test_balancer', state=0, # RUNNING ip='1.2.3.4', port=80, driver=self, extra={}) self._TEST_MEMBER = Member(id='member_id', ip='12.3.4.5', port=443, balancer=self._TEST_BALANCER, extra=None)
def test_destroy_balancer(self): extra = { 'pool_id': '4d360b1f-bc2c-4ab7-9884-1f03ba2768f7', 'network_domain_id': '1234' } balancer = LoadBalancer(id='234', name='test', state=State.RUNNING, ip='1.2.3.4', port=1234, driver=self.driver, extra=extra) response = self.driver.destroy_balancer(balancer) self.assertEqual(response, True)
def create_balancer( self, name, port, protocol, algorithm, members, ex_members_availability_zones=None, ): if ex_members_availability_zones is None: ex_members_availability_zones = ["a"] params = { "Action": "CreateLoadBalancer", "LoadBalancerName": name, "Listeners.member.1.InstancePort": str(port), "Listeners.member.1.InstanceProtocol": protocol.upper(), "Listeners.member.1.LoadBalancerPort": str(port), "Listeners.member.1.Protocol": protocol.upper(), } for i, z in enumerate(ex_members_availability_zones): zone = "".join((self.region, z)) params["AvailabilityZones.member.%d" % (i + 1)] = zone data = self.connection.request(ROOT, params=params).object balancer = LoadBalancer( id=name, name=name, state=State.PENDING, ip=findtext(element=data, xpath="DNSName", namespace=NS), port=port, driver=self.connection.driver, ) balancer._members = [] return balancer
def _to_balancer(self, el): balancer = LoadBalancer(id=findtext(element=el, xpath='LoadBalancerArn', namespace=NS), name=findtext(element=el, xpath='LoadBalancerName', namespace=NS), state=State.UNKNOWN, ip=findtext(el, xpath='DNSName', namespace=NS), port=None, driver=self.connection.driver) balancer.extra = { 'listeners': self._ex_get_balancer_listeners(balancer), 'tags': self._ex_get_balancer_tags(balancer), 'vpc': findtext(el, xpath='VpcId', namespace=NS) } if len(balancer.extra['listeners']) > 0: balancer.port = balancer.extra['listeners'][0].port else: balancer.port = None return balancer
def test_destroy_balancer(driver): extra = { "pool_id": "4d360b1f-bc2c-4ab7-9884-1f03ba2768f7", "network_domain_id": "1234", } balancer = LoadBalancer( id="234", name="test", state=State.RUNNING, ip="1.2.3.4", port=1234, driver=driver, extra=extra, ) response = driver.destroy_balancer(balancer) assert response is True
def test_balancer_list_members(self): extra = { 'pool_id': '4d360b1f-bc2c-4ab7-9884-1f03ba2768f7', 'network_domain_id': '1234' } balancer = LoadBalancer(id='234', name='test', state=State.RUNNING, ip='1.2.3.4', port=1234, driver=self.driver, extra=extra) members = self.driver.balancer_list_members(balancer) self.assertEqual(2, len(members)) self.assertEqual(members[0].ip, '10.0.3.13') self.assertEqual(members[0].id, '3dd806a2-c2c8-4c0c-9a4f-5219ea9266c0') self.assertEqual(members[0].port, 9889)
def __init__(self): # pylint: disable=W0231 self._TEST_BALANCER = LoadBalancer( id="test_id", name="test_balancer", state=0, # RUNNING ip="1.2.3.4", port=80, driver=self, extra={}, ) self._TEST_MEMBER = Member( id="member_id", ip="12.3.4.5", port=443, balancer=self._TEST_BALANCER, extra=None, )