HeatTemplateFormatVersion: '2012-12-12'
Description: Instance creation template for {{datastore_manager}}
Parameters:
  Flavor:
    Type: String
  VolumeSize:
    Type: Number
    Default : '1'
  InstanceId:
    Type: String
  ImageId:
    Type: String
  DatastoreManager:
    Type: String
  AvailabilityZone:
    Type: String
    Default: nova
  TenantId:
    Type: String
Resources:
{% for port in ports %}
  {{ port.name }}:
    Type: OS::Neutron::Port
    Properties:
      network_id: "{{ port.net_id }}"
      security_groups: [{Ref: DatastoreSG}]
      {% if port.fixed_ip %}
      fixed_ips: [{"ip_address": "{{ port.fixed_ip }}"}]
      {% endif %}
{% endfor %}
  BaseInstance:
    Type: AWS::EC2::Instance
    Metadata:
      AWS::CloudFormation::Init:
        config:
          files:
            /etc/guest_info:
              content:
                Fn::Join:
                - ''
                - ["[DEFAULT]\nguest_id=", {Ref: InstanceId},
                  "\ndatastore_manager=", {Ref: DatastoreManager},
                  "\ntenant_id=", {Ref: TenantId}]
              mode: '000644'
              owner: root
              group: root
    Properties:
      ImageId: {Ref: ImageId}
      InstanceType: {Ref: Flavor}
      AvailabilityZone: {Ref: AvailabilityZone}
      SecurityGroups : [{Ref: DatastoreSG}]
      UserData:
        Fn::Base64:
          Fn::Join:
          - ''
          - ["#!/bin/bash -v\n",
              "/opt/aws/bin/cfn-init\n",
              "sudo service trove-guest start\n"]
{% if volume_support %}
  DataVolume:
    Type: AWS::EC2::Volume
    Properties:
      Size: {Ref: VolumeSize}
      AvailabilityZone: {Ref: AvailabilityZone}
      Tags:
      - {Key: Usage, Value: Test}
  MountPoint:
    Type: AWS::EC2::VolumeAttachment
    Properties:
      InstanceId: {Ref: BaseInstance}
      VolumeId: {Ref: DataVolume}
      Device: /dev/vdb
{% endif %}
  DatastoreSG:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupDescription: Default Security group for {{datastore_manager}}
{% if tcp_rules or udp_rules %}
      SecurityGroupIngress:
{% for rule in tcp_rules %}
      - IpProtocol: "tcp"
        FromPort: "{{rule.from_}}"
        ToPort: "{{rule.to_}}"
        CidrIp: "{{rule.cidr}}"
{% endfor %}
{% for rule in udp_rules %}
      - IpProtocol: "udp"
        FromPort: "{{rule.from_}}"
        ToPort: "{{rule.to_}}"
        CidrIp: "{{rule.cidr}}"
{% endfor %}
{% endif %}
  DatabaseIPAddress:
    Type: AWS::EC2::EIP
  DatabaseIPAssoc :
    Type: AWS::EC2::EIPAssociation
    Properties:
      InstanceId: {Ref: BaseInstance}
      EIP: {Ref: DatabaseIPAddress}
