From d1015d13e4b12364598c2044724efb962d3a635d Mon Sep 17 00:00:00 2001 From: purplenicole730 Date: Thu, 21 Nov 2024 10:28:18 -0500 Subject: [PATCH 1/7] add provisioning wrappers --- app/provisioning_client.go | 123 +++++++++++++++++++++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100644 app/provisioning_client.go diff --git a/app/provisioning_client.go b/app/provisioning_client.go new file mode 100644 index 00000000000..820c649f912 --- /dev/null +++ b/app/provisioning_client.go @@ -0,0 +1,123 @@ +package app + +import ( + "context" + + pb "go.viam.com/api/provisioning/v1" + "go.viam.com/utils/rpc" +) + +type ProvisioningInfo struct { + FragmentID string + Model string + Manufacturer string +} + +func provisioningInfoFromProto(info *pb.ProvisioningInfo) *ProvisioningInfo { + return &ProvisioningInfo{ + FragmentID: info.FragmentId, + Model: info.Model, + Manufacturer: info.Manufacturer, + } +} + +type NetworkInfo struct { + Type string + SSID string + Security string + Signal int32 + Connected bool + LastError string +} + +func networkInfoFromProto(info *pb.NetworkInfo) *NetworkInfo { + return &NetworkInfo{ + Type: info.Type, + SSID: info.Ssid, + Security: info.Security, + Signal: info.Signal, + Connected: info.Connected, + LastError: info.LastError, + } +} + +type GetSmartMachineStatusResponse struct { + ProvisioningInfo *ProvisioningInfo + HasSmartMachineCredentials bool + IsOnline bool + LastestConnectionAttempt *NetworkInfo + Errors []string +} + +func getSmartMachineStatusResponseFromProto(resp *pb.GetSmartMachineStatusResponse) *GetSmartMachineStatusResponse { + return &GetSmartMachineStatusResponse{ + ProvisioningInfo: provisioningInfoFromProto(resp.ProvisioningInfo), + HasSmartMachineCredentials: resp.HasSmartMachineCredentials, + IsOnline: resp.IsOnline, + LastestConnectionAttempt: networkInfoFromProto(resp.LatestConnectionAttempt), + Errors: resp.Errors, + } +} + +// CloudConfig is the minimal config to create a /etc/viam.json, containing the smart machine's part ID and secret. +type CloudConfig struct { + ID string + Secret string + AppAddress string +} + +func cloudConfigToProto(config *CloudConfig) *pb.CloudConfig { + return &pb.CloudConfig{ + Id: config.ID, + Secret: config.Secret, + AppAddress: config.AppAddress, + } +} + +type ProvisioningClient struct { + client pb.ProvisioningServiceClient +} + +func NewProvisioningClient(conn rpc.ClientConn) *ProvisioningClient { + return &ProvisioningClient{client: pb.NewProvisioningServiceClient(conn)} +} + +// GetSmartMachineStatus is for retrieving the status of the smart machine including networking. +func (c *ProvisioningClient) GetSmartMachineStatus(ctx context.Context) (*GetSmartMachineStatusResponse, error) { + resp, err := c.client.GetSmartMachineStatus(ctx, &pb.GetSmartMachineStatusRequest{}) + if err != nil { + return nil, err + } + return getSmartMachineStatusResponseFromProto(resp), nil +} + +// SetNetworkCredentials is to set the wifi credentials. +func (c *ProvisioningClient) SetNetworkCredentials(ctx context.Context, credentialsType, ssid, psk string) error { + _, err := c.client.SetNetworkCredentials(ctx, &pb.SetNetworkCredentialsRequest{ + Type: credentialsType, + Ssid: ssid, + Psk: psk, + }) + return err +} + +// SetSmartMachineCredentials is to set the smart machine credentials. +func (c *ProvisioningClient) SetSmartMachineCredentials(ctx context.Context, cloud *CloudConfig) error { + _, err := c.client.SetSmartMachineCredentials(ctx, &pb.SetSmartMachineCredentialsRequest{ + Cloud: cloudConfigToProto(cloud), + }) + return err +} + +// GetNetworkList is to retrieve the list of networks that are visible to the smart machine. +func (c *ProvisioningClient) GetNetworkList(ctx context.Context) ([]*NetworkInfo, error) { + resp, err := c.client.GetNetworkList(ctx, &pb.GetNetworkListRequest{}) + if err != nil { + return nil, err + } + var networks []*NetworkInfo + for _, network := range(resp.Networks) { + networks = append(networks, networkInfoFromProto(network)) + } + return networks, nil +} From 7f4ca8161be14f79e889602a20b01652cd3c1f6e Mon Sep 17 00:00:00 2001 From: purplenicole730 Date: Thu, 21 Nov 2024 10:35:31 -0500 Subject: [PATCH 2/7] add fake provisioning client --- .../inject/provisioning_service_client.go | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 testutils/inject/provisioning_service_client.go diff --git a/testutils/inject/provisioning_service_client.go b/testutils/inject/provisioning_service_client.go new file mode 100644 index 00000000000..1f42fd011e0 --- /dev/null +++ b/testutils/inject/provisioning_service_client.go @@ -0,0 +1,58 @@ +package inject + +import ( + "context" + + provisioningPb "go.viam.com/api/provisioning/v1" + "google.golang.org/grpc" +) + +// ProvisioningServiceClient represents a fake instance of a provisioning client. +type ProvisioningServiceClient struct { + provisioningPb.ProvisioningServiceClient + GetSmartMachineStatusFunc func(ctx context.Context, in *provisioningPb.GetSmartMachineStatusRequest, + opts ...grpc.CallOption) (*provisioningPb.GetSmartMachineStatusResponse, error) + SetNetworkCredentialsFunc func(ctx context.Context, in *provisioningPb.SetNetworkCredentialsRequest, + opts ...grpc.CallOption) (*provisioningPb.SetNetworkCredentialsResponse, error) + SetSmartMachineCredentialsFunc func(ctx context.Context, in *provisioningPb.SetSmartMachineCredentialsRequest, + opts ...grpc.CallOption) (*provisioningPb.SetSmartMachineCredentialsResponse, error) + GetNetworkListFunc func(ctx context.Context, in *provisioningPb.GetNetworkListRequest, + opts ...grpc.CallOption) (*provisioningPb.GetNetworkListResponse, error) +} + +// GetSmartMachineStatus calls the injected GetSmartMachineStatusFunc or the real version. +func (psc *ProvisioningServiceClient) GetSmartMachineStatus(ctx context.Context, in *provisioningPb.GetSmartMachineStatusRequest, + opts ...grpc.CallOption, +) (*provisioningPb.GetSmartMachineStatusResponse, error) { + if psc.GetSmartMachineStatusFunc == nil { + return psc.ProvisioningServiceClient.GetSmartMachineStatus(ctx, in, opts...) + } + return psc.GetSmartMachineStatusFunc(ctx, in, opts...) +} +// SetNetworkCredentials calls the injected SetNetworkCredentialsFunc or the real version. +func (psc *ProvisioningServiceClient) SetNetworkCredentials(ctx context.Context, in *provisioningPb.SetNetworkCredentialsRequest, + opts ...grpc.CallOption, +) (*provisioningPb.SetNetworkCredentialsResponse, error) { + if psc.SetNetworkCredentialsFunc == nil { + return psc.ProvisioningServiceClient.SetNetworkCredentials(ctx, in, opts...) + } + return psc.SetNetworkCredentialsFunc(ctx, in, opts...) +} +// SetSmartMachineCredentials calls the injected SetSmartMachineCredentialsFunc or the real version. +func (psc *ProvisioningServiceClient) SetSmartMachineCredentials(ctx context.Context, in *provisioningPb.SetSmartMachineCredentialsRequest, + opts ...grpc.CallOption, +) (*provisioningPb.SetSmartMachineCredentialsResponse, error) { + if psc.SetSmartMachineCredentialsFunc == nil { + return psc.ProvisioningServiceClient.SetSmartMachineCredentials(ctx, in, opts...) + } + return psc.SetSmartMachineCredentialsFunc(ctx, in, opts...) +} +// GetNetworkList calls the injected GetNetworkListFunc or the real version. +func (psc *ProvisioningServiceClient) GetNetworkList(ctx context.Context, in *provisioningPb.GetNetworkListRequest, + opts ...grpc.CallOption, +) (*provisioningPb.GetNetworkListResponse, error) { + if psc.GetNetworkListFunc == nil { + return psc.ProvisioningServiceClient.GetNetworkList(ctx, in, opts...) + } + return psc.GetNetworkListFunc(ctx, in, opts...) +} From d92b68d2b488fc4e55fee628a81ae733a225b0d4 Mon Sep 17 00:00:00 2001 From: purplenicole730 Date: Thu, 21 Nov 2024 11:11:35 -0500 Subject: [PATCH 3/7] add tests --- app/provisioning_client_test.go | 133 ++++++++++++++++++++++++++++++++ 1 file changed, 133 insertions(+) create mode 100644 app/provisioning_client_test.go diff --git a/app/provisioning_client_test.go b/app/provisioning_client_test.go new file mode 100644 index 00000000000..76dcf693ed9 --- /dev/null +++ b/app/provisioning_client_test.go @@ -0,0 +1,133 @@ +package app + +import ( + "context" + "testing" + + pb "go.viam.com/api/provisioning/v1" + "go.viam.com/rdk/testutils/inject" + "go.viam.com/test" + "google.golang.org/grpc" +) + +const ( + hasSmartMachineCredentials = true + isOnline = true + errorString = "error" + fragmentID = "fragment_id" + model = "model" + manufacturer = "manufacturer" + networkType = "network_type" + ssid = "ssid" + security = "security" + signal int32 = 4 + connected = true + credentialsType = "credentials_type" + psk = "psk" + secret = "secret" +) + +var ( + provisioningInfo = ProvisioningInfo{ + FragmentID: fragmentID, + Model: model, + Manufacturer: manufacturer, + } + networkInfo = NetworkInfo{ + Type: networkType, + SSID: ssid, + Security: security, + Signal: signal, + Connected: connected, + LastError: errorString, + } + pbNetworkInfo = pb.NetworkInfo{ + Type: getSmartMachineStatusResponse.LastestConnectionAttempt.Type, + Ssid: getSmartMachineStatusResponse.LastestConnectionAttempt.SSID, + Security: getSmartMachineStatusResponse.LastestConnectionAttempt.Security, + Signal: getSmartMachineStatusResponse.LastestConnectionAttempt.Signal, + Connected: getSmartMachineStatusResponse.LastestConnectionAttempt.Connected, + LastError: getSmartMachineStatusResponse.LastestConnectionAttempt.LastError, + } + errorList = []string{errorString} + getSmartMachineStatusResponse = GetSmartMachineStatusResponse{ + ProvisioningInfo: &provisioningInfo, + HasSmartMachineCredentials: hasSmartMachineCredentials, + IsOnline: isOnline, + LastestConnectionAttempt: &networkInfo, + Errors: errorList, + } + cloudConfig = CloudConfig{ + ID: partID, + Secret: secret, + } +) + +func createProvisioningGrpcClient() *inject.ProvisioningServiceClient{ + return &inject.ProvisioningServiceClient{} +} + +func TestProvisioningClient(t *testing.T) { + grpcClient := createProvisioningGrpcClient() + client := ProvisioningClient{client: grpcClient} + + t.Run("GetSmartMachineStatus", func(t *testing.T) { + pbResponse := pb.GetSmartMachineStatusResponse{ + ProvisioningInfo: &pb.ProvisioningInfo{ + FragmentId: getSmartMachineStatusResponse.ProvisioningInfo.FragmentID, + Model: getSmartMachineStatusResponse.ProvisioningInfo.Model, + Manufacturer: getSmartMachineStatusResponse.ProvisioningInfo.Manufacturer, + }, + HasSmartMachineCredentials: getSmartMachineStatusResponse.HasSmartMachineCredentials, + IsOnline: getSmartMachineStatusResponse.IsOnline, + LatestConnectionAttempt: &pbNetworkInfo, + Errors: getSmartMachineStatusResponse.Errors, + } + grpcClient.GetSmartMachineStatusFunc = func( + ctx context.Context, in *pb.GetSmartMachineStatusRequest, opts ...grpc.CallOption, + ) (*pb.GetSmartMachineStatusResponse, error) { + return &pbResponse, nil + } + resp, err := client.GetSmartMachineStatus(context.Background()) + test.That(t, err, test.ShouldBeNil) + test.That(t, resp, test.ShouldResemble, &getSmartMachineStatusResponse) + }) + + t.Run("SetNetworkCredentials", func(t *testing.T) { + grpcClient.SetNetworkCredentialsFunc = func( + ctx context.Context, in *pb.SetNetworkCredentialsRequest, opts ...grpc.CallOption, + ) (*pb.SetNetworkCredentialsResponse, error) { + test.That(t, in.Type, test.ShouldEqual, credentialsType) + test.That(t, in.Ssid, test.ShouldEqual, ssid) + test.That(t, in.Psk, test.ShouldEqual, psk) + return &pb.SetNetworkCredentialsResponse{}, nil + } + err := client.SetNetworkCredentials(context.Background(), credentialsType, ssid, psk) + test.That(t, err, test.ShouldBeNil) + }) + + t.Run("SetSmartMachineCredentials", func(t *testing.T) { + grpcClient.SetSmartMachineCredentialsFunc = func( + ctx context.Context, in *pb.SetSmartMachineCredentialsRequest, opts ...grpc.CallOption, + ) (*pb.SetSmartMachineCredentialsResponse, error) { + test.That(t, in.Cloud, test.ShouldResemble, cloudConfigToProto(&cloudConfig)) + return &pb.SetSmartMachineCredentialsResponse{}, nil + } + err := client.SetSmartMachineCredentials(context.Background(), &cloudConfig) + test.That(t, err, test.ShouldBeNil) + }) + + t.Run("GetNetworkList", func(t *testing.T) { + expectedNetworks := []*NetworkInfo{&networkInfo} + grpcClient.GetNetworkListFunc = func( + ctx context.Context, in *pb.GetNetworkListRequest, opts ...grpc.CallOption, + ) (*pb.GetNetworkListResponse, error) { + return &pb.GetNetworkListResponse{ + Networks: []*pb.NetworkInfo{&pbNetworkInfo}, + }, nil + } + resp, err := client.GetNetworkList(context.Background()) + test.That(t, err, test.ShouldBeNil) + test.That(t, resp, test.ShouldResemble, expectedNetworks) + }) +} From 6013d86bc5f6f7631a77eb5b0a835f54be99dabb Mon Sep 17 00:00:00 2001 From: purplenicole730 Date: Thu, 21 Nov 2024 11:15:47 -0500 Subject: [PATCH 4/7] make lint --- app/provisioning_client.go | 57 +++++++------- app/provisioning_client_test.go | 75 ++++++++++--------- .../inject/provisioning_service_client.go | 9 ++- 3 files changed, 75 insertions(+), 66 deletions(-) diff --git a/app/provisioning_client.go b/app/provisioning_client.go index 820c649f912..8acb64165bf 100644 --- a/app/provisioning_client.go +++ b/app/provisioning_client.go @@ -7,77 +7,82 @@ import ( "go.viam.com/utils/rpc" ) +// ProvisioningInfo holds provisioning info. type ProvisioningInfo struct { - FragmentID string - Model string + FragmentID string + Model string Manufacturer string } func provisioningInfoFromProto(info *pb.ProvisioningInfo) *ProvisioningInfo { return &ProvisioningInfo{ - FragmentID: info.FragmentId, - Model: info.Model, + FragmentID: info.FragmentId, + Model: info.Model, Manufacturer: info.Manufacturer, } } +// NetworkInfo holds network information. type NetworkInfo struct { - Type string - SSID string - Security string - Signal int32 + Type string + SSID string + Security string + Signal int32 Connected bool LastError string } func networkInfoFromProto(info *pb.NetworkInfo) *NetworkInfo { return &NetworkInfo{ - Type: info.Type, - SSID: info.Ssid, - Security: info.Security, - Signal: info.Signal, + Type: info.Type, + SSID: info.Ssid, + Security: info.Security, + Signal: info.Signal, Connected: info.Connected, LastError: info.LastError, } } +// GetSmartMachineStatusResponse contains smart machine status information. type GetSmartMachineStatusResponse struct { - ProvisioningInfo *ProvisioningInfo + ProvisioningInfo *ProvisioningInfo HasSmartMachineCredentials bool - IsOnline bool - LastestConnectionAttempt *NetworkInfo - Errors []string + IsOnline bool + LastestConnectionAttempt *NetworkInfo + Errors []string } func getSmartMachineStatusResponseFromProto(resp *pb.GetSmartMachineStatusResponse) *GetSmartMachineStatusResponse { return &GetSmartMachineStatusResponse{ - ProvisioningInfo: provisioningInfoFromProto(resp.ProvisioningInfo), + ProvisioningInfo: provisioningInfoFromProto(resp.ProvisioningInfo), HasSmartMachineCredentials: resp.HasSmartMachineCredentials, - IsOnline: resp.IsOnline, - LastestConnectionAttempt: networkInfoFromProto(resp.LatestConnectionAttempt), - Errors: resp.Errors, + IsOnline: resp.IsOnline, + LastestConnectionAttempt: networkInfoFromProto(resp.LatestConnectionAttempt), + Errors: resp.Errors, } } // CloudConfig is the minimal config to create a /etc/viam.json, containing the smart machine's part ID and secret. type CloudConfig struct { - ID string - Secret string + ID string + Secret string AppAddress string } func cloudConfigToProto(config *CloudConfig) *pb.CloudConfig { return &pb.CloudConfig{ - Id: config.ID, - Secret: config.Secret, + Id: config.ID, + Secret: config.Secret, AppAddress: config.AppAddress, } } +// ProvisioningClient is a gRPC client for method calls to the Provisioning API. type ProvisioningClient struct { client pb.ProvisioningServiceClient } +// NewProvisioningClient constructs a new ProvisioningClient using the connection passed in by the Viam client. func NewProvisioningClient(conn rpc.ClientConn) *ProvisioningClient { return &ProvisioningClient{client: pb.NewProvisioningServiceClient(conn)} } @@ -96,7 +101,7 @@ func (c *ProvisioningClient) SetNetworkCredentials(ctx context.Context, credenti _, err := c.client.SetNetworkCredentials(ctx, &pb.SetNetworkCredentialsRequest{ Type: credentialsType, Ssid: ssid, - Psk: psk, + Psk: psk, }) return err } @@ -116,7 +121,7 @@ func (c *ProvisioningClient) GetNetworkList(ctx context.Context) ([]*NetworkInfo return nil, err } var networks []*NetworkInfo - for _, network := range(resp.Networks) { + for _, network := range resp.Networks { networks = append(networks, networkInfoFromProto(network)) } return networks, nil diff --git a/app/provisioning_client_test.go b/app/provisioning_client_test.go index 76dcf693ed9..3eea5e4345e 100644 --- a/app/provisioning_client_test.go +++ b/app/provisioning_client_test.go @@ -5,65 +5,66 @@ import ( "testing" pb "go.viam.com/api/provisioning/v1" - "go.viam.com/rdk/testutils/inject" "go.viam.com/test" "google.golang.org/grpc" + + "go.viam.com/rdk/testutils/inject" ) const ( - hasSmartMachineCredentials = true - isOnline = true - errorString = "error" - fragmentID = "fragment_id" - model = "model" - manufacturer = "manufacturer" - networkType = "network_type" - ssid = "ssid" - security = "security" - signal int32 = 4 - connected = true - credentialsType = "credentials_type" - psk = "psk" - secret = "secret" + hasSmartMachineCredentials = true + isOnline = true + errorString = "error" + fragmentID = "fragment_id" + model = "model" + manufacturer = "manufacturer" + networkType = "network_type" + ssid = "ssid" + security = "security" + signal int32 = 4 + connected = true + credentialsType = "credentials_type" + psk = "psk" + secret = "secret" ) var ( provisioningInfo = ProvisioningInfo{ - FragmentID: fragmentID, - Model: model, + FragmentID: fragmentID, + Model: model, Manufacturer: manufacturer, } networkInfo = NetworkInfo{ - Type: networkType, - SSID: ssid, - Security: security, - Signal: signal, + Type: networkType, + SSID: ssid, + Security: security, + Signal: signal, Connected: connected, LastError: errorString, } pbNetworkInfo = pb.NetworkInfo{ - Type: getSmartMachineStatusResponse.LastestConnectionAttempt.Type, - Ssid: getSmartMachineStatusResponse.LastestConnectionAttempt.SSID, - Security: getSmartMachineStatusResponse.LastestConnectionAttempt.Security, - Signal: getSmartMachineStatusResponse.LastestConnectionAttempt.Signal, + Type: getSmartMachineStatusResponse.LastestConnectionAttempt.Type, + Ssid: getSmartMachineStatusResponse.LastestConnectionAttempt.SSID, + Security: getSmartMachineStatusResponse.LastestConnectionAttempt.Security, + Signal: getSmartMachineStatusResponse.LastestConnectionAttempt.Signal, Connected: getSmartMachineStatusResponse.LastestConnectionAttempt.Connected, LastError: getSmartMachineStatusResponse.LastestConnectionAttempt.LastError, } - errorList = []string{errorString} + errorList = []string{errorString} getSmartMachineStatusResponse = GetSmartMachineStatusResponse{ - ProvisioningInfo: &provisioningInfo, + ProvisioningInfo: &provisioningInfo, HasSmartMachineCredentials: hasSmartMachineCredentials, - IsOnline: isOnline, - LastestConnectionAttempt: &networkInfo, - Errors: errorList, + IsOnline: isOnline, + LastestConnectionAttempt: &networkInfo, + Errors: errorList, } cloudConfig = CloudConfig{ - ID: partID, + ID: partID, Secret: secret, } ) -func createProvisioningGrpcClient() *inject.ProvisioningServiceClient{ +func createProvisioningGrpcClient() *inject.ProvisioningServiceClient { return &inject.ProvisioningServiceClient{} } @@ -74,14 +75,14 @@ func TestProvisioningClient(t *testing.T) { t.Run("GetSmartMachineStatus", func(t *testing.T) { pbResponse := pb.GetSmartMachineStatusResponse{ ProvisioningInfo: &pb.ProvisioningInfo{ - FragmentId: getSmartMachineStatusResponse.ProvisioningInfo.FragmentID, - Model: getSmartMachineStatusResponse.ProvisioningInfo.Model, + FragmentId: getSmartMachineStatusResponse.ProvisioningInfo.FragmentID, + Model: getSmartMachineStatusResponse.ProvisioningInfo.Model, Manufacturer: getSmartMachineStatusResponse.ProvisioningInfo.Manufacturer, }, HasSmartMachineCredentials: getSmartMachineStatusResponse.HasSmartMachineCredentials, - IsOnline: getSmartMachineStatusResponse.IsOnline, - LatestConnectionAttempt: &pbNetworkInfo, - Errors: getSmartMachineStatusResponse.Errors, + IsOnline: getSmartMachineStatusResponse.IsOnline, + LatestConnectionAttempt: &pbNetworkInfo, + Errors: getSmartMachineStatusResponse.Errors, } grpcClient.GetSmartMachineStatusFunc = func( ctx context.Context, in *pb.GetSmartMachineStatusRequest, opts ...grpc.CallOption, diff --git a/testutils/inject/provisioning_service_client.go b/testutils/inject/provisioning_service_client.go index 1f42fd011e0..4e636546c64 100644 --- a/testutils/inject/provisioning_service_client.go +++ b/testutils/inject/provisioning_service_client.go @@ -29,7 +29,8 @@ func (psc *ProvisioningServiceClient) GetSmartMachineStatus(ctx context.Context, } return psc.GetSmartMachineStatusFunc(ctx, in, opts...) } -// SetNetworkCredentials calls the injected SetNetworkCredentialsFunc or the real version. + +// SetNetworkCredentials calls the injected SetNetworkCredentialsFunc or the real version. func (psc *ProvisioningServiceClient) SetNetworkCredentials(ctx context.Context, in *provisioningPb.SetNetworkCredentialsRequest, opts ...grpc.CallOption, ) (*provisioningPb.SetNetworkCredentialsResponse, error) { @@ -38,7 +39,8 @@ func (psc *ProvisioningServiceClient) SetNetworkCredentials(ctx context.Context, } return psc.SetNetworkCredentialsFunc(ctx, in, opts...) } -// SetSmartMachineCredentials calls the injected SetSmartMachineCredentialsFunc or the real version. + +// SetSmartMachineCredentials calls the injected SetSmartMachineCredentialsFunc or the real version. func (psc *ProvisioningServiceClient) SetSmartMachineCredentials(ctx context.Context, in *provisioningPb.SetSmartMachineCredentialsRequest, opts ...grpc.CallOption, ) (*provisioningPb.SetSmartMachineCredentialsResponse, error) { @@ -47,7 +49,8 @@ func (psc *ProvisioningServiceClient) SetSmartMachineCredentials(ctx context.Con } return psc.SetSmartMachineCredentialsFunc(ctx, in, opts...) } -// GetNetworkList calls the injected GetNetworkListFunc or the real version. + +// GetNetworkList calls the injected GetNetworkListFunc or the real version. func (psc *ProvisioningServiceClient) GetNetworkList(ctx context.Context, in *provisioningPb.GetNetworkListRequest, opts ...grpc.CallOption, ) (*provisioningPb.GetNetworkListResponse, error) { From 98758b63efa2a5408aa8aef2e1e9519a3bad12ef Mon Sep 17 00:00:00 2001 From: purplenicole730 Date: Thu, 21 Nov 2024 11:20:41 -0500 Subject: [PATCH 5/7] add provisioningclient to viam client --- app/viam_client.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/app/viam_client.go b/app/viam_client.go index 6a8e3075012..fbd920dce85 100644 --- a/app/viam_client.go +++ b/app/viam_client.go @@ -16,6 +16,7 @@ import ( type ViamClient struct { conn rpc.ClientConn dataClient *DataClient + provisioningClient *ProvisioningClient } // Options has the options necessary to connect through gRPC. @@ -73,6 +74,16 @@ func (c *ViamClient) DataClient() *DataClient { return c.dataClient } +// ProvisioningClient initializes and returns a ProvisioningClient instance used to make provisioning method calls. +// To use ProvisioningClient, you must first instantiate a ViamClient. +func (c *ViamClient) ProvisioningClient() *ProvisioningClient { + if c.provisioningClient != nil { + return c.provisioningClient + } + c.provisioningClient = NewProvisioningClient(c.conn) + return c.provisioningClient +} + // Close closes the gRPC connection. func (c *ViamClient) Close() error { return c.conn.Close() From aff5ebb93264d952bf890f203e3a865b60f253a6 Mon Sep 17 00:00:00 2001 From: purplenicole730 Date: Thu, 21 Nov 2024 15:57:49 -0500 Subject: [PATCH 6/7] make lint --- app/viam_client.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/viam_client.go b/app/viam_client.go index fbd920dce85..3c361afaf69 100644 --- a/app/viam_client.go +++ b/app/viam_client.go @@ -14,8 +14,8 @@ import ( // ViamClient is a gRPC client for method calls to Viam app. type ViamClient struct { - conn rpc.ClientConn - dataClient *DataClient + conn rpc.ClientConn + dataClient *DataClient provisioningClient *ProvisioningClient } From a8b44078f838f56ab1808e0596fe0cd3ded8a2e9 Mon Sep 17 00:00:00 2001 From: purplenicole730 Date: Thu, 21 Nov 2024 18:51:07 -0500 Subject: [PATCH 7/7] add provisioningclient tests to viam client test --- app/viam_client_test.go | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/app/viam_client_test.go b/app/viam_client_test.go index 08b93ad1c31..b56f1a81930 100644 --- a/app/viam_client_test.go +++ b/app/viam_client_test.go @@ -5,7 +5,8 @@ import ( "testing" "github.com/viamrobotics/webrtc/v3" - pb "go.viam.com/api/app/data/v1" + datapb "go.viam.com/api/app/data/v1" + provisioningpb "go.viam.com/api/provisioning/v1" "go.viam.com/test" "go.viam.com/utils" "go.viam.com/utils/rpc" @@ -119,7 +120,7 @@ func TestCreateViamClientWithAPIKeyTests(t *testing.T) { } } -func TestNewDataClient(t *testing.T) { +func TestNewAppClients(t *testing.T) { originalDialDirectGRPC := dialDirectGRPC dialDirectGRPC = mockDialDirectGRPC defer func() { dialDirectGRPC = originalDialDirectGRPC }() @@ -138,10 +139,20 @@ func TestNewDataClient(t *testing.T) { dataClient := client.DataClient() test.That(t, dataClient, test.ShouldNotBeNil) test.That(t, dataClient, test.ShouldHaveSameTypeAs, &DataClient{}) - test.That(t, dataClient.client, test.ShouldImplement, (*pb.DataServiceClient)(nil)) + test.That(t, dataClient.client, test.ShouldImplement, (*datapb.DataServiceClient)(nil)) // Testing that a second call to DataClient() returns the same instance dataClient2 := client.DataClient() test.That(t, dataClient2, test.ShouldNotBeNil) - test.That(t, dataClient, test.ShouldResemble, dataClient2) + test.That(t, dataClient, test.ShouldEqual, dataClient2) + + provisioningClient := client.ProvisioningClient() + test.That(t, provisioningClient, test.ShouldNotBeNil) + test.That(t, provisioningClient, test.ShouldHaveSameTypeAs, &ProvisioningClient{}) + test.That(t, provisioningClient.client, test.ShouldImplement, (*provisioningpb.ProvisioningServiceClient)(nil)) + + // Testing that a second call to ProvisioningClient() returns the same instance + provisioningClient2 := client.ProvisioningClient() + test.That(t, provisioningClient2, test.ShouldNotBeNil) + test.That(t, provisioningClient, test.ShouldEqual, provisioningClient2) }