ovs-vsctl: Allow "get" commands to create @names also.
authorBen Pfaff <blp@nicira.com>
Mon, 20 Sep 2010 17:56:15 +0000 (10:56 -0700)
committerBen Pfaff <blp@nicira.com>
Tue, 21 Sep 2010 21:28:02 +0000 (14:28 -0700)
This is useful for adding records that refer to other records by UUID, e.g.
   ovs-vsctl \
     -- set bridge br0 mirrors=@m \
     -- --id=@eth0 get port eth0 \
     -- --id=@eth0 get port eth1 \
     -- --id=@m create mirror name=mymirror select-dst-port=@eth0 \
                              select-src-port=@eth0 output-port=@eth1

tests/ovs-vsctl.at
utilities/ovs-vsctl.8.in
utilities/ovs-vsctl.c

index 66ead6d..152a658 100644 (file)
@@ -705,6 +705,55 @@ AT_CHECK([cat stdout4], [0], [500
 OVS_VSCTL_CLEANUP
 AT_CLEANUP
 
+AT_SETUP([--id option on create, get commands])
+AT_KEYWORDS([ovs-vsctl])
+OVS_VSCTL_SETUP
+AT_CHECK([RUN_OVS_VSCTL([add-br br0],
+                        [add-port br0 eth0],
+                        [add-port br0 eth1])])
+AT_CHECK(
+  [RUN_OVS_VSCTL_TOGETHER(
+    [set bridge br0 mirrors=@m],
+    [--id=@eth0 get port eth0],
+    [--id=@eth1 get port eth1],
+    [--id=@m create mirror name=mymirror select-dst-port=@eth0 select-src-port=@eth0 output-port=@eth1])],
+  [0], [stdout], [], [OVS_VSCTL_CLEANUP])
+AT_CHECK(
+  [perl $srcdir/uuidfilt.pl stdout], [0], [dnl
+
+
+
+<0>
+],
+  [], [OVS_VSCTL_CLEANUP])
+AT_CHECK(
+  [RUN_OVS_VSCTL(
+    [list port eth0 eth1],
+    [list mirror],
+    [list bridge br0])],
+  [0], [stdout], [], [OVS_VSCTL_CLEANUP])
+AT_CHECK(
+  [sed -n -e '/uuid/p' -e '/name/p' -e '/mirrors/p' -e '/select/p' -e '/output/p' < stdout | $srcdir/uuidfilt.pl], [0], [dnl
+[_uuid               : <0>
+name                : "eth0"
+_uuid               : <1>
+name                : "eth1"
+_uuid               : <2>
+name                : mymirror
+output_port         : <1>
+output_vlan         : []
+select_all          : false
+select_dst_port     : [<0>]
+select_src_port     : [<0>]
+select_vlan         : []
+_uuid               : <3>
+mirrors             : [<2>]
+name                : "br0"
+]],
+  [], [OVS_VSCTL_CLEANUP])
+OVS_VSCTL_CLEANUP
+AT_CLEANUP
+
 dnl This test really shows a bug -- "create" followed by "list" in
 dnl the same execution shows the wrong UUID on the "list" command.
 dnl The bug is documented in ovs-vsctl.8.
index ef080bb..cfb9114 100644 (file)
@@ -490,7 +490,7 @@ pair of double quotes (\fB""\fR).
 .IP "UUID"
 Either a universally unique identifier in the style of RFC 4122,
 e.g. \fBf81d4fae\-7dec\-11d0\-a765\-00a0c91e6bf6\fR, or an \fB@\fIname\fR
-defined by the \fBcreate\fR command within the same \fBovs\-vsctl\fR
+defined by a \fBget\fR or \fBcreate\fR command within the same \fBovs\-vsctl\fR
 invocation.
 .PP
 Multiple values in a single column may be separated by spaces or a
@@ -518,7 +518,7 @@ records are specified, lists all the records in \fItable\fR.
 The UUIDs shown for rows created in the same \fBovs\-vsctl\fR
 invocation will be wrong.
 .
-.IP "[\fB\-\-if\-exists\fR] \fBget \fItable record column\fR[\fB:\fIkey\fR]..."
+.IP "[\fB\-\-id=@\fIname\fR] [\fB\-\-if\-exists\fR] \fBget \fItable record \fR[\fIcolumn\fR[\fB:\fIkey\fR]]..."
 Prints the value of each specified \fIcolumn\fR in the given
 \fIrecord\fR in \fItable\fR.  For map columns, a \fIkey\fR may
 optionally be specified, in which case the value associated with
@@ -528,6 +528,10 @@ For a map column, without \fB\-\-if\-exists\fR it is an error if
 \fIkey\fR does not exist; with it, a blank line is printed.  If
 \fIcolumn\fR is not a map column or if \fIkey\fR is not specified,
 \fB\-\-if\-exists\fR has no effect.
+.IP
+If \fB@\fIname\fR is specified, then the UUID for \fIrecord\fR may be
+referred to by that name later in the same \fBovs\-vsctl\fR
+invocation in contexts where a UUID is expected.
 .
 .IP "\fBset \fItable record column\fR[\fB:\fIkey\fR]\fB=\fIvalue\fR..."
 Sets the value of each specified \fIcolumn\fR in the given
index 884a41f..d09cf74 100644 (file)
@@ -1975,6 +1975,28 @@ get_column(const struct vsctl_table_class *table, const char *column_name,
     }
 }
 
+static struct uuid *
+create_symbol(struct ovsdb_symbol_table *symtab, const char *id, bool *newp)
+{
+    struct ovsdb_symbol *symbol;
+
+    if (id[0] != '@') {
+        vsctl_fatal("row id \"%s\" does not begin with \"@\"", id);
+    }
+
+    if (newp) {
+        *newp = ovsdb_symbol_table_get(symtab, id) == NULL;
+    }
+
+    symbol = ovsdb_symbol_table_insert(symtab, id);
+    if (symbol->used) {
+        vsctl_fatal("row id \"%s\" may only be specified on one --id option",
+                    id);
+    }
+    symbol->used = true;
+    return &symbol->uuid;
+}
+
 static char *
 missing_operator_error(const char *arg, const char **allowed_operators,
                        size_t n_allowed)
@@ -2142,6 +2164,7 @@ error:
 static void
 cmd_get(struct vsctl_context *ctx)
 {
+    const char *id = shash_find_data(&ctx->options, "--id");
     bool if_exists = shash_find(&ctx->options, "--if-exists");
     const char *table_name = ctx->argv[1];
     const char *record_id = ctx->argv[2];
@@ -2152,6 +2175,15 @@ cmd_get(struct vsctl_context *ctx)
 
     table = get_table(table_name);
     row = must_get_row(ctx, table, record_id);
+    if (id) {
+        bool new;
+
+        *create_symbol(ctx->symtab, id, &new) = row->uuid;
+        if (!new) {
+            vsctl_fatal("row id \"%s\" specified on \"get\" command was used "
+                        "before it was defined", id);
+        }
+    }
     for (i = 3; i < ctx->argc; i++) {
         const struct ovsdb_idl_column *column;
         const struct ovsdb_datum *datum;
@@ -2453,24 +2485,7 @@ cmd_create(struct vsctl_context *ctx)
     const struct uuid *uuid;
     int i;
 
-    if (id) {
-        struct ovsdb_symbol *symbol;
-
-        if (id[0] != '@') {
-            vsctl_fatal("row id \"%s\" does not begin with \"@\"", id);
-        }
-
-        symbol = ovsdb_symbol_table_insert(ctx->symtab, id);
-        if (symbol->used) {
-            vsctl_fatal("row id \"%s\" may only be used to insert a single "
-                        "row", id);
-        }
-        symbol->used = true;
-
-        uuid = &symbol->uuid;
-    } else {
-        uuid = NULL;
-    }
+    uuid = id ? create_symbol(ctx->symtab, id, NULL) : NULL;
 
     table = get_table(table_name);
     row = ovsdb_idl_txn_insert(ctx->txn, table->class, uuid);
@@ -2865,7 +2880,7 @@ static const struct vsctl_command_syntax all_commands[] = {
     {"emer-reset", 0, 0, cmd_emer_reset, NULL, ""},
 
     /* Parameter commands. */
-    {"get", 3, INT_MAX, cmd_get, NULL, "--if-exists"},
+    {"get", 2, INT_MAX, cmd_get, NULL, "--if-exists,--id="},
     {"list", 1, INT_MAX, cmd_list, NULL, ""},
     {"set", 3, INT_MAX, cmd_set, NULL, ""},
     {"add", 4, INT_MAX, cmd_add, NULL, ""},