Example #1
0
    def _callMethod(self, methodName, *args, **kwargs):
        try:
            method = _COMMAND_CONVERTER[methodName]
        except KeyError as e:
            raise Exception("Attempt to call function: %s with "
                            "arguments: %s error: %s" % (methodName, args, e))

        class_name, method_name = method.split('.')
        params = self._prepare_args(class_name, method_name, args, kwargs)

        timeout = kwargs.pop('_transport_timeout', CALL_TIMEOUT)

        req = JsonRpcRequest(method, params, reqId=str(uuid4()))

        responses = self._client.call(req, timeout=timeout)
        if responses:
            resp = responses[0]
        else:
            raise JsonRpcNoResponseError(method)

        if resp.error is not None:
            return response.error_raw(resp.error["code"],
                                      resp.error["message"])

        if not self._xml_compat:
            return response.success_raw(resp.result)

        if resp.result and resp.result is not True:
            # None is translated to True inside our JSONRPC implementation
            return response.success(**resp.result)

        return response.success()
Example #2
0
 def test_unregister_missing(self):
     existing_sec = make_secret()
     secret.register([existing_sec])
     missing_sec = make_secret()
     res = secret.unregister([missing_sec["uuid"], existing_sec["uuid"]])
     self.assertEqual(res, response.success())
     self.assertEqual({}, self.connection.secrets)
Example #3
0
def unregister(uuids):
    try:
        uuids = [str(uuid.UUID(s)) for s in uuids]
    except ValueError as e:
        logging.warning("Attempt to unregister invalid uuid %s: %s" %
                        (uuids, e))
        return response.error("secretBadRequestErr")

    con = libvirtconnection.get()
    try:
        for sec_uuid in uuids:
            logging.info("Unregistering secret %r", sec_uuid)
            try:
                virsecret = con.secretLookupByUUIDString(sec_uuid)
            except libvirt.libvirtError as e:
                if e.get_error_code() != libvirt.VIR_ERR_NO_SECRET:
                    raise
                logging.debug("No such secret %r", sec_uuid)
            else:
                virsecret.undefine()
    except libvirt.libvirtError as e:
        logging.error("Could not unregister secrets: %s", e)
        return response.error("secretUnregisterErr")

    return response.success()
Example #4
0
    def test_success_with_message(self):
        MESSAGE = "the message was overwritten"
        res = response.success(message=MESSAGE)

        template = doneCode
        self.assertEqual(res["status"]["code"], template["code"])
        self.assertEqual(res["status"]["message"], MESSAGE)
Example #5
0
def delete(job_id):
    try:
        _delete(job_id)
    except ClientError as e:
        logging.info('Cannot delete job, error: %s', e)
        return response.error(e.name)
    return response.success()
Example #6
0
 def test_unregister_missing(self):
     existing_sec = make_secret()
     secret.register([existing_sec])
     missing_sec = make_secret()
     res = secret.unregister([missing_sec["uuid"], existing_sec["uuid"]])
     self.assertEqual(res, response.success())
     self.assertEqual({}, self.connection.secrets)
Example #7
0
def unregister(uuids):
    try:
        uuids = [str(uuid.UUID(s)) for s in uuids]
    except ValueError as e:
        logging.warning("Attempt to unregister invalid uuid %s: %s" %
                        (uuids, e))
        return response.error("secretBadRequestErr")

    con = libvirtconnection.get()
    try:
        for sec_uuid in uuids:
            logging.info("Unregistering secret %r", sec_uuid)
            try:
                virsecret = con.secretLookupByUUIDString(sec_uuid)
            except libvirt.libvirtError as e:
                if e.get_error_code() != libvirt.VIR_ERR_NO_SECRET:
                    raise
                logging.debug("No such secret %r", sec_uuid)
            else:
                virsecret.undefine()
    except libvirt.libvirtError as e:
        logging.error("Could not unregister secrets: %s", e)
        return response.error("secretUnregisterErr")

    return response.success()
Example #8
0
def delete(job_id):
    try:
        _delete(job_id)
    except ClientError as e:
        logging.info('Cannot delete job, error: %s', e)
        return response.error(e.name)
    return response.success()
