Class: OsCtld::Commands::Container::FindByUgid

Inherits:
Base
  • Object
show all
Defined in:
lib/osctld/commands/container/find_by_ugid.rb

Instance Attribute Summary

Attributes inherited from Base

#client, #client_handler, #id, #opts

Instance Method Summary collapse

Methods inherited from Base

#base_execute, #call_cmd, #call_cmd!, #error, #error!, handle, #handled, #indirect?, #initialize, #manipulate, #manipulation_holder, #ok, #progress, #request_stop, run, run!

Constructor Details

This class inherits a constructor from OsCtld::Commands::Base

Instance Method Details

#check_ids!(ids) ⇒ Object (protected)



41
42
43
44
45
46
47
48
49
# File 'lib/osctld/commands/container/find_by_ugid.rb', line 41

def check_ids!(ids)
  ids.each do |id|
    next if id.is_a?(Integer) && id >= 0

    error!("#{id.inspect} is not a valid user/group ID")
  end

  ids
end

#executeObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/osctld/commands/container/find_by_ugid.rb', line 7

def execute
  uids = check_ids!(opts.fetch(:uids, []))
  gids = check_ids!(opts.fetch(:gids, []))

  by_uid = {}
  by_gid = {}

  DB::Containers.each do |ct|
    uids.each do |uid|
      next unless ct.user.uid_map.include_host_id?(uid)

      by_uid[uid] ||= []
      by_uid[uid] << {
        ns_id: ct.user.uid_map.host_to_ns(uid),
        ct: ct.export
      }
    end

    gids.each do |gid|
      next unless ct.user.gid_map.include_host_id?(gid)

      by_gid[gid] ||= []
      by_gid[gid] << {
        ns_id: ct.user.gid_map.host_to_ns(gid),
        ct: ct.export
      }
    end
  end

  ok({ by_uid: hash_to_array(by_uid), by_gid: hash_to_array(by_gid) })
end

#hash_to_array(hash) ⇒ Object (protected)



51
52
53
54
55
# File 'lib/osctld/commands/container/find_by_ugid.rb', line 51

def hash_to_array(hash)
  # JSON does not allow integer keys in a hash + osctld client code symbolizes names,
  # which break them as well.
  hash.map { |id, cts| [id, cts] }
end