Class: StackMate::CloudStackVirtualMachine

Inherits:
CloudStackResource show all
Includes:
Intrinsic, Logging, Resolver
Defined in:
lib/stackmate/participants/cloudstack_virtualmachine.rb

Constant Summary

Constants included from Resolver

Resolver::INTEXP, Resolver::STRINGEXP, Resolver::UUIDEXP

Instance Attribute Summary

Attributes inherited from CloudStackResource

#name

Instance Method Summary collapse

Methods included from Resolver

#get_named_tag, #get_resolved, #recursive_resolve, #resolve_tags, #resolve_to_deviceid, #validate_param

Methods included from Intrinsic

#fn_base64, #fn_getatt, #fn_join, #fn_lookup, #fn_map, #fn_ref, #fn_select, #intrinsic

Methods included from Logging

configure_logger_for, #logger, logger_for

Methods inherited from CloudStackResource

#initialize, #set_metadata

Constructor Details

This class inherits a constructor from StackMate::CloudStackResource

Instance Method Details

#createObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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
# File 'lib/stackmate/participants/cloudstack_virtualmachine.rb', line 9

def create
  logger.debug("Creating resource #{@name}")
  workitem[@name] = {}
  name_cs = workitem['StackName'] + '-' + @name
  args={}
  begin
    args['templateid'] = get_templateid
    args['serviceofferingid'] = get_serviceofferingid
    args['zoneid'] = get_zoneid
    args['securitygroupnames'] = get_securitygroupnames if @props.has_key?('securitygroupnames')
    args['affinitygroupids'] = get_affinitygroupids if @props.has_key?('affinitygroupids')
    args['startvm'] = get_startvm if @props.has_key?('startvm')
    args['displayvm'] = get_displayvm if @props.has_key?('displayvm')
    args['diskofferingid'] = get_diskofferingid if @props.has_key?('diskofferingid')
    args['hypervisor'] = get_hypervisor if @props.has_key?('hypervisor')
    args['keyboard'] = get_keyboard if @props.has_key?('keyboard')
    args['name'] = workitem['StackName'] +'-' +get_name if @props.has_key?('name')

    if @props.has_key?('iptonetworklist')
      ipnetworklist = get_iptonetworklist
      #split
      list_params = ipnetworklist.split("&")
      list_params.each do |p|
        fields = p.split("=")
        args[fields[0]] = fields[1]
      end
    end
    args['networkids'] = get_networkids if @props.has_key?('networkids')
    args['account'] =  if @props.has_key?('account')
    args['userdata'] = get_userdata if @props.has_key?('userdata')
    args['keypair'] = get_keypair if @props.has_key?('keypair')
    args['projectid'] = get_projectid if @props.has_key?('projectid')
    args['ipaddress'] = get_ipaddress if @props.has_key?('ipaddress')
    args['displayname'] = get_displayname if @props.has_key?('displayname')
    args['ip6address'] = get_ip6address if @props.has_key?('ip6address')
    args['affinitygroupnames'] = get_affinitygroupnames if @props.has_key?('affinitygroupnames')
    args['domainid'] = get_domainid if @props.has_key?('domainid')
    args['size'] = get_size if @props.has_key?('size')
    args['hostid'] = get_hostid if @props.has_key?('hostid')
    args['securitygroupids'] = get_securitygroupids if @props.has_key?('securitygroupids')
    args['group'] = get_group if @props.has_key?('group')

    logger.info("Creating resource #{@name} with following arguments")
    p args
    result_obj = make_async_request('deployVirtualMachine',args)
    resource_obj = result_obj['VirtualMachine'.downcase]

    #doing it this way since it is easier to change later, rather than cloning whole object
    resource_obj.each_key do |k|
      val = resource_obj[k]
      if('id'.eql?(k))
        k = 'physical_id'
      end
      workitem[@name][k] = val
    end
    set_tags(@props['tags'],workitem[@name]['physical_id'],"UserVM") if @props.has_key?('tags')
     if workitem['Resources'][@name].has_key?('Metadata')
    workitem['ResolvedNames'][@name] = name_cs
    workitem['IdMap'][workitem[@name]['physical_id']] = @name
  workitem[@name][:PrivateIp] = resource_obj['nic'][0]['ipaddress']

  rescue NoMethodError => nme
    logger.error("Create request failed for resource . Cleaning up the stack")
    raise nme
  rescue Exception => e
    logger.error(e.message)
    raise e
  end
  