Example #9
0
    def test_success_with_message(self):
        MESSAGE = "the message was overwritten"
        res = response.success(message=MESSAGE)

        template = doneCode
        self.assertEqual(res["status"]["code"], template["code"])
        self.assertEqual(res["status"]["message"], MESSAGE)
Example #10
0
 def test_unregister_existing(self):
     sec1 = make_secret(password="******")
     sec2 = make_secret(password="******")
     secret.register([sec1, sec2])
     res = secret.unregister([sec1["uuid"]])
     self.assertEqual(res, response.success())
     self.assertNotIn(sec1["uuid"], self.connection.secrets)
     self.assertIn(sec2["uuid"], self.connection.secrets)
Example #11
0
    def wrapper(self, *args, **kwargs):

        _log.debug("START %s args=%s kwargs=%s", func.__name__, args, kwargs)
        try:
            ret = func(self, *args, **kwargs)
        except Exception as e:
            _log.exception("FINISH %s error=%s", func.__name__, e)
            if not isinstance(e, exception.VdsmException):
                e = exception.GeneralException(str(e))
            return e.response()

        _log.debug("FINISH %s response=%s", func.__name__, ret)

        if ret is None:
            return response.success()
        else:
            return response.success(**ret)
Example #12
0
def abort(job_id):
    try:
        job = get(job_id)
        job.abort()
    except ClientError as e:
        logging.info('Cannot abort job, error: %s', e)
        return response.error(e.name)
    return response.success()
Example #13
0
 def test_unregister_existing(self):
     sec1 = make_secret(password="******")
     sec2 = make_secret(password="******")
     secret.register([sec1, sec2])
     res = secret.unregister([sec1["uuid"]])
     self.assertEqual(res, response.success())
     self.assertNotIn(sec1["uuid"], self.connection.secrets)
     self.assertIn(sec2["uuid"], self.connection.secrets)
Example #14
0
def abort(job_id):
    try:
        job = get(job_id)
        job.abort()
    except ClientError as e:
        logging.info('Cannot abort job, error: %s', e)
        return response.error(e.name)
    return response.success()
Example #15
0
File: api.py Project: rexhsu/vdsm
    def wrapper(self, *args, **kwargs):

        _log.debug("START %s args=%s kwargs=%s", func.__name__, args, kwargs)
        try:
            ret = func(self, *args, **kwargs)
        except Exception as e:
            _log.exception("FINISH %s error=%s", func.__name__, e)
            if not isinstance(e, exception.VdsmException):
                e = exception.GeneralException(str(e))
            return e.response()

        _log.debug("FINISH %s response=%s", func.__name__, ret)

        if ret is None:
            return response.success()
        else:
            return response.success(**ret)
Example #16
0
 def test_register_new(self):
     sec1 = make_secret(password="******")
     sec2 = make_secret(password="******")
     res = secret.register([sec1, sec2])
     self.assertEqual(res, response.success())
     virsec1 = self.connection.secrets[sec1["uuid"]]
     self.assertEqual("sec1 password", virsec1.value)
     virsec2 = self.connection.secrets[sec2["uuid"]]
     self.assertEqual("sec2 password", virsec2.value)
Example #17
0
def delete(job_id):
    try:
        job = get(job_id)
        job.validate_not_running()
        _delete(job_id)
    except ClientError as e:
        logging.info('Cannot delete job, error: %s', e)
        return response.error(e.name)
    return response.success()
Example #18
0
def delete(job_id):
    try:
        job = get(job_id)
        job.validate_not_active()
        _delete(job_id)
    except ClientError as e:
        logging.info('Cannot delete job, error: %s', e)
        return response.error(e.name)
    return response.success()
Example #19
0
 def test_register_change_usage_id(self):
     sec = make_secret(usage_id="ovirt/provider_uuid/secert_uuid")
     secret.register([sec])
     # Change usage id
     sec["usageID"] = "ovirt/domain_uuid/secret_uuid"
     res = secret.register([sec])
     self.assertEqual(res, response.success())
     virsec = self.connection.secrets[sec["uuid"]]
     self.assertEqual("ovirt/domain_uuid/secret_uuid", virsec.usage_id)
