import yaml


def generate_fw_template(destination_ranges, project, network, firewall_rule_name, firewall_rule_description):
    """
    Generates a firewall rule manifest.

    :param destination_ranges: Destination ipv4 CIDR ranges
    :param project: Google Cloud Platform project name
    :param network: Google Cloud network name
    :param firewall_rule_name: firewall rule name
    :param firewall_rule_description: custom firewall rule description to be appended to the default description
    :return: dumped yaml string of the manifest
    """
    firewall = {
        "resources": [{
            "name": firewall_rule_name,
            "type": "compute.v1.firewall",
            "properties": {
               "network": f"projects/{project}/global/networks/{network}",
               "direction": "EGRESS",
               "priority": 65530,
               "description": f"Allowing VMs to reach all Google services based on a difference-set of "
                              f"https://www.gstatic.com/ipranges/cloud.json and goog.json. "
                              f"{firewall_rule_description}",
               "allowed": [{
                   "IPProtocol": "tcp",
                   "ports": [80, 443, 8080]
               }],
               "targetTags": ["allow-egress-google-services"]
            }
        }]
    }

    firewall['resources'][0]['properties']['destinationRanges'] = destination_ranges
    return yaml.dump(firewall, default_flow_style=False)