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



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

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

#add_from_hash(hash) ⇒ Object



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

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

#add_from_string(str_entry, **opts) ⇒ Object



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

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

#each(&block) ⇒ Object



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

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

#host_to_ns(id) ⇒ Object

Map ID from the host to the namespace



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

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

#include_host_id?(id) ⇒ Boolean

Returns:

  • (Boolean)


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

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

  false
end

#ns_to_host(id) ⇒ Object

Map ID from the namespace to the host



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

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



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

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

#valid?Boolean

Returns:

  • (Boolean)


40
41
42
43
44
45
46
47
48
49
50
51
# 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.count < 1
  end

  true

rescue Exceptions::IdMappingError
  false
end