Example #20
0
 def test_register_new(self):
     sec1 = make_secret(password="******")
     sec2 = make_secret(password="******")
     res = secret.register([sec1, sec2])
     self.assertEqual(res, response.success())
     virsec1 = self.connection.secrets[sec1["uuid"]]
     self.assertEqual("sec1 password", virsec1.value)
     virsec2 = self.connection.secrets[sec2["uuid"]]
     self.assertEqual("sec2 password", virsec2.value)
Example #21
0
 def test_register_change_usage_id(self):
     sec = make_secret(usage_id="ovirt/provider_uuid/secert_uuid")
     secret.register([sec])
     # Change usage id
     sec["usageID"] = "ovirt/domain_uuid/secret_uuid"
     res = secret.register([sec])
     self.assertEqual(res, response.success())
     virsec = self.connection.secrets[sec["uuid"]]
     self.assertEqual("ovirt/domain_uuid/secret_uuid", virsec.usage_id)
Example #22
0
def get_external_vm_names(uri, username, password):
    try:
        conn = libvirtconnection.open_connection(uri=uri,
                                                 username=username,
                                                 passwd=password)
    except libvirt.libvirtError as e:
        logging.error('error connecting to hypervisor: %r', e.message)
        return response.error('V2VConnection', e.message)

    with closing(conn):
        vms = [vm.name() for vm in _list_domains(conn)]
        return response.success(vmNames=vms)
Example #23
0
def get_external_vm_names(uri, username, password):
    try:
        conn = libvirtconnection.open_connection(uri=uri,
                                                 username=username,
                                                 passwd=password)
    except libvirt.libvirtError as e:
        logging.error('error connecting to hypervisor: %r', e.message)
        return response.error('V2VConnection', e.message)

    with closing(conn):
        vms = [vm.name() for vm in _list_domains(conn)]
        return response.success(vmNames=vms)
Example #24
0
 def test_register_replace(self):
     # Register 2 secrets
     sec1 = make_secret(password="******")
     sec2 = make_secret(password="******")
     secret.register([sec1, sec2])
     # Replace existing secret value
     sec2["password"] = make_password("sec2 new password")
     res = secret.register([sec2])
     self.assertEqual(res, response.success())
     virsec1 = self.connection.secrets[sec1["uuid"]]
     self.assertEqual("sec1 password", virsec1.value)
     virsec2 = self.connection.secrets[sec2["uuid"]]
     self.assertEqual("sec2 new password", virsec2.value)
Example #25
0
    def _callMethod(self, methodName, *args, **kwargs):
        try:
            method = _COMMAND_CONVERTER[methodName]
        except KeyError as e:
            raise Exception("Attempt to call function: %s with "
                            "arguments: %s error: %s" %
                            (methodName, args, e))

        class_name, method_name = method.split('.')
        params = self._prepare_args(class_name, method_name, args, kwargs)

        req = JsonRpcRequest(method, params, reqId=str(uuid4()))

        responses = self._client.call(
            req, timeout=self._timeouts.get(
                method_name,
                kwargs.pop('_transport_timeout', self._default_timeout)))
        if responses:
            resp = responses[0]
        else:
            raise JsonRpcNoResponseError(method)

        if resp.error is not None:
            return response.error_raw(resp.error["code"],
                                      resp.error["message"])

        if not self._xml_compat:
            return response.success_raw(resp.result)

        if resp.result and resp.result is not True:
            # None is translated to True inside our JSONRPC implementation
            if isinstance(resp.result, list):
                return response.success(items=resp.result)
            elif isinstance(resp.result, six.string_types):
                return response.success(resp.result)
            else:
                return response.success(**resp.result)

        return response.success()
Example #26
0
 def test_register_replace(self):
     # Register 2 secrets
     sec1 = make_secret(password="******")
     sec2 = make_secret(password="******")
     secret.register([sec1, sec2])
     # Replace existing secret value
     sec2["password"] = make_password("sec2 new password")
     res = secret.register([sec2])
     self.assertEqual(res, response.success())
     virsec1 = self.connection.secrets[sec1["uuid"]]
     self.assertEqual("sec1 password", virsec1.value)
     virsec2 = self.connection.secrets[sec2["uuid"]]
     self.assertEqual("sec2 new password", virsec2.value)
