Skip to content

Commit

Permalink
Support fnmatch in the resource selector
Browse files Browse the repository at this point in the history
* Add resourceid.Match(s1, s2 string)
* Add resourceid.T.Match()
* Make resource.T.MatchRID() use resourceid.T.Match()
* Use resourceid.Match() in the GET /resource* api handlers
  instead of the simple rid equality test

Example:

$ ./om3 '*' resource status ls --rid f*0
OBJECT  NODE   RID  TYPE    STATUS
drbd001 dev2n1 fs#0 fs.flag down
drbd001 dev2n2 fs#0 fs.flag down
drbd001 dev2n3 fs#0 fs.flag down
  • Loading branch information
cvaroqui committed Sep 15, 2023
1 parent ca00acb commit ebd5af1
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 17 deletions.
14 changes: 1 addition & 13 deletions core/resource/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -504,19 +504,7 @@ func (t *T) Log() *zerolog.Logger {
// pattern.
// ex: fs#1 matches fs#1
func (t T) MatchRID(s string) bool {
rid, err := resourceid.Parse(s)
if err != nil {
return false
}
if !rid.DriverGroup().IsValid() {
return false
}
if rid.Index() == "" {
// ex: fs#1 matches fs
return t.ResourceID.DriverGroup().String() == rid.DriverGroup().String()
}
// ex: fs#1 matches fs#1
return t.ResourceID.String() == s
return t.ResourceID.Match(s)

}

Expand Down
19 changes: 19 additions & 0 deletions core/resourceid/resourceid.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"strings"

"github.com/danwakefield/fnmatch"
"github.com/opensvc/om3/core/driver"
)

Expand Down Expand Up @@ -74,3 +75,21 @@ func (t *T) UnmarshalJSON(b []byte) error {
t.Name = temp
return nil
}

func Match(s1, s2 string) bool {
if rid1, err := Parse(s1); err != nil {
return false
} else {
return rid1.Match(s2)
}
}

func (t T) Match(s string) bool {
if rid, err := Parse(s); err == nil && rid.DriverGroup().IsValid() && rid.Index() == "" {
// ex: fs#1 matches fs
return t.DriverGroup().String() == rid.DriverGroup().String()
}
// ex: fs#1 matches fs#1, f*
return fnmatch.Match(s, t.Name, 0)

}
3 changes: 2 additions & 1 deletion daemon/daemonapi/get_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/rs/zerolog/log"

"github.com/opensvc/om3/core/instance"
"github.com/opensvc/om3/core/resourceid"
"github.com/opensvc/om3/daemon/api"
)

Expand All @@ -32,7 +33,7 @@ func (a *DaemonApi) GetResource(ctx echo.Context, params api.GetResourceParams)
monitor := instance.MonitorData.Get(config.Path, config.Node)
status := instance.StatusData.Get(config.Path, config.Node)
for rid, resourceConfig := range config.Value.Resources {
if params.Resource != nil && rid != *params.Resource {
if params.Resource != nil && !resourceid.Match(rid, *params.Resource) {
continue
}
d := api.ResourceItem{
Expand Down
3 changes: 2 additions & 1 deletion daemon/daemonapi/get_resource_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/rs/zerolog/log"

"github.com/opensvc/om3/core/instance"
"github.com/opensvc/om3/core/resourceid"
"github.com/opensvc/om3/daemon/api"
)

Expand All @@ -30,7 +31,7 @@ func (a *DaemonApi) GetResourceConfig(ctx echo.Context, params api.GetResourceCo
continue
}
for rid, resourceConfig := range config.Value.Resources {
if params.Resource != nil && rid != *params.Resource {
if params.Resource != nil && !resourceid.Match(rid, *params.Resource) {
continue
}
d := api.ResourceConfigItem{
Expand Down
3 changes: 2 additions & 1 deletion daemon/daemonapi/get_resource_monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/rs/zerolog/log"

"github.com/opensvc/om3/core/instance"
"github.com/opensvc/om3/core/resourceid"
"github.com/opensvc/om3/daemon/api"
)

Expand All @@ -30,7 +31,7 @@ func (a *DaemonApi) GetResourceMonitor(ctx echo.Context, params api.GetResourceM
continue
}
for rid, resourceMonitor := range monitor.Value.Resources {
if params.Resource != nil && rid != *params.Resource {
if params.Resource != nil && !resourceid.Match(rid, *params.Resource) {
continue
}
d := api.ResourceMonitorItem{
Expand Down
3 changes: 2 additions & 1 deletion daemon/daemonapi/get_resource_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/rs/zerolog/log"

"github.com/opensvc/om3/core/instance"
"github.com/opensvc/om3/core/resourceid"
"github.com/opensvc/om3/daemon/api"
)

Expand All @@ -30,7 +31,7 @@ func (a *DaemonApi) GetResourceStatus(ctx echo.Context, params api.GetResourceSt
continue
}
for rid, resourceStatus := range status.Value.Resources {
if params.Resource != nil && rid != *params.Resource {
if params.Resource != nil && !resourceid.Match(rid, *params.Resource) {
continue
}
d := api.ResourceStatusItem{
Expand Down

0 comments on commit ebd5af1

Please sign in to comment.