Class: HP::Cloud::Connection
- Inherits:
-
Object
- Object
- HP::Cloud::Connection
- Defined in:
- lib/hpcloud/connection.rb
Constant Summary collapse
- VALID_SERVICES =
['storage','compute','cdn', 'block']
- @@instance =
Connection.new
Class Method Summary collapse
Instance Method Summary collapse
- #block ⇒ Object
- #catalog(name, service) ⇒ Object
- #cdn ⇒ Object
- #clear_options ⇒ Object
- #compute ⇒ Object
- #create_options(account_name, zone) ⇒ Object
- #dns ⇒ Object
- #get_account(account_name = nil) ⇒ Object
- #get_acct_catalog_map(account_name) ⇒ Object
-
#initialize ⇒ Connection
constructor
A new instance of Connection.
- #lb ⇒ Object
- #network ⇒ Object
- #read_creds(account, opts, service) ⇒ Object
- #reset_connections ⇒ Object
-
#service_type_name(account_name, name) ⇒ Object
lets identify the real service type name create support for account config to have a section called :service_catalog This section will identify mappings for keys like : object_storage, compute, block_storage, cdn networking, dns, and load_balancer The values can be the real names of the offerings.
- #set_options(options) ⇒ Object
- #storage(account_name = nil) ⇒ Object
- #tenants(account_name = nil) ⇒ Object
- #validate_account(account_name) ⇒ Object
- #write_creds(opts, connection) ⇒ Object
- #zones(service) ⇒ Object
Constructor Details
#initialize ⇒ Connection
Returns a new instance of Connection.
41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/hpcloud/connection.rb', line 41 def initialize @storage_connection = {} @compute_connection = {} @block_connection = {} @cdn_connection = {} @network_connection = {} @dns_connection = {} @lb_connection = {} @authcache = HP::Cloud::AuthCache.new @options = {} end |
Class Method Details
.get_services ⇒ Object
65 66 67 |
# File 'lib/hpcloud/connection.rb', line 65 def self.get_services() return VALID_SERVICES.join(', ') end |
.instance ⇒ Object
55 56 57 |
# File 'lib/hpcloud/connection.rb', line 55 def self.instance return @@instance end |
.is_service(name) ⇒ Object
61 62 63 |
# File 'lib/hpcloud/connection.rb', line 61 def self.is_service(name) return VALID_SERVICES.include?(name) end |
Instance Method Details
#block ⇒ Object
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/hpcloud/connection.rb', line 152 def block account = get_account() return @block_connection[account] unless @block_connection[account].nil? opts = (account, 'volume') opts.delete(:provider) read_creds(account, opts, 'volume') begin @block_connection[account] = Fog::HP::BlockStorageV2.new(opts) write_creds(opts, @block_connection[account]) rescue Exception => e @authcache.remove(opts) raise Fog::HP::Errors::ServiceError, "Please check your HP Cloud Services account to make sure the 'volume' service is activated for the appropriate availability zone.\n Exception: #{e}\n Print the service catalog: hpcloud account:catalog #{account}" end return @block_connection[account] end |
#catalog(name, service) ⇒ Object
254 255 256 257 258 259 260 261 262 263 264 265 |
# File 'lib/hpcloud/connection.rb', line 254 def catalog(name, service) rsp = validate_account(name) cata = rsp[:service_catalog] unless service.empty? hsh = {} service.each{ |x| hsh[x.to_sym] = cata[x.to_sym] } cata = hsh end return cata end |
#cdn ⇒ Object
168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/hpcloud/connection.rb', line 168 def cdn account = get_account() return @cdn_connection[account] unless @cdn_connection[account].nil? opts = (account, 'hpext:cdn') read_creds(account, opts, 'hpext:cdn') begin @cdn_connection[account] = Fog::CDN.new(opts) write_creds(opts, @cdn_connection[account]) rescue Exception => e @authcache.remove(opts) raise Fog::HP::Errors::ServiceError, "Please check your HP Cloud Services account to make sure the 'hpext:cdn' service is activated for the appropriate availability zone.\n Exception: #{e}\n Print the service catalog: hpcloud account:catalog #{account}" end return @cdn_connection[account] end |
#clear_options ⇒ Object
90 91 92 93 |
# File 'lib/hpcloud/connection.rb', line 90 def () @options = {} reset_connections() end |
#compute ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/hpcloud/connection.rb', line 131 def compute account = get_account() return @compute_connection[account] unless @compute_connection[account].nil? opts = (account, 'compute') opts[:version] = :v2 read_creds(account, opts, 'compute') begin @compute_connection[account] = Fog::Compute.new(opts) write_creds(opts, @compute_connection[account]) rescue Exception => e @authcache.remove(opts) raise Fog::HP::Errors::ServiceError, "Please check your HP Cloud Services account to make sure the 'compute' service is activated for the appropriate availability zone.\n Exception: #{e}\n Print the service catalog: hpcloud account:catalog #{account}" end begin @compute_connection[account].set_path('/v2/' + opts[:hp_tenant_id]) rescue Exception => e warn "Unable to enfoce v2 compute" end return @compute_connection[account] end |
#create_options(account_name, zone) ⇒ Object
236 237 238 239 240 241 |
# File 'lib/hpcloud/connection.rb', line 236 def (account_name, zone) real_zone_name = (zone != nil) ? service_type_name(account_name, zone): zone opts = Accounts.new.(account_name, real_zone_name, @options[:availability_zone]) opts[:hp_tenant_id] = @options[:tenantid] unless @options[:tenantid].nil? return opts end |
#dns ⇒ Object
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/hpcloud/connection.rb', line 199 def dns account = get_account() return @dns_connection[account] unless @dns_connection[account].nil? opts = (account, 'hpext:dns') read_creds(account, opts, 'hpext:dns') begin opts.delete(:provider) @dns_connection[account] = Fog::HP::DNS.new(opts) write_creds(opts, @dns_connection[account]) rescue Exception => e @authcache.remove(opts) raise Fog::HP::Errors::ServiceError, "Please check your HP Cloud Services account to make sure the 'hpext:dns' service is activated for the appropriate availability zone.\n Exception: #{e}\n Print the service catalog: hpcloud account:catalog #{account}" end return @dns_connection[account] end |
#get_account(account_name = nil) ⇒ Object
231 232 233 234 |
# File 'lib/hpcloud/connection.rb', line 231 def get_account(account_name = nil) return account_name unless account_name.nil? return @options[:account_name] || Config.new.get(:default_account) || 'hp' end |
#get_acct_catalog_map(account_name) ⇒ Object
243 244 245 |
# File 'lib/hpcloud/connection.rb', line 243 def get_acct_catalog_map(account_name) return Accounts.new.get_acct_catalog_map(account_name) end |
#lb ⇒ Object
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
# File 'lib/hpcloud/connection.rb', line 215 def lb account = get_account() return @lb_connection[account] unless @lb_connection[account].nil? opts = (account, 'hpext:lbaas') read_creds(account, opts, 'hpext:lbaas') begin opts.delete(:provider) @lb_connection[account] = Fog::HP::LB.new(opts) write_creds(opts, @lb_connection[account]) rescue Exception => e @authcache.remove(opts) raise Fog::HP::Errors::ServiceError, "Please check your HP Cloud Services account to make sure the 'hpext:lbaas' service is activated for the appropriate availability zone.\n Exception: #{e}\n Print the service catalog: hpcloud account:catalog #{account}" end return @lb_connection[account] end |
#network ⇒ Object
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/hpcloud/connection.rb', line 183 def network account = get_account() return @network_connection[account] unless @network_connection[account].nil? opts = (account, 'network') read_creds(account, opts, 'network') begin opts.delete(:provider) @network_connection[account] = Fog::HP::Network.new(opts) write_creds(opts, @network_connection[account]) rescue Exception => e @authcache.remove(opts) raise Fog::HP::Errors::ServiceError, "Please check your HP Cloud Services account to make sure the 'network' service is activated for the appropriate availability zone.\n Exception: #{e}\n Print the service catalog: hpcloud account:catalog #{account}" end return @network_connection[account] end |
#read_creds(account, opts, service) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/hpcloud/connection.rb', line 95 def read_creds(account, opts, service) creds = @authcache.read(opts) if creds.nil? return unless opts[:provider] == "hp" creds = validate_account(account) return if creds.nil? @authcache.write(opts, creds) end if opts[:hp_avl_zone].nil? opts[:hp_avl_zone] = @authcache.default_zone(opts, service_type_name(account, service)) end opts[:credentials] = creds end |
#reset_connections ⇒ Object
69 70 71 72 73 74 75 76 77 |
# File 'lib/hpcloud/connection.rb', line 69 def reset_connections @storage_connection = {} @compute_connection = {} @block_connection = {} @cdn_connection = {} @network_connection = {} @dns_connection = {} @lb_connection = {} end |
#service_type_name(account_name, name) ⇒ Object
lets identify the real service type name create support for account config to have a section called :service_catalog This section will identify mappings for keys like : object_storage, compute, block_storage, cdn
networking, dns, and load_balancer
The values can be the real names of the offerings. This enables private cloud implementations to have service catalog names that make more sense for there implementation.
287 288 289 290 291 292 293 294 295 296 297 298 299 300 |
# File 'lib/hpcloud/connection.rb', line 287 def service_type_name(account_name, name) map_opts = get_acct_catalog_map(account_name) case map_opts[:provider] when "hp" service_name = name unless map_opts[:catalog].nil? service_catalog_key = name.to_s.downcase.gsub(' ','_').to_sym service_name = map_opts[:catalog][service_catalog_key] if map_opts[:catalog].has_key?(service_catalog_key) end return service_name else return name end end |
#set_options(options) ⇒ Object
79 80 81 82 83 84 85 86 87 88 |
# File 'lib/hpcloud/connection.rb', line 79 def () if .nil? @options = {} return end if (@options[:availability_zone] != [:availability_zone]) reset_connections() end @options = end |
#storage(account_name = nil) ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/hpcloud/connection.rb', line 115 def storage(account_name=nil) account = get_account(account_name) return @storage_connection[account] unless @storage_connection[account].nil? opts = (account, 'object-store') read_creds(account, opts, 'object-store') begin @storage_connection[account] = Fog::Storage.new(opts) write_creds(opts, @storage_connection[account]) rescue Exception => e @authcache.remove(opts) respo = ErrorResponse.new(e).to_s raise Fog::HP::Errors::ServiceError, "Please check your HP Cloud Services account to make sure the 'Storage' service is activated for the appropriate availability zone.\n Exception: #{respo}\n Print the service catalog: hpcloud account:catalog #{account}" end return @storage_connection[account] end |
#tenants(account_name = nil) ⇒ Object
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 |
# File 'lib/hpcloud/connection.rb', line 302 def tenants(account_name = nil) account = get_account(account_name) opts = (account, nil) creds = read_creds(account, opts, 'whatever') service_url = "#{opts[:hp_auth_uri]}tenants/" = opts[:connection_options] connection = Fog::Connection.new(service_url, false, ) endpoint = URI.parse(opts[:hp_auth_uri]) scheme = endpoint.scheme host = endpoint.host port = endpoint.port.to_s path = endpoint.path.slice(1, endpoint.path.length) + 'tenants' request_body = {} auth_token = creds[:auth_token] response = connection.request( { :expects => 200, :headers => { 'X-Auth-Token' => auth_token }, :host => host, :port => port, :method => 'GET', :path => path, } ) YAML::load(response.body.to_s)["tenants"] end |
#validate_account(account_name) ⇒ Object
267 268 269 270 271 272 273 274 275 276 277 278 279 |
# File 'lib/hpcloud/connection.rb', line 267 def validate_account(account_name) = (account_name, nil) case [:provider] when "hp" unless [:connection_options].nil? [:ssl_verify_peer] = [:connection_options][:ssl_verify_peer] end Fog::HP.authenticate_v2(, [:connection_options]) else Fog::Storage.new().directories return true end end |
#write_creds(opts, connection) ⇒ Object
109 110 111 112 113 |
# File 'lib/hpcloud/connection.rb', line 109 def write_creds(opts, connection) if connection.respond_to? :credentials @authcache.write(opts, connection.credentials) end end |
#zones(service) ⇒ Object
247 248 249 250 251 252 |
# File 'lib/hpcloud/connection.rb', line 247 def zones(service) name = get_account(nil) cata = catalog(name, service) hsh = YAML::load(cata) hsh[service.to_sym].keys end |