ovs-vsctl: Make "create" command print new row's UUID.
authorBen Pfaff <blp@nicira.com>
Thu, 28 Jan 2010 22:00:22 +0000 (14:00 -0800)
committerBen Pfaff <blp@nicira.com>
Fri, 29 Jan 2010 00:10:01 +0000 (16:10 -0800)
Also, document limitations of other commands regarding UUIDs of new rows.

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

index 84e978e..d80310b 100644 (file)
@@ -455,6 +455,9 @@ overridden this way.
 .IP "\fBlist \fItable \fR[\fIrecord\fR]..."
 List the values of all columns of each specified \fIrecord\fR.  If no
 records are specified, lists all the records in \fItable\fR.
+.IP
+The UUIDs shown for rows created in the same \fBovs\-vsctl\fR
+invocation will be wrong.
 .
 .IP "\fBget \fItable record column\fR[\fB:\fIkey\fR]..."
 Prints the value of each specified \fIcolumn\fR in the given
@@ -498,7 +501,7 @@ that are allowed to be empty.
 .IP "\fB\-\-force create \fItable column\fR[\fB:\fIkey\fR]\fB=\fIvalue\fR..."
 Creates a new record in \fItable\fR and sets the initial values of
 each \fIcolumn\fR.  Columns not explicitly set will receive their
-default values.
+default values.  Outputs the UUID of the new row.
 .IP
 This command requires the \fB\-\-force\fR option.
 .
index a555565..60ab0f5 100644 (file)
@@ -2348,6 +2348,30 @@ cmd_create(struct vsctl_context *ctx)
     ds_put_format(&ctx->output, UUID_FMT, UUID_ARGS(&row->uuid));
 }
 
+/* This function may be used as the 'postprocess' function for commands that
+ * insert new rows into the database.  It expects that the command's 'run'
+ * function prints the UUID reported by ovsdb_idl_txn_insert() as the command's
+ * sole output.  It replaces that output by the row's permanent UUID assigned
+ * by the database server and appends a new-line.
+ *
+ * Currently we use this only for "create", because the higher-level commands
+ * are supposed to be independent of the actual structure of the vswitch
+ * configuration. */
+static void
+post_create(struct vsctl_context *ctx)
+{
+    const struct uuid *real;
+    struct uuid dummy;
+
+    uuid_from_string(&dummy, ds_cstr(&ctx->output));
+    real = ovsdb_idl_txn_get_insert_uuid(ctx->txn, &dummy);
+    if (real) {
+        ds_clear(&ctx->output);
+        ds_put_format(&ctx->output, UUID_FMT, UUID_ARGS(real));
+    }
+    ds_put_char(&ctx->output, '\n');
+}
+
 static void
 cmd_destroy(struct vsctl_context *ctx)
 {
@@ -2585,7 +2609,7 @@ static const struct vsctl_command_syntax all_commands[] = {
     {"add", 4, INT_MAX, cmd_add, NULL, "--force"},
     {"remove", 4, INT_MAX, cmd_remove, NULL, "--force"},
     {"clear", 3, INT_MAX, cmd_clear, NULL, "--force"},
-    {"create", 2, INT_MAX, cmd_create, NULL, "--force"},
+    {"create", 2, INT_MAX, cmd_create, post_create, "--force"},
     {"destroy", 1, INT_MAX, cmd_destroy, NULL, "--force,--if-exists"},
 
     {NULL, 0, 0, NULL, NULL, NULL},