Class: HpcloudController

Inherits:
Object
  • Object
show all
Defined in:
lib/process/cloud/providers/hpcloud/hpcloud.rb,
lib/process/cloud/providers/hpcloud/hpcloud_query.rb,
lib/process/cloud/providers/hpcloud/hpcloud_generic.rb

Overview

Define the HPCloud controller interface with Lorj. Lorj uses following functions:

  • create(object_type, params)

  • delete(object_type, params)

  • get(object_type, id, param)

  • query(object_type, query, params)

  • update(object_type, object, param)

The controller driver will need to dispatch the task to the real FOG task.

Currently, Hpcloud controller is moving a more generic ruby code. and only query has been implemented that way, in order to use Most of lorj and FOG features.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.base_method(crud_type) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/process/cloud/providers/hpcloud/hpcloud_generic.rb', line 69

def self.base_method(crud_type)
  define_method(crud_type) do |sObjectType, hParams|
    method_name = "#{crud_type}_#{sObjectType}"
    if self.class.method_defined? method_name
      send(method_name, hParams)
    else
      controller_error "'%s' is not a valid object for '%s'",
                       sObjectType, crud_type
    end
  end
end

.def_cruds(*crud_types) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/process/cloud/providers/hpcloud/hpcloud_generic.rb', line 32

def self.def_cruds(*crud_types)
  crud_types.each do |crud_type|
    case crud_type
    when :create, :delete
      base_method(crud_type)
    when :query, :get
      query_method(crud_type)
    when :update
      update_method(crud_type)
    end
  end
end

.def_query(requires, name, property_name = nil) ⇒ Object

Implementation of API NOT supporting query Hash The function will filter itself. It must support

  • Regexp

  • simple value equality



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/process/cloud/providers/hpcloud/hpcloud_query.rb', line 35

def self.def_query(requires, name, property_name = nil)
  property_name = property_name.nil? ? name.to_s + 's' : property_name.to_s

  define_method("query_#{name}") do |hParams, query|
    requires = [requires] unless requires.is_a?(Array)
    requires.each { |r| required?(hParams, r) }

    connection = requires[0]

    yield hParams, query if block_given?

    func = hParams[connection].send(property_name).method(:all)
    objects = _compat_query(func, __method__, query)
    # Uses :[] or :<key> to match object and query attr.
    Lorj.debug(4, "'%s' gets %d records", __method__, objects.length)
    # Return the select objects.
    ctrl_query_each objects, query, :before => method(:before_trg)
  end
end

.query_method(crud_type) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/process/cloud/providers/hpcloud/hpcloud_generic.rb', line 57

def self.query_method(crud_type)
  define_method(crud_type) do |sObjectType, sCondition, hParams|
    method_name = "#{crud_type}_#{sObjectType}"
    if self.class.method_defined? method_name
      send(method_name, hParams, sCondition)
    else
      controller_error "'%s' is not a valid object for '%s'",
                       sObjectType, crud_type
    end
  end
end

.update_method(crud_type) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/process/cloud/providers/hpcloud/hpcloud_generic.rb', line 45

def self.update_method(crud_type)
  define_method(crud_type) do |sObjectType, obj, hParams|
    method_name = "#{crud_type}_#{sObjectType}"
    if self.class.method_defined? method_name
      send(method_name, obj, hParams)
    else
      controller_error "'%s' is not a valid object for '%s'",
                       sObjectType, crud_type
    end
  end
end

Instance Method Details

#_compat_query(func, cur_method, query) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/process/cloud/providers/hpcloud/hpcloud_query.rb', line 55

def _compat_query(func, cur_method, query)
  # Ruby 1.9.2 introduce Method.parameters.
  if RUBY_VERSION < '1.9.2'
    Lorj.debug(4, "RUBY '%s': '%s' try Openstack API filter feature.",
               RUBY_VERSION, cur_method)
    begin
      objects = func.call ctrl_query_select(query, String)
    rescue
      Lorj.debug(4, "RUBY '%s': '%s' No filter parameter.",
                 RUBY_VERSION, cur_method)
      objects = func.call
    end
  else
    if func.parameters.length > 0
      Lorj.debug(4, "'%s' uses Openstack API filter feature.", cur_method)
      objects = func.call ctrl_query_select(query, String)
    else
      objects = func.call
    end
  end
  objects
end

#_get_instance_attr(oControlerObject, key) ⇒ Object



215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/process/cloud/providers/hpcloud/hpcloud.rb', line 215

def _get_instance_attr(oControlerObject, key)
  found, ret = (oControlerObject, key[0])
  found, ret = _get_from(oControlerObject, key[0]) unless found

  unless found
    Lorj.debug(4, "Unable to get '%s' from '%s'. Attribute inexistent.",
               key[0], oControlerObject.class)
    return nil
  end

  return ret if key.length == 1 || !ret.is_a?(Hash)
  ret.rh_get(key[1..-1])
end

#_server_metadata_get(oControlerObject, key) ⇒ Object



