From 6d65eee8b1f4d96b9c925d3b31758268345a4636 Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Mon, 16 Nov 2009 15:12:04 -0800 Subject: [PATCH] ovsdb-client: New command "transact". --- ovsdb/ovsdb-client.1.in | 6 ++++++ ovsdb/ovsdb-client.c | 42 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/ovsdb/ovsdb-client.1.in b/ovsdb/ovsdb-client.1.in index c07a88c1..0337c3dc 100644 --- a/ovsdb/ovsdb-client.1.in +++ b/ovsdb/ovsdb-client.1.in @@ -18,6 +18,8 @@ ovsdb\-client \- command-line interface to \fBovsdb-server\fR(1) .br \fBovsdb\-client \fR[\fIoptions\fR] \fBlist-columns\fI server \fR[\fItable\fR] .br +\fBovsdb\-client \fR[\fIoptions\fR] \fBtransact\fI server transaction\fR +.br \fBovsdb\-client help\fR .IP "Output formatting options:" [\fB--format=\fIformat\fR] @@ -60,6 +62,10 @@ Connects to \fIserver\fR, retrieves the database schema, and prints a table listing the names, type, and comment (if any) on each column. If \fItable\fR is specified, only columns in that table are listed; otherwise, the tables include columns in all tables. +.IP "\fBovsdb\-client \fR[\fIoptions\fR] \fBtransact\fI server transaction\fR" +Connects to \fIserver\fR, sends it the specified \fItransaction\fR, +which must be a JSON array containing one or more valid OVSDB +operations, and prints the received reply on stdout. .SH OPTIONS .SS "Output Formatting Options" Much of the output from \fBovsdb\-client\fR is in the form of tables. diff --git a/ovsdb/ovsdb-client.c b/ovsdb/ovsdb-client.c index 93a91e7c..249cafe5 100644 --- a/ovsdb/ovsdb-client.c +++ b/ovsdb/ovsdb-client.c @@ -146,7 +146,10 @@ usage(void) "\n list-tables SERVER\n" " list SERVER's tables\n" "\n list-columns SERVER [TABLE]\n" - " list columns in TABLE (or all tables) on SERVER\n", + " list columns in TABLE (or all tables) on SERVER\n" + "\n transact SERVER TRANSACTION\n" + " run TRANSACTION (a JSON array of operations) on SERVER\n" + " and print the results as JSON on stdout\n", program_name, program_name); stream_usage("SERVER", true, true); printf("\nOutput formatting options:\n" @@ -161,6 +164,16 @@ usage(void) exit(EXIT_SUCCESS); } +static struct json * +parse_json(const char *s) +{ + struct json *json = json_from_string(s); + if (json->type == JSON_STRING) { + ovs_fatal(0, "\"%s\": %s", s, json->u.string); + } + return json; +} + static struct jsonrpc * open_jsonrpc(const char *server) { @@ -576,6 +589,32 @@ do_list_columns(int argc UNUSED, char *argv[]) table_print(&t); } +static void +do_transact(int argc UNUSED, char *argv[] UNUSED) +{ + struct jsonrpc_msg *request, *reply; + struct json *transaction; + struct jsonrpc *rpc; + int error; + + transaction = parse_json(argv[2]); + + rpc = open_jsonrpc(argv[1]); + request = jsonrpc_create_request("transact", transaction); + error = jsonrpc_transact_block(rpc, request, &reply); + if (error) { + ovs_fatal(error, "transaction failed"); + } + if (reply->error) { + ovs_fatal(error, "transaction returned error: %s", + json_to_string(reply->error, JSSF_SORT)); + } + print_json(reply->result); + putchar('\n'); + jsonrpc_msg_destroy(reply); + jsonrpc_close(rpc); +} + static void do_help(int argc UNUSED, char *argv[] UNUSED) { @@ -586,6 +625,7 @@ static const struct command all_commands[] = { { "get-schema", 1, 1, do_get_schema }, { "list-tables", 1, 1, do_list_tables }, { "list-columns", 1, 2, do_list_columns }, + { "transact", 2, 2, do_transact }, { "help", 0, INT_MAX, do_help }, { NULL, 0, 0, NULL }, }; -- 2.30.2