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.
29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/hpcloud/connection.rb', line 29 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
53 54 55 |
# File 'lib/hpcloud/connection.rb', line 53 def self.get_services() return VALID_SERVICES.join(', ') end |
.instance ⇒ Object
43 44 45 |
# File 'lib/hpcloud/connection.rb', line 43 def self.instance return @@instance end |
.is_service(name) ⇒ Object
49 50 51 |
# File 'lib/hpcloud/connection.rb', line 49 def self.is_service(name) return VALID_SERVICES.include?(name) end |
Instance Method Details
#block ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/hpcloud/connection.rb', line 135 def block account = get_account() return @block_connection[account] unless @block_connection[account].nil? opts = (account, 'Block Storage') opts.delete(:provider) read_creds(account, opts, 'Block Storage') 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 'BlockStorage' 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
237 238 239 240 241 242 243 244 245 246 247 248 |
# File 'lib/hpcloud/connection.rb', line 237 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
151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/hpcloud/connection.rb', line 151 def cdn account = get_account() return @cdn_connection[account] unless @cdn_connection[account].nil? opts = (account, 'CDN') read_creds(account, opts, '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 '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
78 79 80 81 |
# File 'lib/hpcloud/connection.rb', line 78 def () @options = {} reset_connections() end |
#compute ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/hpcloud/connection.rb', line 119 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 return @compute_connection[account] end |
#create_options(account_name, zone) ⇒ Object
219 220 221 222 223 224 |
# File 'lib/hpcloud/connection.rb', line 219 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
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/hpcloud/connection.rb', line 182 def dns account = get_account() return @dns_connection[account] unless @dns_connection[account].nil? opts = (account, 'DNS') read_creds(account, opts, '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 '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
214 215 216 217 |
# File 'lib/hpcloud/connection.rb', line 214 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
226 227 228 |
# File 'lib/hpcloud/connection.rb', line 226 def get_acct_catalog_map(account_name) return Accounts.new.get_acct_catalog_map(account_name) end |
#lb ⇒ Object
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/hpcloud/connection.rb', line 198 def lb account = get_account() return @lb_connection[account] unless @lb_connection[account].nil? opts = (account, 'Load Balancer') read_creds(account, opts, 'Load Balancer') 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 'Load Balancer' 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
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/hpcloud/connection.rb', line 166 def network account = get_account() return @network_connection[account] unless @network_connection[account].nil? opts = (account, 'Networking') read_creds(account, opts, 'Networking') 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
83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/hpcloud/connection.rb', line 83 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
57 58 59 60 61 62 63 64 65 |
# File 'lib/hpcloud/connection.rb', line 57 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.
270 271 272 273 274 275 276 277 278 279 280 281 282 283 |
# File 'lib/hpcloud/connection.rb', line 270 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
67 68 69 70 71 72 73 74 75 76 |
# File 'lib/hpcloud/connection.rb', line 67 def () if .nil? @options = {} return end if (@options[:availability_zone] != [:availability_zone]) reset_connections() end @options = end |
#storage(account_name = nil) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/hpcloud/connection.rb', line 103 def storage(account_name=nil) account = get_account(account_name) return @storage_connection[account] unless @storage_connection[account].nil? opts = (account, 'Object Storage') read_creds(account, opts, 'Object Storage') 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
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 |
# File 'lib/hpcloud/connection.rb', line 285 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
250 251 252 253 254 255 256 257 258 259 260 261 262 |
# File 'lib/hpcloud/connection.rb', line 250 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
97 98 99 100 101 |
# File 'lib/hpcloud/connection.rb', line 97 def write_creds(opts, connection) if connection.respond_to? :credentials @authcache.write(opts, connection.credentials) end end |
#zones(service) ⇒ Object
230 231 232 233 234 235 |
# File 'lib/hpcloud/connection.rb', line 230 def zones(service) name = get_account(nil) cata = catalog(name, service) hsh = YAML::load(cata) hsh[service.to_sym].keys end |