204
205
206
207
208
209
210
211
212
213
# File 'lib/process/cloud/providers/hpcloud/hpcloud.rb', line 204

def (oControlerObject, key)
  return [false, nil] unless key == :metadata
  ret = {}
  oControlerObject..each do |m|
    k = m.attributes[:key]
    v = m.attributes[:value]
    ret[k] = v
  end
  [true, ret]
end

#before_trg(*p) ⇒ Object

Ensure all data are loaded.



20
21
22
23
24
25
26
27
28
# File 'lib/process/cloud/providers/hpcloud/hpcloud_query.rb', line 20

def before_trg(*p)
  o = p[0]
  return true unless o.is_a?(Fog::Compute::HPV2::Server)

  return true if p[1].is_a?(Hash) &&
                 (p[1].keys - [:id, :name, 'id', 'name']).empty?
  o.reload if o.class.method_defined?(:created_at) && o.created_at.nil?
  true
end

#connect(sObjectType, hParams) ⇒ Object

rubocop: disable Metrics/ClassLength



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/process/cloud/providers/hpcloud/hpcloud.rb', line 36

def connect(sObjectType, hParams)
  case sObjectType
  when :services
    Fog::HP.authenticate_v2(hParams[:hdata], hParams[:excon_opts])
  when :compute_connection
    Fog::Compute.new hParams[:hdata].merge(:provider => :hp, :version => 'v2')
  when :network_connection
    Fog::HP::Network.new(hParams[:hdata])
  else
    controller_error "'%s' is not a valid object for 'connect'", sObjectType
  end
end

#create(sObjectType, hParams) ⇒ Object

Create controller for Hpcloud



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/process/cloud/providers/hpcloud/hpcloud.rb', line 52

def create(sObjectType, hParams)
  case sObjectType
  when :public_ip
    required?(hParams, :compute_connection)
    required?(hParams, :server)
    HPCompute.server_assign_address(hParams[:compute_connection],
                                    hParams[:server])
  when :server
    required?(hParams, :compute_connection)
    required?(hParams, :image)
    required?(hParams, :network)
    required?(hParams, :flavor)
    required?(hParams, :keypairs)
    required?(hParams, :security_groups)
    required?(hParams, :server_name)

    options = {
      :name => hParams[:server_name],
      :flavor_id => hParams[:flavor].id,
      :image_id => hParams[:image].id,
      :key_name => hParams[:keypairs].name,
      :security_groups => [hParams[:security_groups].name],
      :networks => [hParams[:network].id]
    }

    HPCompute.create_server(hParams[:compute_connection], options,
                            hParams[:user_data], hParams[:meta_data])
  when :image
    required?(hParams, :compute_connection)
    required?(hParams, 'server#image_name')

    HPCompute.get_image(hParams[:compute_connection],
                        hParams['server#image_name'])
  when :network
    required?(hParams, :network_connection)
    required?(hParams, :network_name)

    HPNetwork.create_network(hParams[:network_connection],
                             hParams[:network_name])
  when :subnetwork
    required?(hParams, :network_connection)
    required?(hParams, :network)
    required?(hParams, :subnetwork_name)

    HPNetwork.create_subnetwork(hParams[:network_connection],
                                hParams[:network],
                                hParams[:subnetwork_name])
  when :security_groups
    required?(hParams, :network_connection)
    required?(hParams, :security_group)

    HPSecurityGroups.create_sg(hParams[:network_connection],
                               hParams[:security_group], hParams[:sg_desc])
  when :keypairs
    required?(hParams, :compute_connection)
    required?(hParams, 'credentials#keypair_name')
    required?(hParams, :public_key)

    HPKeyPairs.create_keypair(hParams[:compute_connection],
                              hParams['credentials#keypair_name'],
                              hParams[:public_key])
  when :router
    required?(hParams, :network_connection)
    required?(hParams, :router_name)

    # Forcelly used admin_status_up to true.
    hParams[:hdata] = hParams[:hdata].merge(:admin_state_up => true)

    HPNetwork.create_router(hParams[:network_connection], hParams[:hdata])
  when :rule
    required?(hParams, :network_connection)
    required?(hParams, :security_groups)
    HPSecurityGroups.create_rule(hParams[:network_connection],
                                 hParams[:hdata])
  when :router_interface
    required?(hParams, :router)
    required?(hParams, :subnetwork)
    HPNetwork.add_interface(hParams[:router], hParams[:subnetwork])
  else
    controller_error "'%s' is not a valid object for 'create'", sObjectType
  end
end

#delete(sObjectType, hParams) ⇒ Object

rubocop: enable CyclomaticComplexity, MethodLength



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/process/cloud/providers/hpcloud/hpcloud.rb', line 137

def delete(sObjectType, hParams)
  case sObjectType
  when :network
    HPNetwork.delete_network(hParams[:network_connection],
                             hParams[:network])
  when :rule
    HPSecurityGroups.delete_rule(hParams[:network_connection],
                                 hParams[:id])
    obj = hParams[:network_connection]
    obj.security_group_rules.get(hParams[:id]).destroy
  when :server
    required?(hParams, :compute_connection)
    required?(hParams, :server)
    HPCompute.delete_server(hParams[:compute_connection],
                            hParams[:server])
  end
