Exemplo n.º 1
0
    def startElement(self, name, attrs, connection):
        ret = SCObject.startElement(self, name, attrs, connection)
        if ret is not None:
            return ret

        if name == 'device':
            for key, value in attrs.items():
                setattr(self, key, value)

        elif name == 'volume':
            self.volume = Volume(connection)
            self.volume.startElement(name, attrs, connection)
            return self.volume
        elif name == 'provider':
            self.provider = Provider(connection)
            self.provider.startElement(name, attrs, connection)
            return self.provider
        elif name == 'partitionList':
            partitionList = ResultSet([('partition', Partition)], name)
            return self.partitionList
        elif name == 'subDevices':
            self.__subDevices = ResultSet([('subDevices', Device)], name)
            return self.subDevices
        else:
            return None
Exemplo n.º 2
0
    def startElement(self, name, attrs, connection):
        ret = SCObject.startElement(self, name, attrs, connection)
        if ret is not None:
            return ret

        if name == 'device':
            for key, value in attrs.items():
                setattr(self, key, value)

        elif name == 'volume':
            self.volume = Volume(connection)
            self.volume.startElement(name, attrs, connection)
            return self.volume
        elif name == 'provider':
            self.provider = Provider(connection)
            self.provider.startElement(name, attrs, connection)
            return self.provider
        elif name == 'partitionList':
            partitionList = ResultSet([('partition', Partition)], name)
            return self.partitionList
        elif name == 'subDevices':
            self.__subDevices = ResultSet([('subDevices', Device)], name)
            return self.subDevices
        else:
            return None
Exemplo n.º 3
0
    def startElement(self, name, attrs, connection):
        ret = SCObject.startElement(self, name, attrs, connection)
        if ret is not None:
            return ret

        if name == 'vm':
            # keep all attributes
            for key, value in attrs.items():
                setattr(self, key, value)
        elif name == 'provider':
            self.provider = Provider(connection)
            self.provider.startElement(name, attrs, connection)
            return self.provider
        elif name == 'securecloudAgent':
            self.securecloudAgent = SCAgent(connection)
            self.securecloudAgent.startElement(name, attrs, connection)
            return self.securecloudAgent
        elif name == 'devices':
            self.devices = ResultSet([('device', Device)], name)
            return self.devices
        else:
            return None
Exemplo n.º 4
0
 def startElement(self, name, attrs, connection):
     ret = SCObject.startElement(self, name, attrs, connection)
     if ret is not None:
         return ret
     
     if name == 'vm':
         # keep all attributes
         for key, value in attrs.items():
             setattr(self, key, value)
     elif name == 'provider':
         self.provider = Provider(connection)
         self.provider.startElement(name, attrs, connection)
         return self.provider
     elif name == 'securecloudAgent':
         self.securecloudAgent = SCAgent(connection)
         self.securecloudAgent.startElement(name, attrs, connection)
         return self.securecloudAgent
     elif name == 'devices':
         self.devices = ResultSet([('device', Device)], name)
         return self.devices
     else:
         return None
