Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add vehicle to tesla telemetry server with tests #285

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions charts/devices-api/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ env:
SYNTHETIC_FINGERPRINT_CONSUMER_GROUP: consumer.synthetic.fingerprint
TESLA_TOKEN_URL: https://auth.tesla.com/oauth2/v3/token
TESLA_FLEET_URL: https://fleet-api.prd.%s.vn.cloud.tesla.com
TESLA_TELEMETRY_HOST_NAME: tesla-dev.dev.dimo.zone
TESLA_TELEMETRY_PORT: 443
TESLA_TELEMETRY_CA_CERTIFICATE: -----BEGIN CERTIFICATE-----\nMIIBvDCCAWKgAwIBAgIRAL6QCUcK/8jy48V7ElERABowCgYIKoZIzj0EAwIwIzEh\nMB8GA1UEAxMYRElNTyBDQSBEZXZlbG9wbWVudCBSb290MCAXDTIyMDQyMzExMTEw\nM1oYDzIwNzIwNDEwMTExMTAzWjAyMTAwLgYDVQQDEydESU1PIENBIERldmVsb3Bt\nZW50IFNlcnZlciBJbnRlcm1lZGlhdGUwWTATBgcqhkjOPQIBBggqhkjOPQMBBwNC\nAAQMyh6plUM3p4KWWfK0CqWXr1B9NWk53+c9ps8OpgZZIyXjxiw1EHxrpcqU7C9e\nhw+6JfmvTqqi3F4ES8K+Tt/mo2YwZDAOBgNVHQ8BAf8EBAMCAQYwEgYDVR0TAQH/\nBAgwBgEB/wIBADAdBgNVHQ4EFgQU+7zrfioO4bjNpD9KiG8fbTcIq8kwHwYDVR0j\nBBgwFoAUeMfSSqt+S65xQF82yRnjr+J5XC8wCgYIKoZIzj0EAwIDSAAwRQIhAK3s\nWtlk+d0fnkii091dTZGt+dtzEbM4HuizaG6mO5zPAiApi03qU/hdsAxXwlbhufH/\n5HuUiCLgBK8vPvL2YdMaKQ==\n-----END CERTIFICATE-----\n-----BEGIN CERTIFICATE-----\nMIIBrTCCAVKgAwIBAgIQEgthFz9Ww3+VaErBc3nDFjAKBggqhkjOPQQDAjAjMSEw\nHwYDVQQDExhESU1PIENBIERldmVsb3BtZW50IFJvb3QwIBcNMjIwNDIzMTExMTAz\nWhgPMjEyMjAzMzAxMTExMDNaMCMxITAfBgNVBAMTGERJTU8gQ0EgRGV2ZWxvcG1l\nbnQgUm9vdDBZMBMGByqGSM49AgEGCCqGSM49AwEHA0IABBuxEb6jTyfrUwI8RiBV\nKCQWqTAeLdHPj60Qk7HeMeaEcGjzF799xgpl6/8iNKaHN/w+705cdxp5pRswbUtu\nizWjZjBkMA4GA1UdDwEB/wQEAwIBBjASBgNVHRMBAf8ECDAGAQH/AgEBMB0GA1Ud\nDgQWBBR4x9JKq35LrnFAXzbJGeOv4nlcLzAfBgNVHSMEGDAWgBR4x9JKq35LrnFA\nXzbJGeOv4nlcLzAKBggqhkjOPQQDAgNJADBGAiEAlslTE9mX+VjPSYLKEsy48Rzh\nOUCdaWovmF+28PyAi4wCIQDXRKpYK+VMFyUR1GJVoV3gWezQcJmFswuWq+7M+XPb\nGQ==\n-----END CERTIFICATE-----
0xdev22 marked this conversation as resolved.
Show resolved Hide resolved
META_TRANSACTION_PROCESSOR_GRPC_ADDR: meta-transaction-processor-dev:8086
service:
type: ClusterIP
Expand Down
3 changes: 3 additions & 0 deletions internal/config/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ type Settings struct {
TeslaClientSecret string `yaml:"TESLA_CLIENT_SECRET"`
TeslaTokenURL string `yaml:"TESLA_TOKEN_URL"`
TeslaFleetURL string `yaml:"TESLA_FLEET_URL"`
TeslaTelemetryHostName string `yaml:"TESLA_TELEMETRY_HOST_NAME"`
TeslaTelemetryPort int `yaml:"TESLA_TELEMETRY_PORT"`
TeslaTelemetryCACertificate string `yaml:"TESLA_TELEMETRY_CA_CERTIFICATE"`
}

