#
# Author:: <NAME> <<EMAIL>>
#
# Copyright 2014, Balanced, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

from troposphere.ec2 import NetworkInterfaceProperty

import stratosphere
from stratosphere import Base64, Ref

from .base import Template, RoleMixin


class GatewayInstance(stratosphere.ec2.Instance):
    def AvailabilityZone(self):
        return self.template.subnet()['AvailabilityZone']

    def IamInstanceProfile(self):
        return Ref(self.template.insp())

    def ImageId(self):
        return Ref(self.template.param_AmiId())

    def InstanceType(self):
        return 'm1.small' # yolo

    def KeyName(self):
        return Ref(self.template.param_KeyName())

    def NetworkInterfaces(self):
        return [NetworkInterfaceProperty(
            AssociatePublicIpAddress=True,
            DeviceIndex='0',
            GroupSet=[Ref(self.template.sg())],
            SubnetId=Ref(self.template.subnet()),
        )]

    def SourceDestCheck(self):
        return False

    def UserData(self):
        return Base64(''.join([
            '#!/bin/bash -xe\n',
            '/opt/bootstrap.sh nat production role-nat\n',
        ]))


class BalancedGateway(RoleMixin, Template):
    """NAT gateway configuration."""

    def param_AvailabilityZone(self):
        """Availability zone."""
        return {'Type': 'String'}

    def param_Cidr(self):
        """CIDR block for this network."""
        return {'Type': 'String'}

    def param_AmiId(self):
        """AMI ID."""
        return {'Type': 'String'}

    def param_PublicRouteTableId(self):
        """Route table to use for public subnet."""
        return {'Type': 'String'}

    def subnet(self):
        """Gateway network subnet."""
        return {
            'VpcId': Ref(self.param_VpcId()),
            'AvailabilityZone': Ref(self.param_AvailabilityZone()),
            'CidrBlock': Ref(self.param_Cidr()),
        }

    def srta(self):
        """Association between the subnet and the public subnet route table."""
        return {
            'RouteTableId': Ref(self.param_PublicRouteTableId()),
            'SubnetId': Ref(self.subnet()),
        }

    def sg(self):
        """Security group for gateway instance."""
        return {'AllowSSH': True}

    def instance(self):
        """EC2 instance serving as a NAT gateway."""
        return GatewayInstance('GatewayInstance', template=self)

    def out_Instance(self):
        """Gateway instance ID."""
        return {'Value': Ref(self.instance())}

    def out_SecurityGroup(self):
        """Gateway security group."""
        return {'Value': Ref(self.sg())}


if __name__ == '__main__':
    print(BalancedGateway().to_json())

#
# Author:: <NAME> <<EMAIL>>
#
# Copyright 2014, Balanced, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

from troposphere.ec2 import NetworkInterfaceProperty

import stratosphere
from stratosphere import Base64, Ref

from .base import Template, RoleMixin


class GatewayInstance(stratosphere.ec2.Instance):
    def AvailabilityZone(self):
        return self.template.subnet()['AvailabilityZone']

    def IamInstanceProfile(self):
        return Ref(self.template.insp())

    def ImageId(self):
        return Ref(self.template.param_AmiId())

    def InstanceType(self):
        return 'm1.small' # yolo

    def KeyName(self):
        return Ref(self.template.param_KeyName())

    def NetworkInterfaces(self):
        return [NetworkInterfaceProperty(
            AssociatePublicIpAddress=True,
            DeviceIndex='0',
            GroupSet=[Ref(self.template.sg())],
            SubnetId=Ref(self.template.subnet()),
        )]

    def SourceDestCheck(self):
        return False

    def UserData(self):
        return Base64(''.join([
            '#!/bin/bash -xe\n',
            '/opt/bootstrap.sh nat production role-nat\n',
        ]))


class BalancedGateway(RoleMixin, Template):
    """NAT gateway configuration."""

    def param_AvailabilityZone(self):
        """Availability zone."""
        return {'Type': 'String'}

    def param_Cidr(self):
        """CIDR block for this network."""
        return {'Type': 'String'}

    def param_AmiId(self):
        """AMI ID."""
        return {'Type': 'String'}

    def param_PublicRouteTableId(self):
        """Route table to use for public subnet."""
        return {'Type': 'String'}

    def subnet(self):
        """Gateway network subnet."""
        return {
            'VpcId': Ref(self.param_VpcId()),
            'AvailabilityZone': Ref(self.param_AvailabilityZone()),
            'CidrBlock': Ref(self.param_Cidr()),
        }

    def srta(self):
        """Association between the subnet and the public subnet route table."""
        return {
            'RouteTableId': Ref(self.param_PublicRouteTableId()),
            'SubnetId': Ref(self.subnet()),
        }

    def sg(self):
        """Security group for gateway instance."""
        return {'AllowSSH': True}

    def instance(self):
        """EC2 instance serving as a NAT gateway."""
        return GatewayInstance('GatewayInstance', template=self)

    def out_Instance(self):
        """Gateway instance ID."""
        return {'Value': Ref(self.instance())}

    def out_SecurityGroup(self):
        """Gateway security group."""
        return {'Value': Ref(self.sg())}


if __name__ == '__main__':
    print(BalancedGateway().to_json())

# # Author:: <NAME> <<EMAIL>> # # Copyright 2014, Balanced, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # # yolo NAT gateway configuration. Availability zone. CIDR block for this network. AMI ID. Route table to use for public subnet. Gateway network subnet. Association between the subnet and the public subnet route table. Security group for gateway instance. EC2 instance serving as a NAT gateway. Gateway instance ID. Gateway security group.