<commit_before># -*- coding: utf-8 -*-
'''
Apache Libcloud Load Balancer State
===================================

Manage load balancers using libcloud

    :codeauthor: :email:`<NAME> <<EMAIL>>`

Apache Libcloud load balancer management for a full list
of supported clouds, see http://libcloud.readthedocs.io/en/latest/loadbalancer/supported_providers.html

Clouds include Amazon ELB, ALB, Google, Aliyun, CloudStack, Softlayer

.. versionadded:: Oxygen

:configuration:
    This module uses a configuration profile for one or multiple Cloud providers

    .. code-block:: yaml

        libcloud_loadbalancer:
            profile_test1:
              driver: gce
              key: <KEY>
              secret: mysecret
            profile_test2:
              driver: alb
              key: 12345
              secret: mysecret

:depends: apache-libcloud
'''

# Import Python Libs
from __future__ import absolute_import
import logging

# Import salt libs
import salt.utils

log = logging.getLogger(__name__)


def __virtual__():
    return True


def __init__(opts):
    salt.utils.compat.pack_dunder(__name__)


def state_result(result, message):
    return {'result': result, 'comment': message}


def balancer_present(name, port, protocol, profile):
    '''
    Ensures a load balancer is present.

    :param name: Load Balancer name
    :type  name: ``str``

    :param port: Port the load balancer should listen on, defaults to 80
    :type  port: ``str``

    :param protocol: Loadbalancer protocol, defaults to http.
    :type  protocol: ``str``

    :param profile: The profile key
    :type  profile: ``str``
    '''
    balancers = __salt__['libcloud_loadbalancer.list_balancers'](profile)
    match = [z for z in balancers if z['name'] == name]
    if len(match) > 0:
        return state_result(True, "Balancer already exists")
    else:
        result = __salt__['libcloud_loadbalancer.create_balancer'](name, port, protocol, profile)
        return state_result(result, "Created new load balancer")


def balancer_absent(name, profile):
    '''
    Ensures a load balancer is absent.

    :param name: Load Balancer name
    :type  name: ``str``

    :param profile: The profile key
    :type  profile: ``str``
    '''
    balancers = __salt__['libcloud_loadbalancer.list_balancers'](profile)
    match = [z for z in balancers if z['name'] == name]
    if len(match) == 0:
        return state_result(True, "Balancer already absent")
    else:
        result = __salt__['libcloud_loadbalancer.delete_balancer'](match['id'], profile)
        return state_result(result, "Deleted load balancer")

<commit_msg>add commands for adding and removing members from a balancer<commit_after># -*- coding: utf-8 -*-
'''
Apache Libcloud Load Balancer State
===================================

Manage load balancers using libcloud

    :codeauthor: :email:`<NAME> <<EMAIL>>`

Apache Libcloud load balancer management for a full list
of supported clouds, see http://libcloud.readthedocs.io/en/latest/loadbalancer/supported_providers.html

Clouds include Amazon ELB, ALB, Google, Aliyun, CloudStack, Softlayer

.. versionadded:: Oxygen

:configuration:
    This module uses a configuration profile for one or multiple Cloud providers

    .. code-block:: yaml

        libcloud_loadbalancer:
            profile_test1:
              driver: gce
              key: <KEY>
              secret: mysecret
            profile_test2:
              driver: alb
              key: 12345
              secret: mysecret

:depends: apache-libcloud
'''

# Import Python Libs
from __future__ import absolute_import
import logging

# Import salt libs
import salt.utils

log = logging.getLogger(__name__)


def __virtual__():
    return True


def __init__(opts):
    salt.utils.compat.pack_dunder(__name__)


def state_result(result, message):
    return {'result': result, 'comment': message}


def balancer_present(name, port, protocol, profile):
    '''
    Ensures a load balancer is present.

    :param name: Load Balancer name
    :type  name: ``str``

    :param port: Port the load balancer should listen on, defaults to 80
    :type  port: ``str``

    :param protocol: Loadbalancer protocol, defaults to http.
    :type  protocol: ``str``

    :param profile: The profile key
    :type  profile: ``str``
    '''
    balancers = __salt__['libcloud_loadbalancer.list_balancers'](profile)
    match = [z for z in balancers if z['name'] == name]
    if len(match) > 0:
        return state_result(True, "Balancer already exists")
    else:
        result = __salt__['libcloud_loadbalancer.create_balancer'](name, port, protocol, profile)
        return state_result(result, "Created new load balancer")


def balancer_absent(name, profile):
    '''
    Ensures a load balancer is absent.

    :param name: Load Balancer name
    :type  name: ``str``

    :param profile: The profile key
    :type  profile: ``str``
    '''
    balancers = __salt__['libcloud_loadbalancer.list_balancers'](profile)
    match = [z for z in balancers if z['name'] == name]
    if len(match) == 0:
        return state_result(True, "Balancer already absent")
    else:
        result = __salt__['libcloud_loadbalancer.delete_balancer'](match['id'], profile)
        return state_result(result, "Deleted load balancer")


def member_present(ip, port, balancer_id, profile):
    '''
    Ensure a load balancer member is present

    :param ip: IP address for the new member
    :type  ip: ``str``

    :param port: Port for the new member
    :type  port: ``int``

    :param balancer_id: id of a load balancer you want to attach the member to
    :type  balancer_id: ``str``

    :param profile: The profile key
    :type  profile: ``str``
    '''
    existing_members = __salt__['libcloud_loadbalancer.list_balancer_members'](balancer_id, profile)
    for member in existing_members:
        if member['ip'] == ip and member['port'] == port:
            return state_result(True, "Member already present")
    member = __salt__['libcloud_loadbalancer.balancer_attach_member'](balancer_id, ip, port, profile)
    return state_result(True, "Member added to balancer, id: {0}".format(member['id']))


def member_absent(ip, port, balancer_id, profile):
    '''
    Ensure a load balancer member is absent, based on IP and Port

    :param ip: IP address for the member
    :type  ip: ``str``

    :param port: Port for the member
    :type  port: ``int``

    :param balancer_id: id of a load balancer you want to detach the member from
    :type  balancer_id: ``str``

    :param profile: The profile key
    :type  profile: ``str``
    '''
    existing_members = __salt__['libcloud_loadbalancer.list_balancer_members'](balancer_id, profile)
    for member in existing_members:
        if member['ip'] == ip and member['port'] == port:
            result = __salt__['libcloud_loadbalancer.balancer_detach_member'](balancer_id, member['id'], profile)
            return state_result(result, "Member removed")
    return state_result(True, "Member already absent")
{'fraction_non_alphanumeric': 0.1127381290872903, 'fraction_numerical': 0.0035541654819448393, 'mean_word_length': 3.302752293577982, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3730057', 'n_tokens_mistral': 2129, 'n_tokens_neox': 2057, 'n_words': 665}