end

#get(sObjectType, sUniqId, hParams) ⇒ Object

rubocop: disable CyclomaticComplexity,



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/process/cloud/providers/hpcloud/hpcloud.rb', line 155

def get(sObjectType, sUniqId, hParams)
  case sObjectType
  when :server_log
    required?(hParams, :server)

    hParams[:server].console_output(sUniqId)
  when :server
    required?(hParams, :compute_connection)
    HPCompute.get_server(hParams[:compute_connection], sUniqId)
  when :image
    required?(hParams, :compute_connection)
    HPCompute.get_image(hParams[:compute_connection], sUniqId)
  when :network
    required?(hParams, :network_connection)
    HPNetwork.get_network(hParams[:network_connection], sUniqId)
  when :keypairs
    required?(hParams, :compute_connection)
    HPKeyPairs.get_keypair(hParams[:compute_connection], sUniqId)
  when :public_ip
    required?(hParams, :compute_connection)
    required?(hParams, :server)
    HPCompute.get_server_assigned_address(hParams[:compute_connection],
                                          sUniqId)
  else
    forjError "'%s' is not a valid object for 'get'", sObjectType
  end
end

#get_attr(oControlerObject, key) ⇒ Object



194
195
196
197
198
199
200
201
202
# File 'lib/process/cloud/providers/hpcloud/hpcloud.rb', line 194

def get_attr(oControlerObject, key)
  if oControlerObject.is_a?(Excon::Response)
    oControlerObject.data.rh_get(:body, key)
  else
    _get_instance_attr(oControlerObject, key)
  end
rescue => e
  controller_error "Unable to map '%s'. %s", key, e.message
end

#get_services(sObjectType, oParams) ⇒ Object

This function requires to return an Array of values or nil.



259
260
261
262
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
# File 'lib/process/cloud/providers/hpcloud/hpcloud.rb', line 259

def get_services(sObjectType, oParams)
  case sObjectType
  when :services
    # oParams[sObjectType] will provide the controller object.
    # This one can be interpreted only by controller code,
    # except if controller declares how to map with this object.
    # Processes can deal only process mapped data.
    # Currently there is no services process function. No need to map.
    services = oParams[:services]
    if !oParams[:list_services].is_a?(Array)
      service_to_find = [oParams[:list_services]]
    else
      service_to_find = oParams[:list_services]
    end
    # Search for service. Ex: Can be :Networking or network. I currently do
    # not know why...
    search_services = services.rh_get(:service_catalog)
    service = nil
    service_to_find.each do |sServiceElem|
      if search_services.key?(sServiceElem)
        service = sServiceElem
        break
      end
    end

    controller_error 'Unable to find services %s',
                     service_to_find if service.nil?
    result = services.rh_get(:service_catalog, service).keys
    result.delete('name')
    result.each_index do |iIndex|
      result[iIndex] = result[iIndex].to_s if result[iIndex].is_a?(Symbol)
    end
    return result
  else
    controller_error "'%s' is not a valid object for 'get_services'",
                     sObjectType
  end
end

#query_each(oFogObject) ⇒ Object

rubocop: enable CyclomaticComplexity



184
185
186
187
188
189
190
191
192
# File 'lib/process/cloud/providers/hpcloud/hpcloud.rb', line 184

def query_each(oFogObject)
  case oFogObject.class.to_s
  when 'Fog::HP::Network::Networks'
    oFogObject.each { |value| yield(value) }
  else
    controller_error "'%s' is not a valid list for 'each'",
                     oFogObject.class
  end
end

#set_attr(oControlerObject, key, value) ⇒ Object



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/process/cloud/providers/hpcloud/hpcloud.rb', line 229

def set_attr(oControlerObject, key, value)
  controller_class = oControlerObject.class

  controller_error "No set feature for '%s'",
                   controller_class if oControlerObject.is_a?(Excon::Response)

  attributes = oControlerObject.attributes
  def_attributes = oControlerObject.class.attributes
  controller_error "attribute '%s' is unknown in '%s'. Valid one are : '%s'",
                   key[0], oControlerObject.class,
                   def_attributes unless def_attributes.include?(key[0])
  attributes.rh_set(value, key)
rescue => e
  controller_error "Unable to map '%s' on '%s'\n%s",
                   key, sObjectType, e.message
end

#update(sObjectType, oObject, _hParams) ⇒ Object



246
247
248
249
250
251
252
253
254
255
256
# File 'lib/process/cloud/providers/hpcloud/hpcloud.rb', line 246

def update(sObjectType, oObject, _hParams)
  case sObjectType
  when :router
    controller_error 'Object to update is nil' if oObject.nil?

    HPNetwork.update_router(oObject[:object])
  else
    controller_error "'%s' is not a valid list for 'update'",
                     oFogObject.class
  end
end