require "db/validators/deploy_env/flavor"
require "db/validators/deploy_env/image"
require "db/validators/deploy_env/subnet_not_empty"
require "db/validators/deploy_env/subnet_belongs_to_provider"
require "db/validators/deploy_env/groups"
require "db/validators/deploy_env/stack_template"

module Validators
  class DeployEnv::CloudParameters < Base

    def valid?
      is_stack = !@model.stack_template.nil?
      is_server_params = !@model.flavor.nil? or !@model.image.nil? or !@model.subnets.nil? or !@model.groups.nil?
      if !is_server_params and !is_stack
        @msg = "You should define 'stack_template' or all instance parameters (image, flavor, subnets, groups)"
        return false
      end
      if is_stack
        StackTemplate.new(@model).validate!
      end
      if is_server_params
        Flavor.new(@model).validate!
        Image.new(@model).validate!
        SubnetNotEmpty.new(@model).validate!
        SubnetBelongsToProvider.new(@model).validate!
        Groups.new(@model).validate!
      end
      return true
    rescue InvalidRecord => e
      @msg = e.message
      return false
    end

    def message
      @msg
    end

  end
end

