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

Instance Method Summary collapse

Constructor Details

#initialize(str_entries = [], opts = {}) ⇒ IdMap

Returns a new instance of IdMap



19
20
21
# File 'lib/libosctl/id_map.rb', line 19

def initialize(str_entries = [], opts = {})
  @entries = str_entries.map { |str| Entry.from_string(str, opts) }
end

Instance Attribute Details

#entriesObject (readonly, protected)

Returns the value of attribute entries



77
78
79
# File 'lib/libosctl/id_map.rb', line 77

def entries
  @entries
end

Instance Method Details

#==(other) ⇒ Object



72
73
74
# File 'lib/libosctl/id_map.rb', line 72

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

#add_from_string(str_entry, opts = {}) ⇒ Object



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

def add_from_string(str_entry, opts = {})
  @entries << Entry.from_string(str_entry, opts)
end

#each(&block) ⇒ Object



40
41
42
# File 'lib/libosctl/id_map.rb', line 40

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

#host_to_ns(id) ⇒ Object

Map ID from the host to the namespace



58
59
60
61
62
63
64
65
66
# File 'lib/libosctl/id_map.rb', line 58

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

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

#ns_to_host(id) ⇒ Object

Map ID from the namespace to the host



47
48
49
50
51
52
53
54
55
# File 'lib/libosctl/id_map.rb', line 47

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

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

#to_sObject



68
69
70
# File 'lib/libosctl/id_map.rb', line 68

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

#valid?Boolean

Returns:

  • (Boolean)


23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/libosctl/id_map.rb', line 23

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.count < 1
  end

  true

rescue Exceptions::IdMappingError
  false
end