Class: HP::Cloud::Connection

Inherits:
Object
  • Object
show all
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

Constructor Details

#initializeConnection

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_servicesObject



65
66
67
# File 'lib/hpcloud/connection.rb', line 65

def self.get_services()
  return VALID_SERVICES.join(', ')
end

.instanceObject



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

#blockObject



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/hpcloud/connection.rb', line 152

def block
   = ()
  return @block_connection[] unless @block_connection[].nil?
  opts = create_options(, 'volume')
  opts.delete(:provider)
  read_creds(, opts, 'volume')
  begin
    @block_connection[] = Fog::HP::BlockStorageV2.new(opts)
    write_creds(opts, @block_connection[])
  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 #{}"
  end
  return @block_connection[]
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 = (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

#cdnObject



168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/hpcloud/connection.rb', line 168

def cdn
   = ()
  return @cdn_connection[] unless @cdn_connection[].nil?
  opts = create_options(, 'hpext:cdn')
  read_creds(, opts, 'hpext:cdn')
  begin
    @cdn_connection[] = Fog::CDN.new(opts)
    write_creds(opts, @cdn_connection[])
  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 #{}"
  end
  return @cdn_connection[]
end

#clear_optionsObject



90
91
92
93
# File 'lib/hpcloud/connection.rb', line 90

def clear_options()
  @options = {}
  reset_connections()
end

#computeObject



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
   = ()
  return @compute_connection[] unless @compute_connection[].nil?
  opts = create_options(, 'compute')
  opts[:version] = :v2
  read_creds(, opts, 'compute')
  begin
    @compute_connection[] = Fog::Compute.new(opts)
    write_creds(opts, @compute_connection[])
  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 #{}"
  end
  begin
    @compute_connection[].set_path('/v2/' + opts[:hp_tenant_id])
  rescue Exception => e
    warn "Unable to enfoce v2 compute"
  end
  return @compute_connection[]
end

#create_options(account_name, zone) ⇒ Object



236
237
238
239
240
241
# File 'lib/hpcloud/connection.rb', line 236

def create_options(, zone)
  real_zone_name = (zone != nil) ? service_type_name(, zone): zone
  opts = Accounts.new.create_options(, real_zone_name, @options[:availability_zone])
  opts[:hp_tenant_id] = @options[:tenantid] unless @options[:tenantid].nil?
  return opts
end

#dnsObject



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/hpcloud/connection.rb', line 199

def dns
   = ()
  return @dns_connection[] unless @dns_connection[].nil?
  opts = create_options(, 'hpext:dns')
  read_creds(, opts, 'hpext:dns')
  begin
    opts.delete(:provider)
    @dns_connection[] = Fog::HP::DNS.new(opts)
    write_creds(opts, @dns_connection[])
  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 #{}"
  end
  return @dns_connection[]
end

#get_account(account_name = nil) ⇒ Object



231
232
233
234
# File 'lib/hpcloud/connection.rb', line 231

def ( = nil)
  return  unless .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()
  return Accounts.new.get_acct_catalog_map()
end

#lbObject



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/hpcloud/connection.rb', line 215

def lb
   = ()
  return @lb_connection[] unless @lb_connection[].nil?
  opts = create_options(, 'hpext:lbaas')
  read_creds(, opts, 'hpext:lbaas')
  begin
    opts.delete(:provider)
    @lb_connection[] = Fog::HP::LB.new(opts)
    write_creds(opts, @lb_connection[])
  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 #{}"
  end
  return @lb_connection[]
end

#networkObject



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/hpcloud/connection.rb', line 183

def network
   = ()
  return @network_connection[] unless @network_connection[].nil?
  opts = create_options(, 'network')
  read_creds(, opts, 'network')
  begin
    opts.delete(:provider)
    @network_connection[] = Fog::HP::Network.new(opts)
    write_creds(opts, @network_connection[])
  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 #{}"
  end
  return @network_connection[]
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(, opts, service)
  creds = @authcache.read(opts)
  if creds.nil?
    return unless opts[:provider] == "hp"
    creds = ()
    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(, service))
  end
  opts[:credentials] = creds
end

#reset_connectionsObject



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(, name)
  map_opts = get_acct_catalog_map()
  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 set_options(options)
  if options.nil?
    @options = {}
    return
  end
  if (@options[:availability_zone] != options[:availability_zone])
    reset_connections()
  end
  @options = 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(=nil)
   = ()
  return @storage_connection[] unless @storage_connection[].nil?
  opts = create_options(, 'object-store')
  read_creds(, opts, 'object-store')
  begin
    @storage_connection[] = Fog::Storage.new(opts)
    write_creds(opts, @storage_connection[])
  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 #{}"
  end
  return @storage_connection[]
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( = nil)
   = ()
  opts = create_options(, nil)
  creds = read_creds(, opts, 'whatever')
  service_url = "#{opts[:hp_auth_uri]}tenants/"
  connection_options = opts[:connection_options]
  connection = Fog::Connection.new(service_url, false, connection_options)
  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 ()
  options = create_options(, nil)
  case options[:provider]
  when "hp"
    unless options[:connection_options].nil?
      options[:ssl_verify_peer] = options[:connection_options][:ssl_verify_peer]
    end
    Fog::HP.authenticate_v2(options, options[:connection_options])
  else
    Fog::Storage.new(options).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 = (nil)
  cata = catalog(name, service)
  hsh = YAML::load(cata)
  hsh[service.to_sym].keys
end