Exemplo n.º 5
0
class Device(SCObject):

    ValidAttributes = [
        'id', 'msUID', "name", 'href', 'deviceType', 'cspDeviceType',
        'deviceState', 'deviceStatus', 'info', 'detachable', 'lastModified',
        'writeAccess', 'EncryptedName', 'partitionType', 'provisionProgress',
        'provisionState', 'raidLevel'
    ]

    def __init__(self, connection):
        SCObject.__init__(self, connection)
        # Device attributes
        self.id = None
        self.msUID = None
        self.name = None
        self.href = None

        self.deviceType = None
        self.cspDeviceType = None
        self.deviceState = None
        self.deviceStatus = None

        self.info = None
        self.detachable = None
        self.lastModified = None
        self.writeAccess = None

        self.EncryptedName = None
        self.partitionType = None
        self.provisionProgress = None
        self.provisionState = None
        self.raidLevel = None

        # elements
        self.description = None
        self.fileSystem = None

        # subDevices List
        self.__subDevices = ResultSet([('subDevices', Device)], 'subDevices')
        # volume object
        self.volume = None
        # provider object
        self.provider = None
        # partition list
        self.partitionList = None

    # ---------------
    # properties
    # ---------------
    @property
    def subDevices(self):
        return self.__subDevices

    def startElement(self, name, attrs, connection):
        ret = SCObject.startElement(self, name, attrs, connection)
        if ret is not None:
            return ret

        if name == 'device':
            for key, value in attrs.items():
                setattr(self, key, value)

        elif name == 'volume':
            self.volume = Volume(connection)
            self.volume.startElement(name, attrs, connection)
            return self.volume
        elif name == 'provider':
            self.provider = Provider(connection)
            self.provider.startElement(name, attrs, connection)
            return self.provider
        elif name == 'partitionList':
            partitionList = ResultSet([('partition', Partition)], name)
            return self.partitionList
        elif name == 'subDevices':
            self.__subDevices = ResultSet([('subDevices', Device)], name)
            return self.subDevices
        else:
            return None

    def endElement(self, name, value, connection):

        if name == 'description':
            self.description = value
        elif name == 'fileSystem':
            self.fileSystem = value
        else:
            setattr(self, name, value)

    def buildElements(self, elements=None):
        device = ElementTree.Element('device')

        # build attributes
        for e in self.ValidAttributes:
            if getattr(self, e): device.attrib[e] = getattr(self, e)

        if self.description:
            ElementTree.SubElement(device,
                                   'description').text = self.description
        if self.fileSystem:
            ElementTree.SubElement(device, 'fileSystem').text = self.fileSystem

        # inner objects
        if self.partitionList:
            device.append(self.partitionList.buildElements())
        if self.volume: device.append(self.volume.buildElements())
        if self.provider: device.append(self.provider.buildElements())
        if self.__subDevices: device.append(self.__subDevices.buildElements())

        return device

    # ---------- function ----------

    def cancelEncryption(self):
        # Build XML elements structures
        action = 'vm/%s/device/%s/encrypt' % (self.imageGUID, self.msUID)
        data = self.tostring()
        response = self.connection.make_request(action,
                                                data=data,
                                                method='DELETE')
        return response

    def exportKey(self):
        action = 'vm/%s/device/%s/keyfile/' % (self.imageGUID, self.msUID)
        data = self.tostring()
        response = self.connection.make_request(action,
                                                data=data,
                                                method='POST')
        return response

    def importKey(self):
        action = 'vm/%s/device/%s/key/' % (self.imageGUID, self.msUID)
        data = self.tostring()
        response = self.connection.make_request(action,
                                                data=data,
                                                method='POST')
        return response

    def deleteKey(self):
        action = 'vm/%s/device/%s/key/' % (self.imageGUID, self.msUID)
        data = self.tostring()
        response = self.connection.make_request(action,
                                                data=data,
                                                method='DELETE')
        return response
