Namespaces:
    res: io.murano.resources
    sys: io.murano.system
    =: io.murano

Name: CloudRegion

Properties:
  name:
    Contract: $.string()

  agentListener:
    Contract: $.class(sys:AgentListener)
    Usage: Runtime

  stack:
    Contract: $.class(sys:HeatStack)
    Usage: Runtime

  defaultNetworks:
    Contract:
      environment: $.class(res:Network)
      flat: $.class(res:Network)

  securityGroupManager:
    Contract: $.class(sys:SecurityGroupManager)
    Usage: Runtime

Methods:
  getConfig:
    Body:
      - Return: $._environment.regionConfigs.get(
          $.name, $._environment.regionConfigs.get(''))

  .init:
    Body:
      - $._environment: $.find(Environment).require()
      - $generatedStackName: $.getAttr(generatedStackName)
      - If: $generatedStackName = null
        Then:
          - $generatedStackName: list($.name, randomName()).join('-')
          - $.setAttr(generatedStackName, $generatedStackName)
      - $this.agentListener: new(sys:AgentListener, $this, name => $generatedStackName)
      - $stackDescriptionFormat: 'This stack was generated by Murano for environment {0} (ID: {1}) - region {2}'
      - $this.stack: new(sys:HeatStack,
          regionName => $.name,
          name => 'murano-' + $generatedStackName,
          description => $stackDescriptionFormat.format($._environment.name, id($._environment), $.name))
      - sys:GC.subscribeDestruction($this, $this.stack)

      - $this.securityGroupManager:
          coalesce($.defaultNetworks.environment, $.defaultNetworks.flat)?.
            generateSecurityGroupManager()

  .destroy:
    Body:
      - $.stack.delete()
