class DigitalOceanInventory::Collector
Attributes
client[R]
config[R]
Public Class Methods
build(config)
click to toggle source
# File lib/digital_ocean_inventory/collector.rb, line 7 def self.build(config) new config end
new(config)
click to toggle source
# File lib/digital_ocean_inventory/collector.rb, line 11 def initialize(config) @config = config @client = config.client end
Public Instance Methods
call(inventory)
click to toggle source
# File lib/digital_ocean_inventory/collector.rb, line 16 def call(inventory) droplets = get_droplets .select { |d| select_droplet? d } .reject { |d| ignore_droplet? d } droplets.each do |droplet| groups = droplet_groups droplet groups.each { |g| inventory.add_group g } inventory.add_host droplet, groups end inventory end
Private Instance Methods
droplet_groups(droplet)
click to toggle source
# File lib/digital_ocean_inventory/collector.rb, line 71 def droplet_groups(droplet) groups = %w[digitalocean] groups << droplet.region.slug if config.group_by_region groups << droplet_vpc(droplet) droplet.tags &.uniq &.select { |t| t.match? /^ansible:g:/ } &.map { |t| t.gsub "ansible:g:", "" } &.each { |t| groups << t } groups.uniq.map { |g| g.gsub('-', '_') } end
droplet_vpc(droplet)
click to toggle source
# File lib/digital_ocean_inventory/collector.rb, line 85 def droplet_vpc(droplet) id = droplet.vpc_uuid vpc = config.client.vpcs.find id: id vpc.name end
get_droplets()
click to toggle source
# File lib/digital_ocean_inventory/collector.rb, line 44 def get_droplets resources = client.projects.list_resources id: project.id resources.select { |r| r.urn.include? "do:droplet" }.map do |r| ns, type, id = r.urn.split ":" client.droplets.find id: id end end
ignore_droplet?(droplet)
click to toggle source
# File lib/digital_ocean_inventory/collector.rb, line 57 def ignore_droplet?(droplet) ignore_tags = config.ignore_tags ignore_hosts = config.ignore_hosts if ignore_tags.empty? && ignore_hosts.empty? return false end return true if ignore_hosts.include? droplet.name tags = droplet.tags || [] ignore_tags.any? { |t| tags.include? t } end
project()
click to toggle source
# File lib/digital_ocean_inventory/collector.rb, line 33 def project if !config.project || config.project == "default" client.projects.find_default else proj = client.projects.all.find { |p| p.name == config.project } raise "Couldn't find project with name #{config.project}" unless proj proj end end
select_droplet?(droplet)
click to toggle source
# File lib/digital_ocean_inventory/collector.rb, line 52 def select_droplet?(droplet) tags = droplet.tags || [] tags.any? { |t| t == "ansible:enable" } end