From c81f18b9dd4888145ac979addb4ef5d73585a176 Mon Sep 17 00:00:00 2001
From: "Eduardo Lima (Etrunko)" <etrunko@redhat.com>
Date: Tue, 19 Sep 2017 12:02:32 -0300
Subject: [PATCH] vm-display: Move XML parsing from ovirt-vm-xml.c file

Following the model of other resources, the code for parsing the XML
elements for the OvirtVmDisplay object where it really belongs to.

Signed-off-by: Eduardo Lima (Etrunko) <etrunko@redhat.com>
---
 govirt/Makefile.am        |  1 -
 govirt/ovirt-vm-display.c | 60 +++++++++++++++++++++++++++++
 govirt/ovirt-vm-display.h |  2 +
 govirt/ovirt-vm-xml.c     | 96 -----------------------------------------------
 govirt/ovirt-vm.c         | 15 ++++++--
 5 files changed, 74 insertions(+), 100 deletions(-)
 delete mode 100644 govirt/ovirt-vm-xml.c

diff --git a/govirt/Makefile.am b/govirt/Makefile.am
index 9bf0eba..1a59f2c 100644
--- a/govirt/Makefile.am
+++ b/govirt/Makefile.am
@@ -73,7 +73,6 @@ libgovirt_la_SOURCES =						\
	ovirt-utils.c						\
	ovirt-vm.c						\
	ovirt-vm-display.c					\
-	ovirt-vm-xml.c						\
	ovirt-vm-pool.c						\
	$(NULL)

diff --git a/govirt/ovirt-vm-display.c b/govirt/ovirt-vm-display.c
index b03c303..ebb04c2 100644
--- a/govirt/ovirt-vm-display.c
+++ b/govirt/ovirt-vm-display.c
@@ -24,6 +24,7 @@

 #include "ovirt-enum-types.h"
 #include "ovirt-vm-display.h"
+#include "ovirt-utils.h"

 #define OVIRT_VM_DISPLAY_GET_PRIVATE(obj)                         \
         (G_TYPE_INSTANCE_GET_PRIVATE((obj), OVIRT_TYPE_VM_DISPLAY, OvirtVmDisplayPrivate))
@@ -303,3 +304,62 @@ OvirtVmDisplay *ovirt_vm_display_new(void)
 {
     return OVIRT_VM_DISPLAY(g_object_new(OVIRT_TYPE_VM_DISPLAY, NULL));
 }
+
+static gboolean ovirt_vm_display_set_from_xml(OvirtVmDisplay *display, RestXmlNode *node)
+{
+    OvirtVmDisplayType type;
+    OvirtXmlElement display_elements[] = {
+        { .prop_name = "type",
+          .xml_path = "type",
+        },
+        { .prop_name = "address",
+          .xml_path = "address",
+        },
+        { .prop_name = "port",
+          .xml_path = "port",
+        },
+        { .prop_name = "secure-port",
+          .xml_path = "secure_port",
+        },
+        { .prop_name = "monitor-count",
+          .xml_path = "monitors",
+        },
+        { .prop_name = "smartcard",
+          .xml_path = "smartcard_enabled",
+        },
+        { .prop_name = "allow-override",
+          .xml_path = "allow_override",
+        },
+        { .prop_name = "host-subject",
+          .xml_path = "certificate/subject",
+        },
+        { .prop_name = "proxy-url",
+          .xml_path = "proxy",
+        },
+        { NULL, },
+    };
+
+    ovirt_rest_xml_node_parse(node, G_OBJECT(display), display_elements);
+    g_object_get(G_OBJECT(display), "type", &type, NULL);
+    if (type == OVIRT_VM_DISPLAY_INVALID) {
+        return FALSE;
+    }
+
+    return TRUE;
+}
+
+OvirtVmDisplay *ovirt_vm_display_new_from_xml(RestXmlNode *node)
+{
+    OvirtVmDisplay *display;
+
+    g_return_val_if_fail(node != NULL, NULL);
+
+    display = ovirt_vm_display_new();
+
+    if (!ovirt_vm_display_set_from_xml(display, node)) {
+        g_object_unref(display);
+        return NULL;
+    }
+
+    return display;
+}
diff --git a/govirt/ovirt-vm-display.h b/govirt/ovirt-vm-display.h
index 38ef9b7..11a5074 100644
--- a/govirt/ovirt-vm-display.h
+++ b/govirt/ovirt-vm-display.h
@@ -24,6 +24,7 @@

 #include <glib-object.h>
 #include <govirt/ovirt-types.h>
+#include <rest/rest-xml-node.h>

 G_BEGIN_DECLS

