From b94398317bc7340b018f25bac0a13baea70e6158 Mon Sep 17 00:00:00 2001 From: Chris Hung Date: Fri, 18 Mar 2022 12:14:50 +0800 Subject: [PATCH] fix(metadata): ignore validation result if device service unavailable Signed-off-by: Chris Hung --- go.mod | 8 ++++---- go.sum | 16 +++++++++------- internal/core/metadata/application/notify.go | 8 +++++++- .../core/metadata/controller/http/device_test.go | 4 ++++ 4 files changed, 24 insertions(+), 12 deletions(-) diff --git a/go.mod b/go.mod index 8401fc33c5..bf6cdf2480 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/edgexfoundry/edgex-go require ( bitbucket.org/bertimus9/systemstat v0.0.0-20180207000608-0eeff89b0690 github.com/edgexfoundry/go-mod-bootstrap/v2 v2.2.0-dev.6 - github.com/edgexfoundry/go-mod-core-contracts/v2 v2.2.0-dev.13 + github.com/edgexfoundry/go-mod-core-contracts/v2 v2.2.0-dev.17 github.com/edgexfoundry/go-mod-messaging/v2 v2.2.0-dev.8 github.com/edgexfoundry/go-mod-registry/v2 v2.1.0 github.com/edgexfoundry/go-mod-secrets/v2 v2.1.0 @@ -15,7 +15,7 @@ require ( github.com/lib/pq v1.10.4 github.com/pelletier/go-toml v1.9.4 github.com/stretchr/testify v1.7.1 - golang.org/x/crypto v0.0.0-20210915214749-c084706c2272 + golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3 gopkg.in/eapache/queue.v1 v1.1.0 gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b ) @@ -31,7 +31,7 @@ require ( github.com/go-logfmt/logfmt v0.5.1 // indirect github.com/go-playground/locales v0.14.0 // indirect github.com/go-playground/universal-translator v0.18.0 // indirect - github.com/go-playground/validator/v10 v10.10.0 // indirect + github.com/go-playground/validator/v10 v10.10.1 // indirect github.com/go-redis/redis/v7 v7.3.0 // indirect github.com/gorilla/websocket v1.4.2 // indirect github.com/hashicorp/consul/api v1.9.1 // indirect @@ -55,7 +55,7 @@ require ( github.com/pmezard/go-difflib v1.0.0 // indirect github.com/stretchr/objx v0.1.1 // indirect github.com/x448/float16 v0.8.4 // indirect - golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4 // indirect + golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2 // indirect golang.org/x/sys v0.0.0-20210917161153-d61c044b1678 // indirect golang.org/x/text v0.3.7 // indirect ) diff --git a/go.sum b/go.sum index 0676ca2d92..7c9ba946a5 100644 --- a/go.sum +++ b/go.sum @@ -55,8 +55,8 @@ github.com/edgexfoundry/go-mod-bootstrap/v2 v2.2.0-dev.6/go.mod h1:12Szn7YDPPoQL github.com/edgexfoundry/go-mod-configuration/v2 v2.1.0 h1:wiLtHYo1QxImARJZt7TYj88cUhq1ANh8se4TBvFsYvw= github.com/edgexfoundry/go-mod-configuration/v2 v2.1.0/go.mod h1:MvHit0MxBXN4bC8LL0NZRsw72ByRE1XwtVLQP9C+2vg= github.com/edgexfoundry/go-mod-core-contracts/v2 v2.1.0/go.mod h1:I6UhBPCREubcU0ouIGBdZlNG5Xx4NijUVN5rvEtD03k= -github.com/edgexfoundry/go-mod-core-contracts/v2 v2.2.0-dev.13 h1:Y9Q3X/YBsoSNC+sBArP/upEDPW69PPtpp4wRulziBlE= -github.com/edgexfoundry/go-mod-core-contracts/v2 v2.2.0-dev.13/go.mod h1:FYFqOHl4OxULCnWecVcBatvk9f/BnRPwivLmARi+YCM= +github.com/edgexfoundry/go-mod-core-contracts/v2 v2.2.0-dev.17 h1:v0y5pxvlo20Uoha6tqlRI55GvcvZSYOYRaXbu7VDSsI= +github.com/edgexfoundry/go-mod-core-contracts/v2 v2.2.0-dev.17/go.mod h1:dvdhRDNAvlWU54g0Pa2+SZLPQII9UeGtPaRvQjx5tDQ= github.com/edgexfoundry/go-mod-messaging/v2 v2.2.0-dev.8 h1:JQ7/Ky2BBhapLhZ+mwy6FW9/W/Zp4UVDkH3VsNSdNEs= github.com/edgexfoundry/go-mod-messaging/v2 v2.2.0-dev.8/go.mod h1:+X6C0h8ZTJe+lLU2AGJfiAzCJK3zL+yM6cej9VC+79E= github.com/edgexfoundry/go-mod-registry/v2 v2.1.0 h1:ks0ejtLLUYKuoKrUBbWxygCU7q2mBFJlHE/+dEzV2Gw= @@ -97,8 +97,8 @@ github.com/go-playground/locales v0.14.0/go.mod h1:sawfccIbzZTqEDETgFXqTho0QybSa github.com/go-playground/universal-translator v0.18.0 h1:82dyy6p4OuJq4/CByFNOn/jYrnRPArHwAcmLoJZxyho= github.com/go-playground/universal-translator v0.18.0/go.mod h1:UvRDBj+xPUEGrFYl+lu/H90nyDXpg0fqeB/AQUGNTVA= github.com/go-playground/validator/v10 v10.9.0/go.mod h1:74x4gJWsvQexRdW8Pn3dXSGrTK4nAUsbPlLADvpJkos= -github.com/go-playground/validator/v10 v10.10.0 h1:I7mrTYv78z8k8VXa/qJlOlEXn/nBh+BF8dHX5nt/dr0= -github.com/go-playground/validator/v10 v10.10.0/go.mod h1:74x4gJWsvQexRdW8Pn3dXSGrTK4nAUsbPlLADvpJkos= +github.com/go-playground/validator/v10 v10.10.1 h1:uA0+amWMiglNZKZ9FJRKUAe9U3RX91eVn1JYXMWt7ig= +github.com/go-playground/validator/v10 v10.10.1/go.mod h1:i+3WkQ1FvaUjjxh1kSvIA4dMGDBiPU55YFDl0WbKdWU= github.com/go-redis/redis/v7 v7.3.0 h1:3oHqd0W7f/VLKBxeYTEpqdMUsmMectngjM9OtoRoIgg= github.com/go-redis/redis/v7 v7.3.0/go.mod h1:JDNMw23GTyLNC4GZu9njt15ctBQVn7xjRfnwdHj/Dcg= github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= @@ -357,8 +357,8 @@ golang.org/x/crypto v0.0.0-20200323165209-0ec3e9974c59/go.mod h1:LzIPMQfyMNhhGPh golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20210314154223-e6e6c4f2bb5b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4= golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= -golang.org/x/crypto v0.0.0-20210915214749-c084706c2272 h1:3erb+vDS8lU1sxfDHF4/hhWyaXnhIaO+7RgL4fDZORA= -golang.org/x/crypto v0.0.0-20210915214749-c084706c2272/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= +golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3 h1:0es+/5331RGQPcXlMfP+WrnIIS6dNnNRe0WB02W0F4M= +golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= @@ -387,8 +387,9 @@ golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81R golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= golang.org/x/net v0.0.0-20201110031124-69a78807bb2b/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= -golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4 h1:4nGaVu0QrbjT/AK2PRLuQfQuh6DJve+pELhqTdAj3x0= golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= +golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2 h1:CIJ76btIcR3eFI5EgSo6k1qKw9KJexJuRLI9G7Hp5wE= +golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= @@ -428,6 +429,7 @@ golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210403161142-5e06dd20ab57/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210603081109-ebe580a85c40/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= diff --git a/internal/core/metadata/application/notify.go b/internal/core/metadata/application/notify.go index 0a320f1cda..a9b407571e 100644 --- a/internal/core/metadata/application/notify.go +++ b/internal/core/metadata/application/notify.go @@ -74,7 +74,13 @@ func validateDeviceCallback(ctx context.Context, dic *di.Container, device dtos. request := requests.NewAddDeviceRequest(device) _, err = deviceServiceCallbackClient.ValidateDeviceCallback(ctx, request) if err != nil { - return errors.NewCommonEdgeX(errors.KindServerError, "device validation failed", err) + // TODO: reconsider the validity in v3 + // allow this case for the backward-compatability in v2 + if err.Code() == http.StatusServiceUnavailable { + lc.Warnf("Skipping device validation for device %s (device service %s unavailable)", device.Name, device.ServiceName) + } else { + return errors.NewCommonEdgeX(errors.KindServerError, "device validation failed", err) + } } return nil diff --git a/internal/core/metadata/controller/http/device_test.go b/internal/core/metadata/controller/http/device_test.go index a40d0ec8a4..6d853f5e62 100644 --- a/internal/core/metadata/controller/http/device_test.go +++ b/internal/core/metadata/controller/http/device_test.go @@ -155,6 +155,7 @@ func TestAddDevice(t *testing.T) { dbClientMock.On("DeviceProfileNameExists", deviceModel.ProfileName).Return(true, nil) dbClientMock.On("AddDevice", deviceModel).Return(deviceModel, nil) dbClientMock.On("DeviceServiceByName", deviceModel.ServiceName).Return(models.DeviceService{BaseAddress: mockDeviceServiceServer.URL}, nil) + dbClientMock.On("DeviceServiceByName", "unavailable").Return(models.DeviceService{BaseAddress: "http://unavailable"}, nil) notFoundService := testDevice notFoundService.Device.ServiceName = "notFoundService" @@ -183,6 +184,8 @@ func TestAddDevice(t *testing.T) { emptyProtocols.Device.Protocols = map[string]dtos.ProtocolProperties{} invalidProtocols := testDevice invalidProtocols.Device.Protocols = map[string]dtos.ProtocolProperties{"others": {}} + serviceUnavailable := testDevice + testDevice.Device.ServiceName = "unavailable" dic.Update(di.ServiceConstructorMap{ container.DBClientInterfaceName: func(get di.Get) interface{} { @@ -209,6 +212,7 @@ func TestAddDevice(t *testing.T) { {"Invalid - no protocols", []requests.AddDeviceRequest{noProtocols}, http.StatusBadRequest}, {"Invalid - empty protocols", []requests.AddDeviceRequest{emptyProtocols}, http.StatusBadRequest}, {"Invalid - invalid protocols", []requests.AddDeviceRequest{invalidProtocols}, http.StatusInternalServerError}, + {"Valid - device service unavailable", []requests.AddDeviceRequest{serviceUnavailable}, http.StatusCreated}, } for _, testCase := range tests { t.Run(testCase.name, func(t *testing.T) {