Exemplo n.º 6
0
class VirtualMachine(SCObject):
    #===========================================================================
    # present vm object
    #===========================================================================
        
    # Present valid vm object attributes, not inner objects
    ValidAttributes = [ 'SecurityGroupGUID', 'SecurityGroupName',
                        'autoProvision', 'detectedKeyCount', 
                        'encryptableDeviceCount', 'encryptedDeviceCount', 'encryptingDeviceCount', 
                        'hostname', 'href',
                        'imageGUID', 'imageID', 'imageName', 
                        'instanceGUID','instanceID', 'lastModified', 
                        'nonEncryptedDeviceCount', 'pendingDeviceCount']
    
    
    def __init__(self, connection):
        SCObject.__init__(self, connection)
        #-----------------------------------------------------------------------
        # # Attributes
        #-----------------------------------------------------------------------
        self.SecurityGroupGUID = None
        self.SecurityGroupName = None
        self.autoProvision = None
        self.detectedKeyCount = None
        self.encryptableDeviceCount = None
        self.encryptedDeviceCount = None
        self.encryptingDeviceCount = None
        self.href = None
        self.hostname = None
        self.imageGUID = None
        self.imageID = None
        self.imageName = None
        self.instanceGUID = None
        self.instanceID = None
        self.lastModified = None
        self.nonEncryptedDeviceCount = None
        self.pendingDeviceCount = None
        #-----------------------------------------------------------------------
        # # elements
        #-----------------------------------------------------------------------
        self.imageDescription = None
        #-----------------------------------------------------------------------
        # # Inner objects
        #-----------------------------------------------------------------------
        self.provider = None
        self.platform = None
        self.securecloudAgent = None
        # Device list
        self.devices = ResultSet([('device', Device)], 'devices')
        
        pass

    def startElement(self, name, attrs, connection):
        ret = SCObject.startElement(self, name, attrs, connection)
        if ret is not None:
            return ret
        
        if name == 'vm':
            # keep all attributes
            for key, value in attrs.items():
                setattr(self, key, value)
        elif name == 'provider':
            self.provider = Provider(connection)
            self.provider.startElement(name, attrs, connection)
            return self.provider
        elif name == 'securecloudAgent':
            self.securecloudAgent = SCAgent(connection)
            self.securecloudAgent.startElement(name, attrs, connection)
            return self.securecloudAgent
        elif name == 'devices':
            self.devices = ResultSet([('device', Device)], name)
            return self.devices
        else:
            return None

    def endElement(self, name, value, connection):
        if name == 'platform':
            self.platform = value
        elif name == 'imageDescription':
            self.imageDescription = value
        else:
            setattr(self, name, value)
            
    def buildElements(self):

        vm = ElementTree.Element('vm')
        
        # build attributes
        for e in self.ValidAttributes:
            if getattr(self, e): vm.attrib[e] = getattr(self, e)

        if self.imageDescription:
            description = ElementTree.SubElement(vm, "imageDescription")
            description.text = self.imageDescription

        # append inner objects
        if getattr(self, 'provider'): vm.append( self.provider.buildElements() )
        if getattr(self, 'platform'): vm.append( self.platform.buildElements() )
        if getattr(self, 'securecloudAgent'): vm.append( self.securecloudAgent.buildElements() )
        if getattr(self, 'devices'): vm.append( self.devices.buildElements() )
            
        return vm

    def _update(self, updated):
        self.__dict__.update(updated.__dict__)

    # ----- functions start 

    def update(self):
        action = 'vm/%s/' % self.imageGUID
        data = self.tostring()
        updated = self.connection.get_object(action, VirtualMachine, data=data, method='POST')
        if updated:
            self._update(updated)
            return self
        
        return updated
    
    def delete(self):
        #-----------------------------------------------------------------------
        # delete vm itself
        #-----------------------------------------------------------------------
        action = 'vm/%s/' % self.imageGUID
        return self.connection.get_status(action, method='DELETE')

    def getDevice(self, deviceID):
        #-----------------------------------------------------------------------
        # list all devices
        #-----------------------------------------------------------------------
        action = 'vm/%s/device/%s/' % (self.imageGUID, deviceID)
        return self.connection.get_object(action, Device)
    
    def deleteDevice(self, deviceID):
        #-----------------------------------------------------------------------
        # delete device inside a virtual machine
        #-----------------------------------------------------------------------
        # :deviceID the targe device to be delete in a virtual machine'
        
        action = 'vm/%s/device/%s/' % (self.imageGUID, deviceID)
        return self.connection.get_status(action, method='DELETE')

    # ----- Encryption -----

    def encrypt(self, deviceID):
        #-----------------------------------------------------------------------
        # encrypt a VM/Computer
        #-----------------------------------------------------------------------
        
        action = 'vm/%s/encrypt/' % (self.imageGUID)
        return self.connection.get_status(action)

    # ----- Create RAID device
       
    def createRAID(self, name, filesystem, mountpoint, device_id_list, deviceID=None):

        # create raid device object
        dev = Device(self.connection)
        dev.name = name
        if deviceID:
            dev.msUID = deviceID
        else:
            dev.msUID = str(uuid.uuid4())
        dev.raidLevel = 'RAID0'
        dev.fileSystem = filesystem
        dev.volume = Volume(self.connection)
        dev.volume.mountPoint = mountpoint

        for d in device_id_list:
            new = Device(self.connection)
            new.msUID = d
            dev.subDevices.append(new)

        # call create RAID API
        action = 'vm/%s/device/raid/' % (self.imageGUID)
        data = dev.tostring()
        return self.connection.get_status(action, Device, data=data, method='POST')
