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.



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_servicesObject



53
54
55
# File 'lib/hpcloud/connection.rb', line 53

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

.instanceObject



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

#blockObject



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/hpcloud/connection.rb', line 135

def block
   = ()
  return @block_connection[] unless @block_connection[].nil?
  opts = create_options(, 'Block Storage')
  opts.delete(:provider)
  read_creds(, opts, 'Block Storage')
  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 'BlockStorage' 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



237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/hpcloud/connection.rb', line 237

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



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

def cdn
   = ()
  return @cdn_connection[] unless @cdn_connection[].nil?
  opts = create_options(, 'CDN')
  read_creds(, opts, '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 '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



78
79
80
81
# File 'lib/hpcloud/connection.rb', line 78

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

#computeObject



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/hpcloud/connection.rb', line 119

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
  return @compute_connection[]
end

#create_options(account_name, zone) ⇒ Object



219
220
221
222
223
224
# File 'lib/hpcloud/connection.rb', line 219

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



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

def dns
   = ()
  return @dns_connection[] unless @dns_connection[].nil?
  opts = create_options(, 'DNS')
  read_creds(, opts, '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 '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



214
215
216
217
# File 'lib/hpcloud/connection.rb', line 214

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

#lbObject



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

def lb
   = ()
  return @lb_connection[] unless @lb_connection[].nil?
  opts = create_options(, 'Load Balancer')
  read_creds(, opts, 'Load Balancer')
  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 'Load Balancer' 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



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

def network
   = ()
  return @network_connection[] unless @network_connection[].nil?
  opts = create_options(, 'Networking')
  read_creds(, opts, 'Networking')
  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



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/hpcloud/connection.rb', line 83

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



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(, 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



67
68
69
70
71
72
73
74
75
76
# File 'lib/hpcloud/connection.rb', line 67

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



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/hpcloud/connection.rb', line 103

def storage(=nil)
   = ()
  return @storage_connection[] unless @storage_connection[].nil?
  opts = create_options(, 'Object Storage')
  read_creds(, opts, 'Object Storage')
  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



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( = 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



250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/hpcloud/connection.rb', line 250

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



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 = (nil)
  cata = catalog(name, service)
  hsh = YAML::load(cata)
  hsh[service.to_sym].keys
end