class OVH::Provisioner::APIList

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

Attributes

list[R]

Public Class Methods

new(api_object, parent, *targets) click to toggle source

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

# 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

Public Instance Methods

format(*fields) click to toggle source
# File lib/ovh/provisioner/api_list.rb, line 55
def format(*fields)
  items = classify(list, *fields)
  items = deep_sort(items)
  yamlize(items)
end
init_properties() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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

Private Instance Methods

classify(items, *fields) click to toggle source
# File lib/ovh/provisioner/api_list.rb, line 100
def classify(items, *fields)
  items.each_with_object({}) do |item, hash|
    item = organize(item, *fields)
    insert!(hash, @url[1..-1].tr('/', '_') => item)
    hash
  end
end
deep_sort(hash) click to toggle source
# File lib/ovh/provisioner/api_list.rb, line 129
def deep_sort(hash)
  return hash.sort if hash.is_a?(Array)
  return hash unless hash.is_a?(Hash)

  result = {}
  hash.each { |k, v| result[k] = deep_sort(v) }
  result.sort.to_h
end
generate_filter(targets) click to toggle source
# File lib/ovh/provisioner/api_list.rb, line 92
def generate_filter(targets)
  lambda do |id|
    targets.reduce(false) do |acc, target|
      acc | (id.to_s =~ /#{target.to_s}/)
    end
  end
end
get(path = '') click to toggle source
# File lib/ovh/provisioner/api_list.rb, line 79
def get(path = '')
  OVH::Provisioner.client.get("#{@url}/#{path}")
end
insert!(hash, item) click to toggle source
# File lib/ovh/provisioner/api_list.rb, line 116
def insert!(hash, item)
  if hash.is_a?(Array)
    hash << item.first
  else
    key = item.keys.first
    if hash[key].nil?
      hash.merge!(item)
    else
      insert!(hash[key], item[key])
    end
  end
end
organize(item, *fields) click to toggle source
# File lib/ovh/provisioner/api_list.rb, line 108
def organize(item, *fields)
  head, *tail = *fields
  return [item.to_s] if head.nil?

  key = item.send(head.to_sym)
  { key => organize(item, *tail) }
end
prep_for_yaml(obj, token) click to toggle source
# File lib/ovh/provisioner/api_list.rb, line 144
def prep_for_yaml(obj, token)
  if obj.is_a?(Hash)
    obj.map { |k, v| [k, prep_for_yaml(v, token)] }.to_h
  elsif obj.is_a?(Array)
    obj.map { |e| prep_for_yaml(e, token) }
  elsif obj.is_a?(String)
    "#{obj}\n#{token}"
  else
    obj
  end
end
wait(write = false) click to toggle source
# File lib/ovh/provisioner/api_list.rb, line 83
def wait(write = false)
  @futures.each_pair do |key, future|
    v = future.value
    @list << v if write
    @futures.delete(key)
  end
  @list.sort! if write
end
yamlize(items) click to toggle source
# File lib/ovh/provisioner/api_list.rb, line 138
def yamlize(items)
  token = 'TO_REMOVE_AFTER_TOYAML'
  yaml = prep_for_yaml(items, token).to_yaml
  yaml.lines[1..-1].grep_v(/\- \|\-/).grep_v(/#{token}/).join('')
end