Class: Brightbox::BBConfig
- Inherits:
-
Object
- Object
- Brightbox::BBConfig
- Defined in:
- lib/bbcloud/config.rb
Instance Attribute Summary collapse
Class Method Summary collapse
Instance Method Summary collapse
- #[](k) ⇒ Object
- #cache_id(cid) ⇒ Object
- #cache_path ⇒ Object
- #clients ⇒ Object
- #config ⇒ Object
- #config_filename ⇒ Object
- #configured? ⇒ Boolean
- #delete_section(name) ⇒ Object
- #dir ⇒ Object
- #finish ⇒ Object
-
#initialize(options = {}) ⇒ BBConfig
constructor
A new instance of BBConfig.
- #oauth_token ⇒ Object
- #oauth_token_filename ⇒ Object
- #save! ⇒ Object
- #to_fog ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ BBConfig
Returns a new instance of BBConfig.
10 11 12 |
# File 'lib/bbcloud/config.rb', line 10 def initialize( = {}) @options = end |
Instance Attribute Details
#client_name ⇒ Object
98 99 100 101 102 103 104 105 |
# File 'lib/bbcloud/config.rb', line 98 def client_name if @client_name @client_name else default_client = config['core']['default_client'] @client_name = default_client || clients.first end end |
Class Method Details
.config ⇒ Object
14 15 16 17 18 19 20 |
# File 'lib/bbcloud/config.rb', line 14 def self.config Brightbox.const_set(:CONFIG, new()) require "bbcloud/gli_global_hooks" yield ensure CONFIG.finish() end |
Instance Method Details
#[](k) ⇒ Object
80 81 82 |
# File 'lib/bbcloud/config.rb', line 80 def [](k) config[k] end |
#cache_id(cid) ⇒ Object
67 68 69 |
# File 'lib/bbcloud/config.rb', line 67 def cache_id(cid) FileUtils.touch(File.join(cache_path, cid)) unless cid.nil? end |
#cache_path ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/bbcloud/config.rb', line 52 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 |
#clients ⇒ Object
94 95 96 |
# File 'lib/bbcloud/config.rb', line 94 def clients config.sections.find_all { |s| s != 'core' } end |
#config ⇒ Object
71 72 73 74 75 76 77 78 |
# File 'lib/bbcloud/config.rb', line 71 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_filename ⇒ Object
43 44 45 46 |
# File 'lib/bbcloud/config.rb', line 43 def config_filename dir @config_filename end |
#configured? ⇒ Boolean
152 153 154 |
# File 'lib/bbcloud/config.rb', line 152 def configured? client_name != nil and !clients.empty? end |
#delete_section(name) ⇒ Object
84 85 86 |
# File 'lib/bbcloud/config.rb', line 84 def delete_section(name) config.delete_section name end |
#dir ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/bbcloud/config.rb', line 22 def dir return @dir if @dir return nil if @dir == false @dir = File.(@options[:dir] || default_config_dir) @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 |
#finish ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/bbcloud/config.rb', line 139 def finish begin if configured? and @oauth_token != Api.conn.oauth_token File.open(oauth_token_filename + ".#{$$}", "w") do |f| f.write Api.conn.oauth_token end FileUtils.mv oauth_token_filename + ".#{$$}", oauth_token_filename end rescue StandardError => e warn "Error writing auth token #{oauth_token_filename}: #{e.class}: #{e}" end end |
#oauth_token ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/bbcloud/config.rb', line 124 def oauth_token if @oauth_token.nil? if File.exists? oauth_token_filename File.open(oauth_token_filename, "r") do |f| @oauth_token = f.read.chomp end @oauth_token else @oauth_token = false end else @oauth_token ? @oauth_token : nil end end |
#oauth_token_filename ⇒ Object
48 49 50 |
# File 'lib/bbcloud/config.rb', line 48 def oauth_token_filename @oauth_token_filename ||= File.join(dir, client_name + '.oauth_token') end |
#save! ⇒ Object
88 89 90 91 92 |
# File 'lib/bbcloud/config.rb', line 88 def save! if @config.is_a? Ini @config.write end end |
#to_fog ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/bbcloud/config.rb', line 107 def to_fog raise Ini::Error, "No api client configured" unless configured? 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 { :provider => 'Brightbox', :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 |