class DigitalOceanInventory::Config

Constants

CONFIG_FILE_PATHS

Attributes

cache_max_age[R]
cache_path[R]
client[R]
group_by_region[R]
group_by_tag[R]
ignore_hosts[R]
ignore_tags[R]
project[R]
select_tags[R]
token[R]

Public Class Methods

new() click to toggle source
# File lib/digital_ocean_inventory/config.rb, line 25
def initialize
  conf = load_from_file.merge load_from_env

  @token = conf[:access_token]
  raise MissingAccessTokenError unless @token

  @client = DropletKit::Client.new access_token: @token

  @project         = conf.fetch :project, "default"
  @select_tags     = conf.fetch :select_tags, []
  @ignore_tags     = conf.fetch :ignore_tags, []
  @ignore_hosts    = conf.fetch :ignore_hosts, []
  @group_by_region = conf.fetch :group_by_region, true
  @group_by_tag    = conf.fetch :group_by_tag, true
  @cache_path      = conf.fetch :cache_path, "."
  @cache_max_age   = conf.fetch :cache_max_age, 900
end

Public Instance Methods

cache_file() click to toggle source
# File lib/digital_ocean_inventory/config.rb, line 43
def cache_file
  "#{cache_path}/.ansible_digital_ocean_inventory_cache.json"
end

Private Instance Methods

load_from_env() click to toggle source
# File lib/digital_ocean_inventory/config.rb, line 49
def load_from_env
  token           = ENV.fetch "DO_ACCESS_TOKEN", nil
  project         = ENV.fetch "DO_INVENTORY_PROJECT", nil
  select_tags     = ENV.fetch "DO_INVENTORY_SELECT_TAGS", nil
  select_tags     = select_tags.split "," if select_tags
  ignore_tags     = ENV.fetch "DO_INVENTORY_IGNORE_TAGS", nil
  ignore_tags     = ignore_tags.split "," if ignore_tags
  ignore_hosts    = ENV.fetch "DO_INVENTORY_IGNORE_HOSTS", nil
  ignore_hosts    = ignore_hosts.split "," if ignore_hosts
  group_by_region = ENV.fetch "DO_INVENTORY_GROUP_BY_REGION", nil
  group_by_tag    = ENV.fetch "DO_INVENTORY_GROUP_BY_TAG", nil
  cache_path      = ENV.fetch "DO_INVENTORY_CACHE_PATH", nil
  cache_max_age   = ENV.fetch "DO_INVENTORY_CACHE_MAX_AGE", nil


  OpenStruct.new.tap do |c|
    c.access_token = token if token
    c.project = project if project
    c.select_tags = select_tags if select_tags
    c.ignore_tags = ignore_tags if ignore_tags
    c.ignore_hosts = ignore_hosts if ignore_hosts
    c.group_by_region = group_by_region unless group_by_region.nil?
    c.group_by_tag = group_by_tag unless group_by_tag.nil?
    c.cache_path = cache_path unless cache_path.nil?
    c.cache_max_age = cache_max_age unless cache_max_age.nil?
  end.to_h
end
load_from_file() click to toggle source
# File lib/digital_ocean_inventory/config.rb, line 77
def load_from_file
  file = ENV.fetch "DO_INVENTORY_CONFIG_FILE", nil
  file ||= CONFIG_FILE_PATHS.find { |p| File.exists? p }

  return {} unless file

  YAML.load_file file
end