end

#deleteObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/stackmate/participants/cloudstack_virtualmachine.rb', line 80

def delete
  logger.debug("Deleting resource #{@name}")
  begin
    physical_id = workitem[@name]['physical_id'] if !workitem[@name].nil?
    if(!physical_id.nil?)
      args = {'id' => physical_id
            }
      result_obj = make_async_request('destroyVirtualMachine',args)
      if (!(result_obj['error'] == true))
        logger.info("Successfully deleted resource #{@name}")
      else
        workitem[@name]['delete_error'] = true
        logger.info("CloudStack error while deleting resource #{@name}")
      end
    else
      logger.info("Resource  not created in CloudStack. Skipping delete...")
    end
  rescue Exception => e
    logger.error("Unable to delete resorce #{@name}")
  end
end

#get_accountObject



219
220
221
222
223
224
225
# File 'lib/stackmate/participants/cloudstack_virtualmachine.rb', line 219

def 
   = get_resolved(@props['account'],workitem)
  if .nil? || !validate_param(,"string")
    raise "Malformed optional parameter account for resource #{@name}"
  end
  
end

#get_affinitygroupidsObject



147
148
149
150
151
152
153
# File 'lib/stackmate/participants/cloudstack_virtualmachine.rb', line 147

def get_affinitygroupids
  resolved_affinitygroupids = get_resolved(@props['affinitygroupids'],workitem)
  if resolved_affinitygroupids.nil? || !validate_param(resolved_affinitygroupids,"list")
    raise "Malformed optional parameter affinitygroupids for resource #{@name}"
  end
  resolved_affinitygroupids
end

#get_affinitygroupnamesObject



275
276
277
278
279
280
281
# File 'lib/stackmate/participants/cloudstack_virtualmachine.rb', line 275

def get_affinitygroupnames
  resolved_affinitygroupnames = get_resolved(@props['affinitygroupnames'],workitem)
  if resolved_affinitygroupnames.nil? || !validate_param(resolved_affinitygroupnames,"list")
    raise "Malformed optional parameter affinitygroupnames for resource #{@name}"
  end
  resolved_affinitygroupnames
end

#get_diskofferingidObject



171
172
173
174
175
176
177
# File 'lib/stackmate/participants/cloudstack_virtualmachine.rb', line 171

def get_diskofferingid
  resolved_diskofferingid = get_resolved(@props['diskofferingid'],workitem)
  if resolved_diskofferingid.nil? || !validate_param(resolved_diskofferingid,"uuid")
    raise "Malformed optional parameter diskofferingid for resource #{@name}"
  end
  resolved_diskofferingid
end

#get_displaynameObject



259
260
261
262
263
264
265
# File 'lib/stackmate/participants/cloudstack_virtualmachine.rb', line 259

def get_displayname
  resolved_displayname = get_resolved(@props['displayname'],workitem)
  if resolved_displayname.nil? || !validate_param(resolved_displayname,"string")
    raise "Malformed optional parameter displayname for resource #{@name}"
  end
  resolved_displayname
end

#get_displayvmObject



163
164
165
166
167
168
169
# File 'lib/stackmate/participants/cloudstack_virtualmachine.rb', line 163

def get_displayvm
  resolved_displayvm = get_resolved(@props['displayvm'],workitem)
  if resolved_displayvm.nil? || !validate_param(resolved_displayvm,"boolean")
    raise "Malformed optional parameter displayvm for resource #{@name}"
  end
  resolved_displayvm
end

#get_domainidObject



283
284
285
286
287
288
289
# File 'lib/stackmate/participants/cloudstack_virtualmachine.rb', line 283

def get_domainid
  resolved_domainid = get_resolved(@props['domainid'],workitem)
  if resolved_domainid.nil? || !validate_param(resolved_domainid,"uuid")
    raise "Malformed optional parameter domainid for resource #{@name}"
  end
  resolved_domainid
end

#get_groupObject



315
316
317
318
319
320
321
# File 'lib/stackmate/participants/cloudstack_virtualmachine.rb', line 315

