Skip to content

Commit

Permalink
cvlib: Add workspace check to tags functionality
Browse files Browse the repository at this point in the history
Change tags logic to check for workspace presence in the
passed context to the methods. This will allow for Studio
Build Hook actions running in workspace level to access
tags helpers.

Closes=Bug843488

Change-Id: Ia1739b115067c31c39c4f5bee824ce64fcfdc69c
  • Loading branch information
cianmcgrath committed Sep 12, 2023
1 parent 533c658 commit d2f8649
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
6 changes: 6 additions & 0 deletions cloudvision/cvlib/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,12 @@ def initializeStudioCtxFromArgs(self):
buildId=buildId
)

def getWorkspaceId(self):
if not (self.workspace or self.studio):
raise InvalidContextException(("Context does not have a workspace or studio "
"associated with it"))
return self.workspace.id if self.workspace else self.studio.workspaceId

def Get(self, path: List[str], keys: List[str] = [], dataset: str = "analytics"):
'''
Get issues a get request to the provided path/key(s) combo, and returns the contents
Expand Down
15 changes: 9 additions & 6 deletions cloudvision/cvlib/tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
# Use of this source code is governed by the Apache License 2.0
# that can be found in the COPYING file.

from collections.abc import Callable
from typing import Dict, List, Optional
from typing import Dict

from arista.tag.v2.services import (
TagAssignmentServiceStub,
Expand Down Expand Up @@ -32,6 +31,7 @@ class Tag:
'''
Object that represents a tag
'''

def __init__(self, label: str, value: str):
self._label = label if label else ''
self._value = value if value else ''
Expand Down Expand Up @@ -153,7 +153,7 @@ def _getTagUpdatesFromWorkspace(self):
tagRequest = TagAssignmentConfigStreamRequest()
tagFilter = TagAssignmentConfig()
tagFilter.key.element_type = ELEMENT_TYPE_DEVICE
tagFilter.key.workspace_id.value = self.ctx.studio.workspaceId
tagFilter.key.workspace_id.value = self.ctx.getWorkspaceId()
tagRequest.partial_eq_filter.append(tagFilter)
for resp in tagClient.GetAll(tagRequest):
workspaceTagUpdates.append((resp.value.key.device_id.value,
Expand All @@ -173,7 +173,8 @@ def _assignDeviceTagSet(self, deviceId: str, label: str, value: str):
self._createTag(ELEMENT_TYPE_DEVICE, label, value)
# assign the tag
setRequest = TagAssignmentConfigSetRequest()
setRequest.value.key.workspace_id.value = self.ctx.studio.workspaceId
wsID = self.ctx.getWorkspaceId()
setRequest.value.key.workspace_id.value = wsID
setRequest.value.key.element_type = ELEMENT_TYPE_DEVICE
setRequest.value.key.label.value = label
setRequest.value.key.value.value = value
Expand Down Expand Up @@ -227,7 +228,8 @@ def _createTag(self, etype: int, label: str, value: str):
return
# create the tag
setRequest = TagConfigSetRequest()
setRequest.value.key.workspace_id.value = self.ctx.studio.workspaceId
wsID = self.ctx.getWorkspaceId()
setRequest.value.key.workspace_id.value = wsID
setRequest.value.key.element_type = etype
setRequest.value.key.label.value = label
setRequest.value.key.value.value = value
Expand Down Expand Up @@ -264,7 +266,8 @@ def _unassignDeviceTag(self, deviceId: str, label: str, value: str):
return
# unassign the tag
setRequest = TagAssignmentConfigSetRequest()
setRequest.value.key.workspace_id.value = self.ctx.studio.workspaceId
wsID = self.ctx.getWorkspaceId()
setRequest.value.key.workspace_id.value = wsID
setRequest.value.key.element_type = ELEMENT_TYPE_DEVICE
setRequest.value.key.label.value = label
setRequest.value.key.value.value = value
Expand Down
6 changes: 6 additions & 0 deletions test/cvlib/tags/test_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ def __init__(self):
self.workspaceId = "workspace1"


class mockWorkspace:
def __init__(self):
self.id = "1"


class mockClient:
def __init__(self):
self.stub = None
Expand Down Expand Up @@ -91,6 +96,7 @@ def __init__(self):
super().__init__('user')
self.client = mockClient()
self.studio = mockStudio()
self.workspace = mockWorkspace()
self.device = Device()

def getApiClient(self, stub):
Expand Down

0 comments on commit d2f8649

Please sign in to comment.