<commit_before>""" The 'base' cloudformation stack that builds out the virtual datacenter.

This includes the VPC, it's subnets, availability zones, etc.
"""

from troposphere import (
    Ref, Output, Join, FindInMap, Select, GetAZs, Not, Equals, Tags
)
from troposphere import ec2
from troposphere.route53 import HostedZone, HostedZoneVPCs

from .base import Blueprint

NAT_INSTANCE_NAME = 'NatInstance%s'
GATEWAY = 'InternetGateway'
GW_ATTACH = 'GatewayAttach'
VPC_NAME = "VPC"
VPC_ID = Ref(VPC_NAME)
DEFAULT_SG = "DefaultSG"
NAT_SG = "NATSG"


class VPC(Blueprint):
    PARAMETERS = {
        "AZCount": {
            "type": "Number",
            "default": "2",
            "description": "The number of AZs to build the VPC in. NOTE: "
                           "this is used by stacker, not by cloudformation "
                           "directly."},
        "PrivateSubnets": {
            "type": "CommaDelimitedList",
            "description": "Comma separated list of subnets to use for "
                           "non-public hosts. NOTE: Must have as many subnets "
                           "as AZCount"},
        "PublicSubnets": {
            "type": "CommaDelimitedList",
            "description": "Comma separated list of subnets to use for "
                           "public hosts. NOTE: Must have as many subnets "
                           "as AZCount"},
        "InstanceType": {
            "type": "String",
            "description": "NAT EC2 instance type.",
            "default": "m3.medium"},
        "SshKeyName": {
            "type": "AWS::EC2::KeyPair::KeyName"},
        "BaseDomain": {
            "type": "String",
            "default": "",
            "description": "Base domain for the stack."},
        "InternalDomain": {
            "type": "String",
            "default": "",
            "description": "Internal domain name, if you have one."},
        "CidrBlock": {
            "type": "String",
            "description": "Base CIDR block for subnets.",
            "default": "10.128.0.0/16"},
        "ImageName": {
            "type": "String",
            "description": "The image name to use from the AMIMap (usually "
                           "found in the config file.)",
            "default": "NAT"},
    }

    def create_conditions(self):
        self.template.add_condition(
            "NoHostedZones",
            Equals(Ref("InternalDomain"), ""))
        self.template.add_condition(
            "HasHostedZones",
            Not(Equals(Ref("InternalDomain"), "")))

    def create_vpc(self):
        t = self.template
        t.add_resource(ec2.VPC(
            VPC_NAME,
            CidrBlock=Ref("CidrBlock"), EnableDnsSupport=True,
            EnableDnsHostnames=True))
<commit_msg>Factor BaseDomain into NoHostedZones and HasHostedZones conditions<commit_after>""" The 'base' cloudformation stack that builds out the virtual datacenter.

This includes the VPC, it's subnets, availability zones, etc.
"""

from troposphere import (
    Ref, Output, Join, FindInMap, Select, GetAZs, Not, Equals, Tags, And, Or
)
from troposphere import ec2
from troposphere.route53 import HostedZone, HostedZoneVPCs

from .base import Blueprint

NAT_INSTANCE_NAME = 'NatInstance%s'
GATEWAY = 'InternetGateway'
GW_ATTACH = 'GatewayAttach'
VPC_NAME = "VPC"
VPC_ID = Ref(VPC_NAME)
DEFAULT_SG = "DefaultSG"
NAT_SG = "NATSG"


class VPC(Blueprint):
    PARAMETERS = {
        "AZCount": {
            "type": "Number",
            "default": "2",
            "description": "The number of AZs to build the VPC in. NOTE: "
                           "this is used by stacker, not by cloudformation "
                           "directly."},
        "PrivateSubnets": {
            "type": "CommaDelimitedList",
            "description": "Comma separated list of subnets to use for "
                           "non-public hosts. NOTE: Must have as many subnets "
                           "as AZCount"},
        "PublicSubnets": {
            "type": "CommaDelimitedList",
            "description": "Comma separated list of subnets to use for "
                           "public hosts. NOTE: Must have as many subnets "
                           "as AZCount"},
        "InstanceType": {
            "type": "String",
            "description": "NAT EC2 instance type.",
            "default": "m3.medium"},
        "SshKeyName": {
            "type": "AWS::EC2::KeyPair::KeyName"},
        "BaseDomain": {
            "type": "String",
            "default": "",
            "description": "Base domain for the stack."},
        "InternalDomain": {
            "type": "String",
            "default": "",
            "description": "Internal domain name, if you have one."},
        "CidrBlock": {
            "type": "String",
            "description": "Base CIDR block for subnets.",
            "default": "10.128.0.0/16"},
        "ImageName": {
            "type": "String",
            "description": "The image name to use from the AMIMap (usually "
                           "found in the config file.)",
            "default": "NAT"},
    }

    def create_conditions(self):
        self.template.add_condition(
            "NoHostedZones",
            And(
                Equals(Ref("InternalDomain"), ""),
                Equals(Ref("BaseDomain"), ""),
            ))
        self.template.add_condition(
            "HasHostedZones",
            Or(
                Not(Equals(Ref("InternalDomain"), "")),
                Not(Equals(Ref("BaseDomain"), "")),
            ))

    def create_vpc(self):
        t = self.template
        t.add_resource(ec2.VPC(
            VPC_NAME,
            CidrBlock=Ref("CidrBlock"), EnableDnsSupport=True,
            EnableDnsHostnames=True))

{'fraction_non_alphanumeric': 0.12361111111111112, 'fraction_numerical': 0.005902777777777778, 'mean_word_length': 1.8590570719602977, 'pattern_counts': {'":': 64, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '88548', 'n_tokens_mistral': 1690, 'n_tokens_neox': 1631, 'n_words': 440}