.. versionadded:: 2019.2.0

    Create or update a subnet.

    :param name: The name assigned to the subnet being created or updated.

    :param address_prefix: A valid CIDR block within the virtual network.

    :param virtual_network: The virtual network name containing the
        subnet.

    :param resource_group: The resource group name assigned to the
        virtual network.

    CLI Example:

    .. code-block:: bash

        salt-call azurearm_network.subnet_create_or_update testsubnet \
                  '10.0.0.0/24' testnet testgroup

def subnet_create_or_update(name, address_prefix, virtual_network, resource_group, **kwargs):
    '''
    .. versionadded:: 2019.2.0

    Create or update a subnet.

    :param name: The name assigned to the subnet being created or updated.

    :param address_prefix: A valid CIDR block within the virtual network.

    :param virtual_network: The virtual network name containing the
        subnet.

    :param resource_group: The resource group name assigned to the
        virtual network.

    CLI Example:

    .. code-block:: bash

        salt-call azurearm_network.subnet_create_or_update testsubnet \
                  '10.0.0.0/24' testnet testgroup

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

    # Use NSG name to link to the ID of an existing NSG.
    if kwargs.get('network_security_group'):
        nsg = network_security_group_get(
            name=kwargs['network_security_group'],
            resource_group=resource_group,
            **kwargs
        )
        if 'error' not in nsg:
            kwargs['network_security_group'] = {'id': str(nsg['id'])}

    # Use Route Table name to link to the ID of an existing Route Table.
    if kwargs.get('route_table'):
        rt_table = route_table_get(
            name=kwargs['route_table'],
            resource_group=resource_group,
            **kwargs
        )
        if 'error' not in rt_table:
            kwargs['route_table'] = {'id': str(rt_table['id'])}

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

    try:
        subnet = netconn.subnets.create_or_update(
            resource_group_name=resource_group,
            virtual_network_name=virtual_network,
            subnet_name=name,
            subnet_parameters=snetmodel,
        )
        subnet.wait()
        sn_result = subnet.result()
        result = sn_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