def create_balancer(self, name, port, protocol, algorithm, members,
                        ex_bandwidth=None, ex_internet_charge_type=None,
                        ex_address_type=None, ex_vswitch_id=None,
                        ex_master_zone_id=None, ex_slave_zone_id=None,
                        ex_client_token=None,
                        **kwargs):
        """
        Create a new load balancer instance

        @inherits: :class:`Driver.create_balancer`

        :keyword ex_bandwidth: The max bandwidth limit for `paybybandwidth`
                               internet charge type, in Mbps unit
        :type ex_bandwidth: ``int`` in range [1, 1000]

        :keyword ex_internet_charge_type: The internet charge type
        :type ex_internet_charge_type: a ``str`` of `paybybandwidth`
                                       or `paybytraffic`

        :keyword ex_address_type: The listening IP address type
        :type ex_address_type: a ``str`` of `internet` or `intranet`

        :keyword ex_vswitch_id: The vswitch id in a VPC network
        :type ex_vswitch_id: ``str``

        :keyword ex_master_zone_id: The id of the master availability zone
        :type ex_master_zone_id: ``str``

        :keyword ex_slave_zone_id: The id of the slave availability zone
        :type ex_slave_zone_id: ``str``

        :keyword ex_client_token: The token generated by client to
                                  identify requests
        :type ex_client_token: ``str``
        """

        # 1.Create load balancer
        params = {'Action': 'CreateLoadBalancer',
                  'RegionId': self.region}
        if name:
            params['LoadBalancerName'] = name
        if not port:
            raise AttributeError('port is required')
        if not protocol:
            # NOTE(samsong8610): Use http listener as default
            protocol = 'http'
        if protocol not in PROTOCOL_TO_LISTENER_MAP:
            raise AttributeError('unsupport protocol %s' % protocol)

        # Bandwidth in range [1, 1000] Mbps
        bandwidth = -1
        if ex_bandwidth:
            try:
                bandwidth = int(ex_bandwidth)
            except ValueError:
                raise AttributeError('ex_bandwidth should be a integer in '
                                     'range [1, 1000].')
            params['Bandwidth'] = bandwidth

        if ex_internet_charge_type:
            if ex_internet_charge_type.lower() == 'paybybandwidth':
                if bandwidth == -1:
                    raise AttributeError('PayByBandwidth internet charge type'
                                         ' need ex_bandwidth be set')
            params['InternetChargeType'] = ex_internet_charge_type

        if ex_address_type:
            if ex_address_type.lower() not in ('internet', 'intranet'):
                raise AttributeError('ex_address_type should be "internet" '
                                     'or "intranet"')
            params['AddressType'] = ex_address_type

        if ex_vswitch_id:
            params['VSwitchId'] = ex_vswitch_id

        if ex_master_zone_id:
            params['MasterZoneId'] = ex_master_zone_id
        if ex_slave_zone_id:
            params['SlaveZoneId'] = ex_slave_zone_id

        if ex_client_token:
            params['ClientToken'] = ex_client_token

        if members and isinstance(members, list):
            backend_ports = [member.port for member in members]
            if len(set(backend_ports)) != 1:
                raise AttributeError('the ports of members should be unique')
            # NOTE(samsong8610): If members do not provide backend port,
            #                    default to listening port
            backend_port = backend_ports[0] or port
        else:
            backend_port = port

        balancer = None
        try:
            resp_body = self.connection.request(self.path, params).object
            balancer = self._to_balancer(resp_body)
            balancer.port = port

            # 2.Add backend servers
            if members is None:
                members = []
            for member in members:
                self.balancer_attach_member(balancer, member)
            # 3.Create listener
            # NOTE(samsong8610): Assume only create a listener which uses all
            #                    the bandwidth.
            self.ex_create_listener(balancer, backend_port, protocol,
                                    algorithm, bandwidth, **kwargs)
            self.ex_start_listener(balancer, port)
            return balancer
        except Exception:
            e = sys.exc_info()[1]
            if balancer is not None:
                try:
                    self.destroy_balancer(balancer)
                except Exception:
                    pass
            raise e
dataset/ETHPy150Open apache/libcloud/libcloud/loadbalancer/drivers/slb.py/SLBDriver.create_balancer/original