Skip to content

Commit

Permalink
RSDK-7122 Add machine id to cloud metadata (#3756)
Browse files Browse the repository at this point in the history
  • Loading branch information
maximpertsov authored Apr 4, 2024
1 parent 0b91ba7 commit 170dc05
Show file tree
Hide file tree
Showing 16 changed files with 62 additions and 37 deletions.
10 changes: 10 additions & 0 deletions cloud/cloud.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Package cloud contains app-related functionality.
package cloud

// Metadata contains app-related information about the robot.
type Metadata struct {
PrimaryOrgID string
LocationID string
MachineID string
MachinePartID string
}
4 changes: 4 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,7 @@ type Cloud struct {
LocationSecrets []LocationSecret
LocationID string
PrimaryOrgID string
MachineID string
ManagedBy string
FQDN string
LocalFQDN string
Expand All @@ -518,6 +519,7 @@ type cloudData struct {
LocationSecrets []LocationSecret `json:"location_secrets"`
LocationID string `json:"location_id"`
PrimaryOrgID string `json:"primary_org_id"`
MachineID string `json:"machine_id"`
ManagedBy string `json:"managed_by"`
FQDN string `json:"fqdn"`
LocalFQDN string `json:"local_fqdn"`
Expand Down Expand Up @@ -545,6 +547,7 @@ func (config *Cloud) UnmarshalJSON(data []byte) error {
LocationSecrets: temp.LocationSecrets,
LocationID: temp.LocationID,
PrimaryOrgID: temp.PrimaryOrgID,
MachineID: temp.MachineID,
ManagedBy: temp.ManagedBy,
FQDN: temp.FQDN,
LocalFQDN: temp.LocalFQDN,
Expand Down Expand Up @@ -575,6 +578,7 @@ func (config Cloud) MarshalJSON() ([]byte, error) {
LocationSecrets: config.LocationSecrets,
LocationID: config.LocationID,
PrimaryOrgID: config.PrimaryOrgID,
MachineID: config.MachineID,
ManagedBy: config.ManagedBy,
FQDN: config.FQDN,
LocalFQDN: config.LocalFQDN,
Expand Down
2 changes: 2 additions & 0 deletions config/proto_conversions.go
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,7 @@ func CloudConfigToProto(cloud *Cloud) (*pb.CloudConfig, error) {
SignalingInsecure: cloud.SignalingInsecure,
PrimaryOrgId: cloud.PrimaryOrgID,
LocationId: cloud.LocationID,
MachineId: cloud.MachineID,
}, nil
}

Expand All @@ -696,6 +697,7 @@ func CloudConfigFromProto(proto *pb.CloudConfig) (*Cloud, error) {
LocationSecrets: locationSecrets,
LocationID: proto.GetLocationId(),
PrimaryOrgID: proto.GetPrimaryOrgId(),
MachineID: proto.GetMachineId(),
ManagedBy: proto.GetManagedBy(),
FQDN: proto.GetFqdn(),
LocalFQDN: proto.GetLocalFqdn(),
Expand Down
2 changes: 2 additions & 0 deletions config/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ func readFromCloud(
locationSecrets := cfg.Cloud.LocationSecrets
primaryOrgID := cfg.Cloud.PrimaryOrgID
locationID := cfg.Cloud.LocationID
machineID := cfg.Cloud.MachineID

mergeCloudConfig := func(to *Config) {
*to.Cloud = *cloudCfg
Expand All @@ -308,6 +309,7 @@ func readFromCloud(
to.Cloud.TLSPrivateKey = tls.privateKey
to.Cloud.PrimaryOrgID = primaryOrgID
to.Cloud.LocationID = locationID
to.Cloud.MachineID = machineID
}

mergeCloudConfig(cfg)
Expand Down
4 changes: 4 additions & 0 deletions config/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func TestFromReader(t *testing.T) {
LocationSecrets: []LocationSecret{},
LocationID: "the-location",
PrimaryOrgID: "the-primary-org",
MachineID: "the-machine",
}
certProto := &pb.CertificateResponse{
TlsCertificate: "cert",
Expand Down Expand Up @@ -94,6 +95,7 @@ func TestFromReader(t *testing.T) {
TLSPrivateKey: "key",
LocationID: "the-location",
PrimaryOrgID: "the-primary-org",
MachineID: "the-machine",
}
cachedConf := &Config{Cloud: cachedCloud}
err := storeToCache(robotPartID, cachedConf)
Expand Down Expand Up @@ -138,6 +140,7 @@ func TestStoreToCache(t *testing.T) {
AppAddress: "https://app.viam.dev:443",
LocationID: "the-location",
PrimaryOrgID: "the-primary-org",
MachineID: "the-machine",
}
cfg.Cloud = cloud

Expand Down Expand Up @@ -199,6 +202,7 @@ func TestShouldCheckForCert(t *testing.T) {
TLSPrivateKey: "key",
LocationID: "the-location",
PrimaryOrgID: "the-primary-org",
MachineID: "the-machine",
LocationSecrets: []LocationSecret{
{ID: "id1", Secret: "secret1"},
{ID: "id2", Secret: "secret2"},
Expand Down
1 change: 1 addition & 0 deletions config/watcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ func TestNewWatcherCloud(t *testing.T) {
LocationSecrets: []config.LocationSecret{{ID: "1", Secret: "secret"}},
PrimaryOrgID: "the-primary-org",
LocationID: "the-location",
MachineID: "the-machine",
}
}

Expand Down
7 changes: 0 additions & 7 deletions internal/cloud/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,3 @@ func (cm *cloudManagedService) Close(ctx context.Context) error {
// ErrNotCloudManaged is returned if a connection is requested but the robot is not
// yet cloud managed.
var ErrNotCloudManaged = errors.New("this robot is not cloud managed")

// Metadata contains app-related information about the robot.
type Metadata struct {
RobotPartID string
PrimaryOrgID string
LocationID string
}
5 changes: 3 additions & 2 deletions robot/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ import (
"google.golang.org/protobuf/types/known/structpb"
"google.golang.org/protobuf/types/known/timestamppb"

"go.viam.com/rdk/cloud"
"go.viam.com/rdk/grpc"
"go.viam.com/rdk/internal/cloud"
"go.viam.com/rdk/logging"
"go.viam.com/rdk/operation"
"go.viam.com/rdk/pointcloud"
Expand Down Expand Up @@ -954,8 +954,9 @@ func (rc *RobotClient) CloudMetadata(ctx context.Context) (cloud.Metadata, error
if err != nil {
return cloudMD, err
}
cloudMD.RobotPartID = resp.RobotPartId
cloudMD.PrimaryOrgID = resp.PrimaryOrgId
cloudMD.LocationID = resp.LocationId
cloudMD.MachineID = resp.MachineId
cloudMD.MachinePartID = resp.MachinePartId
return cloudMD, nil
}
9 changes: 5 additions & 4 deletions robot/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import (
"google.golang.org/grpc/reflection"
"google.golang.org/grpc/status"

"go.viam.com/rdk/cloud"
"go.viam.com/rdk/components/arm"
"go.viam.com/rdk/components/base"
"go.viam.com/rdk/components/board"
Expand All @@ -56,7 +57,6 @@ import (
"go.viam.com/rdk/config"
"go.viam.com/rdk/gostream"
rgrpc "go.viam.com/rdk/grpc"
"go.viam.com/rdk/internal/cloud"
"go.viam.com/rdk/logging"
"go.viam.com/rdk/operation"
"go.viam.com/rdk/referenceframe"
Expand Down Expand Up @@ -2032,9 +2032,10 @@ func TestCloudMetadata(t *testing.T) {
gServer := grpc.NewServer()

injectCloudMD := cloud.Metadata{
RobotPartID: "the-robot-part",
LocationID: "the-location",
PrimaryOrgID: "the-primary-org",
LocationID: "the-location",
PrimaryOrgID: "the-primary-org",
MachineID: "the-machine",
MachinePartID: "the-robot-part",
}
injectRobot := &inject.Robot{
ResourceNamesFunc: func() []resource.Name { return nil },
Expand Down
19 changes: 9 additions & 10 deletions robot/impl/local_robot.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ import (
"go.viam.com/utils/pexec"
"go.viam.com/utils/rpc"

"go.viam.com/rdk/cloud"
"go.viam.com/rdk/config"
"go.viam.com/rdk/internal/cloud"
icloud "go.viam.com/rdk/internal/cloud"
"go.viam.com/rdk/logging"
"go.viam.com/rdk/operation"
"go.viam.com/rdk/pointcloud"
Expand Down Expand Up @@ -49,7 +50,7 @@ type localRobot struct {
operations *operation.Manager
sessionManager session.Manager
packageManager packages.ManagerSyncer
cloudConnSvc cloud.ConnectionService
cloudConnSvc icloud.ConnectionService
logger logging.Logger
activeBackgroundWorkers sync.WaitGroup
reconfigureWorkers sync.WaitGroup
Expand Down Expand Up @@ -104,11 +105,8 @@ func (r *localRobot) ResourceNames() []resource.Name {
}
rNames := make([]resource.Name, 0, len(names))
for _, n := range names {
n.MachinePartID = md.RobotPartID
rNames = append(
rNames,
n,
)
n.MachinePartID = md.MachinePartID
rNames = append(rNames, n)
}
return rNames
}
Expand Down Expand Up @@ -397,7 +395,7 @@ func newWithResources(
triggerConfig: make(chan struct{}),
configTicker: nil,
revealSensitiveConfigDiffs: rOpts.revealSensitiveConfigDiffs,
cloudConnSvc: cloud.NewCloudConnectionService(cfg.Cloud, logger),
cloudConnSvc: icloud.NewCloudConnectionService(cfg.Cloud, logger),
}
r.mostRecentCfg.Store(config.Config{})
var heartbeatWindow time.Duration
Expand Down Expand Up @@ -732,7 +730,7 @@ func (r *localRobot) updateWeakDependents(ctx context.Context) {
if err := res.Reconfigure(ctxWithTimeout, components, resource.Config{ConvertedAttributes: fsCfg}); err != nil {
r.Logger().CErrorw(ctx, "failed to reconfigure internal service during weak dependencies update", "service", resName, "error", err)
}
case packages.InternalServiceName, packages.DeferredServiceName, cloud.InternalServiceName:
case packages.InternalServiceName, packages.DeferredServiceName, icloud.InternalServiceName:
default:
r.logger.CWarnw(ctx, "do not know how to reconfigure internal service during weak dependencies update", "service", resName)
}
Expand Down Expand Up @@ -1202,8 +1200,9 @@ func (r *localRobot) CloudMetadata(ctx context.Context) (cloud.Metadata, error)
if cloud == nil {
return md, errors.New("cloud metadata not available")
}
md.RobotPartID = cloud.ID
md.PrimaryOrgID = cloud.PrimaryOrgID
md.LocationID = cloud.LocationID
md.MachineID = cloud.MachineID
md.MachinePartID = cloud.ID
return md, nil
}
10 changes: 6 additions & 4 deletions robot/impl/local_robot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

"go.viam.com/rdk/cloud"
"go.viam.com/rdk/components/arm"
"go.viam.com/rdk/components/arm/fake"
"go.viam.com/rdk/components/audioinput"
Expand All @@ -49,7 +50,6 @@ import (
"go.viam.com/rdk/examples/customresources/apis/gizmoapi"
"go.viam.com/rdk/examples/customresources/apis/summationapi"
rgrpc "go.viam.com/rdk/grpc"
"go.viam.com/rdk/internal/cloud"
"go.viam.com/rdk/logging"
"go.viam.com/rdk/referenceframe"
"go.viam.com/rdk/resource"
Expand Down Expand Up @@ -3421,16 +3421,18 @@ func TestCloudMetadata(t *testing.T) {
ID: "the-robot-part",
LocationID: "the-location",
PrimaryOrgID: "the-primary-org",
MachineID: "the-machine",
},
}
robot, shutdown := initTestRobot(t, ctx, cfg, logger)
defer shutdown()
md, err := robot.CloudMetadata(ctx)
test.That(t, err, test.ShouldBeNil)
test.That(t, md, test.ShouldResemble, cloud.Metadata{
RobotPartID: "the-robot-part",
PrimaryOrgID: "the-primary-org",
LocationID: "the-location",
PrimaryOrgID: "the-primary-org",
LocationID: "the-location",
MachineID: "the-machine",
MachinePartID: "the-robot-part",
})
})
}
Expand Down
2 changes: 1 addition & 1 deletion robot/impl/resource_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"go.viam.com/utils/testutils"
"google.golang.org/protobuf/testing/protocmp"

"go.viam.com/rdk/cloud"
"go.viam.com/rdk/components/arm"
fakearm "go.viam.com/rdk/components/arm/fake"
"go.viam.com/rdk/components/base"
Expand All @@ -45,7 +46,6 @@ import (
fakeservo "go.viam.com/rdk/components/servo/fake"
"go.viam.com/rdk/config"
"go.viam.com/rdk/grpc"
"go.viam.com/rdk/internal/cloud"
"go.viam.com/rdk/logging"
"go.viam.com/rdk/module/modmaninterface"
"go.viam.com/rdk/operation"
Expand Down
2 changes: 1 addition & 1 deletion robot/robot.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import (
"github.com/pkg/errors"
"go.viam.com/utils/pexec"

"go.viam.com/rdk/cloud"
"go.viam.com/rdk/config"
"go.viam.com/rdk/grpc"
"go.viam.com/rdk/internal/cloud"
"go.viam.com/rdk/logging"
"go.viam.com/rdk/operation"
"go.viam.com/rdk/pointcloud"
Expand Down
9 changes: 6 additions & 3 deletions robot/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -466,8 +466,11 @@ func (s *Server) GetCloudMetadata(ctx context.Context, _ *pb.GetCloudMetadataReq
return nil, err
}
return &pb.GetCloudMetadataResponse{
RobotPartId: md.RobotPartID,
LocationId: md.LocationID,
PrimaryOrgId: md.PrimaryOrgID,
// TODO: RSDK-7181 remove RobotPartId
RobotPartId: md.MachinePartID, // Deprecated: Duplicates MachinePartId
PrimaryOrgId: md.PrimaryOrgID,
LocationId: md.LocationID,
MachineId: md.MachineID,
MachinePartId: md.MachinePartID,
}, nil
}
11 changes: 7 additions & 4 deletions robot/server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ import (
"google.golang.org/protobuf/types/known/durationpb"
"google.golang.org/protobuf/types/known/timestamppb"

"go.viam.com/rdk/cloud"
"go.viam.com/rdk/components/arm"
"go.viam.com/rdk/components/movementsensor"
"go.viam.com/rdk/internal/cloud"
"go.viam.com/rdk/logging"
"go.viam.com/rdk/operation"
"go.viam.com/rdk/protoutils"
Expand Down Expand Up @@ -97,16 +97,19 @@ func TestServer(t *testing.T) {
req := pb.GetCloudMetadataRequest{}
injectRobot.CloudMetadataFunc = func(ctx context.Context) (cloud.Metadata, error) {
return cloud.Metadata{
RobotPartID: "the-robot-part",
PrimaryOrgID: "the-primary-org",
LocationID: "the-location",
PrimaryOrgID: "the-primary-org",
LocationID: "the-location",
MachineID: "the-machine",
MachinePartID: "the-robot-part",
}, nil
}
resp, err := server.GetCloudMetadata(context.Background(), &req)
test.That(t, err, test.ShouldBeNil)
test.That(t, resp.GetRobotPartId(), test.ShouldEqual, "the-robot-part")
test.That(t, resp.GetLocationId(), test.ShouldEqual, "the-location")
test.That(t, resp.GetPrimaryOrgId(), test.ShouldEqual, "the-primary-org")
test.That(t, resp.GetMachineId(), test.ShouldEqual, "the-machine")
test.That(t, resp.GetMachinePartId(), test.ShouldEqual, "the-robot-part")
})

t.Run("Discovery", func(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion testutils/inject/robot.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"github.com/google/uuid"
"go.viam.com/utils/pexec"

"go.viam.com/rdk/cloud"
"go.viam.com/rdk/config"
"go.viam.com/rdk/internal/cloud"
"go.viam.com/rdk/logging"
"go.viam.com/rdk/operation"
"go.viam.com/rdk/pointcloud"
Expand Down

0 comments on commit 170dc05

Please sign in to comment.