#  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.

Namespaces:
  std: io.murano
  =: io.murano.resources

Name: InstanceAffinityGroup
Extends: std:CloudResource

Properties:

  affinity:
    Contract: $.bool().notNull()
    Default: false   #affinity: true, anti-affinity: false


Methods:
  deploy:
    Body:
      - If: $.affinity
        Then:
          - $policies: ['affinity']
        Else:
          - $policies: ['anti-affinity']
      - $name: $._getHeatName()
      - $affinityTemplate:
          resources:
            $name:
              type: 'OS::Nova::ServerGroup'
              properties:
                name: $name
                policies: $policies
      - $region: $.getRegion()
      - $region.stack.updateTemplate($affinityTemplate)
      - $region.stack.push(async => true)

  .destroy:
    Body:
      - $name: $._getHeatName()
      - $region: $.getRegion()
      - $template: $region.stack.current()
      - $template.resources: $template.resources.delete($name)
      - $region.stack.setTemplate($template)
      - $region.stack.push(async => true)


  getRef:
    Body:
      - Return:
          get_resource: $._getHeatName()

  _getHeatName:
    Body:
      - Return: format('NovaServerGroup-{0}', id($this))
