Browse Source

spr_load can perform mass deletion

Serj Kalichev 3 months ago
parent
commit
98faaa691a
3 changed files with 78 additions and 20 deletions
  1. 20 8
      bin/srp_load.c
  2. 5 0
      src/klish_plugin_sysrepo.h
  3. 53 12
      src/syms.c

+ 20 - 8
bin/srp_load.c

@@ -20,12 +20,14 @@
 
 #define DEFAULT_USER "root"
 #define DEFAULT_DATASTORE SR_DS_CANDIDATE
+#define DEFAULT_OP 's'
 
 typedef struct cmd_opts_s {
 	char *cfg; // Configuration options
 	char *file; // File to load
 	char *user; // NACM user
 	char *current_path; // Current sysrepo path
+	char op; // Operation to execute 's'(set)/'d'(del)
 	bool_t verbose;
 	bool_t stop_on_error;
 	sr_datastore_t datastore;
@@ -78,7 +80,7 @@ int main(int argc, char **argv)
 		faux_argv_parse(cur_path, cmd_opts->current_path);
 	}
 
-	ret = srp_mass_set(fd, cmd_opts->datastore, cur_path,
+	ret = srp_mass_op(cmd_opts->op, fd, cmd_opts->datastore, cur_path,
 		&opts, cmd_opts->user, cmd_opts->stop_on_error);
 
 out:
@@ -104,7 +106,8 @@ static cmd_opts_t *cmd_opts_init(void)
 	opts->stop_on_error = BOOL_FALSE;
 	opts->cfg = NULL;
 	opts->file = NULL;
-	opts->user = NULL;
+	opts->user = faux_str_dup(DEFAULT_USER);
+	opts->op = DEFAULT_OP;
 	opts->datastore = DEFAULT_DATASTORE;
 	opts->current_path = NULL;
 
@@ -127,7 +130,7 @@ static void cmd_opts_free(cmd_opts_t *opts)
 
 static int cmd_opts_parse(int argc, char *argv[], cmd_opts_t *opts)
 {
-	static const char *shortopts = "hf:veu:d:p:";
+	static const char *shortopts = "hf:veu:d:p:o:";
 	static const struct option longopts[] = {
 		{"conf",		1, NULL, 'f'},
 		{"help",		0, NULL, 'h'},
@@ -136,6 +139,7 @@ static int cmd_opts_parse(int argc, char *argv[], cmd_opts_t *opts)
 		{"stop-on-error",	0, NULL, 'e'},
 		{"datastore",		1, NULL, 'd'},
 		{"current-path",	1, NULL, 'p'},
+		{"operation",		1, NULL, 'o'},
 		{NULL,			0, NULL, 0}
 	};
 
@@ -165,6 +169,15 @@ static int cmd_opts_parse(int argc, char *argv[], cmd_opts_t *opts)
 			faux_str_free(opts->cfg);
 			opts->cfg = faux_str_dup(optarg);
 			break;
+		case 'o':
+			opts->op = optarg[0];
+			if ((opts->op != 's') && (opts->op != 'd')) {
+				fprintf(stderr, "Error: Illegal operation '%c'\n",
+					opts->op);
+				help(-1, argv[0]);
+				_exit(-1);
+			}
+			break;
 		case 'd':
 			if (!kly_str2ds(optarg, strlen(optarg), &opts->datastore))
 				return BOOL_FALSE;
@@ -186,10 +199,6 @@ static int cmd_opts_parse(int argc, char *argv[], cmd_opts_t *opts)
 		opts->file = faux_str_dup(argv[optind]);
 	}
 
-	// Validate options
-	if (!opts->user)
-		opts->user = faux_str_dup(DEFAULT_USER);
-
 	return 0;
 }
 
@@ -221,7 +230,10 @@ static void help(int status, const char *argv0)
 		printf("\t-e, --stop-on-error Stop script execution on error\n");
 		printf("\t-u <name>, --user=<name> NACM user name\n");
 		printf("\t-f <path>, --conf=<path> Config file with parsing settings\n");
-		printf("\t-d <ds>, --datastore=<ds> Datastore (Default is 'candidate'\n");
+		printf("\t-o <op>, --operation=<op> Operation to perform\n");
+		printf("\t\t's' Set (default)\n");
+		printf("\t\t'd' Delete\n");
+		printf("\t-d <ds>, --datastore=<ds> Datastore (Default is 'candidate')\n");
 		printf("\t-p <sr-path>, --current-path=<sr-path> Current sysrepo path\n");
 	}
 }

