-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[RSDK-9655] skip reflection for discovery service and add fake discov…
…ery (#4697)
- Loading branch information
Showing
7 changed files
with
246 additions
and
33 deletions.
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
examples/customresources/models/mydiscovery/mydiscovery.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Package mydiscovery implements a discovery that returns some fake components. | ||
package mydiscovery | ||
|
||
import ( | ||
"context" | ||
|
||
"go.viam.com/rdk/components/camera" | ||
"go.viam.com/rdk/components/movementsensor" | ||
"go.viam.com/rdk/logging" | ||
"go.viam.com/rdk/resource" | ||
"go.viam.com/rdk/services/discovery" | ||
) | ||
|
||
// Model is the full model definition. | ||
var Model = resource.NewModel("acme", "demo", "mydiscovery") | ||
|
||
func init() { | ||
resource.RegisterService( | ||
discovery.API, | ||
Model, | ||
resource.Registration[discovery.Service, resource.NoNativeConfig]{Constructor: func( | ||
ctx context.Context, | ||
deps resource.Dependencies, | ||
conf resource.Config, | ||
logger logging.Logger, | ||
) (discovery.Service, error) { | ||
return newDiscovery(conf.ResourceName(), logger), nil | ||
}}) | ||
} | ||
|
||
func newDiscovery(name resource.Name, logger logging.Logger) discovery.Service { | ||
cfg1 := createFakeConfig("fake1", movementsensor.API) | ||
cfg2 := createFakeConfig("fake2", camera.API) | ||
return &Discovery{Named: name.AsNamed(), logger: logger, cfgs: []resource.Config{cfg1, cfg2}} | ||
} | ||
|
||
// DiscoverResources returns the discovered resources. | ||
func (dis *Discovery) DiscoverResources(context.Context, map[string]any) ([]resource.Config, error) { | ||
return dis.cfgs, nil | ||
} | ||
|
||
// Discovery is a fake Discovery service. | ||
type Discovery struct { | ||
resource.Named | ||
resource.TriviallyReconfigurable | ||
resource.TriviallyCloseable | ||
logger logging.Logger | ||
cfgs []resource.Config | ||
} | ||
|
||
// DoCommand echos input back to the caller. | ||
func (dis *Discovery) DoCommand(ctx context.Context, cmd map[string]interface{}) (map[string]interface{}, error) { | ||
return cmd, nil | ||
} | ||
|
||
// createFakeConfig creates a fake component with the defined name, api, and attributes. | ||
func createFakeConfig(name string, api resource.API) resource.Config { | ||
return resource.Config{Name: name, API: api, Model: resource.DefaultModelFamily.WithModel("fake")} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// Package fake implements a fake discovery service. | ||
package fake | ||
|
||
import ( | ||
"context" | ||
|
||
"go.viam.com/rdk/components/camera" | ||
"go.viam.com/rdk/components/movementsensor" | ||
"go.viam.com/rdk/logging" | ||
"go.viam.com/rdk/resource" | ||
"go.viam.com/rdk/services/discovery" | ||
"go.viam.com/rdk/utils" | ||
) | ||
|
||
func init() { | ||
resource.RegisterService( | ||
discovery.API, | ||
resource.DefaultModelFamily.WithModel("fake"), | ||
resource.Registration[discovery.Service, resource.NoNativeConfig]{Constructor: func( | ||
ctx context.Context, | ||
deps resource.Dependencies, | ||
conf resource.Config, | ||
logger logging.Logger, | ||
) (discovery.Service, error) { | ||
return newDiscovery(conf.ResourceName(), logger), nil | ||
}}) | ||
} | ||
|
||
func newDiscovery(name resource.Name, logger logging.Logger) discovery.Service { | ||
cfg1 := createFakeConfig("fake1", movementsensor.API, nil) | ||
cfg2 := createFakeConfig("fake2", camera.API, nil) | ||
return &Discovery{Named: name.AsNamed(), logger: logger, cfgs: []resource.Config{cfg1, cfg2}} | ||
} | ||
|
||
// DiscoverResources returns the discovered resources. | ||
func (dis *Discovery) DiscoverResources(context.Context, map[string]any) ([]resource.Config, error) { | ||
return dis.cfgs, nil | ||
} | ||
|
||
// Discovery is a fake Discovery service that returns. | ||
type Discovery struct { | ||
resource.Named | ||
resource.TriviallyReconfigurable | ||
resource.TriviallyCloseable | ||
logger logging.Logger | ||
cfgs []resource.Config | ||
} | ||
|
||
// DoCommand echos input back to the caller. | ||
func (dis *Discovery) DoCommand(ctx context.Context, cmd map[string]interface{}) (map[string]interface{}, error) { | ||
return cmd, nil | ||
} | ||
|
||
// createFakeConfig creates a fake component with the defined name, api, and attributes. | ||
// additionally the commented code is an example of how to take a model's Config and convert it into Attributes for the api. | ||
// | ||
//nolint:unparam | ||
func createFakeConfig(name string, api resource.API, attributes utils.AttributeMap) resource.Config { | ||
// // using the camera's Config struct in case a breaking change occurs | ||
// attributes := viamrtsp.Config{Address: address} | ||
// var result map[string]interface{} | ||
|
||
// // marshal to bytes | ||
// jsonBytes, err := json.Marshal(attributes) | ||
// if err != nil { | ||
// return resource.Config{}, err | ||
// } | ||
|
||
// // convert to map to be used as attributes in resource.Config | ||
// err = json.Unmarshal(jsonBytes, &result) | ||
// if err != nil { | ||
// return resource.Config{}, err | ||
// } | ||
return resource.Config{Name: name, API: api, Model: resource.DefaultModelFamily.WithModel("fake"), Attributes: attributes} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package fake | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"go.viam.com/test" | ||
|
||
"go.viam.com/rdk/components/camera" | ||
"go.viam.com/rdk/components/movementsensor" | ||
"go.viam.com/rdk/logging" | ||
"go.viam.com/rdk/resource" | ||
"go.viam.com/rdk/services/discovery" | ||
) | ||
|
||
func TestDiscoverResources(t *testing.T) { | ||
ctx := context.Background() | ||
logger := logging.NewTestLogger(t) | ||
|
||
dis := newDiscovery(discovery.Named("foo"), logger) | ||
|
||
expectedCfgs := []resource.Config{ | ||
createFakeConfig("fake1", movementsensor.API, nil), | ||
createFakeConfig("fake2", camera.API, nil), | ||
} | ||
cfgs, err := dis.DiscoverResources(ctx, nil) | ||
test.That(t, err, test.ShouldBeNil) | ||
test.That(t, len(cfgs), test.ShouldEqual, len(expectedCfgs)) | ||
for index, cfg := range cfgs { | ||
test.That(t, cfg.Name, test.ShouldEqual, expectedCfgs[index].Name) | ||
test.That(t, cfg.API, test.ShouldResemble, expectedCfgs[index].API) | ||
test.That(t, cfg.Model, test.ShouldResemble, expectedCfgs[index].Model) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// Package register registers all relevant discovery models and also API specific functions | ||
package register | ||
|
||
import ( | ||
// for discovery models. | ||
_ "go.viam.com/rdk/services/discovery/fake" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters