RIFT OSM R1 Initial Submission
[osm/SO.git] / rwcal / plugins / vala / rwcal_vsphere / rift / vsphere / vsphere.py
1
2 #
3 # Copyright 2016 RIFT.IO Inc
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 #
17
18 import libcloud.compute.providers
19 import libcloud.compute.types
20
21 from gi.repository import RwcalYang
22
23
24 from . import core
25
26
27 class Vsphere(core.Cloud):
28 """This class implements the abstract methods in the Cloud class.
29 This is the Vsphere CAL driver."""
30
31 def __init__(self):
32 super(Vsphere, self).__init__()
33 self._driver_class = libcloud.compute.providers.get_driver(
34 libcloud.compute.providers.Provider.VSPHERE)
35
36 def driver(self, account):
37 return self._driver_class(
38 username=account.username,
39 passwork=account.password,
40 url=url,
41 )
42
43 def get_image_list(self, account):
44 """
45 Return a list of the names of all available images.
46 """
47 images = self.driver(account).list_images()
48 return [image.name for image in images]
49
50 def create_vm(self, account, vminfo):
51 """
52 Create a new virtual machine.
53
54 @param account - account information used authenticate the create of
55 the virtual machine
56 @param vmfinfo - information about the virtual machine to create
57
58 """
59 node = self.driver(account).ex_create_node_from_template(
60 name=vminfo.vm_name,
61 template=vminfo.vsphere.template,
62 )
63
64 vminfo.vm_id = node.id
65
66 return node.id
67
68 def delete_vm(self, account, vm_id):
69 """
70 delete a virtual machine.
71
72 @param vm_id - Instance id of VM to be deleted.
73 """
74 node = Node()
75 node.id = vm_id
76 self.driver(account).destroy_node(node)
77
78 def reboot_vm(self, account, vm_id):
79 """
80 Reboot a virtual machine.
81
82 @param vm_id - Instance id of VM to be deleted.
83 """
84 node = Node()
85 node.id = vm_id
86 self.driver(account).reboot_node(node)