'Create a firewall on a network.
Firewall rules should be supplied in the "allowed" field.  This is a
list of dictionaries formated like so ("ports" is optional)::
[{"IPProtocol": "<protocol string or number>",
"ports": "<port_numbers or ranges>"}]
For example, to allow tcp on port 8080 and udp on all ports, \'allowed\'
would be::
[{"IPProtocol": "tcp",
"ports": ["8080"]},
{"IPProtocol": "udp"}]
See `Firewall Reference <https://developers.google.com/compute/docs/
reference/latest/firewalls/insert>`_ for more information.
:param  name: Name of the firewall to be created
:type   name: ``str``
:param  allowed: List of dictionaries with rules
:type   allowed: ``list`` of ``dict``
:keyword  network: The network that the firewall applies to.
:type     network: ``str`` or :class:`GCENetwork`
:keyword  source_ranges: A list of IP ranges in CIDR format that the
firewall should apply to. Defaults to
[\'0.0.0.0/0\']
:type     source_ranges: ``list`` of ``str``
:keyword  source_tags: A list of source instance tags the rules apply
to.
:type     source_tags: ``list`` of ``str``
:keyword  target_tags: A list of target instance tags the rules apply
to.
:type     target_tags: ``list`` of ``str``
:return:  Firewall object
:rtype:   :class:`GCEFirewall`'

def ex_create_firewall(self, name, allowed, network='default', source_ranges=None, source_tags=None, target_tags=None):

    firewall_data = {}
    if (not hasattr(network, 'name')):
        nw = self.ex_get_network(network)
    else:
        nw = network
    firewall_data['name'] = name
    firewall_data['allowed'] = allowed
    firewall_data['network'] = nw.extra['selfLink']
    if ((source_ranges is None) and (source_tags is None)):
        source_ranges = ['0.0.0.0/0']
    if (source_ranges is not None):
        firewall_data['sourceRanges'] = source_ranges
    if (source_tags is not None):
        firewall_data['sourceTags'] = source_tags
    if (target_tags is not None):
        firewall_data['targetTags'] = target_tags
    request = '/global/firewalls'
    self.connection.async_request(request, method='POST', data=firewall_data)
    return self.ex_get_firewall(name)
