.. versionadded:: 2019.2.0

    Create or update a load balancer within a specified resource group.

    :param name: The name of the load balancer to create.

    :param resource_group: The resource group name assigned to the
        load balancer.

    CLI Example:

    .. code-block:: bash

        salt-call azurearm_network.load_balancer_create_or_update testlb testgroup

def load_balancer_create_or_update(name, resource_group, **kwargs):
    '''
    .. versionadded:: 2019.2.0

    Create or update a load balancer within a specified resource group.

    :param name: The name of the load balancer to create.

    :param resource_group: The resource group name assigned to the
        load balancer.

    CLI Example:

    .. code-block:: bash

        salt-call azurearm_network.load_balancer_create_or_update testlb testgroup

    '''
    if 'location' not in kwargs:
        rg_props = __salt__['azurearm_resource.resource_group_get'](
            resource_group, **kwargs
        )

        if 'error' in rg_props:
            log.error(
                'Unable to determine location from resource group specified.'
            )
            return False
        kwargs['location'] = rg_props['location']

    netconn = __utils__['azurearm.get_client']('network', **kwargs)

    if isinstance(kwargs.get('frontend_ip_configurations'), list):
        for idx in six_range(0, len(kwargs['frontend_ip_configurations'])):
            # Use Public IP Address name to link to the ID of an existing Public IP
            if 'public_ip_address' in kwargs['frontend_ip_configurations'][idx]:
                pub_ip = public_ip_address_get(
                    name=kwargs['frontend_ip_configurations'][idx]['public_ip_address'],
                    resource_group=resource_group,
                    **kwargs
                )
                if 'error' not in pub_ip:
                    kwargs['frontend_ip_configurations'][idx]['public_ip_address'] = {'id': str(pub_ip['id'])}
            # Use Subnet name to link to the ID of an existing Subnet
            elif 'subnet' in kwargs['frontend_ip_configurations'][idx]:
                vnets = virtual_networks_list(
                    resource_group=resource_group,
                    **kwargs
                )
                if 'error' not in vnets:
                    for vnet in vnets:
                        subnets = subnets_list(
                            virtual_network=vnet,
                            resource_group=resource_group,
                            **kwargs
                        )
                        if kwargs['frontend_ip_configurations'][idx]['subnet'] in subnets:
                            kwargs['frontend_ip_configurations'][idx]['subnet'] = {
                                'id': str(subnets[kwargs['frontend_ip_configurations'][idx]['subnet']]['id'])
                            }
                            break

    id_url = '/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.Network/loadBalancers/{2}/{3}/{4}'

    if isinstance(kwargs.get('load_balancing_rules'), list):
        for idx in six_range(0, len(kwargs['load_balancing_rules'])):
            # Link to sub-objects which might be created at the same time as the load balancer
            if 'frontend_ip_configuration' in kwargs['load_balancing_rules'][idx]:
                kwargs['load_balancing_rules'][idx]['frontend_ip_configuration'] = {
                    'id': id_url.format(
                        kwargs.get('subscription_id'),
                        resource_group,
                        name,
                        'frontendIPConfigurations',
                        kwargs['load_balancing_rules'][idx]['frontend_ip_configuration']
                    )
                }
            if 'backend_address_pool' in kwargs['load_balancing_rules'][idx]:
                kwargs['load_balancing_rules'][idx]['backend_address_pool'] = {
                    'id': id_url.format(
                        kwargs.get('subscription_id'),
                        resource_group,
                        name,
                        'backendAddressPools',
                        kwargs['load_balancing_rules'][idx]['backend_address_pool']
                    )
                }
            if 'probe' in kwargs['load_balancing_rules'][idx]:
                kwargs['load_balancing_rules'][idx]['probe'] = {
                    'id': id_url.format(
                        kwargs.get('subscription_id'),
                        resource_group,
                        name,
                        'probes',
                        kwargs['load_balancing_rules'][idx]['probe']
                    )
                }

    if isinstance(kwargs.get('inbound_nat_rules'), list):
        for idx in six_range(0, len(kwargs['inbound_nat_rules'])):
            # Link to sub-objects which might be created at the same time as the load balancer
            if 'frontend_ip_configuration' in kwargs['inbound_nat_rules'][idx]:
                kwargs['inbound_nat_rules'][idx]['frontend_ip_configuration'] = {
                    'id': id_url.format(
                        kwargs.get('subscription_id'),
                        resource_group,
                        name,
                        'frontendIPConfigurations',
                        kwargs['inbound_nat_rules'][idx]['frontend_ip_configuration']
                    )
                }

    if isinstance(kwargs.get('inbound_nat_pools'), list):
        for idx in six_range(0, len(kwargs['inbound_nat_pools'])):
            # Link to sub-objects which might be created at the same time as the load balancer
            if 'frontend_ip_configuration' in kwargs['inbound_nat_pools'][idx]:
                kwargs['inbound_nat_pools'][idx]['frontend_ip_configuration'] = {
                    'id': id_url.format(
                        kwargs.get('subscription_id'),
                        resource_group,
                        name,
                        'frontendIPConfigurations',
                        kwargs['inbound_nat_pools'][idx]['frontend_ip_configuration']
                    )
                }

    if isinstance(kwargs.get('outbound_nat_rules'), list):
        for idx in six_range(0, len(kwargs['outbound_nat_rules'])):
            # Link to sub-objects which might be created at the same time as the load balancer
            if 'frontend_ip_configuration' in kwargs['outbound_nat_rules'][idx]:
                kwargs['outbound_nat_rules'][idx]['frontend_ip_configuration'] = {
                    'id': id_url.format(
                        kwargs.get('subscription_id'),
                        resource_group,
                        name,
                        'frontendIPConfigurations',
                        kwargs['outbound_nat_rules'][idx]['frontend_ip_configuration']
                    )
                }
            if 'backend_address_pool' in kwargs['outbound_nat_rules'][idx]:
                kwargs['outbound_nat_rules'][idx]['backend_address_pool'] = {
                    'id': id_url.format(
                        kwargs.get('subscription_id'),
                        resource_group,
                        name,
                        'backendAddressPools',
                        kwargs['outbound_nat_rules'][idx]['backend_address_pool']
                    )
                }

    try:
        lbmodel = __utils__['azurearm.create_object_model']('network', 'LoadBalancer', **kwargs)
    except TypeError as exc:
        result = {'error': 'The object model could not be built. ({0})'.format(str(exc))}
        return result

    try:
        load_balancer = netconn.load_balancers.create_or_update(
            resource_group_name=resource_group,
            load_balancer_name=name,
            parameters=lbmodel
        )
        load_balancer.wait()
        lb_result = load_balancer.result()
        result = lb_result.as_dict()
    except CloudError as exc:
        __utils__['azurearm.log_cloud_error']('network', str(exc), **kwargs)
        result = {'error': str(exc)}
    except SerializationError as exc:
        result = {'error': 'The object model could not be parsed. ({0})'.format(str(exc))}

    return result