Class: BBConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/bbcloud/config.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ BBConfig

Returns a new instance of BBConfig.



9
10
11
# File 'lib/bbcloud/config.rb', line 9

def initialize(options = {})
  @options = options
end

Instance Attribute Details

#client_nameObject



85
86
87
88
89
90
91
92
# File 'lib/bbcloud/config.rb', line 85

def client_name
  if @client_name
    @client_name
  else
    default_client = config['core']['default_client']
    @client_name = default_client || clients.first
  end
end

Instance Method Details

#[](k) ⇒ Object



67
68
69
# File 'lib/bbcloud/config.rb', line 67

def [](k)
  config[k]
end

#cache_id(cid) ⇒ Object



54
55
56
# File 'lib/bbcloud/config.rb', line 54

def cache_id(cid)
  FileUtils.touch(File.join(cache_path, cid)) unless cid.nil?
end

#cache_pathObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/bbcloud/config.rb', line 39

def cache_path
  if @cache_path
    @cache_path
  else
    @cache_path = File.join(@dir, 'cache')
    unless File.exists? @cache_path
      begin
        FileUtils.mkdir @cache_path
      rescue Errno::EEXIST
      end
    end
    @cache_path
  end
end

#clientsObject



81
82
83
# File 'lib/bbcloud/config.rb', line 81

def clients
  config.sections.find_all { |s| s != 'core' }
end

#configObject



58
59
60
61
62
63
64
65
# File 'lib/bbcloud/config.rb', line 58

def config
  return @config if @config
  return {} if @config == false
  @config ||= Ini.new config_filename
  @config
rescue Ini::Error => e
  raise BBConfigError, "Config problem in #{config_filename}: #{e}"
end

#config_filenameObject



34
35
36
37
# File 'lib/bbcloud/config.rb', line 34

def config_filename
  dir
  @config_filename
end

#delete_section(name) ⇒ Object



71
72
73
# File 'lib/bbcloud/config.rb', line 71

def delete_section(name)
  config.delete_section name
end

#dirObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/bbcloud/config.rb', line 13

def dir
  return @dir if @dir
  return nil if @dir == false
  @dir = File.expand_path(@options[:dir] || "~/.brightbox")
  @config_filename = File.join(@dir, 'config')
  # Make the directory if necessary
  unless File.exists? @dir
    begin
      FileUtils.mkdir @dir
    rescue Errno::EEXIST
    end
  end
  # 
  if File.directory? @dir
    @dir
  else
    @dir = false
    nil
  end
end

#save!Object



75
76
77
78
79
# File 'lib/bbcloud/config.rb', line 75

def save!
  if @config.is_a? Ini
    @config.write
  end
end

#to_fogObject

Raises:



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/bbcloud/config.rb', line 94

def to_fog
  raise Ini::Error, "No api client configured" if clients.empty?
  c = config[client_name]
  %w{api_url client_id secret}.each do |k|
    if c[k].to_s.empty?
      raise BBConfigError, "#{k} option missing from config in section #{client_name}"
    end
  end
  { 
    :brightbox_api_url => c['api_url'],
    :brightbox_auth_url => c['auth_url'] || c['api_url'],
    :brightbox_client_id => c['client_id'],
    :brightbox_secret => c['secret']
  }
end