Class: LabmanagerService

Inherits:
Handsoap::Service
  • Object
show all
Includes:
VmWare::Constants
Defined in:
lib/labmanager/labmanager_service.rb

Constant Summary collapse

@@workspace =
''
@@username =
''
@@password =
''
@@pragmatic =
false
@@testmode =
false

Constants included from VmWare::Constants

VmWare::Constants::CONFIGURATION_ACTION_POWEROFF, VmWare::Constants::CONFIGURATION_ACTION_POWERON, VmWare::Constants::CONFIGURATION_ACTION_RESET, VmWare::Constants::CONFIGURATION_ACTION_RESUME, VmWare::Constants::CONFIGURATION_ACTION_REVERT, VmWare::Constants::CONFIGURATION_ACTION_SHUTDOWN, VmWare::Constants::CONFIGURATION_ACTION_SNAPSHOT, VmWare::Constants::CONFIGURATION_ACTION_SUSPEND, VmWare::Constants::FENCE_MODE_UNFENCED, VmWare::Constants::IP_ADDRESSSING_STATIC_AUTOMATIC, VmWare::Constants::IP_ADDRESSSING_STATIC_MANUAL, VmWare::Constants::MACHINE_ACTION_CONSOLIDATE, VmWare::Constants::MACHINE_ACTION_DEPLOY, VmWare::Constants::MACHINE_ACTION_EJECTCD, VmWare::Constants::MACHINE_ACTION_EJECTFLOPPY, VmWare::Constants::MACHINE_ACTION_FORCEUNDEPLOY, VmWare::Constants::MACHINE_ACTION_POWEROFF, VmWare::Constants::MACHINE_ACTION_POWERON, VmWare::Constants::MACHINE_ACTION_RESET, VmWare::Constants::MACHINE_ACTION_RESUME, VmWare::Constants::MACHINE_ACTION_REVERT, VmWare::Constants::MACHINE_ACTION_SHUTDOWN, VmWare::Constants::MACHINE_ACTION_SNAPSHOT, VmWare::Constants::MACHINE_ACTION_SUSPEND, VmWare::Constants::MACHINE_ACTION_UNDEPLOY, VmWare::Constants::MACHINE_STATUS_DELETED, VmWare::Constants::MACHINE_STATUS_INVALID, VmWare::Constants::MACHINE_STATUS_OFF, VmWare::Constants::MACHINE_STATUS_ON, VmWare::Constants::MACHINE_STATUS_STUCK, VmWare::Constants::MACHINE_STATUS_SUSPENDED, VmWare::Constants::MACHINE_STATUS_UNDEPLOYED

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.configure(config) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/labmanager/labmanager_service.rb', line 20

def self.configure(config)
  @@wsdl = config[:wsdl]

  @@username = config[:username]
  @@password = config[:password]
  @@workspace = config[:workspace]
  @@pragmatic = config[:pragmatic]
    
  endpoint :uri => @@wsdl, :version => 2
  
  
  
end

Instance Method Details

#create_configuration(args = {}) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/labmanager/labmanager_service.rb', line 58

def create_configuration(args = {})
  begin

    response = invoke('labmanager:ConfigurationCreateEx') do |message|
      message.add('labmanager:name',args[:name])
      message.add('labmanager:desc',args[:description])
    end

    
    (response/"//labmanager:ConfigurationCreateExResponse/labmanager:ConfigurationCreateExResult").to_i

  rescue Handsoap::Fault => error
   
    
    if @@pragmatic then
      config = get_configuration_by_name(args[:name])
      config[:id]
    else
      raise error
    end

  end

end

#create_machine(config) ⇒ Object



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/labmanager/labmanager_service.rb', line 186

def create_machine(config)
  begin
    response = invoke('labmanager:ConfigurationAddMachineEx') do |message|
      message.add('labmanager:id',config[:configuration_id])
      message.add('labmanager:template_id',config[:template_id])
      message.add('labmanager:name',config[:name])
      message.add('labmanager:desc',config[:description])
      message.add('labmanager:netInfo') do |netinfo|
        netinfo.add('labmanager:NetInfo') do |network|
          network.add('labmanager:networkId',config[:network_id])
          network.add('labmanager:ipAddress',config[:ipaddress])
          network.add('labmanager:ipAddressingMode',config[:ipaddressingmode])
          network.add('labmanager:isConnected',true)
        end
      end

    end

    (response/"//labmanager:ConfigurationAddMachineExResponse/labmanager:ConfigurationAddMachineExResult").to_i

  rescue Handsoap::Fault => error

    if error.reason.match("The object you selected is currently busy")
      raise LabmanagerBusy
    end

    if @@pragmatic then

      machine = get_machine_by_name(config[:configuration_id],config[:name])
      machine[:id]
    else
      raise error
    end
  end
