diff options
Diffstat (limited to 'cloud')
| -rw-r--r-- | cloud/pkg/common/messagelayer/util.go | 10 | ||||
| -rw-r--r-- | cloud/pkg/common/messagelayer/util_test.go | 8 |
2 files changed, 9 insertions, 9 deletions
diff --git a/cloud/pkg/common/messagelayer/util.go b/cloud/pkg/common/messagelayer/util.go index 01e7aa9e0..a8b1f9b97 100644 --- a/cloud/pkg/common/messagelayer/util.go +++ b/cloud/pkg/common/messagelayer/util.go @@ -36,8 +36,8 @@ const ( ResourceResourceTypeIndex = 3 ResourceResourceNameIndex = 4 - ResourceDeviceIndex = 2 - ResourceDeviceIDIndex = 3 + ResourceDeviceIndex = 2 + ResourceDeviceNamespaceIndex = 3 ResourceDevice = "device" ResourceTypeTwinEdgeUpdated = "twin/edge_updated" @@ -128,11 +128,11 @@ func BuildResourceForDevice(nodeID, resourceType, resourceID string) (resource s return } -// GetDeviceID returns the ID of the device +// GetDeviceID returns the ID of the device,resource's format:$hw/events/device/{namespace}/{deviceName} func GetDeviceID(resource string) (string, error) { res := strings.Split(resource, "/") - if len(res) >= ResourceDeviceIDIndex+1 && res[ResourceDeviceIndex] == ResourceDevice { - return res[ResourceDeviceIDIndex], nil + if len(res) >= ResourceDeviceNamespaceIndex+2 && res[ResourceDeviceIndex] == ResourceDevice { + return res[ResourceDeviceNamespaceIndex] + "/" + res[ResourceDeviceNamespaceIndex+1], nil } return "", errors.New("failed to get device id") } diff --git a/cloud/pkg/common/messagelayer/util_test.go b/cloud/pkg/common/messagelayer/util_test.go index b82e23182..d1bf278df 100644 --- a/cloud/pkg/common/messagelayer/util_test.go +++ b/cloud/pkg/common/messagelayer/util_test.go @@ -91,15 +91,15 @@ func TestGetDeviceID(t *testing.T) { { name: "TestGetDeviceID(): Case 1: success", args: args{ - resource: fmt.Sprintf("node/%s/%s/%s", "nid", ResourceDevice, "did"), + resource: fmt.Sprintf("node/%s/%s/%s/%s", "nid", ResourceDevice, "ns", "did"), }, - want: "did", + want: "ns/did", wantErr: nil, }, { - name: "TestGetDeviceID(): Case 2: length less then 4", + name: "TestGetDeviceID(): Case 2: length less then 5", args: args{ - resource: fmt.Sprintf("node/%s/%s", "nid", ResourceDevice), + resource: fmt.Sprintf("node/%s/%s/%s", "nid", ResourceDevice, "ns"), }, want: "", wantErr: fmt.Errorf("failed to get device id"), |