Exemplo n.º 7
0
class VirtualMachine(SCObject):
    #===========================================================================
    # present vm object
    #===========================================================================

    # Present valid vm object attributes, not inner objects
    ValidAttributes = [
        'SecurityGroupGUID', 'SecurityGroupName', 'autoProvision',
        'detectedKeyCount', 'encryptableDeviceCount', 'encryptedDeviceCount',
        'encryptingDeviceCount', 'hostname', 'href', 'imageGUID', 'imageID',
        'imageName', 'instanceGUID', 'instanceID', 'lastModified',
        'nonEncryptedDeviceCount', 'pendingDeviceCount'
    ]

    def __init__(self, connection):
        SCObject.__init__(self, connection)
        #-----------------------------------------------------------------------
        # # Attributes
        #-----------------------------------------------------------------------
        self.SecurityGroupGUID = None
        self.SecurityGroupName = None
        self.autoProvision = None
        self.detectedKeyCount = None
        self.encryptableDeviceCount = None
        self.encryptedDeviceCount = None
        self.encryptingDeviceCount = None
        self.href = None
        self.hostname = None
        self.imageGUID = None
        self.imageID = None
        self.imageName = None
        self.instanceGUID = None
        self.instanceID = None
        self.lastModified = None
        self.nonEncryptedDeviceCount = None
        self.pendingDeviceCount = None
        #-----------------------------------------------------------------------
        # # elements
        #-----------------------------------------------------------------------
        self.imageDescription = None
        #-----------------------------------------------------------------------
        # # Inner objects
        #-----------------------------------------------------------------------
        self.provider = None
        self.platform = None
        self.securecloudAgent = None
        # Device list
        self.devices = ResultSet([('device', Device)], 'devices')

        pass

    def startElement(self, name, attrs, connection):
        ret = SCObject.startElement(self, name, attrs, connection)
        if ret is not None:
            return ret

        if name == 'vm':
            # keep all attributes
            for key, value in attrs.items():
                setattr(self, key, value)
        elif name == 'provider':
            self.provider = Provider(connection)
            self.provider.startElement(name, attrs, connection)
            return self.provider
        elif name == 'securecloudAgent':
            self.securecloudAgent = SCAgent(connection)
            self.securecloudAgent.startElement(name, attrs, connection)
            return self.securecloudAgent
        elif name == 'devices':
            self.devices = ResultSet([('device', Device)], name)
            return self.devices
        else:
            return None

    def endElement(self, name, value, connection):
        if name == 'platform':
            self.platform = value
        elif name == 'imageDescription':
            self.imageDescription = value
        else:
            setattr(self, name, value)

    def buildElements(self):

        vm = ElementTree.Element('vm')

        # build attributes
        for e in self.ValidAttributes:
            if getattr(self, e): vm.attrib[e] = getattr(self, e)

        if self.imageDescription:
            description = ElementTree.SubElement(vm, "imageDescription")
            description.text = self.imageDescription

        # append inner objects
        if getattr(self, 'provider'): vm.append(self.provider.buildElements())
        if getattr(self, 'platform'): vm.append(self.platform.buildElements())
        if getattr(self, 'securecloudAgent'):
            vm.append(self.securecloudAgent.buildElements())
        if getattr(self, 'devices'): vm.append(self.devices.buildElements())

        return vm

    def _update(self, updated):
        self.__dict__.update(updated.__dict__)

    # ----- functions start

    def update(self):
        action = 'vm/%s/' % self.imageGUID
        data = self.tostring()
        updated = self.connection.get_object(action,
                                             VirtualMachine,
                                             data=data,
                                             method='POST')
        if updated:
            self._update(updated)
            return self

        return updated

    def delete(self):
        #-----------------------------------------------------------------------
        # delete vm itself
        #-----------------------------------------------------------------------
        action = 'vm/%s/' % self.imageGUID
        return self.connection.get_status(action, method='DELETE')

    def getDevice(self, deviceID):
        #-----------------------------------------------------------------------
        # list all devices
        #-----------------------------------------------------------------------
        action = 'vm/%s/device/%s/' % (self.imageGUID, deviceID)
        return self.connection.get_object(action, Device)

    def deleteDevice(self, deviceID):
        #-----------------------------------------------------------------------
        # delete device inside a virtual machine
        #-----------------------------------------------------------------------
        # :deviceID the targe device to be delete in a virtual machine'

        action = 'vm/%s/device/%s/' % (self.imageGUID, deviceID)
        return self.connection.get_status(action, method='DELETE')

    # ----- Encryption -----

    def encrypt(self, deviceID):
        #-----------------------------------------------------------------------
        # encrypt a VM/Computer
        #-----------------------------------------------------------------------

        action = 'vm/%s/encrypt/' % (self.imageGUID)
        return self.connection.get_status(action)

    # ----- Create RAID device

    def createRAID(self,
                   name,
                   filesystem,
                   mountpoint,
                   device_id_list,
                   deviceID=None):

        # create raid device object
        dev = Device(self.connection)
        dev.name = name
        if deviceID:
            dev.msUID = deviceID
        else:
            dev.msUID = str(uuid.uuid4())
        dev.raidLevel = 'RAID0'
        dev.fileSystem = filesystem
        dev.volume = Volume(self.connection)
        dev.volume.mountPoint = mountpoint

        for d in device_id_list:
            new = Device(self.connection)
            new.msUID = d
            dev.subDevices.append(new)

        # call create RAID API
        action = 'vm/%s/device/raid/' % (self.imageGUID)
        data = dev.tostring()
        return self.connection.get_status(action,
                                          Device,
                                          data=data,
                                          method='POST')
