HeatTemplateFormatVersion: '2012-12-12'
Description: 'HEAT Template - Dedicated MySQL server'
Parameters:
  KeyName: 
    Description: Name of an existing EC2 KeyPair to enable SSH access to the instances
    Type: String
    Default: default
  InstanceType:
    Description: Use this flavor
    Type: String
    Default: bm.small
  KeystoneUser:
    Description: Keystone database username.
    Type: String
    Default: keystone
  HeatUser:
    Description: Heat database username.
    Type: String
    Default: heat
  MySQLImage:
    Type: String
  BootstrapHost:
    Description: Load mysqldump from this Host
    Type: String
    Default: ''
  BootstrapRootPassword:
    Description: Root password for localhost access after bootstrap
    Type: String
    Default: ''
  BootstrapDumpPassword:
    Description: Password to use for mysqldump from Bootstrap Host
    Type: String
    Default: ''
  BootstrapSlaveUser:
    Description: User to use for replication from bootstrap host
    Type: String
    Default: ''
  BootstrapSlavePassword:
    Description: Password to use with BootstrapSlaveUser
    Type: String
    Default: ''
Resources:
  ApiAccessPolicy:
    Type: OS::Heat::AccessPolicy
    Properties:
      AllowedResources: [ MySQL ]
  ApiUser:
    Type: AWS::IAM::User
    Properties:
      Policies: [ { Ref: ApiAccessPolicy } ]
  ApiKey:
    Type: AWS::IAM::AccessKey
    Properties:
      UserName:
        Ref: ApiUser
  MySQL:
    Type: AWS::EC2::Instance
    Metadata:
      OpenStack::ImageBuilder::Elements: [ mysql-migration ]
      mysql:
        create-users:
          - database: keystone
            username: {Ref: KeystoneUser}
            userhandle: {Ref: KeystonePasswordHandle}
          - database: heat
            username: {Ref: HeatUser}
            userhandle: {Ref: HeatPasswordHandle}
      mysql-migration:
        users:
          root:
            username: root
            password: {Ref: BootstrapRootPassword}
          dump:
            username: dump
            password: {Ref: BootstrapDumpPassword}
        bootstrap_host: {Ref: BootstrapHost}
        slave_user: {Ref: BootstrapSlaveUser}
        slave_password: {Ref: BootstrapSlavePassword}
      heat:
        access_key_id:
          Ref: ApiKey
        secret_key:
          Fn::GetAtt: [ ApiKey, SecretAccessKey ]
        stack:
          name: {Ref: 'AWS::StackName'}
          region: {Ref: 'AWS::Region'}
        refresh:
          - resource: MySQL
    Properties:
      ImageId:
        {Ref: MySQLImage}
      InstanceType: {Ref: InstanceType}
      KeyName: {Ref: KeyName}
  HeatPasswordHandle:
    Type: AWS::CloudFormation::WaitConditionHandle
  HeatPassword:
    DependsOn: MySQL
    Type: AWS::CloudFormation::WaitCondition
    Properties:
      Handle: {Ref: HeatPasswordHandle}
      Timeout: 90
  KeystonePasswordHandle:
    Type: AWS::CloudFormation::WaitConditionHandle
  KeystonePassword:
    DependsOn: MySQL
    Type: AWS::CloudFormation::WaitCondition
    Properties:
      Handle: {Ref: KeystonePasswordHandle}
      Timeout: 90
Outputs:
    MySQLHost: 
        Fn::GetAtt: [ MySQL , PrivateIp ]
    KeystonePassword:
        Fn::GetAtt: [ KeystonePassword, Data ]
    KeystoneUser: {Ref: KeystoneUser}
    HeatPassword:
        Fn::GetAtt: [ HeatPassword, Data ]
    HeatUser: {Ref: HeatUser}