require "devops-client/handler/deploy_envs/deploy_env"

class DeployEnvOpenstack < DeployEnv

  NAME = "openstack"
  attr_accessor :networks, :networks_table

  def initialize host, options, auth
    @host = host
    self.auth = auth
    self.options = options
  end

  def provider
    NAME
  end

  def fill obj
    super(obj) do |o|
      set_flavor(o)
      set_image(o)
      set_subnets(o)
      set_groups(o)
    end
  end

  def set_subnets d
    set_parameter d, :subnets do
      s = []
      begin
        s = resources_selector.select_available_network(table_title: I18n.t("handler.project.create.subnet.openstack"), provider: self.provider)
      end while s.empty?
      s
    end
  end

  def set_groups d
    set_parameter d, :groups do
      groups = resources_selector.select_available_groups(provider: self.provider)
      groups.map{|t| t['id']}
    end
  end

end
