Class: VolumeSweeper::Providers::Oci

Inherits:
Base
  • Object
show all
Defined in:
lib/volume_sweeper/providers/oci.rb

Constant Summary collapse

DEFAULT_REGION =
'me-jeddah-1'
DEFAULT_PAGE_SIZE =
30
VOLUME_ATTRS =
%i{ id displayName volumeId lifecycleState sizeInGBs timeCreated definedTags freeformTags }

Instance Attribute Summary collapse

Attributes inherited from Base

#base_link

Instance Method Summary collapse

Methods inherited from Base

#delete_volumes, #scan_volumes

Constructor Details

#initialize(config_path: nil, region: nil, mode: :audit, **kwargs) ⇒ Oci

Returns a new instance of Oci.



19
20
21
22
23
24
# File 'lib/volume_sweeper/providers/oci.rb', line 19

def initialize config_path: nil, region: nil, mode: :audit, **kwargs
  super
  @region ||= DEFAULT_REGION
  @base_link = "https://cloud.oracle.com/block-storage/volumes"
  validate_attrs
end

Instance Attribute Details

#config_locationObject

Returns the value of attribute config_location.



17
18
19
# File 'lib/volume_sweeper/providers/oci.rb', line 17

def config_location
  @config_location
end

Instance Method Details

#delete_block_volumes(ids_list) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/volume_sweeper/providers/oci.rb', line 67

def delete_block_volumes ids_list
  @log.msg "oci: #{ids_list&.count || 0} block volumes are eligible for cleanup."
  return if ids_list.blank?

  unless @run_mode == :delete
    @log.msg "oci: running in :#{@run_mode} mode, exiting without delete operations."
    return
  end

  @log.msg "oci: unused volume clean-up operation started."

  ids_list.each do |id|
    @log.msg "oci: deleting block volume #{id} .."
    run_api_call do |config|
      api = OCI::Core::BlockstorageClient.new config: config, region: @region
      output = api.delete_volume id, compartment_id: @compartment_id
      if output.status.to_s =~ /2\d\d/
        @log.msg "oci: block volume #{id} is deleted successfully."
      else
        @log.msg "oci: block volume #{id} has failed."
      end
      sleep 2.5
    end
  end
end

#scan_block_volumesObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/volume_sweeper/providers/oci.rb', line 26

def scan_block_volumes
  volumes = Array.new
  opts = { compartment_id: @compartment_id, limit: DEFAULT_PAGE_SIZE, lifecycle_state: 'AVAILABLE' }

  run_api_call do |config|
    api = OCI::Core::BlockstorageClient.new config: config, region: @region
    page = nil
    begin
      output = api.list_volumes **opts.merge({ page: page })
      page = output.headers['opc-next-page']
      output.data.map { |v| volumes << v.to_hash.compact.slice(*VOLUME_ATTRS) }
      sleep 2
    end until page.nil? || page&.empty?
    @log.msg "oci: collected #{volumes.size} block volumes from the compartment."
  end

  volume_attachments = Array.new
  opts = { limit: DEFAULT_PAGE_SIZE }
  run_api_call do |config|
    api = OCI::Core::ComputeClient.new config: config, region: @region
    page = nil
    begin
      output = api.list_volume_attachments @compartment_id, **opts.merge({ page: page })
      page = output.headers['opc-next-page']
      output.data.map do |v|
        volume_attachments << v.to_hash.compact.slice(*VOLUME_ATTRS) if v.lifecycle_state =~ /ATTACH/
      end
      sleep 2
    end until page.nil? || page&.empty?
    @log.msg "oci: collected #{volume_attachments.size} block volume attachements from the compartment."
  end

  @log.msg "oci: filtering out any block volume with an active attachment."
  result = volumes.reject do |v|
    volume_attachments.any? { |va| va[:volumeId] == v[:id] }
  end || []

  @log.msg "oci: found #{result.size} unattached block volumes."
  [volumes.size, result]
end