end

#delete_configuration(id) ⇒ Object



83
84
85
86
87
88
89
# File 'lib/labmanager/labmanager_service.rb', line 83

def delete_configuration(id)

  response = invoke('labmanager:ConfigurationDelete') do |message|
    message.add('labmanager:configurationId',id)
  end

end

#delete_machine(id) ⇒ Object



252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/labmanager/labmanager_service.rb', line 252

def delete_machine(id)
  begin
    response = invoke('labmanager:MachineDelete') do |message|
      message.add('labmanager:vmId',id)
    end

  rescue Handsoap::Fault => error
    if error.reason.match("The object you selected is currently busy")
      raise LabmanagerBusy
    end

      raise error
  end
end

#deploy_machine(id) ⇒ Object



226
227
228
# File 'lib/labmanager/labmanager_service.rb', line 226

def deploy_machine(id)
  perform_machine_action(id,MACHINE_ACTION_DEPLOY)
end

#get_configuration_by_name(name) ⇒ Object



91
92
93
94
95
96
97
98
99
# File 'lib/labmanager/labmanager_service.rb', line 91

def get_configuration_by_name(name)

    get_configurations.each do |config|
          return config  if config[:name] == name
    end

    return nil

end

#get_configurationsObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/labmanager/labmanager_service.rb', line 101

def get_configurations

  response = invoke('labmanager:GetCurrentWorkspace')


  hashes = (response/"//labmanager:GetCurrentWorkspaceResponse/labmanager:GetCurrentWorkspaceResult/labmanager:Configurations/labmanager:Configuration").map do |configuration|
    {
      :id => (configuration/"labmanager:id").to_s,
      :name => (configuration/"labmanager:name").to_s,
      :description => (configuration/"labmanager:description").to_s,
      :deployed =>  eval((configuration/"labmanager:isDeployed").to_s)
    }
  end

end

#get_machine(id) ⇒ Object



267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/labmanager/labmanager_service.rb', line 267

def get_machine(id)
    response = invoke('labmanager:GetMachine') do |message|
      message.add('labmanager:machineId',id)
    end

    machine =
    {
      :id => (response/"//labmanager:GetMachineResponse/labmanager:GetMachineResult/labmanager:id").to_s,
      :name => (response/"//labmanager:GetMachineResponse/labmanager:GetMachineResult/labmanager:name").to_s,
      :description => (response/"//labmanager:GetMachineResponse/labmanager:GetMachineResult/labmanager:description").to_s,
      :ipaddress => (response/"//labmanager:GetMachineResponse/labmanager:GetMachineResult/labmanager:internalIP").to_s,
      :memory => (response/"//labmanager:GetMachineResponse/labmanager:GetMachineResult/labmanager:memory").to_s,
      :deployed => eval((response/"//labmanager:GetMachineResponse/labmanager:GetMachineResult/labmanager:isDeployed").to_s),
      :status => (response/"//labmanager:GetMachineResponse/labmanager:GetMachineResult/labmanager:status").to_s,
    }
end

#get_machine_by_name(config_id, name) ⇒ Object



284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'lib/labmanager/labmanager_service.rb', line 284

def get_machine_by_name(config_id,name)
  response = invoke('labmanager:GetMachineByName') do |message|
    message.add('labmanager:configurationId',config_id)
    message.add('labmanager:name',name)
  end

  machine =
  {
    :id => (response/"//labmanager:GetMachineByNameResponse/labmanager:GetMachineByNameResult/labmanager:id").to_s,
    :name => (response/"//labmanager:GetMachineByNameResponse/labmanager:GetMachineByNameResult/labmanager:name").to_s,
    :description => (response/"//labmanager:GetMachineByNameResponse/labmanager:GetMachineByNameResult/labmanager:description").to_s,
    :ipaddress => (response/"//labmanager:GetMachineByNameResponse/labmanager:GetMachineByNameResult/labmanager:internalIP").to_s,
    :memory => (response/"//labmanager:GetMachineByNameResponse/labmanager:GetMachineByNameResult/labmanager:memory").to_s,
    :deployed => eval((response/"//labmanager:GetMachineByNameResponse/labmanager:GetMachineByNameResult/labmanager:isDeployed").to_s),
    :status => (response/"//labmanager:GetMachineByNameResponse/labmanager:GetMachineByNameResult/labmanager:status").to_s,

  }

end

#get_network_by_name(name) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/labmanager/labmanager_service.rb', line 160

