namespace :opsmgr do

namespace :product do
  desc 'Upload a Pivotal Product'
  task :upload, [:environment, :om_version, :product_path, :product_name] do |_, args|
    require 'opsmgr/product_upload_wrapper'

    ProductUploadWrapper.wrap_upload(
      environment: args.environment,
      product_path: args.product_path,
      product_name: args.product_name,
      om_version: args.om_version
    )
  end

  desc 'Upload and Add a Pivotal Product'
  task :upload_add, [:environment, :om_version, :product_path, :product_name] do |_, args|
    require 'opsmgr/product_upload_wrapper'

    ProductUploadWrapper.wrap_add(
      environment: args.environment,
      product_path: args.product_path,
      product_name: args.product_name,
      om_version: args.om_version
    )
  end

  desc 'Upgrade a Generic Pivotal Product'
  task :upload_upgrade, [:environment, :om_version, :product_path, :product_name] do |_, args|
    require 'opsmgr/product_upload_wrapper'

    ProductUploadWrapper.wrap_upgrade(
      environment: args.environment,
      product_path: args.product_path,
      product_name: args.product_name,
      om_version: args.om_version
    )
  end

  desc 'Import a stemcell for a Generic Pivotal Product'
  task :import_stemcell, [:environment, :om_version, :stemcell_path, :product_name] do |_, args|
    require 'opsmgr/ui_helpers/ui_spec_runner'

    UiSpecRunner.new(
      environment: args.environment,
      om_version: args.om_version
    ).import_stemcell(args.stemcell_path, args.product_name)
  end

  desc 'Uncheck errands for a Generic Pivotal Product'
  task :uncheck_errands, [:environment, :om_version, :product_name] do |_, args|
    require 'opsmgr/ui_helpers/ui_spec_runner'

    UiSpecRunner.new(
      environment: args.environment,
      om_version: args.om_version
    ).uncheck_errands(args.product_name, args.extras)
  end

  desc 'Download staged manifest for a Generic Pivotal Product'
  task :download_staged_manifest, [:environment, :om_version, :product_name, :manifest_filepath] do |_, args|
    require 'opsmgr/environments'
    require 'opsmgr/cmd/ops_manager'
    require 'opsmgr/api/client'

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

    installed_products = opsmgr_cmd.installed_products(client)
    product_guid = installed_products.guid_for(args.product_name)
    opsmgr_cmd.download_staged_manifest(client, product_guid, args.manifest_filepath)
  end
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.