Sha256: 4314c16ac461093292f833d5527aacd67d84bdca4cc3e6b8bf24f7aa4affccf8

Contents?: true

Size: 1.3 KB

Versions: 49

Compression:

Stored size: 1.3 KB

Contents

module VmShepherd
  module Vcloud
    class Destroyer
      def initialize(client:, vdc_name:)
        @client = client
        @vdc_name = vdc_name
      end

      def clean_catalog_and_vapps(catalog:, vapp_names:, logger:)
        clean_vapps(vapp_names, logger)
        delete_catalog(catalog)
      end

      private

      def vdc
        @vdc ||= @client.find_vdc_by_name(@vdc_name)
      end

      def clean_vapps(vapp_names, logger)
        vapp_names.each do |vapp_name|
          begin
            clean_vapp(vapp_name)
            delete_vapp(vapp_name)
          rescue VCloudSdk::ObjectNotFoundError => e
            logger.debug "Could not delete vapp '#{vapp_name}': #{e.inspect}"
          end
        end
      end

      def clean_vapp(vapp_name)
        vapp = vdc.find_vapp_by_name(vapp_name)
        vapp.vms.map do |vm|
          vm.independent_disks.map do |disk|
            vm.detach_disk(disk)
            vdc.delete_disk_by_name(disk.name)
          end
        end
      end

      def delete_vapp(vapp_name)
        vapp = vdc.find_vapp_by_name(vapp_name)

        if vapp.vms.any?
          vapp.power_off
          vapp.delete
        end
      end

      def delete_catalog(catalog)
        @client.delete_catalog_by_name(catalog) if @client.catalog_exists?(catalog)
      end
    end
  end
end

Version data entries

49 entries across 49 versions & 1 rubygems

Version Path
vm_shepherd-3.7.3 lib/vm_shepherd/vcloud/destroyer.rb
vm_shepherd-3.7.1 lib/vm_shepherd/vcloud/destroyer.rb
vm_shepherd-3.7.0 lib/vm_shepherd/vcloud/destroyer.rb
vm_shepherd-3.6.2 lib/vm_shepherd/vcloud/destroyer.rb
vm_shepherd-3.6.1 lib/vm_shepherd/vcloud/destroyer.rb
vm_shepherd-3.6.0 lib/vm_shepherd/vcloud/destroyer.rb
vm_shepherd-3.5.0 lib/vm_shepherd/vcloud/destroyer.rb
vm_shepherd-3.4.3 lib/vm_shepherd/vcloud/destroyer.rb
vm_shepherd-3.4.2 lib/vm_shepherd/vcloud/destroyer.rb
vm_shepherd-3.4.1 lib/vm_shepherd/vcloud/destroyer.rb
vm_shepherd-3.4.0 lib/vm_shepherd/vcloud/destroyer.rb
vm_shepherd-3.3.2 lib/vm_shepherd/vcloud/destroyer.rb
vm_shepherd-3.3.1 lib/vm_shepherd/vcloud/destroyer.rb
vm_shepherd-3.3.0 lib/vm_shepherd/vcloud/destroyer.rb
vm_shepherd-3.2.1 lib/vm_shepherd/vcloud/destroyer.rb
vm_shepherd-3.2.0 lib/vm_shepherd/vcloud/destroyer.rb
vm_shepherd-3.1.8 lib/vm_shepherd/vcloud/destroyer.rb
vm_shepherd-3.1.7 lib/vm_shepherd/vcloud/destroyer.rb
vm_shepherd-3.1.6 lib/vm_shepherd/vcloud/destroyer.rb
vm_shepherd-3.1.5 lib/vm_shepherd/vcloud/destroyer.rb