<commit_before>#
#
#
module ManageIQ::Providers
  class StorageManager::CinderManager::RefreshParser < ManageIQ::Providers::CloudManager::RefreshParser
    include ManageIQ::Providers::StorageManager::CinderManager::RefreshHelperMethods
    include Vmdb::Logging

    attr_accessor :data

    def self.ems_inv_to_hashes(ems, options = nil)
      new(ems, options).ems_inv_to_hashes
    end

    def initialize(ems, options = nil)
      @ems               = ems
      @connection        = ems.connect
      @options           = options || {}
      @data              = {}
      @data_index        = {}

      @cinder_service    = ems.parent_manager.cinder_service
    end

    def ems_inv_to_hashes
      log_header = "MIQ(#{self.class.name}.#{__method__}) Collecting data for EMS name: [#{@ems.name}] id: [#{@ems.id}]"

      $fog_log.info("#{log_header}...")
      get_volumes
      get_snapshots
      # TODO: volume backup
      # get_backups

      $fog_log.info("#{log_header}...Complete")

      link_storage_associations

      @data
    end

    def volumes
      @volumes ||= @cinder_service.handled_list(:volumes)
    end

    def get_volumes
      process_collection(volumes, :cloud_volumes) { |volume| parse_volume(volume) }
    end

    def get_snapshots
      process_collection(@cinder_service.handled_list(:list_snapshots_detailed,
                                                      :__request_body_index => "snapshots"),
                         :cloud_volume_snapshots) { |snap| parse_snapshot(snap) }
    end

    def get_backups
      process_collection(@cinder_service.handled_list(:list_backups_detailed,
                                                      :__request_body_index => "backups"),
                         :cloud_volume_backups) { |backup| parse_backup(backup) }
    end

    def parse_backup(backup)
      uid = backup['id']
      new_result = {
        :ems_ref               => uid,
        :type                  => "ManageIQ::Providers::Openstack::CloudManager::CloudVolumeBackup",
        # Supporting both Cinder v1 and Cinder v2
        :name                  => backup['display_name'] || backup['name'],
        :status                => backup['status'],
        :creation_time         => backup['created_at'],
        # Supporting both Cinder v1 and Cinder v2
        :description           => backup['display_description'] || backup['description'],
        :size                  => backup['size'].to_i.gigabytes,
        :object_count          => backup['object_count'].to_i,
        :is_incremental        => backup['is_incremental'],
        :has_dependent_backups => backup['has_dependent_backups'],
        :availability_zone     => @data_index.fetch_path(:availability_zones,
                                  "volume-" + backup['availability_zone'] || "null_az"),
        :volume                => @data_index.fetch_path(:cloud_volumes, backup['volume_id'])
      }
      return uid, new_result
    end

    def parse_snapshot(snap)
      uid = snap['id']
      new_result = {
        :ems_ref       => uid,
        :type          => "ManageIQ::Providers::Openstack::CloudManager::CloudVolumeSnapshot",
        # Supporting both Cinder v1 and Cinder v2<commit_msg>Enable cinder to handle cloud volume backup<commit_after>#
#
#
module ManageIQ::Providers
  class StorageManager::CinderManager::RefreshParser < ManageIQ::Providers::CloudManager::RefreshParser
    include ManageIQ::Providers::StorageManager::CinderManager::RefreshHelperMethods
    include Vmdb::Logging

    attr_accessor :data

    def self.ems_inv_to_hashes(ems, options = nil)
      new(ems, options).ems_inv_to_hashes
    end

    def initialize(ems, options = nil)
      @ems               = ems
      @connection        = ems.connect
      @options           = options || {}
      @data              = {}
      @data_index        = {}

      @cinder_service    = ems.parent_manager.cinder_service
    end

    def ems_inv_to_hashes
      log_header = "MIQ(#{self.class.name}.#{__method__}) Collecting data for EMS name: [#{@ems.name}] id: [#{@ems.id}]"

      $fog_log.info("#{log_header}...")
      get_volumes
      get_snapshots
      get_backups

      $fog_log.info("#{log_header}...Complete")

      link_storage_associations

      @data
    end

    def volumes
      @volumes ||= @cinder_service.handled_list(:volumes)
    end

    def get_volumes
      process_collection(volumes, :cloud_volumes) { |volume| parse_volume(volume) }
    end

    def get_snapshots
      process_collection(@cinder_service.handled_list(:list_snapshots_detailed,
                                                      :__request_body_index => "snapshots"),
                         :cloud_volume_snapshots) { |snap| parse_snapshot(snap) }
    end

    def get_backups
      process_collection(@cinder_service.list_backups_detailed.body["backups"],
                         :cloud_volume_backups) { |backup| parse_backup(backup) }
    end

    def parse_backup(backup)
      uid = backup['id']
      new_result = {
        :ems_ref               => uid,
        :type                  => "ManageIQ::Providers::Openstack::CloudManager::CloudVolumeBackup",
        # Supporting both Cinder v1 and Cinder v2
        :name                  => backup['display_name'] || backup['name'],
        :status                => backup['status'],
        :creation_time         => backup['created_at'],
        # Supporting both Cinder v1 and Cinder v2
        :description           => backup['display_description'] || backup['description'],
        :size                  => backup['size'].to_i.gigabytes,
        :object_count          => backup['object_count'].to_i,
        :is_incremental        => backup['is_incremental'],
        :has_dependent_backups => backup['has_dependent_backups'],
        :availability_zone     => @data_index.fetch_path(:availability_zones,
                                  "volume-" + backup['availability_zone'] || "null_az"),
        :volume                => @data_index.fetch_path(:cloud_volumes, backup['volume_id'])
      }
      return uid, new_result
    end

    def parse_snapshot(snap)
      uid = snap['id']
      new_result = {
        :ems_ref       => uid,
        :type          => "ManageIQ::Providers::Openstack::CloudManager::CloudVolumeSnapshot",
        # Supporting both Cinder v1 and Cinder v2
{'fraction_non_alphanumeric': 0.11153059619317288, 'fraction_numerical': 0.0018876828692779614, 'mean_word_length': 2.2111111111111112, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6650406', 'n_tokens_mistral': 1869, 'n_tokens_neox': 1798, 'n_words': 312}