Exemplo n.º 8
0
class Device(SCObject):

    ValidAttributes = ['id', 'msUID', "name", 'href',
                       'deviceType', 'cspDeviceType', 'deviceState', 'deviceStatus',
                       'info', 'detachable', 'lastModified', 'writeAccess',
                       'EncryptedName', 'partitionType', 'preserveData',
                       'provisionProgress', 'provisionState',
                       'raidLevel']

    def __init__(self, connection, tag='Device'):
        SCObject.__init__(self, connection, tag)

        #----------------------------------------------------------------------
        # Attributes
        #----------------------------------------------------------------------
        self.id = None
        self.msUID = None
        self.name = None
        self.href = None

        self.deviceType = None
        self.cspDeviceType = None
        self.deviceState = None
        self.deviceStatus = None

        self.info = None
        self.detachable = None
        self.lastModified = None
        self.writeAccess = None

        self.EncryptedName = None
        self.preserveData = None
        self.partitionType = None
        self.provisionProgress = None
        self.provisionState = None
        self.raidLevel = None
        #----------------------------------------------------------------------
        # Elements
        #----------------------------------------------------------------------
        self.description = None
        self.fileSystem = None
        #----------------------------------------------------------------------
        # subDevices List
        #----------------------------------------------------------------------
        self.__subDevices = ResultSet([('subDevices', Device)], 'subDevices')
        #----------------------------------------------------------------------
        # volume object
        #----------------------------------------------------------------------
        self.volume = None
        #----------------------------------------------------------------------
        # Provider object
        #----------------------------------------------------------------------
        self.provider = None
        #----------------------------------------------------------------------
        # partition list
        #----------------------------------------------------------------------
        self.partitionList = None

    # ---------------
    # properties
    # ---------------
    @property
    def subDevices(self):
        return self.__subDevices

    def startElement(self, name, attrs, connection):
        ret = SCObject.startElement(self, name, attrs, connection)
        if ret is not None:
            return ret

        if name == 'device':
            for key, value in attrs.items():
                setattr(self, key, value)

        elif name == 'volume':
            self.volume = Volume(connection)
            self.volume.startElement(name, attrs, connection)
            return self.volume
        elif name == 'provider':
            self.provider = Provider(connection)
            self.provider.startElement(name, attrs, connection)
            return self.provider
        elif name == 'partitionList':
            partitionList = ResultSet([('partition', Partition)], name)
            return self.partitionList
        elif name == 'subDevices':
            self.__subDevices = ResultSet([('subDevices', Device)], name)
            return self.subDevices
        else:
            return None

    def endElement(self, name, value, connection):

        if name == 'description':
            self.description = value
        elif name == 'fileSystem':
            self.fileSystem = value
        else:
            setattr(self, name, value)

    def buildElements(self, elements=None):
        device = ElementTree.Element('device')

        # build attributes
        for e in self.ValidAttributes:
            if getattr(self, e):
                device.attrib[e] = getattr(self, e)

        if self.description:
            ElementTree.SubElement(
                device, 'description').text = self.description
        if self.fileSystem:
            ElementTree.SubElement(device, 'fileSystem').text = self.fileSystem

        # inner objects
        if self.partitionList:
            device.append(self.partitionList.buildElements())
        if self.volume:
            device.append(self.volume.buildElements())
        if self.provider:
            device.append(self.provider.buildElements())
        if self.__subDevices:
            device.append(self.__subDevices.buildElements())

        return device

    def _update(self, updated):
        self.__dict__.update(updated.__dict__)
   
    def update(self, imageGUID):
        action = 'vm/%s/device/%s/' % (imageGUID, self.msUID)
        data = self.tostring()
        updated = self.connection.make_request(action, data=data, method='POST')

        if updated:
            self._update(updated)
            return self
        
        return updated

    #
    # ---------- encryption key function ----------
    #
    # TODO: wait bug fixing for self.href attribute to present correct device URL
    #

    def exportKey(self):
        action = '%s/keyfile/' % (self.href)
        data = self.tostring()
        response = self.connection.make_request(
            action, data=data, method='POST')
        return response

    def importKey(self):
        action = '%s/key/' % (self.href)
        data = self.tostring()
        response = self.connection.make_request(
            action, data=data, method='POST')
        return response

    def deleteKey(self):
        action = '%s/key/' % (self.href)
        data = self.tostring()
        response = self.connection.make_request(
            action, method='DELETE')
        return response
