Class: OsCtl::Lib::IdMap

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/libosctl/id_map.rb

Overview

IdMap represents ID mappings for user namespaces, be it user or group IDs

Defined Under Namespace

Classes: Entry

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(entries = []) ⇒ IdMap

Returns a new instance of IdMap.

Parameters:

  • entries (Array<Entry>) (defaults to: [])


36
37
38
# File 'lib/libosctl/id_map.rb', line 36

def initialize(entries = [])
  @entries = entries.clone
end

Instance Attribute Details

#entriesObject (readonly, protected)

Returns the value of attribute entries.



106
107
108
# File 'lib/libosctl/id_map.rb', line 106

def entries
  @entries
end

Class Method Details

.from_hash_list(list) ⇒ IdMap

Parameters:

  • list (Array<Hash>)

Returns:



31
32
33
# File 'lib/libosctl/id_map.rb', line 31

def self.from_hash_list(list)
  new(list.map { |hash| Entry.from_hash(hash) })
end

.from_string_list(list, **opts) ⇒ IdMap

Parameters:

  • list (Array<String>)

Returns:



25
26
27
# File 'lib/libosctl/id_map.rb', line 25

def self.from_string_list(list, **opts)
  new(list.map { |str| Entry.from_string(str, **opts) })
end

Instance Method Details

#==(other) ⇒ Object



100
101
102
# File 'lib/libosctl/id_map.rb', line 100

def ==(other)
  to_s == other.to_s
end

#add_from_hash(hash) ⇒ Object



56
57
58
# File 'lib/libosctl/id_map.rb', line 56

def add_from_hash(hash)
  @entries << Entry.from_hash(hash)
end

#add_from_string(str_entry) ⇒ Object



52
53
54
# File 'lib/libosctl/id_map.rb', line 52

def add_from_string(str_entry, **)
  @entries << Entry.from_string(str_entry, **)
end

#eachObject



60
61
62
# File 'lib/libosctl/id_map.rb', line 60

def each(&)
  entries.each(&)
end

#host_to_ns(id) ⇒ Object

Map ID from the host to the namespace



78
79
80
81
82
83
84
85
86
# File 'lib/libosctl/id_map.rb', line 78

def host_to_ns(id)
  entries.each do |e|
    if id >= e.host_id && id < (e.host_id + e.id_count)
      return (id - e.host_id) + e.ns_id
    end
  end

  raise Exceptions::IdMappingError.new(self, id)
end

#include_host_id?(id) ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
91
92
93
94
# File 'lib/libosctl/id_map.rb', line 88

def include_host_id?(id)
  entries.each do |e|
    return true if id >= e.host_id && id < (e.host_id + e.id_count)
  end

  false
end

#ns_to_host(id) ⇒ Object

Map ID from the namespace to the host



67
68
69
70
71
72
73
74
75
# File 'lib/libosctl/id_map.rb', line 67

def ns_to_host(id)
  entries.each do |e|
    if id >= e.ns_id && id < (e.ns_id + e.id_count)
      return e.host_id + (id - e.ns_id)
    end
  end

  raise Exceptions::IdMappingError.new(self, id)
end

#to_sObject



96
97
98
# File 'lib/libosctl/id_map.rb', line 96

def to_s
  entries.map(&:to_s).join(',')
end

#valid?Boolean

Returns:

  • (Boolean)


40
41
42
43
44
45
46
47
48
49
50
# File 'lib/libosctl/id_map.rb', line 40

def valid?
  return false if ns_to_host(0) < 0

  entries.each do |e|
    return false if e.ns_id < 0 || e.host_id < 0 || e.id_count < 1
  end

  true
rescue Exceptions::IdMappingError
  false
end