Class: HP::Cloud::Accounts
- Inherits:
-
Object
- Object
- HP::Cloud::Accounts
- Defined in:
- lib/hpcloud/accounts.rb
Constant Summary collapse
- DEFAULT_ACCOUNT =
"default_account"
- TOP_LEVEL =
[:username, :provider]
- CREDENTIALS =
[:account_id, :secret_key, :auth_uri, :tenant_id, :userpass]
- ZONES =
[:compute, :"object storage", :cdn, :dns, :lbaas, :db, :network, :"block storage"]
- CATALOG =
we only list whats available in connection.rb
[:catalog_compute, :catalog_object_storage, :catalog_block_storage, :catalog_cdn, :catalog_dns, :catalog_load_balancer, :catalog_networking]
- OPTIONS =
[:connect_timeout, :read_timeout, :write_timeout, :ssl_verify_peer, :ssl_ca_path, :ssl_ca_file, :preferred_flavor, :preferred_win_image, :preferred_image]
- @@home =
nil
Instance Attribute Summary collapse
-
#directory ⇒ Object
readonly
Returns the value of attribute directory.
Class Method Summary collapse
Instance Method Summary collapse
- #copy(src, dest) ⇒ Object
- #create(account) ⇒ Object
- #create_options(account_name, zone, avl_zone = nil) ⇒ Object
- #get(account) ⇒ Object
- #get_acct_catalog_map(account_name) ⇒ Object
- #get_file_name(account) ⇒ Object
-
#initialize ⇒ Accounts
constructor
A new instance of Accounts.
- #list ⇒ Object
- #migrate ⇒ Object
- #read(account, createIt = false) ⇒ Object
- #remove(account) ⇒ Object
- #set(account, key, value) ⇒ Object
- #set_cred(account, cred) ⇒ Object
- #set_regions(account, regions) ⇒ Object
- #write(account) ⇒ Object
Constructor Details
#initialize ⇒ Accounts
Returns a new instance of Accounts.
63 64 65 66 67 68 69 |
# File 'lib/hpcloud/accounts.rb', line 63 def initialize if @@home.nil? @@home = ENV['HOME'] end @directory = @@home + "/.hpcloud/accounts/" @accts = {} end |
Instance Attribute Details
#directory ⇒ Object (readonly)
Returns the value of attribute directory.
27 28 29 |
# File 'lib/hpcloud/accounts.rb', line 27 def directory @directory end |
Class Method Details
.get_known ⇒ Object
75 76 77 78 79 80 81 82 |
# File 'lib/hpcloud/accounts.rb', line 75 def self.get_known ret = "" CREDENTIALS.each{|key| ret += "\n * " + key.to_s } ZONES.each{|key| ret += "\n * " + key.to_s } CATALOG.each{|key| ret += "\n * " + key.to_s } OPTIONS.each{|key| ret += "\n * " + key.to_s } return ret end |
.home_directory=(dir) ⇒ Object
71 72 73 |
# File 'lib/hpcloud/accounts.rb', line 71 def self.home_directory=(dir) @@home = dir end |
Instance Method Details
#copy(src, dest) ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/hpcloud/accounts.rb', line 110 def copy(src, dest) begin src_name = get_file_name(src) dest_name = get_file_name(dest) FileUtils.cp(src_name, dest_name) return true rescue Exception => e raise Exception.new('Error copying ' + src_name + ' to ' + dest_name) end return false end |
#create(account) ⇒ Object
158 159 160 161 162 163 164 165 166 167 |
# File 'lib/hpcloud/accounts.rb', line 158 def create(account) if @accts[account].nil? uri = Config.new.get(:default_auth_uri) @accts[account] = {:credentials=>{:auth_uri=>uri}, :regions=>{}, :catalog=>{}, :options=>{}} end return @accts[account] end |
#create_options(account_name, zone, avl_zone = nil) ⇒ Object
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 |
# File 'lib/hpcloud/accounts.rb', line 263 def (account_name, zone, avl_zone = nil) hsh = get(account_name) provider = hsh[:provider] || 'hp' provider = provider.downcase creds = hsh[:credentials] regions = hsh[:regions] || {} catalog = hsh[:catalog] || {} opts = hsh[:options] opts.delete(:preferred_flavor) opts.delete(:preferred_image) opts.delete(:preferred_win_image) opts.delete(:checker_url) opts.delete(:checker_deferment) unless zone.nil? avl_zone = avl_zone || regions[zone.to_s.downcase.to_sym] end = {} if provider == 'hp' [:hp_access_key] = creds[:account_id] || creds[:hp_access_key] || creds[:hp_account_id] [:hp_secret_key] = creds[:secret_key] || creds[:hp_secret_key] [:hp_use_upass_auth_style] = true if creds[:userpass] == true [:hp_auth_uri] = creds[:auth_uri] || creds[:hp_auth_uri] [:hp_tenant_id ] = creds[:tenant_id] || creds[:hp_tenant_id] [:hp_avl_zone] = avl_zone [:connection_options] = opts [:user_agent] = "HPCloud-UnixCLI/#{HP::Cloud::VERSION}" [:hp_service_type] = zone if zone != nil else = creds end [:provider] = provider return end |
#get(account) ⇒ Object
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/hpcloud/accounts.rb', line 209 def get(account) hsh = read(account).clone settings = Config.new.settings hsh[:provider] ||= 'hp' hsh[:options][:connect_timeout] ||= settings[:connect_timeout] hsh[:options][:read_timeout] ||= settings[:read_timeout] hsh[:options][:write_timeout] ||= settings[:write_timeout] hsh[:options][:preferred_flavor] ||= settings[:preferred_flavor] hsh[:options][:preferred_image] ||= settings[:preferred_image] hsh[:options][:connect_timeout] = hsh[:options][:connect_timeout].to_i hsh[:options][:read_timeout] = hsh[:options][:read_timeout].to_i hsh[:options][:write_timeout] = hsh[:options][:write_timeout].to_i if hsh[:options][:ssl_verify_peer].nil? hsh[:options][:ssl_verify_peer] = settings[:ssl_verify_peer] end if hsh[:options][:ssl_verify_peer].to_s == "false" || hsh[:options][:ssl_verify_peer].to_s == "no" hsh[:options][:ssl_verify_peer] = false else hsh[:options][:ssl_verify_peer] = true end hsh[:options][:ssl_ca_path] ||= settings[:ssl_ca_path] hsh[:options][:ssl_ca_path] ||= settings[:ssl_ca_path] hsh[:options][:ssl_ca_file] ||= settings[:ssl_ca_file] hsh[:options].delete_if{ |k,v| v.nil? } return hsh end |
#get_acct_catalog_map(account_name) ⇒ Object
252 253 254 255 256 257 258 259 260 261 |
# File 'lib/hpcloud/accounts.rb', line 252 def get_acct_catalog_map(account_name) hsh = get(account_name) provider = hsh[:provider] || 'hp' provider = provider.downcase catalog = hsh[:catalog] || {} = {} [:catalog] = catalog [:provider] = provider return end |
#get_file_name(account) ⇒ Object
84 85 86 |
# File 'lib/hpcloud/accounts.rb', line 84 def get_file_name(account) "#{@directory}#{account.to_s.downcase.gsub(' ', '_')}" end |
#list ⇒ Object
88 89 90 91 92 93 94 95 96 97 |
# File 'lib/hpcloud/accounts.rb', line 88 def list unless File.directory?("#{@directory}") return "No such file or directory - #{@directory}\n" + "Run account:setup to create accounts." end ray = Dir.entries("#{@directory}") ray.delete_if{|item| File.file?("#{@directory}/#{item}") == false } ray.sort! return ray.join("\n") end |
#migrate ⇒ Object
299 300 301 302 303 304 305 306 307 308 309 310 311 312 |
# File 'lib/hpcloud/accounts.rb', line 299 def migrate ray = list return false if ray.index('default').nil? return false unless ray.index('hp').nil? warn "Renaming account 'default' to 'hp'..." return false unless copy('default', 'hp') remove('default') config = Config.new(true) config.set(:default_account, 'hp') config.write() warn "Account 'hp' is now the default" return true end |
#read(account, createIt = false) ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/hpcloud/accounts.rb', line 122 def read(account, createIt=false) return @accts[account] if @accts[account].nil? == false file_name = get_file_name(account) if account == 'hp' && File.exists?(file_name) == false migrate end if File.exists?(file_name) begin hsh = YAML::load(File.open(file_name)) hsh[:credentials] = {} if hsh[:credentials].nil? if ((hsh[:zones].nil? == false) && (hsh[:regions].nil? == true)) regions = {} zones = hsh[:zones] regions[:compute] = zones[:compute_availability_zone] unless zones[:compute_availability_zone].nil? regions[:"object storage"] = zones[:storage_availability_zone] unless zones[:storage_availability_zone].nil? regions[:cdn] = zones[:cdn_availability_zone] unless zones[:cdn_availability_zone].nil? regions[:"block storage"] = zones[:block_availability_zone] unless zones[:block_availability_zone].nil? hsh[:zones] = nil hsh[:regions] = regions end hsh[:regions] = {} if hsh[:regions].nil? hsh[:catalog] = {} if hsh[:catalog].nil? hsh[:options] = {} if hsh[:options].nil? @accts[account] = hsh rescue Exception => e raise Exception.new('Error reading account file: ' + file_name) end else if createIt return create(account) end raise Exception.new('Could not find account file: ' + file_name) end return @accts[account] end |
#remove(account) ⇒ Object
99 100 101 102 103 104 105 106 107 108 |
# File 'lib/hpcloud/accounts.rb', line 99 def remove(account) begin file_name = get_file_name(account) File.delete(file_name) return true rescue Exception => e raise Exception.new('Error removing account file: ' + file_name) end return false end |
#set(account, key, value) ⇒ Object
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/hpcloud/accounts.rb', line 188 def set(account, key, value) hsh = @accts[account] return false if hsh.nil? key = key.to_sym if CREDENTIALS.include?(key) hsh[:credentials][key] = value elsif ZONES.include?(key) hsh[:regions][key] = value elsif CATALOG.include?(key) cat_key = key.to_s.gsub(/^catalog_/,'').to_sym hsh[:catalog][cat_key] = value elsif OPTIONS.include?(key) hsh[:options][key] = value elsif TOP_LEVEL.include?(key) hsh[key] = value else return false end return true end |
#set_cred(account, cred) ⇒ Object
169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/hpcloud/accounts.rb', line 169 def set_cred(account, cred) if @accts[account].nil? @accts[account] = {:credentials=>{}, :regions=>{}, :catalog=>{}, :options=>{}} end @accts[account][:credentials] = cred unless cred[:hp_auth_uri].nil? if cred[:hp_auth_uri].match(/hpcloud.net/) @accts[account][:options][:ssl_verify_peer] = false end end end |
#set_regions(account, regions) ⇒ Object
181 182 183 184 185 186 |
# File 'lib/hpcloud/accounts.rb', line 181 def set_regions(account, regions) if @accts[account].nil? @accts[account] = {:credentials=>{}, :regions=>{}, :catalog=>{}, :options=>{}} end @accts[account][:regions] = regions end |
#write(account) ⇒ Object
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
# File 'lib/hpcloud/accounts.rb', line 236 def write(account) config = @accts[account] if config.nil? raise Exception.new("Cannot find account information for #{account}") end file_name = get_file_name(account) begin FileUtils.mkpath(@directory) File.open(file_name, 'w') do |file| file.write config.to_yaml end rescue Exception => e raise Exception.new("Error writing file #{file_name}") end end |