def _get_object(self, client, collection_name, name, query_dict):
        _query_dict = dict(query_dict)
        _query_dict['filter[]'] = Q.from_dict({'name': name}).as_filters

        resources = self._get_objects(client, collection_name, _query_dict)

        obj = None
        if len(resources) > 0:
            self.logger.debug("{} '{}' already exists.".format(collection_name, name))
            # this object already exists
            obj = resources[0]
        else:
            self.logger.debug("{} '{}' does not exists.".format(collection_name, name))

        return obj
Example #2
0
    def get(self, client, kwargs_dict):
        """Reads a single custom attributes on the VM
        :param client:
        :param kwargs_dict:
        :returns: a dictionary of the results returned from ManageIQ
        :rtype: dict
        """
        id = self._get_arg("id", kwargs_dict)
        key = self._get_arg("key", kwargs_dict)

        query_dict = self._get_query()
        query_dict['filter[]'] = Q.from_dict({'name': key}).as_filters

        object_response = client.collections.vms(id)
        search_results = object_response.custom_attributes.query_string(**query_dict)
        return self._resources_from_search_results(search_results)
def set_conversion_host_api(appliance, transformation_method, source_provider,
                            target_provider):
    """
    Setting conversion host for RHV and OSP provider via REST

    Note: Support for using VMs as UCI conversion hosts will be added for RHV in 5.11.5.
    """
    vmware_ssh_private_key = None
    vmware_vddk_package_url = None

    delete_hosts = appliance.ssh_client.run_rails_command(
        "'MiqTask.delete_all; ConversionHost.delete_all'")
    if not delete_hosts.success:
        pytest.skip(
            f"Failed to delete all conversion hosts: {delete_hosts.output}")

    conversion_data = get_conversion_data(appliance, target_provider)
    if transformation_method == "SSH":
        vmware_key = conf.credentials[source_provider.data["private-keys"]
                                      ["vmware-ssh-key"]["credentials"]]
        vmware_ssh_private_key = vmware_key.password
    else:
        vmware_vddk_package_url = vddk_url()

    for host in conversion_data["hosts"]:
        conversion_entity = ("hosts" if target_provider.one_of(RHEVMProvider)
                             and appliance.version < '5.11.5' else "vms")
        host_id = (getattr(appliance.rest_api.collections,
                           conversion_entity).filter(
                               Q.from_dict({"name": host})).resources[0].id)
        response = appliance.rest_api.collections.conversion_hosts.action.create(
            resource_id=host_id,
            resource_type=conversion_data["resource_type"],
            vmware_vddk_package_url=vmware_vddk_package_url,
            vmware_ssh_private_key=vmware_ssh_private_key,
            conversion_host_ssh_private_key=conversion_data["private_key"],
            auth_user=conversion_data["auth_user"])[0]
        response.reload()
        wait_for(
            lambda: response.task.state == "Finished",
            fail_func=response.task.reload,
            num_sec=240,
            delay=3,
            message="Waiting for conversion configuration task to be finished")