def get_group
  resolved_group = get_resolved(@props['group'],workitem)
  if resolved_group.nil? || !validate_param(resolved_group,"string")
    raise "Malformed optional parameter group for resource #{@name}"
  end
  resolved_group
end

#get_hostidObject



299
300
301
302
303
304
305
# File 'lib/stackmate/participants/cloudstack_virtualmachine.rb', line 299

def get_hostid
  resolved_hostid = get_resolved(@props['hostid'],workitem)
  if resolved_hostid.nil? || !validate_param(resolved_hostid,"uuid")
    raise "Malformed optional parameter hostid for resource #{@name}"
  end
  resolved_hostid
end

#get_hypervisorObject



179
180
181
182
183
184
185
# File 'lib/stackmate/participants/cloudstack_virtualmachine.rb', line 179

def get_hypervisor
  resolved_hypervisor = get_resolved(@props['hypervisor'],workitem)
  if resolved_hypervisor.nil? || !validate_param(resolved_hypervisor,"string")
    raise "Malformed optional parameter hypervisor for resource #{@name}"
  end
  resolved_hypervisor
end

#get_ip6addressObject



267
268
269
270
271
272
273
# File 'lib/stackmate/participants/cloudstack_virtualmachine.rb', line 267

def get_ip6address
  resolved_ip6address = get_resolved(@props['ip6address'],workitem)
  if resolved_ip6address.nil? || !validate_param(resolved_ip6address,"string")
    raise "Malformed optional parameter ip6address for resource #{@name}"
  end
  resolved_ip6address
end

#get_ipaddressObject



251
252
253
254
255
256
257
# File 'lib/stackmate/participants/cloudstack_virtualmachine.rb', line 251

def get_ipaddress
  resolved_ipaddress = get_resolved(@props['ipaddress'],workitem)
  if resolved_ipaddress.nil? || !validate_param(resolved_ipaddress,"string")
    raise "Malformed optional parameter ipaddress for resource #{@name}"
  end
  resolved_ipaddress
end

#get_iptonetworklistObject



203
204
205
206
207
208
209
# File 'lib/stackmate/participants/cloudstack_virtualmachine.rb', line 203

def get_iptonetworklist
  resolved_iptonetworklist = get_resolved(@props['iptonetworklist'],workitem)
  if resolved_iptonetworklist.nil? || !validate_param(resolved_iptonetworklist,"map")
    raise "Malformed optional parameter iptonetworklist for resource #{@name}"
  end
  resolved_iptonetworklist
end

#get_keyboardObject



187
188
189
190
191
192
193
# File 'lib/stackmate/participants/cloudstack_virtualmachine.rb', line 187

def get_keyboard
  resolved_keyboard = get_resolved(@props['keyboard'],workitem)
  if resolved_keyboard.nil? || !validate_param(resolved_keyboard,"string")
    raise "Malformed optional parameter keyboard for resource #{@name}"
  end
  resolved_keyboard
end

#get_keypairObject



235
236
237
238
239
240
241
# File 'lib/stackmate/participants/cloudstack_virtualmachine.rb', line 235

def get_keypair
  resolved_keypair = get_resolved(@props['keypair'],workitem)
  if resolved_keypair.nil? || !validate_param(resolved_keypair,"string")
    raise "Malformed optional parameter keypair for resource #{@name}"
  end
  resolved_keypair
end

#get_nameObject



195
196
197
198
199
200
201
# File 'lib/stackmate/participants/cloudstack_virtualmachine.rb', line 195

def get_name
  resolved_name = get_resolved(@props['name'],workitem)
  if resolved_name.nil? || !validate_param(resolved_name,"string")
    raise "Malformed optional parameter name for resource #{@name}"
  end
  resolved_name
end

#get_networkidsObject



211
212
213
214
215
216
217
# File 'lib/stackmate/participants/cloudstack_virtualmachine.rb', line 211

def get_networkids
  resolved_networkids = get_resolved(@props['networkids'],workitem)
  if resolved_networkids.nil? || !validate_param(resolved_networkids,"list")
    raise "Malformed optional parameter networkids for resource #{@name}"
  end
  resolved_networkids
end

#get_projectidObject



243
244
245
246
247
248
249
# File 'lib/stackmate/participants/cloudstack_virtualmachine.rb', line 243

def get_projectid
  resolved_projectid = get_resolved(@props['projectid'],workitem)
  if resolved_projectid.nil? || !validate_param(resolved_projectid,"uuid")
    raise "Malformed optional parameter projectid for resource #{@name}"
  end
  resolved_projectid
end

#get_securitygroupidsObject



307
308
309
310
311
312
313
# File 'lib/stackmate/participants/cloudstack_virtualmachine.rb', line 307

def get_securitygroupids
  resolved_securitygroupids = get_resolved(@props['securitygroupids'],workitem)
  if resolved_securitygroupids.nil? || !validate_param(resolved_securitygroupids,"list")
    raise "Malformed optional parameter securitygroupids for resource #{@name}"
  end
  resolved_securitygroupids
end

#get_securitygroupnamesObject



139
140
141
142
143
144
145
# File 'lib/stackmate/participants/cloudstack_virtualmachine.rb', line 139

def get_securitygroupnames
  resolved_securitygroupnames = get_resolved(@props['securitygroupnames'],workitem)
  if resolved_securitygroupnames.nil? || !validate_param(resolved_securitygroupnames,"list")
    raise "Malformed optional parameter securitygroupnames for resource #{@name}"
  end
  resolved_securitygroupnames
end

#get_serviceofferingidObject



123
124
125
126
127
128
129
# File 'lib/stackmate/participants/cloudstack_virtualmachine.rb', line 123

def get_serviceofferingid
  resolved_serviceofferingid = get_resolved(@props["serviceofferingid"],workitem)
  if resolved_serviceofferingid.nil? || !validate_param(resolved_serviceofferingid,"uuid")
    raise "Missing mandatory parameter serviceofferingid for resource #{@name}"
  end
  resolved_serviceofferingid
end

#get_sizeObject



291
292
293
294
295
296
297
# File 'lib/stackmate/participants/cloudstack_virtualmachine.rb', line 291

def get_size
  resolved_size = get_resolved(@props['size'],workitem)
  if resolved_size.nil? || !validate_param(resolved_size,"long")
    raise "Malformed optional parameter size for resource #{@name}"
  end
  resolved_size
end

#get_startvmObject



155
156
157
158
159
160
161
# File 'lib/stackmate/participants/cloudstack_virtualmachine.rb', line 155

def get_startvm
  resolved_startvm = get_resolved(@props['startvm'],workitem)
  if resolved_startvm.nil? || !validate_param(resolved_startvm,"boolean")
    raise "Malformed optional parameter startvm for resource #{@name}"
  end
  resolved_startvm
end

#get_templateidObject



115
116
117
118
119
120
121
# File 'lib/stackmate/participants/cloudstack_virtualmachine.rb', line 115

def get_templateid
  resolved_templateid = get_resolved(@props["templateid"],workitem)
  if resolved_templateid.nil? || !validate_param(resolved_templateid,"uuid")
    raise "Missing mandatory parameter templateid for resource #{@name}"
  end
  resolved_templateid
end

#get_userdataObject



227
228
229
230
231
232
233
# File 'lib/stackmate/participants/cloudstack_virtualmachine.rb', line 227

def get_userdata
  resolved_userdata = get_resolved(@props['userdata'],workitem)
  if resolved_userdata.nil? || !validate_param(resolved_userdata,"string")
    raise "Malformed optional parameter userdata for resource #{@name}"
  end
  resolved_userdata
end

#get_zoneidObject



131
132
133
134
135
136
137
# File 'lib/stackmate/participants/cloudstack_virtualmachine.rb', line 131

def get_zoneid
  resolved_zoneid = get_resolved(@props["zoneid"],workitem)
  if resolved_zoneid.nil? || !validate_param(resolved_zoneid,"uuid")
    raise "Missing mandatory parameter zoneid for resource #{@name}"
  end
  resolved_zoneid
end

#on_workitemObject



102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/stackmate/participants/cloudstack_virtualmachine.rb', line 102

def on_workitem
  @name = workitem.participant_name
  @props = workitem['Resources'][@name]['Properties']
  @props.downcase_key
  @resolved_names = workitem['ResolvedNames']
  if workitem['params']['operation'] == 'create'
    create
  else
    delete
  end
  reply
end