func (s *Settings) IsProduction() bool {
Expand Down
35 changes: 24 additions & 11 deletions internal/controllers/user_integrations_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@ import (
smartcar "github.com/smartcar/go-sdk"

ddgrpc "github.com/DIMO-Network/device-definitions-api/pkg/grpc"
"github.com/DIMO-Network/devices-api/internal/constants"
"github.com/DIMO-Network/devices-api/internal/controllers/helpers"
"github.com/DIMO-Network/devices-api/internal/services"
"github.com/DIMO-Network/devices-api/internal/services/registry"
"github.com/DIMO-Network/devices-api/models"
"github.com/DIMO-Network/shared"
pb "github.com/DIMO-Network/shared/api/users"
"github.com/ethereum/go-ethereum/common"
Expand All @@ -33,6 +28,12 @@ import (
"github.com/volatiletech/sqlboiler/v4/queries/qm"
"golang.org/x/exp/slices"
"golang.org/x/mod/semver"

"github.com/DIMO-Network/devices-api/internal/constants"
"github.com/DIMO-Network/devices-api/internal/controllers/helpers"
"github.com/DIMO-Network/devices-api/internal/services"
"github.com/DIMO-Network/devices-api/internal/services/registry"
"github.com/DIMO-Network/devices-api/models"
)

// GetUserDeviceIntegration godoc
Expand Down Expand Up @@ -72,7 +73,7 @@ func (udc *UserDevicesController) deleteDeviceIntegration(ctx context.Context, u
if err != nil {
return err
}
defer tx.Rollback() //nolint
defer tx.Rollback() // nolint
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having no space here is correct. It's a comment for machines.


apiInt, err := models.UserDeviceAPIIntegrations(
models.UserDeviceAPIIntegrationWhere.UserDeviceID.EQ(userDeviceID),
Expand Down Expand Up @@ -168,7 +169,7 @@ func (udc *UserDevicesController) DeleteUserDeviceIntegration(c *fiber.Ctx) erro
return err
}

defer tx.Rollback() //nolint
defer tx.Rollback() // nolint

device, err := models.UserDevices(
models.UserDeviceWhere.ID.EQ(userDeviceID),
Expand Down Expand Up @@ -812,7 +813,7 @@ func (udc *UserDevicesController) PostPairAutoPi(c *fiber.Ctx) error {
if err != nil {
return err
}
defer tx.Rollback() //nolint
defer tx.Rollback() // nolint

vnft, ad, err := udc.checkPairable(c.Context(), tx, userDeviceID, pairReq.ExternalID)
if err != nil {
Expand Down Expand Up @@ -1095,7 +1096,7 @@ func (udc *UserDevicesController) UnpairAutoPi(c *fiber.Ctx) error {
if err != nil {
return err
}
defer tx.Rollback() //nolint
defer tx.Rollback() // nolint

vnft, apnft, err := udc.checkUnpairable(c.Context(), tx, userDeviceID)
if err != nil {
Expand Down Expand Up @@ -1316,7 +1317,7 @@ func (udc *UserDevicesController) PostClaimAutoPi(c *fiber.Ctx) error {
if err != nil {
return err
}
defer tx.Rollback() //nolint
defer tx.Rollback() // nolint

udc.log.Info().Interface("payload", reqBody).Msg("Got claim request.")

Expand Down Expand Up @@ -1447,7 +1448,7 @@ func (udc *UserDevicesController) registerDeviceIntegrationInner(c *fiber.Ctx, u
if err != nil {
return fiber.NewError(fiber.StatusInternalServerError, fmt.Sprintf("failed to create transaction: %s", err))
}
defer tx.Rollback() //nolint
defer tx.Rollback() // nolint
ud, err := models.UserDevices(
models.UserDeviceWhere.ID.EQ(userDeviceID),
).One(c.Context(), tx)
Expand Down Expand Up @@ -1948,6 +1949,14 @@ func (udc *UserDevicesController) registerDeviceTesla(c *fiber.Ctx, logger *zero
return err
}

if apiVersion == constants.TeslaAPIV2 {
err = udc.registerTeslaForTelemetryServer(c.Context(), region, reqBody.AccessToken, v)
if err != nil {
// We should consider pushing this to a Queue for retry
logger.Err(err).Int("id", v.ID).Msg("Could not register Tesla for Telemetry")
}
}

logger.Info().Msg("Finished Tesla device registration")

if apiVersion == constants.TeslaAPIV2 && teslaV2CacheKey != "" {
Expand Down Expand Up @@ -2011,6 +2020,10 @@ func (udc *UserDevicesController) getTeslaAuthFromCache(ctx context.Context, cac
return teslaAuth, nil
}

func (udc *UserDevicesController) registerTeslaForTelemetryServer(ctx context.Context, region, token string, v *services.TeslaVehicle) error {
return udc.teslaFleetAPISvc.RegisterToTelemetryServer(ctx, token, region, v.VIN)
}

// fixTeslaDeviceDefinition tries to use the VIN provided by Tesla to correct the device definition
// used by a device.
//
Expand Down
34 changes: 19 additions & 15 deletions internal/controllers/user_integrations_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,6 @@ import (
"github.com/DIMO-Network/shared/db"

ddgrpc "github.com/DIMO-Network/device-definitions-api/pkg/grpc"
"github.com/DIMO-Network/devices-api/internal/config"
"github.com/DIMO-Network/devices-api/internal/constants"
"github.com/DIMO-Network/devices-api/internal/contracts"
"github.com/DIMO-Network/devices-api/internal/middleware/owner"
"github.com/DIMO-Network/devices-api/internal/services"
mock_services "github.com/DIMO-Network/devices-api/internal/services/mocks"
"github.com/DIMO-Network/devices-api/internal/services/registry"
"github.com/DIMO-Network/devices-api/internal/test"
"github.com/DIMO-Network/devices-api/models"
"github.com/DIMO-Network/shared"
smock "github.com/Shopify/sarama/mocks"
"github.com/gofiber/fiber/v2"
Expand All @@ -50,6 +41,16 @@ import (
"github.com/volatiletech/sqlboiler/v4/boil"
"github.com/volatiletech/sqlboiler/v4/types"
"go.uber.org/mock/gomock"

"github.com/DIMO-Network/devices-api/internal/config"
"github.com/DIMO-Network/devices-api/internal/constants"
"github.com/DIMO-Network/devices-api/internal/contracts"
"github.com/DIMO-Network/devices-api/internal/middleware/owner"
"github.com/DIMO-Network/devices-api/internal/services"
mock_services "github.com/DIMO-Network/devices-api/internal/services/mocks"
"github.com/DIMO-Network/devices-api/internal/services/registry"
"github.com/DIMO-Network/devices-api/internal/test"
"github.com/DIMO-Network/devices-api/models"
)

type UserIntegrationsControllerTestSuite struct {
Expand Down Expand Up @@ -668,7 +669,7 @@ func (s *UserIntegrationsControllerTestSuite) TestGetAutoPiInfoNoUDAI_ShouldUpda
// assert
assert.Equal(s.T(), fiber.StatusOK, response.StatusCode)
body, _ := io.ReadAll(response.Body)
//assert
// assert
assert.Equal(s.T(), false, gjson.GetBytes(body, "isUpdated").Bool())
assert.Equal(s.T(), unitID, gjson.GetBytes(body, "unitId").String())
assert.Equal(s.T(), "4321", gjson.GetBytes(body, "deviceId").String())
Expand Down Expand Up @@ -708,7 +709,7 @@ func (s *UserIntegrationsControllerTestSuite) TestGetAutoPiInfoNoUDAI_UpToDate()
// assert
assert.Equal(s.T(), fiber.StatusOK, response.StatusCode)
body, _ := io.ReadAll(response.Body)
//assert
// assert
assert.Equal(s.T(), true, gjson.GetBytes(body, "isUpdated").Bool())
assert.Equal(s.T(), "1.22.8", gjson.GetBytes(body, "releaseVersion").String())
assert.Equal(s.T(), false, gjson.GetBytes(body, "shouldUpdate").Bool()) // returned version is 1.21.9 which is our cutoff
Expand Down Expand Up @@ -745,7 +746,7 @@ func (s *UserIntegrationsControllerTestSuite) TestGetAutoPiInfoNoUDAI_FutureUpda
// assert
assert.Equal(s.T(), fiber.StatusOK, response.StatusCode)
body, _ := io.ReadAll(response.Body)
//assert
// assert
assert.Equal(s.T(), false, gjson.GetBytes(body, "isUpdated").Bool())
assert.Equal(s.T(), "1.23.1", gjson.GetBytes(body, "releaseVersion").String())
assert.Equal(s.T(), false, gjson.GetBytes(body, "shouldUpdate").Bool())
Expand Down Expand Up @@ -784,7 +785,7 @@ func (s *UserIntegrationsControllerTestSuite) TestGetAutoPiInfoNoUDAI_ShouldUpda
// assert
assert.Equal(s.T(), fiber.StatusOK, response.StatusCode)
body, _ := io.ReadAll(response.Body)
//assert
// assert
assert.Equal(s.T(), unitID, gjson.GetBytes(body, "unitId").String())
assert.Equal(s.T(), "v1.22.8", gjson.GetBytes(body, "releaseVersion").String())
assert.Equal(s.T(), false, gjson.GetBytes(body, "shouldUpdate").Bool()) // this because releaseVersion below 1.21.9
Expand Down Expand Up @@ -949,15 +950,18 @@ func (s *UserIntegrationsControllerTestSuite) TestPostTesla_V2() {
},
)

s.teslaFleetAPISvc.EXPECT().GetVehicle(gomock.Any(), "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c", "na", 1145).Return(&services.TeslaVehicle{
version := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"
region := "na"
vin := "5YJYGDEF9NF010423"
s.teslaFleetAPISvc.EXPECT().GetVehicle(gomock.Any(), version, region, 1145).Return(&services.TeslaVehicle{
ID: 1145,
VehicleID: 223,
VIN: "5YJYGDEF9NF010423",
}, nil)
s.teslaFleetAPISvc.EXPECT().WakeUpVehicle(gomock.Any(), "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c", "na", 1145).Return(nil)
s.deviceDefSvc.EXPECT().GetDeviceDefinitionByID(gomock.Any(), ud.DeviceDefinitionID).Times(2).Return(dd[0], nil)
s.deviceDefSvc.EXPECT().FindDeviceDefinitionByMMY(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Times(1).Return(dd[0], nil)

s.teslaFleetAPISvc.EXPECT().RegisterToTelemetryServer(gomock.Any(), version, region, vin).Return(nil)
userEthAddr := common.HexToAddress("1").String()
s.userClient.EXPECT().GetUser(gomock.Any(), &pbuser.GetUserRequest{Id: testUserID}).Return(&pbuser.User{EthereumAddress: &userEthAddr}, nil).AnyTimes()

Expand Down
18 changes: 16 additions & 2 deletions internal/services/mocks/tesla_fleet_api_service_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion internal/services/registry/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import (
"math/big"
"time"

"github.com/DIMO-Network/devices-api/internal/contracts"
"github.com/DIMO-Network/shared"
"github.com/Shopify/sarama"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/common/math"
signer "github.com/ethereum/go-ethereum/signer/core/apitypes"
"github.com/segmentio/ksuid"

"github.com/DIMO-Network/devices-api/internal/contracts"
)

type Client struct {
Expand Down
Loading
Loading