Exemplo n.º 9
0
class VirtualMachine(SCObject):
    # =========================================================================
    # present vm object
    # =========================================================================

    # Present valid vm object attributes, not inner objects
    ValidAttributes = [
        "SecurityGroupGUID",
        "autoProvision",
        "nonEncryptedDeviceCount",
        "detectedKeyCount",
        "encryptableDeviceCount",
        "encryptedDeviceCount",
        "encryptingDeviceCount",
        "pendingDeviceCount",
        "hostname",
        "href",
        "imageGUID",
        "imageID",
        "imageName",
        "instanceID",
        "instanceGUID",
        "lastModified",
    ]

    def __init__(self, connection, tag="vm"):
        SCObject.__init__(self, connection, tag)
        # ----------------------------------------------------------------------
        # Attributes
        # ----------------------------------------------------------------------
        self.SecurityGroupGUID = None
        self.autoProvision = None
        self.detectedKeyCount = None
        self.encryptableDeviceCount = None
        self.encryptedDeviceCount = None
        self.encryptingDeviceCount = None
        self.href = None
        self.hostname = None
        self.imageGUID = None
        self.imageID = None
        self.imageName = None
        self.instanceGUID = None
        self.instanceID = None
        self.lastModified = None
        self.nonEncryptedDeviceCount = None
        self.pendingDeviceCount = None
        # ----------------------------------------------------------------------
        # elements
        # ----------------------------------------------------------------------
        self.imageDescription = None
        # ----------------------------------------------------------------------
        # Inner objects
        # ----------------------------------------------------------------------
        self.provider = None
        self.platform = None
        self.securecloudAgent = None
        # Device list
        self.devices = ResultSet([("device", Device)], "devices")

        pass

    def startElement(self, name, attrs, connection):
        ret = SCObject.startElement(self, name, attrs, connection)
        if ret is not None:
            return ret

        if name == "vm":
            # keep all attributes
            for key, value in attrs.items():
                setattr(self, key, value)
        elif name == "provider":
            self.provider = Provider(connection)
            self.provider.startElement(name, attrs, connection)
            return self.provider
        elif name == "securecloudAgent":
            self.securecloudAgent = SCAgent(connection)
            self.securecloudAgent.startElement(name, attrs, connection)
            return self.securecloudAgent
        elif name == "devices":
            self.devices = ResultSet([("device", Device)], name)
            return self.devices
        else:
            return None

    def endElement(self, name, value, connection):
        if name == "platform":
            self.platform = value
        elif name == "imageDescription":
            self.imageDescription = value
        else:
            setattr(self, name, value)

    def buildElements(self):

        vm = ElementTree.Element("vm")

        # build attributes
        for e in self.ValidAttributes:
            if getattr(self, e):
                vm.attrib[e] = getattr(self, e)

        if self.imageDescription:
            description = ElementTree.SubElement(vm, "imageDescription")
            description.text = self.imageDescription

        # append inner objects
        if getattr(self, "provider"):
            vm.append(self.provider.buildElements())
        if getattr(self, "platform"):
            vm.attrib["platform"] = self.platform
        if getattr(self, "securecloudAgent"):
            vm.append(self.securecloudAgent.buildElements())
        if getattr(self, "devices"):
            vm.append(self.devices.buildElements())

        return vm

    def _update(self, to_update):
        self.__dict__.update(to_update.__dict__)

    # ----------------------------------------------------------------------
    # Member functions
    # ----------------------------------------------------------------------

    def update(self):
        action = "vm/%s/" % self.imageGUID
        data = self.tostring()
        update = self.connection.get_object(action, VirtualMachine, data=data, method="POST")

        if update:
            self._update(update)
            return self

        return update

    def delete(self):
        # ----------------------------------------------------------------------
        # delete vm itself
        # ----------------------------------------------------------------------
        action = "vm/%s/" % (self.imageGUID)
        return self.connection.get_status(action, method="DELETE")

    def getDevice(self, deviceID):
        # ----------------------------------------------------------------------
        # list all devices
        # ----------------------------------------------------------------------
        action = "vm/%s/device/%s/" % (self.imageGUID, deviceID)
        return self.connection.get_object(action, Device)

    def deleteDevice(self, deviceID):
        # ----------------------------------------------------------------------
        # delete device inside a virtual machine
        # ----------------------------------------------------------------------
        # :deviceID the targe device to be delete in a virtual machine'

        action = "vm/%s/device/%s/" % (self.imageGUID, deviceID)
        return self.connection.get_status(action, method="DELETE")

    # ----- Encryption -----

    def encrypt(self):
        # -----------------------------------------------------------------------
        # encrypt a VM/Computer
        # -----------------------------------------------------------------------

        action = "vm/%s/encrypt/" % (self.imageGUID)

        vm = VirtualMachine(self.connection)
        vm.imageGUID = self.imageGUID

        for objDevice in self.devices:
            device = Device(self.connection)
            device.msUID = objDevice.msUID
            device.preserveData = "yes"
            device.fileSystem = objDevice.fileSystem
            # Create inner volume object
            device.volume = Volume(self.connection)
            device.volume.mountPoint = objDevice.volume.mountPoint
            vm.devices.append(device)

        data = vm.tostring()

        return self.connection.get_status(action, data=data, method="POST")

    def encryptDevice(self, DeviceObj, filesystem, mountpoint, preserveData="no"):
        # ----------------------------------------------------------------------
        # encrypt devices in a VM/Computer
        # ----------------------------------------------------------------------

        # Create New Virtual Machine message for encrypt device object
        response = None

        if isinstance(DeviceObj, Device):
            vm = VirtualMachine(self.connection)
            vm.imageGUID = self.imageGUID
            device = Device(self.connection)
            device.msUID = DeviceObj.msUID
            device.preserveData = preserveData
            device.fileSystem = filesystem
            # Create inner volume object
            device.volume = Volume(self.connection)
            device.volume.mountPoint = mountpoint
            vm.devices.append(device)

            # Do Request
            action = "vm/%s/encrypt/" % (self.imageGUID)
            data = vm.tostring()
            response = self.connection.get_status(action, method="POST", data=data)

        return response

    def cancelEncryption(self, DeviceObj):
        # Create New Virtual Machine message for encrypt device object
        response = None

        if isinstance(DeviceObj, Device):
            action = "vm/%s/device/%s/encrypt/" % (self.imageGUID, DeviceObj.msUID)
            response = self.connection.get_status(action, method="DELETE")

        return response

    def deleteKey(self, DeviceObj):
        # Create New Virtual Machine message for encrypt device object
        response = None

        if isinstance(DeviceObj, Device):
            action = "vm/%s/device/%s/key/" % (self.imageGUID, DeviceObj.msUID)
            response = self.connection.get_status(action, method="DELETE")

        return response

    # ----- Create RAID device

    def createRAID(self, name, filesystem, mountpoint, device_id_list, deviceID=None):

        # create raid device object
        dev = Device(self.connection)
        dev.name = name
        if deviceID:
            dev.msUID = deviceID
        else:
            dev.msUID = str(uuid.uuid4())

        dev.raidLevel = "RAID0"
        dev.fileSystem = filesystem

        dev.volume = Volume(self.connection)
        dev.volume.mountPoint = mountpoint

        for d in device_id_list:
            new = Device(self.connection)
            new.msUID = d
            dev.subDevices.append(new)

        # call create RAID API
        action = "vm/%s/device/raid/" % (self.imageGUID)
        data = dev.tostring()
        return self.connection.get_status(action, Device, data=data, method="POST")
