From nobody Mon Sep 16 19:00:59 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 170.10.129.124 as permitted sender) client-ip=170.10.129.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=fail(p=none dis=none) header.from=riseup.net ARC-Seal: i=1; a=rsa-sha256; t=1693756233; cv=none; d=zohomail.com; s=zohoarc; b=GmOKqKt2DPJynGK62OOpw/wR+wqKgNFx4a52vRc6X5k3n+KcIt2su0uCbIPf4w8GxapXqU4Tfe0n2TscYTTtgbMeNilY5E6EkH3xWUh3Te8UZx9MEoFsoYcavrlTBdvIh2GTwnk5v0B+fABI5VGq+cz0D55NYpi39dKrk+yWBQQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1693756233; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=LnB5mxsZEL6D7s3lxWP215S3oZ01UN2+ttH8ZNRmnKI=; b=X32V+q5Gu7eSBG8UEDiAjhBHF4Mm5j/zoi8jOJtSwK1p0jjTC6f71dZuChxdlMubuLkP00obRamGyUQILNTv3teBYjvj0OdMwi6Lt+pBdL2Prk8wS7v8nuLBE8iRwYRTXh9aAgWbTkE4tJLiurNWxWZ2P+V3lkJLMCnxxw6Txs4= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=fail header.from= (p=none dis=none) Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by mx.zohomail.com with SMTPS id 1693756233107274.780719251935; Sun, 3 Sep 2023 08:50:33 -0700 (PDT) Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-515-TtaT5QgBMj6r-hk2G6IAew-1; Sun, 03 Sep 2023 11:50:27 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id BBDC9897F66; Sun, 3 Sep 2023 15:50:25 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com [10.30.29.100]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9F50621EE560; Sun, 3 Sep 2023 15:50:25 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (localhost [IPv6:::1]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id 5E0FF1946A46; Sun, 3 Sep 2023 15:50:25 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id 94F111946597 for ; Sun, 3 Sep 2023 15:50:24 +0000 (UTC) Received: by smtp.corp.redhat.com (Postfix) id 69A914043CED; Sun, 3 Sep 2023 15:50:24 +0000 (UTC) Received: from mimecast-mx02.redhat.com (mimecast08.extmail.prod.ext.rdu2.redhat.com [10.11.55.24]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 6200240C206F for ; Sun, 3 Sep 2023 15:50:24 +0000 (UTC) Received: from us-smtp-1.mimecast.com (us-smtp-inbound-delivery-1.mimecast.com [207.211.31.81]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 466773811F35 for ; Sun, 3 Sep 2023 15:50:24 +0000 (UTC) Received: from mx1.riseup.net (mx1.riseup.net [198.252.153.129]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-117--5eIf75NOuCfgjvngZJfLw-1; Sun, 03 Sep 2023 11:50:21 -0400 Received: from fews02-sea.riseup.net (fews02-sea-pn.riseup.net [10.0.1.112]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx1.riseup.net (Postfix) with ESMTPS id 4Rdx6z2pCKzDrCZ for ; Sun, 3 Sep 2023 15:50:19 +0000 (UTC) Received: from [127.0.0.1] (localhost [127.0.0.1]) by fews02-sea.riseup.net (Postfix) with ESMTPSA id 4Rdx6y1pVwzFpfK; Sun, 3 Sep 2023 15:50:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1693756232; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=LnB5mxsZEL6D7s3lxWP215S3oZ01UN2+ttH8ZNRmnKI=; b=CNzFAVYTdaMKBxEGGlz5o3MoxSaTCjY+rY/h5zcLhMcWGwerbTfpsTsuhaYMRizee9jz3V 4B40NorrVYwlBTH3LGHPBldaNGOz7KD26iwZRFCfj7V/pdrTxc++M1RuMjjiQoOF5tkp+Q 3l528Jgj4cPk2mDy8GBrEpjF4YEhC7Y= X-MC-Unique: TtaT5QgBMj6r-hk2G6IAew-1 X-Original-To: libvir-list@listman.corp.redhat.com X-MC-Unique: -5eIf75NOuCfgjvngZJfLw-1 X-Riseup-User-ID: BA55BC3A7CCB1EF14FD4128BBC58210F537DAC43A7F9DA9AEEEDD61A5AC7419E From: K Shiva Kiran To: libvir-list@redhat.com Subject: [PATCH 09/16] virsh: Add new command `nwfilter-desc` Date: Sun, 3 Sep 2023 21:19:10 +0530 Message-ID: <20230903155034.211476-10-shiva_kr@riseup.net> In-Reply-To: <20230903155034.211476-1-shiva_kr@riseup.net> References: <20230903155034.211476-1-shiva_kr@riseup.net> MIME-Version: 1.0 X-Mimecast-Impersonation-Protect: Policy=CLT - Impersonation Protection Definition; Similar Internal Domain=false; Similar Monitored External Domain=false; Custom External Domain=false; Mimecast External Domain=false; Newly Observed Domain=false; Internal User Name=false; Custom Display Name List=false; Reply-to Address Mismatch=false; Targeted Threat Dictionary=false; Mimecast Threat Dictionary=false; Custom Threat Dictionary=false X-Scanned-By: MIMEDefang 3.1 on 10.11.54.1 X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: K Shiva Kiran Errors-To: libvir-list-bounces@redhat.com Sender: "libvir-list" X-Scanned-By: MIMEDefang 3.1 on 10.11.54.6 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: riseup.net Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1693756233438100002 Content-Type: text/plain; charset="utf-8"; x-default="true" This command can be used to view/modify the `` and `<description>` fields of the NWFilter object. Signed-off-by: K Shiva Kiran <shiva_kr@riseup.net> --- tools/virsh-nwfilter.c | 209 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 209 insertions(+) diff --git a/tools/virsh-nwfilter.c b/tools/virsh-nwfilter.c index 92b2b7b3bc..615d126def 100644 --- a/tools/virsh-nwfilter.c +++ b/tools/virsh-nwfilter.c @@ -26,6 +26,7 @@ #include "viralloc.h" #include "virfile.h" #include "vsh-table.h" +#include "virxml.h" =20 virNWFilterPtr virshCommandOptNWFilterBy(vshControl *ctl, const vshCmd *cmd, @@ -345,6 +346,53 @@ virshNWFilterListCollect(vshControl *ctl, return list; } =20 +/* extract description or title from nwfilter xml */ +static char * +virshGetNWFilterDescription(vshControl *ctl, virNWFilterPtr nwfilter, + bool title, unsigned int flags, + unsigned int queryflags) +{ + char *desc =3D NULL; + g_autoptr(xmlDoc) doc =3D NULL; + g_autoptr(xmlXPathContext) ctxt =3D NULL; + int type; + + if (title) + type =3D VIR_NWFILTER_METADATA_TITLE; + else + type =3D VIR_NWFILTER_METADATA_DESCRIPTION; + + if ((desc =3D virNWFilterGetMetadata(nwfilter, type, NULL, flags))) { + return desc; + } else { + int errCode =3D virGetLastErrorCode(); + + if (errCode =3D=3D VIR_ERR_NO_NWFILTER_METADATA) { + desc =3D g_strdup(""); + vshResetLibvirtError(); + return desc; + } + + if (errCode !=3D VIR_ERR_NO_SUPPORT) + return desc; + } + + /* fall back to xml */ + if (virshNWFilterGetXMLFromNWFilter(ctl, nwfilter, queryflags, &doc, &= ctxt) < 0) + return NULL; + + if (title) + desc =3D virXPathString("string(./title[1])", ctxt); + else + desc =3D virXPathString("string(./description[1])", ctxt); + + if (!desc) + desc =3D g_strdup(""); + + return desc; +} + + /* * "nwfilter-list" command */ @@ -768,6 +816,161 @@ cmdNWFilterBindingList(vshControl *ctl, const vshCmd = *cmd G_GNUC_UNUSED) } =20 =20 +/* + * "nwfilter-desc" command + */ +static const vshCmdInfo info_nwfilter_desc[] =3D { + {.name =3D "help", + .data =3D N_("show or set network filter's description or title") + }, + {.name =3D "desc", + .data =3D N_("Allows setting or modifying the description or title of= a network filter.") + }, + {.name =3D NULL} +}; + +static const vshCmdOptDef opts_nwfilter_desc[] =3D { + {.name =3D "nwfilter", + .type =3D VSH_OT_DATA, + .flags =3D VSH_OFLAG_REQ, + .help =3D N_("network filter name or uuid"), + .completer =3D virshNWFilterNameCompleter, + }, + {.name =3D "title", + .type =3D VSH_OT_BOOL, + .help =3D N_("modify/get the title instead of description") + }, + {.name =3D "edit", + .type =3D VSH_OT_BOOL, + .help =3D N_("open an editor to modify the description") + }, + {.name =3D "remove", + .type =3D VSH_OT_BOOL, + .help =3D N_("remove the element") + }, + {.name =3D "new-desc", + .type =3D VSH_OT_ARGV, + .help =3D N_("message") + }, + {.name =3D NULL} +}; + +static bool +cmdNWFilterDesc(vshControl *ctl, const vshCmd *cmd) +{ + g_autoptr(virshNWFilter) nwfilter =3D NULL; + bool title =3D vshCommandOptBool(cmd, "title"); + bool edit =3D vshCommandOptBool(cmd, "edit"); + bool remove =3D vshCommandOptBool(cmd, "remove"); + int type; + g_autofree char *descArg =3D NULL; + const vshCmdOpt *opt =3D NULL; + g_auto(virBuffer) buf =3D VIR_BUFFER_INITIALIZER; + unsigned int flags =3D 0; + unsigned int queryflags =3D 0; + + VSH_EXCLUSIVE_OPTIONS("remove", "edit"); + + if (!(nwfilter =3D virshCommandOptNWFilter(ctl, cmd, NULL))) + return false; + + if (title) + type =3D VIR_NWFILTER_METADATA_TITLE; + else + type =3D VIR_NWFILTER_METADATA_DESCRIPTION; + + + while ((opt =3D vshCommandOptArgv(ctl, cmd, opt))) + virBufferAsprintf(&buf, "%s ", opt->data); + + virBufferTrim(&buf, " "); + + descArg =3D virBufferContentAndReset(&buf); + + if (remove) { + + if (descArg) { + vshPrintExtra(ctl, "unexpected data: \'%s\'", descArg); + return false; + } + + if (virNWFilterSetMetadata(nwfilter, type, "", NULL, NULL, flags) = < 0) + goto error; + + vshPrintExtra(ctl, "%s removed successfully", title ? "Title" : "D= escription"); + + } else if (edit || descArg) { + + g_autofree char *descNWFilter =3D NULL; + g_autofree char *descNew =3D NULL; + + if (!(descNWFilter =3D virshGetNWFilterDescription(ctl, nwfilter, + title, flags, que= ryflags))) + return false; + + if (!descArg) + descArg =3D g_strdup(descNWFilter); + + if (edit) { + g_autoptr(vshTempFile) tmp =3D NULL; + g_autofree char *desc_edited =3D NULL; + char *tmpstr; + + /* Create and open a temporary file. */ + if (!(tmp =3D vshEditWriteToTempFile(ctl, descArg))) + return false; + + /* Start the editor. */ + if (vshEditFile(ctl, tmp) =3D=3D -1) + return false; + + /* Read back the edited file. */ + if (!(desc_edited =3D vshEditReadBackFile(ctl, tmp))) + return false; + + /* strip a possible newline at the end */ + if (title && + (tmpstr =3D strrchr(desc_edited, '\n')) && + *(tmpstr+1) =3D=3D '\0') + *tmpstr =3D '\0'; + + /* Check whether XML has changed */ + if (STREQ(descNWFilter, desc_edited)) { + vshPrintExtra(ctl, "Network filter %s has not changed", ti= tle ? "title" : "description"); + return true; + } + + descNew =3D g_steal_pointer(&desc_edited); + + } else { + descNew =3D g_steal_pointer(&descArg); + } + + if (virNWFilterSetMetadata(nwfilter, type, descNew, NULL, NULL, fl= ags) < 0) + goto error; + + vshPrintExtra(ctl, "Network filter %s updated successfully", title= ? "title" : "description"); + + } else { + g_autofree char *desc =3D virshGetNWFilterDescription(ctl, nwfilte= r, title, flags, queryflags); + if (!desc) + return false; + + if (strlen(desc) > 0) { + vshPrint(ctl, "%s", desc); + } else { + vshPrintExtra(ctl, _("No %1$s for network filter: %2$s"), titl= e ? "title" : "description", virNWFilterGetName(nwfilter)); + } + } + + return true; + + error: + vshError(ctl, "Failed to set %s for network filter", title ? "titl= e" : "description"); + return false; +} + + const vshCmdDef nwfilterCmds[] =3D { {.name =3D "nwfilter-define", .handler =3D cmdNWFilterDefine, @@ -823,5 +1026,11 @@ const vshCmdDef nwfilterCmds[] =3D { .info =3D info_nwfilter_binding_list, .flags =3D 0 }, + {.name =3D "nwfilter-desc", + .handler =3D cmdNWFilterDesc, + .opts =3D opts_nwfilter_desc, + .info =3D info_nwfilter_desc, + .flags =3D 0 + }, {.name =3D NULL} }; --=20 2.42.0