@@ -61,6 +62,7 @@ typedef enum {

 GType ovirt_vm_display_get_type(void);
 OvirtVmDisplay *ovirt_vm_display_new(void);
+OvirtVmDisplay *ovirt_vm_display_new_from_xml(RestXmlNode *node);

 G_END_DECLS

diff --git a/govirt/ovirt-vm-xml.c b/govirt/ovirt-vm-xml.c
deleted file mode 100644
index 0603427..0000000
--- a/govirt/ovirt-vm-xml.c
+++ /dev/null
@@ -1,96 +0,0 @@
-/*
- * ovirt-vm-xml.c
- *
- * Copyright (C) 2011, 2013 Red Hat, Inc.
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library. If not, see
- * <http://www.gnu.org/licenses/>.
- *
- * Author: Christophe Fergeau <cfergeau@redhat.com>
- */
-#include <config.h>
-
-#include <stdlib.h>
-#include <string.h>
-
-#include "ovirt-enum-types.h"
-#include "ovirt-utils.h"
-#include "ovirt-vm.h"
-#include "ovirt-vm-display.h"
-#include "ovirt-vm-private.h"
-
-static gboolean vm_set_display_from_xml(OvirtVm *vm,
-                                        RestXmlNode *root)
-{
-    OvirtVmDisplay *display;
-    OvirtVmDisplayType type;
-    OvirtXmlElement display_elements[] = {
-        { .prop_name = "type",
-          .xml_path = "type",
-        },
-        { .prop_name = "address",
-          .xml_path = "address",
-        },
-        { .prop_name = "port",
-          .xml_path = "port",
-        },
-        { .prop_name = "secure-port",
-          .xml_path = "secure_port",
-        },
-        { .prop_name = "monitor-count",
-          .xml_path = "monitors",
-        },
-        { .prop_name = "smartcard",
-          .xml_path = "smartcard_enabled",
-        },
-        { .prop_name = "allow-override",
-          .xml_path = "allow_override",
-        },
-        { .prop_name = "host-subject",
-          .xml_path = "certificate/subject",
-        },
-        { .prop_name = "proxy-url",
-          .xml_path = "proxy",
-        },
-        { NULL, },
-    };
-
-    if (root == NULL) {
-        return FALSE;
-    }
-    root = rest_xml_node_find(root, "display");
-    if (root == NULL) {
-        g_debug("Could not find 'display' node");
-        return FALSE;
-    }
-    display = ovirt_vm_display_new();
-    ovirt_rest_xml_node_parse(root, G_OBJECT(display), display_elements);
-    g_object_get(G_OBJECT(display), "type", &type, NULL);
-    if (type == OVIRT_VM_DISPLAY_INVALID) {
-        return FALSE;
-    }
-
-    /* FIXME: this overrides the ticket/expiry which may
-     * already be set
-     */
-    g_object_set(G_OBJECT(vm), "display", display, NULL);
-    g_object_unref(G_OBJECT(display));
-
-    return TRUE;
-}
-
-G_GNUC_INTERNAL gboolean ovirt_vm_refresh_from_xml(OvirtVm *vm, RestXmlNode *node)
-{
-    return vm_set_display_from_xml(vm, node);
-}
diff --git a/govirt/ovirt-vm.c b/govirt/ovirt-vm.c
index f30022d..95c1e4d 100644
--- a/govirt/ovirt-vm.c
+++ b/govirt/ovirt-vm.c
@@ -180,7 +180,8 @@ static gboolean ovirt_vm_init_from_xml(OvirtResource *resource,
                                        RestXmlNode *node,
                                        GError **error)
 {
-    gboolean parsed_ok;
+    OvirtVmDisplay *display;
+    RestXmlNode *display_node;
     OvirtResourceClass *parent_class;
     OvirtXmlElement vm_elements[] = {
         { .prop_name = "host-href",
@@ -205,11 +206,19 @@ static gboolean ovirt_vm_init_from_xml(OvirtResource *resource,
         { NULL, },
     };

-    parsed_ok = ovirt_vm_refresh_from_xml(OVIRT_VM(resource), node);
-    if (!parsed_ok) {
+    display_node = rest_xml_node_find(node, "display");
+    if (display_node == NULL) {
+        g_debug("Could not find 'display' node");
         return FALSE;
     }

+    display = ovirt_vm_display_new_from_xml(display_node);
+    if (display == NULL)
+        return FALSE;
+
+    g_object_set(G_OBJECT(resource), "display", display, NULL);
+    g_object_unref(G_OBJECT(display));
+
     if (!ovirt_rest_xml_node_parse(node, G_OBJECT(resource), vm_elements))
         return FALSE;

--
2.14.4
