[ovs-dev] ovn-nbctl: Add lsp-get-ls command

Message ID 20190227172853.6792-1-lucasagomes@gmail.com
State New
Headers show
Series
  • [ovs-dev] ovn-nbctl: Add lsp-get-ls command
Related show

Commit Message

Lucas Alvares Gomes Feb. 27, 2019, 5:28 p.m.
From: Lucas Alvares Gomes <lucasagomes@gmail.com>

This commit adds the following command:

lsp-get-ls: Get the logical switch which the port belongs to.

This command is handy for scripting since there's no logical switch id
in the Logical_Switch_Port table.

Signed-off-by: Lucas Alvares Gomes <lucasagomes@gmail.com>
---
 ovn/utilities/ovn-nbctl.8.xml |  5 +++++
 ovn/utilities/ovn-nbctl.c     | 26 ++++++++++++++++++++++++++
 tests/ovn-nbctl.at            | 14 ++++++++++++++
 3 files changed, 45 insertions(+)

Comments

Ben Pfaff Feb. 27, 2019, 6:10 p.m. | #1
On Wed, Feb 27, 2019 at 05:28:53PM +0000, lucasagomes@gmail.com wrote:
> From: Lucas Alvares Gomes <lucasagomes@gmail.com>
> 
> This commit adds the following command:
> 
> lsp-get-ls: Get the logical switch which the port belongs to.
> 
> This command is handy for scripting since there's no logical switch id
> in the Logical_Switch_Port table.
> 
> Signed-off-by: Lucas Alvares Gomes <lucasagomes@gmail.com>

Thanks, applied to master.
0-day Robot Feb. 27, 2019, 6:13 p.m. | #2
Bleep bloop.  Greetings Lucas Alvares Gomes, I am a robot and I have tried out your patch.
Thanks for your contribution.

I encountered some error that I wasn't expecting.  See the details below.


checkpatch:
WARNING: Line lacks whitespace around operator
#44 FILE: ovn/utilities/ovn-nbctl.c:616:
  lsp-get-ls PORT           get the logical switch which the port belongs to\n\

ERROR: Improper whitespace around control block
#65 FILE: ovn/utilities/ovn-nbctl.c:1893:
    NBREC_LOGICAL_SWITCH_FOR_EACH(ls, ctx->idl) {

Lines checked: 115, Warnings: 1, Errors: 1


Please check this out.  If you feel there has been an error, please email aconole@bytheb.org

Thanks,
0-day Robot

Patch

diff --git a/ovn/utilities/ovn-nbctl.8.xml b/ovn/utilities/ovn-nbctl.8.xml
index 14cc02f53..a7a9c2701 100644
--- a/ovn/utilities/ovn-nbctl.8.xml
+++ b/ovn/utilities/ovn-nbctl.8.xml
@@ -476,6 +476,11 @@ 
         Get the configured DHCPv6 options for the logical port.
       </dd>
 
+      <dt><code>lsp-get-ls</code> <var>port</var></dt>
+      <dd>
+        Get the logical switch which the <var>port</var> belongs to.
+      </dd>
+
     </dl>
 
     <h1>Logical Router Commands</h1>
diff --git a/ovn/utilities/ovn-nbctl.c b/ovn/utilities/ovn-nbctl.c
index cca7dbaa1..8a72e9574 100644
--- a/ovn/utilities/ovn-nbctl.c
+++ b/ovn/utilities/ovn-nbctl.c
@@ -613,6 +613,7 @@  Logical switch port commands:\n\
   lsp-set-dhcpv6-options PORT [DHCP_OPTIONS_UUID]\n\
                             set dhcpv6 options for PORT\n\
   lsp-get-dhcpv6-options PORT  get the dhcpv6 options for PORT\n\
+  lsp-get-ls PORT           get the logical switch which the port belongs to\n\
 \n\
 Logical router commands:\n\
   lr-add [ROUTER]           create a logical router named ROUTER\n\
@@ -1876,6 +1877,30 @@  nbctl_lsp_get_dhcpv6_options(struct ctl_context *ctx)
     }
 }
 
+static void
+nbctl_lsp_get_ls(struct ctl_context *ctx)
+{
+    const char *id = ctx->argv[1];
+    const struct nbrec_logical_switch_port *lsp = NULL;
+
+    char *error = lsp_by_name_or_uuid(ctx, id, true, &lsp);
+    if (error) {
+        ctx->error = error;
+        return;
+    }
+
+    const struct nbrec_logical_switch *ls;
+    NBREC_LOGICAL_SWITCH_FOR_EACH(ls, ctx->idl) {
+        for (size_t i = 0; i < ls->n_ports; i++) {
+            if (ls->ports[i] == lsp) {
+                ds_put_format(&ctx->output, UUID_FMT " (%s)\n",
+                      UUID_ARGS(&ls->header_.uuid), ls->name);
+                break;
+            }
+        }
+    }
+}
+
 enum {
     DIR_FROM_LPORT,
     DIR_TO_LPORT
@@ -5115,6 +5140,7 @@  static const struct ctl_command_syntax nbctl_commands[] = {
       nbctl_lsp_set_dhcpv6_options, NULL, "", RW },
     { "lsp-get-dhcpv6-options", 1, 1, "PORT", NULL,
       nbctl_lsp_get_dhcpv6_options, NULL, "", RO },
+    { "lsp-get-ls", 1, 1, "PORT", NULL, nbctl_lsp_get_ls, NULL, "", RO },
 
     /* logical router commands. */
     { "lr-add", 0, 1, "[ROUTER]", NULL, nbctl_lr_add, NULL,
diff --git a/tests/ovn-nbctl.at b/tests/ovn-nbctl.at
index a4c8ed3c3..f884fc7d2 100644
--- a/tests/ovn-nbctl.at
+++ b/tests/ovn-nbctl.at
@@ -153,6 +153,20 @@  AT_CHECK([ovn-nbctl --may-exist lsp-add ls0 lp2 lp3 5], [1], [],
 
 dnl ---------------------------------------------------------------------
 
+OVN_NBCTL_TEST([ovn_nbctl_lsp_get_ls], [lsp get ls], [
+AT_CHECK([ovn-nbctl ls-add ls0])
+AT_CHECK([ovn-nbctl lsp-add ls0 lp0])
+
+AT_CHECK([ovn-nbctl lsp-get-ls lp0 | uuidfilt], [0], [dnl
+<0> (ls0)
+])
+
+AT_CHECK([ovn-nbctl lsp-get-ls lp1], [1], [],
+  [ovn-nbctl: lp1: port name not found
+])])
+
+dnl ---------------------------------------------------------------------
+
 OVN_NBCTL_TEST([ovn_nbctl_lport_addresses], [lport addresses], [
 AT_CHECK([ovn-nbctl ls-add ls0])
 AT_CHECK([ovn-nbctl lsp-add ls0 lp0])