예제 #1
0
static void list_storage_domains(OvirtApi *api, OvirtProxy *proxy)
{
    GError *error = NULL;
    OvirtCollection *collection;
    GHashTableIter storage_domains;
    gpointer value;


    collection = ovirt_api_get_storage_domains(api);
    ovirt_collection_fetch(collection, proxy, &error);
    if (error != NULL) {
        g_debug("failed to fetch storage domains: %s", error->message);
        g_clear_error(&error);
    }

    g_hash_table_iter_init(&storage_domains, ovirt_collection_get_resources(collection));
    while (g_hash_table_iter_next(&storage_domains, NULL, &value)) {
        OvirtStorageDomain *domain;
        OvirtCollection *file_collection;
        GHashTableIter files;
        char *name;
        int type;

        domain = OVIRT_STORAGE_DOMAIN(value);
        g_object_get(G_OBJECT(domain), "type", &type, "name", &name, NULL);
        g_print("storage domain: %s (type %s)\n", name,
                genum_get_nick(OVIRT_TYPE_STORAGE_DOMAIN_TYPE, type));

        file_collection = ovirt_storage_domain_get_files(domain);
        if (file_collection == NULL) {
            goto next;
        }
        ovirt_collection_fetch(file_collection, proxy, &error);
        if (error != NULL) {
            g_debug("failed to fetch files for storage domain %s: %s",
                    name, error->message);
            g_clear_error(&error);
        }
        g_hash_table_iter_init(&files, ovirt_collection_get_resources(file_collection));
        while (g_hash_table_iter_next(&files, NULL, &value)) {
            char *filename;

            g_object_get(G_OBJECT(value), "name", &filename, NULL);
            g_print("file: %s\n", filename);
            g_free(filename);
        }

next:
        g_free(name);
    }


}
예제 #2
0
static void test_govirt_list_duplicate_vms(void)
{
    OvirtProxy *proxy;
    OvirtApi *api;
    OvirtCollection *vms;
    OvirtResource *vm;
    GError *error = NULL;
    GovirtMockHttpd *httpd;

    const char *vms_body = "<vms> \
                              <vm href=\"api/vms/uuid0\" id=\"uuid0\"> \
                                <name>vm0</name> \
                              </vm> \
                              <vm href=\"api/vms/uuid1\" id=\"uuid1\"> \
                                <name>vm0</name> \
                              </vm> \
                            </vms>";

    httpd = govirt_mock_httpd_new(GOVIRT_HTTPS_PORT);
    govirt_mock_httpd_add_request(httpd, "GET", "/ovirt-engine/api",
                                  "<api><link href=\"/ovirt-engine/api/vms\" rel=\"vms\"/></api>");
    govirt_mock_httpd_add_request(httpd, "GET", "/ovirt-engine/api/vms", vms_body);
    govirt_mock_httpd_start(httpd);

    proxy = ovirt_proxy_new("localhost:" G_STRINGIFY(GOVIRT_HTTPS_PORT));
    ovirt_proxy_set_mock_ca(proxy);
    api = ovirt_proxy_fetch_api(proxy, &error);
    g_test_assert_expected_messages();
    g_assert_nonnull(api);
    g_assert_no_error(error);

    g_test_expect_message("libgovirt", G_LOG_LEVEL_MESSAGE,
                          "'vm' resource with the same name ('vm0') already exists");
    vms = ovirt_api_get_vms(api);
    ovirt_collection_fetch(vms, proxy, &error);
    g_test_assert_expected_messages();
    g_assert_no_error(error);

    vm = ovirt_collection_lookup_resource(vms, "vm0");
    g_assert_nonnull(vm);
    g_object_unref(vm);

    g_object_unref(proxy);

    govirt_mock_httpd_stop(httpd);
}
예제 #3
0
int main(int argc, char **argv)
{
    OvirtApi *api;
    OvirtCollection *vms;
    OvirtProxy *proxy = NULL;
    OvirtVm *vm = NULL;
    OvirtVmDisplay *display = NULL;
    OvirtVmState state;
    GError *error = NULL;
    char *host = NULL;
    guint port;
    guint secure_port;
    OvirtVmDisplayType type;
    gchar *ticket = NULL;
    GByteArray *ca_cert = NULL;

    g_type_init();

    if (argc != 3) {
        g_print("Usage: %s URI VM-NAME\n", argv[0]);
        exit(1);
    }

    proxy = ovirt_proxy_new (argv[1]);
    if (proxy == NULL)
        goto error;

    g_signal_connect(G_OBJECT(proxy), "authenticate",
                     G_CALLBACK(authenticate_cb), NULL);

    ovirt_proxy_fetch_ca_certificate(proxy, &error);
    if (error != NULL) {
        g_debug("failed to get CA certificate: %s", error->message);
        goto error;
    }
    g_object_get(G_OBJECT(proxy), "ca-cert", &ca_cert, NULL);

    api = ovirt_proxy_fetch_api(proxy, &error);
    if (error != NULL) {
        g_debug("failed to lookup %s: %s", argv[2], error->message);
        goto error;
    }

    g_assert(api != NULL);
    vms= ovirt_api_get_vms(api);
    g_assert(vms != NULL);
    ovirt_collection_fetch(vms, proxy, &error);
    if (error != NULL) {
        g_debug("failed to lookup %s: %s", argv[2], error->message);
        goto error;
    }
    vm = OVIRT_VM(ovirt_collection_lookup_resource(vms, argv[2]));
    g_return_val_if_fail(vm != NULL, -1);
    g_object_get(G_OBJECT(vm), "state", &state, NULL);
    if (state != OVIRT_VM_STATE_UP) {
        ovirt_vm_start(vm, proxy, &error);
        if (error != NULL) {
            g_debug("failed to start %s: %s", argv[2], error->message);
            goto error;
        }
    }

    if (!ovirt_vm_get_ticket(vm, proxy, &error)) {
        g_debug("failed to get ticket for %s: %s", argv[2], error->message);
        goto error;
    }

    g_object_get(G_OBJECT(vm), "display", &display, NULL);
    if (display == NULL) {
        goto error;
    }

    g_object_get(G_OBJECT(display),
                 "type", &type,
                 "address", &host,
                 "port", &port,
                 "secure-port", &secure_port,
                 "ticket", &ticket,
                 NULL);
    g_print("Connection info for %s:\n", argv[2]);
    g_print("\tConnection type: %s\n",
            (type == OVIRT_VM_DISPLAY_SPICE?"spice":"vnc"));
    g_print("\tVM IP address: %s\n", host);
    g_print("\tPort: %d\n", port);
    g_print("\tSecure port: %d\n", secure_port);
    g_print("\tCA certificate: %p\n", ca_cert);
    g_print("\tTicket: %s\n", ticket);

    {
    list_storage_domains(api, proxy);
    }


error:
    g_free(ticket);
    g_free(host);
    if (ca_cert != NULL)
        g_byte_array_unref(ca_cert);
    if (error != NULL)
        g_error_free(error);
    if (display != NULL)
        g_object_unref(display);
    if (vm != NULL)
        g_object_unref(vm);
    if (proxy != NULL)
        g_object_unref(proxy);

    return 0;
}
예제 #4
0
static void test_govirt_list_vms(void)
{
    OvirtProxy *proxy;
    OvirtApi *api;
    OvirtCollection *vms;
    OvirtResource *vm;
    GError *error = NULL;
    GovirtMockHttpd *httpd;

    const char *vms_body = "<vms> \
                              <vm href=\"api/vms/uuid0\" id=\"uuid0\"> \
                                <name>vm0</name> \
                              </vm> \
                              <vm href=\"api/vms/uuid1\" id=\"uuid1\"> \
                                <name>vm1</name> \
                                <type>desktop</type> \
                                <status><state>up</state></status> \
                                <display> \
                                    <type>spice</type> \
                                    <address>10.0.0.123</address> \
                                    <secure_port>5900</secure_port> \
                                    <monitors>1</monitors> \
                                    <single_qxl_pci>true</single_qxl_pci> \
                                    <allow_override>false</allow_override> \
                                    <smartcard_enabled>false</smartcard_enabled> \
                                    <proxy>10.0.0.10</proxy> \
                                    <file_transfer_enabled>true</file_transfer_enabled> \
                                    <copy_paste_enabled>true</copy_paste_enabled> \
                                </display> \
                              </vm> \
                              <vm href=\"api/vms/uuid2\" id=\"uuid2\"> \
                                <name>vm2</name> \
                              </vm> \
                            </vms>";

    httpd = govirt_mock_httpd_new(GOVIRT_HTTPS_PORT);
    govirt_mock_httpd_add_request(httpd, "GET", "/ovirt-engine/api",
                                  "<api><link href=\"/ovirt-engine/api/vms\" rel=\"vms\"/></api>");
    govirt_mock_httpd_add_request(httpd, "GET", "/ovirt-engine/api/vms", vms_body);
    govirt_mock_httpd_start(httpd);

    proxy = ovirt_proxy_new("localhost:" G_STRINGIFY(GOVIRT_HTTPS_PORT));
    ovirt_proxy_set_mock_ca(proxy);
    api = ovirt_proxy_fetch_api(proxy, &error);
    g_test_assert_expected_messages();
    g_assert_nonnull(api);
    g_assert_no_error(error);

    vms = ovirt_api_get_vms(api);
    ovirt_collection_fetch(vms, proxy, &error);
    g_assert_no_error(error);

    vm = ovirt_collection_lookup_resource(vms, "vm1");
    g_assert_nonnull(vm);

    check_vm_display(OVIRT_VM(vm));

    g_object_unref(vm);
    g_object_unref(proxy);

    govirt_mock_httpd_stop(httpd);
}