Exemplo n.º 10
0
class VirtualMachine(SCObject):
    #=========================================================================
    # present vm object
    #=========================================================================

    # Present valid vm object attributes, not inner objects
    ValidAttributes = ['SecurityGroupGUID',
                       'autoProvision', 'nonEncryptedDeviceCount', 'detectedKeyCount',
                       'encryptableDeviceCount', 'encryptedDeviceCount', 'encryptingDeviceCount', 'pendingDeviceCount',
                       'hostname', 'href',
                       'imageGUID', 'imageID', 'imageName', 'instanceID', 'instanceGUID',
                       'lastModified']

    def __init__(self, connection, tag='vm'):
        SCObject.__init__(self, connection, tag)
        #----------------------------------------------------------------------
        # Attributes
        #----------------------------------------------------------------------
        self.SecurityGroupGUID = None
        self.autoProvision = None
        self.detectedKeyCount = None
        self.encryptableDeviceCount = None
        self.encryptedDeviceCount = None
        self.encryptingDeviceCount = None
        self.href = None
        self.hostname = None
        self.imageGUID = None
        self.imageID = None
        self.imageName = None
        self.instanceGUID = None
        self.instanceID = None
        self.lastModified = None
        self.nonEncryptedDeviceCount = None
        self.pendingDeviceCount = None
        #----------------------------------------------------------------------
        # elements
        #----------------------------------------------------------------------
        self.imageDescription = None
        #----------------------------------------------------------------------
        # Inner objects
        #----------------------------------------------------------------------
        self.provider = None
        self.platform = None
        self.securecloudAgent = None
        # Device list
        self.devices = ResultSet([('device', Device)], 'devices')

        pass

    def startElement(self, name, attrs, connection):
        ret = SCObject.startElement(self, name, attrs, connection)
        if ret is not None:
            return ret

        if name == 'vm':
            # keep all attributes
            for key, value in attrs.items():
                setattr(self, key, value)
        elif name == 'provider':
            self.provider = Provider(connection)
            self.provider.startElement(name, attrs, connection)
            return self.provider
        elif name == 'securecloudAgent':
            self.securecloudAgent = SCAgent(connection)
            self.securecloudAgent.startElement(name, attrs, connection)
            return self.securecloudAgent
        elif name == 'devices':
            self.devices = ResultSet([('device', Device)], name)
            return self.devices
        else:
            return None

    def endElement(self, name, value, connection):
        if name == 'platform':
            self.platform = value
        elif name == 'imageDescription':
            self.imageDescription = value
        else:
            setattr(self, name, value)

    def buildElements(self):

        vm = ElementTree.Element('vm')

        # build attributes
        for e in self.ValidAttributes:
            if getattr(self, e):
                vm.attrib[e] = getattr(self, e)

        if self.imageDescription:
            description = ElementTree.SubElement(vm, "imageDescription")
            description.text = self.imageDescription

        # append inner objects
        if getattr(self, 'provider'):
            vm.append(self.provider.buildElements())
        if getattr(self, 'platform'):
            vm.attrib['platform'] = self.platform
        if getattr(self, 'securecloudAgent'):
            vm.append(self.securecloudAgent.buildElements())
        if getattr(self, 'devices'):
            vm.append(self.devices.buildElements())

        return vm

    def _update(self, to_update):
        self.__dict__.update(to_update.__dict__)

    #----------------------------------------------------------------------
    # Member functions
    #----------------------------------------------------------------------

    def update(self):
        action = 'vm/%s/' % self.imageGUID
        data = self.tostring()
        update = self.connection.get_object(
            action, VirtualMachine, data=data, method='POST')

        if update:
            self._update(update)
            return self

        return update

    def delete(self):
        #----------------------------------------------------------------------
        # delete vm itself
        #----------------------------------------------------------------------
        action = 'vm/%s/' % (self.imageGUID)
        return self.connection.get_status(action, method='DELETE')

    def getDevice(self, deviceID):
        #----------------------------------------------------------------------
        # list all devices
        #----------------------------------------------------------------------
        action = 'vm/%s/device/%s/' % (self.imageGUID, deviceID)
        return self.connection.get_object(action, Device)

    def deleteDevice(self, deviceID):
        #----------------------------------------------------------------------
        # delete device inside a virtual machine
        #----------------------------------------------------------------------
        # :deviceID the targe device to be delete in a virtual machine'

        action = 'vm/%s/device/%s/' % (self.imageGUID, deviceID)
        return self.connection.get_status(action, method='DELETE')

    # ----- Encryption -----

    def encrypt(self):
        #-----------------------------------------------------------------------
        # encrypt a VM/Computer
        #-----------------------------------------------------------------------

        action = 'vm/%s/encrypt/' % (self.imageGUID)

        vm = VirtualMachine(self.connection)
        vm.imageGUID = self.imageGUID

        for objDevice in self.devices:
            device = Device(self.connection)
            device.msUID = objDevice.msUID
            device.preserveData = 'yes'
            device.fileSystem = objDevice.fileSystem
            # Create inner volume object
            device.volume = Volume(self.connection)
            device.volume.mountPoint = objDevice.volume.mountPoint
            vm.devices.append(device)

        data = vm.tostring()

        return self.connection.get_status(action, data=data, method='POST')

    def encryptDevice(self, DeviceObj, filesystem, mountpoint, preserveData="no"):
        #----------------------------------------------------------------------
        # encrypt devices in a VM/Computer
        #----------------------------------------------------------------------

        # Create New Virtual Machine message for encrypt device object
        response = None

        if isinstance(DeviceObj, Device):
            vm = VirtualMachine(self.connection)
            vm.imageGUID = self.imageGUID
            device = Device(self.connection)
            device.msUID = DeviceObj.msUID
            device.preserveData = preserveData
            device.fileSystem = filesystem
            # Create inner volume object
            device.volume = Volume(self.connection)
            device.volume.mountPoint = mountpoint
            vm.devices.append(device)

            # Do Request
            action = 'vm/%s/encrypt/' % (self.imageGUID)
            data = vm.tostring()
            response = self.connection.get_status(
                action, method='POST', data=data)

        return response

    def cancelEncryption(self, DeviceObj):
        # Create New Virtual Machine message for encrypt device object
        response = None

        if isinstance(DeviceObj, Device):
            action = 'vm/%s/device/%s/encrypt/' % (self.imageGUID,
                                                   DeviceObj.msUID)
            response = self.connection.get_status(
                action, method='DELETE')

        return response

    def deleteKey(self, DeviceObj):
        # Create New Virtual Machine message for encrypt device object
        response = None

        if isinstance(DeviceObj, Device):
            action = 'vm/%s/device/%s/key/' % (self.imageGUID,
                                                   DeviceObj.msUID)
            response = self.connection.get_status(
                action, method='DELETE')

        return response

    # ----- Create RAID device

    def createRAID(self, name, raid_level, filesystem, writeAccess, mountpoint, device_id_list, deviceID=None):

        # create raid device object
        dev = Device(self.connection)
        dev.name = name
        if deviceID:
            dev.msUID = deviceID
        else:
            dev.msUID = str(uuid.uuid4())