Class: OVH::Provisioner::APIList

Inherits:
Object
  • Object
show all
Includes:
Celluloid
Defined in:
lib/ovh/provisioner/api_list.rb

Overview

Represent an API list, that is a collection of a given kind of object

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_object, parent, *targets) ⇒ APIList

targets is a list of regex on object id to match nothing, do not set any target to match everything, add a ” target



33
34
35
36
37
38
39
40
# File 'lib/ovh/provisioner/api_list.rb', line 33

def initialize(api_object, parent, *targets)
  @parent = parent
  @url = api_object.entrypoint(parent).to_s
  @name = api_object.name.split('::').last
  @filter = generate_filter(targets)
  @futures = {}
  @list = []
end

Instance Attribute Details

#listObject (readonly)

Returns the value of attribute list.



28
29
30
# File 'lib/ovh/provisioner/api_list.rb', line 28

def list
  @list
end

Instance Method Details

#format(*fields) ⇒ Object



55
56
57
58
59
# File 'lib/ovh/provisioner/api_list.rb', line 55

def format(*fields)
  items = classify(list, *fields)
  items = deep_sort(items)
  yamlize(items)
end

#init_propertiesObject



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/ovh/provisioner/api_list.rb', line 42

def init_properties
  write = !@completed
  unless @completed
    # In ATOM concurrency, this is enough to avoid a race condition.
    @completed = true
    spawner = Actor[Spawner::NAME]
    get.keep_if { |s| @filter.call(s) }.each do |i|
      @futures[i] = spawner.future.get(@name, parent: @parent, id: i)
    end
  end
  wait(write)
end

#parallel_map(method, args = [], subject = nil) ⇒ Object



61
62
63
64
65
66
67
68
69
# File 'lib/ovh/provisioner/api_list.rb', line 61

def parallel_map(method, args = [], subject = nil)
  futures =
    if subject.nil?
      list.map { |item| item.future.send(method, *args) }
    else
      list.map { |item| subject.future.send(method, item, *args) }
    end
  futures.map(&:value)
end

#puts_each(method, args = [], subject = nil, remove_duplicate = true) ⇒ Object



71
72
73
74
75
# File 'lib/ovh/provisioner/api_list.rb', line 71

def puts_each(method, args = [], subject = nil, remove_duplicate = true)
  results = parallel_map(method, args, subject)
  results = results.map(&:lines).flatten.compact.uniq if remove_duplicate
  results.each { |i| puts i unless i.nil? }
end