Skip to content

Commit

Permalink
RSDK-8167 - Enable conversion between MaintenanceConfig struct and Ma…
Browse files Browse the repository at this point in the history
…intenanceConfig proto messages (#4437)

Co-authored-by: Cheuk <[email protected]>
  • Loading branch information
Kschappacher and cheukt authored Oct 11, 2024
1 parent b3864fc commit 2273b4a
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 11 deletions.
33 changes: 22 additions & 11 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,18 @@ import (

// A Config describes the configuration of a robot.
type Config struct {
Cloud *Cloud
Modules []Module
Remotes []Remote
Components []resource.Config
Processes []pexec.ProcessConfig
Services []resource.Config
Packages []PackageConfig
Network NetworkConfig
Auth AuthConfig
Debug bool
LogConfig []logging.LoggerPatternConfig
Cloud *Cloud
Modules []Module
Remotes []Remote
Components []resource.Config
Processes []pexec.ProcessConfig
Services []resource.Config
Packages []PackageConfig
Network NetworkConfig
Auth AuthConfig
Debug bool
LogConfig []logging.LoggerPatternConfig
MaintenanceConfig *MaintenanceConfig

ConfigFilePath string

Expand Down Expand Up @@ -77,6 +78,13 @@ type Config struct {
toCache []byte
}

// MaintenanceConfig specifies a sensor that the machine will check to determine if the machine should reconfigure.
// This Config is not validated during config processing but it will be validated during reconfiguration.
type MaintenanceConfig struct {
SensorName string `json:"sensor_name"`
MaintenanceAllowedKey string `json:"maintenance_allowed_key"`
}

// NOTE: This data must be maintained with what is in Config.
type configData struct {
Cloud *Cloud `json:"cloud,omitempty"`
Expand All @@ -93,6 +101,7 @@ type configData struct {
EnableWebProfile bool `json:"enable_web_profile"`
LogConfig []logging.LoggerPatternConfig `json:"log,omitempty"`
Revision string `json:"revision,omitempty"`
MaintenanceConfig *MaintenanceConfig `json:"maintenance,omitempty"`
}

// AppValidationStatus refers to the.
Expand Down Expand Up @@ -299,6 +308,7 @@ func (c *Config) UnmarshalJSON(data []byte) error {
c.EnableWebProfile = conf.EnableWebProfile
c.LogConfig = conf.LogConfig
c.Revision = conf.Revision
c.MaintenanceConfig = conf.MaintenanceConfig

return nil
}
Expand Down Expand Up @@ -330,6 +340,7 @@ func (c Config) MarshalJSON() ([]byte, error) {
EnableWebProfile: c.EnableWebProfile,
LogConfig: c.LogConfig,
Revision: c.Revision,
MaintenanceConfig: c.MaintenanceConfig,
})
}

Expand Down
20 changes: 20 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1232,3 +1232,23 @@ func TestConfigRobotRevision(t *testing.T) {

test.That(t, cfg.Revision, test.ShouldEqual, "rev1")
}

func TestConfigMarshalUnMarshal(t *testing.T) {
c := config.Config{
MaintenanceConfig: &config.MaintenanceConfig{
SensorName: "SensorName",
MaintenanceAllowedKey: "Key",
},
}
expectedVal := config.Config{
MaintenanceConfig: &config.MaintenanceConfig{
SensorName: "SensorName",
MaintenanceAllowedKey: "Key",
},
}
val, err := c.MarshalJSON()
test.That(t, err, test.ShouldBeNil)

c.UnmarshalJSON(val)
test.That(t, c, test.ShouldResemble, expectedVal)
}
34 changes: 34 additions & 0 deletions config/proto_conversions.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"google.golang.org/protobuf/types/known/durationpb"

"go.viam.com/rdk/logging"
protoRdkUtils "go.viam.com/rdk/protoutils"
"go.viam.com/rdk/referenceframe"
"go.viam.com/rdk/resource"
spatial "go.viam.com/rdk/spatialmath"
Expand Down Expand Up @@ -96,6 +97,14 @@ func FromProto(proto *pb.RobotConfig, logger logging.Logger) (*Config, error) {

logAnyFragmentOverwriteErrors(logger, proto.OverwriteFragmentStatus)

if proto.Maintenance != nil {
maintenanceConfig, err := MaintenanceConfigFromProto(proto.Maintenance)
if err != nil {
return nil, errors.Wrap(err, "error converting maintenance config from proto")
}
cfg.MaintenanceConfig = maintenanceConfig
}

return &cfg, nil
}

Expand Down Expand Up @@ -633,6 +642,22 @@ func NetworkConfigToProto(network *NetworkConfig) (*pb.NetworkConfig, error) {
return &proto, nil
}

// MaintenanceConfigToProto converts MaintenanceConfig from the proto equivalent.
func MaintenanceConfigToProto(maintenanceConfig *MaintenanceConfig) (*pb.MaintenanceConfig, error) {
// convert from string to resource name
name, err := resource.NewFromString(maintenanceConfig.SensorName)
if err != nil {
return nil, err
}

proto := pb.MaintenanceConfig{
SensorName: protoRdkUtils.ResourceNameToProto(name),
MaintenanceAllowedKey: maintenanceConfig.MaintenanceAllowedKey,
}

return &proto, nil
}

// NetworkConfigFromProto creates NetworkConfig from the proto equivalent.
func NetworkConfigFromProto(proto *pb.NetworkConfig) (*NetworkConfig, error) {
network := NetworkConfig{
Expand All @@ -648,6 +673,15 @@ func NetworkConfigFromProto(proto *pb.NetworkConfig) (*NetworkConfig, error) {
return &network, nil
}

// MaintenanceConfigFromProto creates a MaintenanceConfig from the proto equivalent.
func MaintenanceConfigFromProto(proto *pb.MaintenanceConfig) (*MaintenanceConfig, error) {
MaintenanceConfig := MaintenanceConfig{
SensorName: protoRdkUtils.ResourceNameFromProto(proto.SensorName).String(),
MaintenanceAllowedKey: proto.GetMaintenanceAllowedKey(),
}
return &MaintenanceConfig, nil
}

// AuthConfigToProto converts AuthConfig to the proto equivalent.
func AuthConfigToProto(auth *AuthConfig) (*pb.AuthConfig, error) {
handlers, err := mapSliceWithErrors(auth.Handlers, authHandlerConfigToProto)
Expand Down
38 changes: 38 additions & 0 deletions config/proto_conversions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1005,3 +1005,41 @@ func TestPackageTypeConversion(t *testing.T) {
test.That(t, fmt.Sprint(err), test.ShouldContainSubstring, "invalid-package-type")
test.That(t, converted, test.ShouldResemble, packagespb.PackageType_PACKAGE_TYPE_UNSPECIFIED.Enum())
}

func TestMaintenanceConfigToProtoSuccess(t *testing.T) {
testMaintenanceConfig := MaintenanceConfig{
SensorName: "rdk:component:sensor/car",
MaintenanceAllowedKey: "honk",
}

proto, err := MaintenanceConfigToProto(&testMaintenanceConfig)
test.That(t, err, test.ShouldBeNil)
out, err := MaintenanceConfigFromProto(proto)
test.That(t, err, test.ShouldBeNil)

test.That(t, *out, test.ShouldResemble, testMaintenanceConfig)
}

func TestMaintenanceConfigToProtoRemoteSuccess(t *testing.T) {
testMaintenanceConfig := MaintenanceConfig{
SensorName: "rdk:component:sensor/go:store",
MaintenanceAllowedKey: "fast",
}

proto, err := MaintenanceConfigToProto(&testMaintenanceConfig)
test.That(t, err, test.ShouldBeNil)
out, err := MaintenanceConfigFromProto(proto)
test.That(t, err, test.ShouldBeNil)

test.That(t, *out, test.ShouldResemble, testMaintenanceConfig)
}

func TestMaintenanceConfigToProtoFailure(t *testing.T) {
testMaintenanceConfig := MaintenanceConfig{
SensorName: "car:go",
MaintenanceAllowedKey: "slow",
}

_, err := MaintenanceConfigToProto(&testMaintenanceConfig)
test.That(t, err.Error(), test.ShouldEqual, "string \"car:go\" is not a fully qualified resource name")
}

0 comments on commit 2273b4a

Please sign in to comment.