Example #27
0
File: v2v.py Project: fancyKai/vdsm
def get_ova_info(ova_path):
    ns = {'ovf': _OVF_NS, 'rasd': _RASD_NS}

    try:
        root = ET.fromstring(_read_ovf_from_ova(ova_path))
    except ET.ParseError as e:
        raise V2VError('Error reading ovf from ova, position: %r' % e.position)

    vm = {}
    _add_general_ovf_info(vm, root, ns)
    _add_disks_ovf_info(vm, root, ns)
    _add_networks_ovf_info(vm, root, ns)

    return response.success(vmList=vm)
Example #28
0
def get_ova_info(ova_path):
    ns = {'ovf': _OVF_NS, 'rasd': _RASD_NS}

    try:
        root = ET.fromstring(_read_ovf_from_ova(ova_path))
    except ET.ParseError as e:
        raise V2VError('Error reading ovf from ova, position: %r' % e.position)

    vm = {}
    _add_general_ovf_info(vm, root, ns)
    _add_disks_ovf_info(vm, root, ns)
    _add_networks_ovf_info(vm, root, ns)

    return response.success(vmList=vm)
Example #29
0
    def _callMethod(self, methodName, *args):
        try:
            method = _COMMAND_CONVERTER[methodName]
        except KeyError as e:
            raise Exception("Attempt to call function: %s with "
                            "arguments: %s error: %s" %
                            (methodName, args, e))

        req = JsonRpcRequest(method, args, reqId=str(uuid4()))
        responses = self._client.call(req)
        if responses:
            resp = responses[0]
        else:
            raise JsonRpcNoResponseError(method)

        if resp.error is not None:
            return response.error_raw(resp.error["code"],
                                      resp.error["message"])

        if resp.result and resp.result is not True:
            # None is translated to True inside our JSONRPC implementation
            return response.success(**resp.result)

        return response.success()
Example #30
0
 def test_register_clear(self):
     self.connection.secrets = {
         "uuid1": vmfakelib.Secret(self.connection, "uuid1", "ceph",
                                   "ovirt/name1", None),
         "uuid2": vmfakelib.Secret(self.connection, "uuid2", "ceph",
                                   "name2", None),
     }
     sec = make_secret()
     res = secret.register([sec], clear=True)
     # Should succeed
     self.assertEqual(res, response.success())
     # Should remove existing ovirt secrets
     self.assertNotIn("uuid1", self.connection.secrets)
     # Should keep non-ovirt secrets
     self.assertIn("uuid2", self.connection.secrets)
     # Should register new secret
     virsec = self.connection.secrets[sec["uuid"]]
     self.assertEqual(sec["password"].value, virsec.value)
Example #31
0
 def test_register_clear(self):
     self.connection.secrets = {
         "uuid1":
         vmfakelib.Secret(self.connection, "uuid1", "ceph", "ovirt/name1",
                          None),
         "uuid2":
         vmfakelib.Secret(self.connection, "uuid2", "ceph", "name2", None),
     }
     sec = make_secret()
     res = secret.register([sec], clear=True)
     # Should succeed
     self.assertEqual(res, response.success())
     # Should remove existing ovirt secrets
     self.assertNotIn("uuid1", self.connection.secrets)
     # Should keep non-ovirt secrets
     self.assertIn("uuid2", self.connection.secrets)
     # Should register new secret
     virsec = self.connection.secrets[sec["uuid"]]
     self.assertEqual(sec["password"].value, virsec.value)
Example #32
0
def register(secrets, clear=False):
    try:
        secrets = [Secret(params) for params in secrets]
    except ValueError as e:
        logging.warning("Attempt to register invalid secret: %s", e)
        return response.error("secretBadRequestErr")

    con = libvirtconnection.get()
    try:
        for secret in secrets:
            logging.info("Registering secret %s", secret)
            secret.register(con)
        if clear:
            uuids = frozenset(sec.uuid for sec in secrets)
            for virsecret in con.listAllSecrets():
                if (virsecret.UUIDString() not in uuids and
                        _is_ovirt_secret(virsecret)):
                    virsecret.undefine()
    except libvirt.libvirtError as e:
        logging.error("Could not register secret %s: %s", secret, e)
        return response.error("secretRegisterErr")

    return response.success()
Example #33
0
def register(secrets, clear=False):
    try:
        secrets = [Secret(params) for params in secrets]
    except ValueError as e:
        logging.warning("Attempt to register invalid secret: %s", e)
        return response.error("secretBadRequestErr")

    con = libvirtconnection.get()
    try:
        for secret in secrets:
            logging.info("Registering secret %s", secret)
            secret.register(con)
        if clear:
            uuids = frozenset(sec.uuid for sec in secrets)
            for virsecret in con.listAllSecrets():
                if (virsecret.UUIDString() not in uuids
                        and _is_ovirt_secret(virsecret)):
                    virsecret.undefine()
    except libvirt.libvirtError as e:
        logging.error("Could not register secret %s: %s", secret, e)
        return response.error("secretRegisterErr")

    return response.success()
Example #34
0
    def test_success_with_args(self):
        res = response.success(a=1, b=2)

        self.assertEqual(res, {"status": doneCode, "a": 1, "b": 2})
Example #35
0
 def test_success_with_extra_args(self, args):
     res = response.success(**args)
     self.assertEqual(res['status']['code'], 0)
     self.assertEqual(res['status']['message'], 'Done')
     for key in args:
         self.assertEqual(res[key], args[key])
Example #36
0
    def test_success(self):
        res = response.success()

        self.assertEqual(res, {"status": doneCode})
Example #37
0
 def createVm(self, vmParams, vmRecover=False):
     self.vmRequests[vmParams['vmId']] = (vmParams, vmRecover)
     return response.success(vmList={})
Example #38
0
File: v2v.py Project: kanalun/vdsm
def convert_ova(ova_path, vminfo, job_id, irs):
    command = OvaCommand(ova_path, vminfo, job_id, irs)
    job = ImportVm(job_id, command)
    job.start()
    _add_job(job_id, job)
    return response.success()
Example #39
0
File: v2v.py Project: fancyKai/vdsm
def convert_ova(ova_path, vminfo, job_id, irs):
    job = ImportVm.from_ova(ova_path, vminfo, job_id, irs)
    job.start()
    _add_job(job_id, job)
    return response.success()
Example #40
0
    def test_success_with_args(self):
        res = response.success(a=1, b=2)

        self.assertEqual(res, {"status": doneCode, "a": 1, "b": 2})
Example #41
0
File: v2v.py Project: fancyKai/vdsm
def convert_ova(ova_path, vminfo, job_id, irs):
    job = ImportVm.from_ova(ova_path, vminfo, job_id, irs)
    job.start()
    _add_job(job_id, job)
    return response.success()
Example #42
0
    def test_success(self):
        res = response.success()

        self.assertEqual(res, {"status": doneCode})
Example #43
0
 def test_delete_inactive_job(self, status):
     job = TestingJob()
     job._status = status
     jobs.add(job)
     self.assertEqual(response.success(), jobs.delete(job.id))
Example #44
0
 def test_success_without_return(self):
     res = self.vm.succeed()
     self.assertEquals(res, response.success())
Example #45
0
 def __init__(self, *args, **kwargs):
     self.status = response.success()
     self._alive = False
Example #46
0
 def test_success_with_extra_args(self, args):
     res = response.success(**args)
     self.assertEqual(res['status']['code'], 0)
     self.assertEqual(res['status']['message'], 'Done')
     for key in args:
         self.assertEqual(res[key], args[key])
Example #47
0
def convert_ova(ova_path, vminfo, job_id, irs):
    command = OvaCommand(ova_path, vminfo, job_id, irs)
    job = ImportVm(job_id, command)
    job.start()
    _add_job(job_id, job)
    return response.success()