namespace :opsmgr do

namespace :microbosh do
  desc 'Print out a copy-pasteable partial bosh command (eg bosh -t example.com -u user -p password)'
  task :command, [:environment, :om_version] do |_, args|
    require 'opsmgr/cmd/bosh_command'
    puts Opsmgr::Cmd::BoshCommand.new(
      env_name: args.environment,
      om_version: args.om_version
    ).command
  end

  desc 'Print out a bosh command to set the target to the bosh for the given environment'
  task :target, [:environment, :om_version] do |_, args|
    require 'opsmgr/cmd/bosh_command'
    puts Opsmgr::Cmd::BoshCommand.new(
      env_name: args.environment,
      om_version: args.om_version
    ).target
  end

  desc 'Print out the ip address of the director'
  task :director_ip, [:environment, :om_version] do |_, args|
    require 'opsmgr/cmd/bosh_command'
    puts Opsmgr::Cmd::BoshCommand.new(
      env_name: args.environment,
      om_version: args.om_version
    ).director_ip
  end

  desc 'Configure Microbosh [:environment, :om_version]'
  task :configure, [:environment, :om_version] do |_, args|
    require 'opsmgr/ui_helpers/ui_spec_runner'
    require 'opsmgr/environments'
    require 'opsmgr/api/client'
    require 'opsmgr/cmd/ops_manager'

    environment = Opsmgr::Environments.for(args.environment)
    client = Opsmgr::Api::Client.new(environment, args.om_version)
    Opsmgr::Cmd::OpsManager.new(environment).configure_microbosh_infrastructure(client)

    UiSpecRunner.new(
      environment: args.environment,
      om_version: args.om_version
    ).configure_microbosh
  end
end

desc 'Download BOSH Stemcell from bosh.io'
task :download_stemcell, [:iaas, :version] do |_, args|
  case args.iaas
  when 'aws'
    system("wget --content-disposition --progress=dot:giga https://bosh.io/d/stemcells/bosh-aws-xen-hvm-ubuntu-trusty-go_agent?v=#{args.version}")
  when 'vsphere'
    system("wget --content-disposition --progress=dot:giga https://bosh.io/d/stemcells/bosh-vsphere-esxi-ubuntu-trusty-go_agent?v=#{args.version}")
  when 'vcloud'
    system("wget --content-disposition --progress=dot:giga https://bosh.io/d/stemcells/bosh-vcloud-esxi-ubuntu-trusty-go_agent?v=#{args.version}")
  when 'openstack'
    system("wget --content-disposition --progress=dot:giga https://bosh.io/d/stemcells/bosh-openstack-kvm-ubuntu-trusty-go_agent-raw?v=#{args.version}")
  end
  system('ls *bosh*.tgz > stemcell_reference.txt')
end

desc 'run a bosh command'
task :run_command, [:environment_name, :om_version, :command] do |_, args|
  require 'opsmgr/cmd/bosh_command'
  require 'opsmgr/log'
  require 'opsmgr/bosh_command_runner'

  logger = Opsmgr.logger_for('Rake')
  bosh_command = Opsmgr::Cmd::BoshCommand.new(
    env_name: args.environment_name,
    om_version: args.om_version
  )
  Opsmgr::BoshCommandRunner.new(
    bosh_command: bosh_command,
    logger: logger
  ).run(args.command)
end

desc 'run a bosh command with a deployment'
task :run_command_with_deployment, [:environment_name, :om_version, :command, :deployment] do |_, args|
  require 'opsmgr/cmd/bosh_command'
  require 'opsmgr/log'
  require 'opsmgr/bosh_command_runner'

  logger = Opsmgr.logger_for('Rake')
  bosh_command = Opsmgr::Cmd::BoshCommand.new(
    env_name: args.environment_name,
    om_version: args.om_version
  )
  Opsmgr::BoshCommandRunner.new(
    bosh_command: bosh_command,
    logger: logger
  ).run_with_deployment(args.command, args.deployment)
end

end # Copyright © 2014-2015 Pivotal Software, Inc. # All rights reserved. # THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, # INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR # PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE # USE OR OTHER DEALINGS IN THE SOFTWARE.