def get_network_by_name(name)
  response = invoke('labmanager:GetNetworkTemplateByName') do |message|
    message.add('labmanager:name',name)
  end

  network =
  {
    :id => (response/"//labmanager:GetNetworkTemplateByNameResponse/labmanager:GetNetworkTemplateByNameResult/labmanager:NetID").to_s,
    :name => (response/"//labmanager:GetNetworkTemplateByNameResponse/labmanager:GetNetworkTemplateByNameResult/labmanager:Name").to_s,
    :description => (response/"//labmanager:GetNetworkTemplateByNameResponse/labmanager:GetNetworkTemplateByNameResult/labmanager:Description").to_s
  }

end

#get_template(id) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/labmanager/labmanager_service.rb', line 130

def get_template(id)
  response = invoke('labmanager:GetTemplate') do |message|
    message.add('labmanager:id',id)
  end

  template =
  {
    :id => (response/"//labmanager:GetTemplateResponse/labmanager:GetTemplateResult/labmanager:id").to_s,
    :name => (response/"//labmanager:GetTemplateResponse/labmanager:GetTemplateResult/labmanager:name").to_s,
    :description => (response/"//labmanager:GetTemplateResponse/labmanager:GetTemplateResult/labmanager:description").to_s
  }

end

#get_template_by_name(name) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/labmanager/labmanager_service.rb', line 144

def get_template_by_name(name)
  response = invoke('labmanager:GetTemplateByName') do |message|
    message.add('labmanager:name',name)
  end

  hashes = (response/"//labmanager:GetTemplateByNameResponse/labmanager:GetTemplateByNameResult/labmanager:Template").map do |template|
    {
      :id => (template/"labmanager:id").to_s,
      :name => (template/"labmanager:name").to_s,
      :description => (template/"labmanager:description").to_s
    }
  end

  return hashes[0]
end

#list_machines(config_id) ⇒ Object



304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# File 'lib/labmanager/labmanager_service.rb', line 304

def list_machines(config_id)
  response = invoke('labmanager:ListMachines') do |message|
    message.add('labmanager:configurationId',config_id)
  end

  hashes = (response/"//labmanager:ListMachinesResponse/labmanager:ListMachinesResult/labmanager:Machine").map do |machine|
    {
      :id => (machine/"labmanager:id").to_s,
      :name => (machine/"labmanager:name").to_s,
      :description => (machine/"labmanager:description").to_s
    }
  end

  return hashes
end

#list_networksObject



174
175
176
177
178
179
180
181
182
183
184
# File 'lib/labmanager/labmanager_service.rb', line 174

def list_networks()
  response = invoke('labmanager:ListNetworks')

  hashes = (response/"//labmanager:ListNetworksResponse/labmanager:ListNetworksResult/labmanager:Network").map do |network|
    {
      :id => (network/"labmanager:NetID").to_s,
      :name => (network/"labmanager:Name").to_s,
      :description => (network/"labmanager:Description").to_s
    }
  end
end

#list_templatesObject



117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/labmanager/labmanager_service.rb', line 117

def list_templates()
  response = invoke('labmanager:ListTemplates')

  hashes = (response/"//labmanager:ListTemplatesResponse/labmanager:ListTemplatesResult/labmanager:Template").map do |template|
    {
      :id => (template/"labmanager:id").to_s,
      :name => (template/"labmanager:name").to_s,
      :description => (template/"labmanager:description").to_s
    }
  end

end

#on_create_document(doc) ⇒ Object

Register namespaces for request



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/labmanager/labmanager_service.rb', line 35

def on_create_document(doc)
  
  
  doc.alias 'labmanager', 'http://vmware.com/labmanager'

  header = doc.find("Header")
  header.add "labmanager:AuthenticationHeader" do |s|
    s.set_attr "env:mustUnderstand", "1"

    s.add "labmanager:workspacename", @@workspace
    s.add "labmanager:username", @@username
    s.add "labmanager:password", @@password

  end
  
  

end

#on_response_document(doc) ⇒ Object



54
55
56
# File 'lib/labmanager/labmanager_service.rb', line 54

def on_response_document(doc)
  doc.add_namespace 'labmanager', 'http://vmware.com/labmanager'
end

#perform_machine_action(id, action) ⇒ Object



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/labmanager/labmanager_service.rb', line 230

def perform_machine_action(id,action)
  begin
    response = invoke('labmanager:MachinePerformAction') do |message|
      message.add('labmanager:machineId',id)
      message.add('labmanager:action',action)
    end

  rescue Handsoap::Fault => error
    if error.reason.match("The object you selected is currently busy")
      raise LabmanagerBusy
    else

    if error.reason.match("Error in undeploying. Contact the Administrator if the problem persists")
      raise LabmanagerBusy
    end

      raise error
    end

  end
end

#undeploy_machine(id) ⇒ Object



222
223
224
# File 'lib/labmanager/labmanager_service.rb', line 222

def undeploy_machine(id)
  perform_machine_action(id,MACHINE_ACTION_FORCEUNDEPLOY)
end