#  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:
  =: com.intel.magnum.plugin
  std: io.murano

Name: MagnumBaymodel

Properties:
  name:
    Contract: $.string().notNull()

  imageId:
    Contract: $.string().notNull()

  flavorId:
    Contract: $.string()

  masterFlavorId:
    Contract: $.string()

  keypairId:
    Contract: $.string().notNull()

  externalNetworkId:
    Contract: $.string().notNull()

  fixedNetwork:
    Contract: $.string()

  coe:
    Contract: $.string().notNull().check($ in list(kubernetes, swarm, mesos))

  dnsNameServer:
    Contract: $.string()

  dockerVolumeSize:
    Contract: $.string()

  labels:
    Contract: $.string()

  httpProxy:
    Contract: $.string()

  httpsProxy:
    Contract: $.string()

  noProxy:
    Contract: $.string()

  networkDriver:
    Contract: $.string()

  volumeDriver:
    Contract: $.string()

  tlsDisabled:
    Contract: $.bool()

  public:
    Contract: $.bool()

  registryEnabled:
    Contract: $.bool()

Methods:
  .init:
    Body:
      - $._environment: $.find(std:Environment).require()
      - Try:
        - $._magnum: new('io.murano.extensions.mirantis.magnum.Magnum', $._environment)
        Catch:
          With: 'murano.dsl.exceptions.NoPackageForClassFound'
          Do:
            Throw: PluginNotFoundException
            Message: 'Plugin for interaction with Magnum is not installed'

  create:
    Body:
      - $msg: format('Creating Magnum baymodel {0}', $.name)
      - $._environment.reporter.report($this, $msg)
      - $params:
          name: $.name
          image_id: $.imageId
          keypair_id: $.keypairId
          external_network_id: $.externalNetworkId
          coe: $.coe
          flavor_id: $.flavorId
          master_flavor_id: $.masterFlavorId
          fixed_network: $.fixedNetwork
          dns_nameserver: $.dnsNameServer
          network_driver: $.networkDriver
          docker_volume_size: $.dockerVolumeSize
          labels: $.labels
          http_proxy: $.httpProxy
          https_proxy: $.httpsProxy
          no_proxy: $.noProxy
          volume_driver: $.volumeDriver
          tls_disabled: $.tlsDisabled
          public: $.public
          registry_enabled: $.registryEnabled
      - Try:
        - $baymodelId: $._magnum.createBaymodel($params)
        Catch:
          - As: e
            Do:
              - $formatString: 'Error: {0}'
              - $._environment.reporter.report_error($, $formatString.format($e.message))
              - Rethrow:
      - $.setAttr(baymodelId, $baymodeId)
      - $msg: format('Magnum baymodel is created {0}', $.name)
      - $._environment.reporter.report($this, $msg)
      - Return: $baymodelId

  delete:
    Body:
      - $baymodelId: $.getAttr(baymodelId, null)
      - $._magnum.deleteBaymodel($baymodelId)
      - $msg: format('Magnum baymodel {0} is deleted', $.name)
      - $._environment.reporter.report($this, $msg)