+ 5 - 0
src/klish_plugin_sysrepo.h

@@ -64,6 +64,7 @@ typedef enum {
 	PT_DEL =
 		PAT_CONTAINER |
 		PAT_LIST_KEY |
+		PAT_LIST_KEY_INCOMPLETED |
 		PAT_LEAF |
 		PAT_LEAF_EMPTY |
 		PAT_LEAFLIST |
@@ -290,6 +291,10 @@ int srp_deactivate(kcontext_t *context);
 // Service functions
 int srp_mass_set(int fd, sr_datastore_t ds, const faux_argv_t *cur_path,
 	const pline_opts_t *opts, const char *user, bool_t stop_on_error);
+int srp_mass_del(int fd, sr_datastore_t ds, const faux_argv_t *cur_path,
+	const pline_opts_t *opts, const char *user, bool_t stop_on_error);
+int srp_mass_op(char op, int fd, sr_datastore_t ds, const faux_argv_t *cur_path,
+	const pline_opts_t *opts, const char *user, bool_t stop_on_error);
 
 // Plugin's user-data service functions
 pline_opts_t *srp_udata_opts(kcontext_t *context);

+ 53 - 12
src/syms.c

@@ -1220,10 +1220,8 @@ int srp_compl_xpath(kcontext_t *context)
 }
 
 
-// Function for mass config strings load. It can load stream of KPath strings
-// (without "set" command, only path and value). Function doesn't use
-// pre-connected session because it can be executed within FILTER or utility.
-int srp_mass_set(int fd, sr_datastore_t ds, const faux_argv_t *cur_path,
+// Function for mass operations.
+int srp_mass_op(char op, int fd, sr_datastore_t ds, const faux_argv_t *cur_path,
 	const pline_opts_t *opts, const char *user, bool_t stop_on_error)
 {
 	int ret = -1;
@@ -1290,15 +1288,38 @@ int srp_mass_set(int fd, sr_datastore_t ds, const faux_argv_t *cur_path,
 
 			iter = faux_list_head(pline->exprs);
 			while ((expr = (pexpr_t *)faux_list_each(&iter))) {
-				if (!(expr->pat & PT_SET)) {
+				// Set
+				if (op == 's') {
+					if (!(expr->pat & PT_SET)) {
+						err_num++;
+						fprintf(stderr, "Error: Illegal expression"
+							" for set operation\n");
+						break;
+					}
+					if (sr_set_item_str(sess, expr->xpath,
+						expr->value, NULL, 0) != SR_ERR_OK) {
+						err_num++;
+						fprintf(stderr, "Error: Can't set data\n");
+						break;
+					}
+				// Del
+				} else if (op == 'd') {
+					if (!(expr->pat & PT_DEL)) {
+						err_num++;
+						fprintf(stderr, "Error: Illegal expression"
+							" for del operation\n");
+						break;
+					}
+					if (sr_delete_item(sess, expr->xpath, 0) !=
+						SR_ERR_OK) {
+						err_num++;
+						fprintf(stderr, "Error: Can't del data\n");
+						break;
+					}
+				} else {
 					err_num++;
-					fprintf(stderr, "Error: Illegal expression for set operation\n");
-					break;
-				}
-				if (sr_set_item_str(sess, expr->xpath, expr->value, NULL, 0) !=
-					SR_ERR_OK) {
-					err_num++;
-					fprintf(stderr, "Error: Can't set data\n");
+					fprintf(stderr, "Error: Illegal operation '%c'\n",
+						op);
 					break;
 				}
 			}
@@ -1330,3 +1351,23 @@ out:
 
 	return ret;
 }
+
+
+// Function for mass config strings load. It can load stream of KPath strings
+// (without "set" command, only path and value). Function doesn't use
+// pre-connected session because it can be executed within FILTER or utility.
+int srp_mass_set(int fd, sr_datastore_t ds, const faux_argv_t *cur_path,
+	const pline_opts_t *opts, const char *user, bool_t stop_on_error)
+{
+	return srp_mass_op('s', fd, ds, cur_path, opts, user, stop_on_error);
+}
+
+
+// Function for mass config strings deletion. It can get stream of KPath strings
+// (without "del" command, only path). Function doesn't use
+// pre-connected session because it can be executed within FILTER or utility.
+int srp_mass_del(int fd, sr_datastore_t ds, const faux_argv_t *cur_path,
+	const pline_opts_t *opts, const char *user, bool_t stop_on_error)
+{
+	return srp_mass_op('d', fd, ds, cur_path, opts, user, stop_on_error);
+}