Re: [libvirt-designer][PATCH v2 4/4] domain: Introduce interface support

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Mon, Sep 10, 2012 at 03:58:28PM +0200, Michal Privoznik wrote:
> Let users add NICs to domains.
> ---
>  examples/virtxml.c                         |   78 ++++++++++++++++++++++++++
>  libvirt-designer/libvirt-designer-domain.c |   83 ++++++++++++++++++++++++++++
>  libvirt-designer/libvirt-designer-domain.h |    3 +
>  libvirt-designer/libvirt-designer.sym      |    1 +
>  4 files changed, 165 insertions(+), 0 deletions(-)
> 
> diff --git a/examples/virtxml.c b/examples/virtxml.c
> index 20e3f3c..df83f57 100644
> --- a/examples/virtxml.c
> +++ b/examples/virtxml.c
> @@ -35,6 +35,7 @@
>  #include <unistd.h>
>  
>  GList *disk_str_list = NULL;
> +GList *iface_str_list = NULL;
>  
>  #define print_error(...) \
>      print_error_impl(__FUNCTION__, __LINE__, __VA_ARGS__)
> @@ -215,6 +216,79 @@ add_disk_str(const gchar *option_name,
>      return TRUE;
>  }
>  
> +static void
> +add_iface(gpointer data,
> +          gpointer user_data)
> +{
> +    GVirDesignerDomain *domain = (GVirDesignerDomain *) user_data;
> +    char *network = (char *) data;
> +    char *param = NULL;
> +    GVirConfigDomainInterface *iface = NULL;
> +    GError *error = NULL;
> +
> +    param = strchr(network, ',');
> +    if (param) {
> +        *param = '\0';
> +        param++;
> +    }

glib provides a g_strsplit (and plenty of useful functions in
http://developer.gnome.org/glib/stable/glib-String-Utility-Functions.html )

> +
> +    iface = gvir_designer_domain_add_interface_network(domain, network, &error);
> +    if (error) {
> +        print_error("%s", error->message);
> +        exit(EXIT_FAILURE);

An alternative here can be g_error

> +    }
> +
> +    while (param && *param) {
> +        char *key = param;
> +        char *val;
> +        GVirConfigDomainInterfaceLinkState link;
> +
> +        /* move to next token */
> +        param = strchr(param, ',');
> +        if (param) {
> +            *param = '\0';
> +            param++;
> +        }
> +
> +        /* parse token */
> +        val = strchr(key, '=');
> +        if (!val) {
> +            print_error("Invalid format: %s", key);
> +            exit(EXIT_FAILURE);
> +        }
> +
> +        *val = '\0';
> +        val++;
> +
> +        if (!strcmp(key, "mac")) {
> +            gvir_config_domain_interface_set_mac(iface, val);
> +        } else if (!strcmp(key, "link")) {
> +            if (!strcmp(val, "up")) {
> +                link = GVIR_CONFIG_DOMAIN_INTERFACE_LINK_STATE_UP;
> +            } else if (!strcmp(val, "down")) {
> +                link = GVIR_CONFIG_DOMAIN_INTERFACE_LINK_STATE_DOWN;
> +            } else {
> +                print_error("Unknown value: %s", val);
> +                exit(EXIT_FAILURE);
> +            }

If you find yourself doing similar parsing in several places, a function
similar to  gvir_config_genum_get_value can be really useful
( http://libvirt.org/git/?p=libvirt-glib.git;a=blob;f=libvirt-gconfig/libvirt-gconfig-helpers.c;h=87b13752158a376f5f39b3f51ad98020fc93f8a9;hb=HEAD#l264
)

Christophe

Attachment: pgpFnzyk7OkMc.pgp
Description: PGP signature

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list

[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]