SEBA-804 Add filter options for regex and contains
Change-Id: Ifa3d29d7db7b07f00a0c81e01602088752eac884
diff --git a/README.md b/README.md
index 90cdd06..b81ccae 100644
--- a/README.md
+++ b/README.md
@@ -94,7 +94,7 @@
cordctl model list Slice --filter "id>10, controller_kind=Deployment"
```
-Supported operators in the filters include `=`, `!=`, `>`, `<`, `>=`, `<=`.
+Supported operators in the filters include `=`, `!=`, `>`, `<`, `>=`, `<=`, `~=`, and `[=`.
The core also permits models to be filtered based on state, and the `--state` argument can be used to filter based on a state. States include `all`, `dirty`, `deleted`, `dirtypol`, and `deletedpol`. `default` is a synonym for `all`. For example,
@@ -103,6 +103,16 @@
cordctl model list ONOSApp --state deleted
```
+The `~=` operator is a regular expression search, and the `[=` operator is a substring search. For example,
+
+```bash
+# List slices that match the regular expression .*-operator
+./cordctl model list Slice --filter "name~=.*-operator"
+
+# List slices that contain the substring etcd
+./cordctl model list Slice --filter name[=etcd
+```
+
### Updating models
The `model update` command is a flexible way to update one or more models. The most basic syntax uses one or more model IDs. For example,
diff --git a/internal/pkg/commands/orm.go b/internal/pkg/commands/orm.go
index d09f15d..eb7d64f 100644
--- a/internal/pkg/commands/orm.go
+++ b/internal/pkg/commands/orm.go
@@ -66,6 +66,10 @@
return strings.TrimSpace(query[2:]), "EQUAL", true, nil
} else if strings.HasPrefix(query, "==") {
return "", "", false, corderrors.NewInvalidInputError("Operator == is now allowed. Suggest using = instead.")
+ } else if strings.HasPrefix(query, "~=") {
+ return strings.TrimSpace(query[2:]), "REGEX", false, nil
+ } else if strings.HasPrefix(query, "[=") {
+ return strings.TrimSpace(query[2:]), "CONTAINS", false, nil
} else if strings.HasPrefix(query, "=") {
return strings.TrimSpace(query[1:]), "EQUAL", false, nil
} else if strings.HasPrefix(query, ">=") {
@@ -156,7 +160,7 @@
operator_pos := -1
for i, ch := range query_str {
if allow_inequality {
- if (ch == '!') || (ch == '=') || (ch == '>') || (ch == '<') {
+ if (ch == '!') || (ch == '=') || (ch == '>') || (ch == '<') || (ch == '~') || (ch == '[') {
operator_pos = i
break
}