Skip to content

Commit

Permalink
fix: added regex match to set workspace method (#2427)
Browse files Browse the repository at this point in the history
# Description

I re-used the logic for the dataset regex check for the workspace
declaration too.

Closes #2388 
**Type of change**

(Please delete options that are not relevant. Remember to title the PR
according to the type of change)

- [X] Bug fix (non-breaking change which fixes an issue)


**How Has This Been Tested**

(Please describe the tests that you ran to verify your changes. And
ideally, reference `tests`)

- [X] [Test
A](https://github.com/argilla-io/argilla/blob/5138961e4148749dca85e647fa32d8c9a13f87f2/tests/client/test_api.py#L97)


**Checklist**
  • Loading branch information
davidberenstein1957 authored Mar 2, 2023
1 parent f5834a5 commit d789fa1
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/argilla/_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@
_OLD_API_KEY_HEADER_NAME = "X-Rubrix-Api-Key"
_OLD_WORKSPACE_HEADER_NAME = "X-Rubrix-Workspace"

DATASET_NAME_REGEX_PATTERN = r"^(?!-|_)[a-z0-9-_]+$"
ES_INDEX_REGEX_PATTERN = r"^(?!-|_)[a-z0-9-_]+$"
17 changes: 13 additions & 4 deletions src/argilla/client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@
import re
import warnings
from asyncio import Future
from typing import Any, Dict, Iterable, List, Optional, Tuple, Type, Union
from typing import Any, Dict, Iterable, List, Optional, Tuple, Union

from rich import print as rprint
from rich.progress import Progress

from argilla._constants import (
_OLD_WORKSPACE_HEADER_NAME,
DATASET_NAME_REGEX_PATTERN,
DEFAULT_API_KEY,
ES_INDEX_REGEX_PATTERN,
WORKSPACE_HEADER_NAME,
)
from argilla.client.apis.datasets import Datasets
Expand Down Expand Up @@ -208,6 +208,15 @@ def set_workspace(self, workspace: str):
if not workspace:
raise Exception("Must provide a workspace")

if not re.match(ES_INDEX_REGEX_PATTERN, workspace):
raise InputValueError(
f"Provided workspace name {workspace} does not match the pattern"
f" {ES_INDEX_REGEX_PATTERN}. Please, use a valid name for your"
" workspace. This limitation is caused by naming conventions for indexes"
" in Elasticsearch. If applicable, you can try to lowercase the name of your workspace."
" https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-create-index.html"
)

if workspace != self.get_workspace():
if workspace == self._user.username:
self._client.headers.pop(WORKSPACE_HEADER_NAME, workspace)
Expand Down Expand Up @@ -326,10 +335,10 @@ async def log_async(
if not name:
raise InputValueError("Empty dataset name has been passed as argument.")

if not re.match(DATASET_NAME_REGEX_PATTERN, name):
if not re.match(ES_INDEX_REGEX_PATTERN, name):
raise InputValueError(
f"Provided dataset name {name} does not match the pattern"
f" {DATASET_NAME_REGEX_PATTERN}. Please, use a valid name for your"
f" {ES_INDEX_REGEX_PATTERN}. Please, use a valid name for your"
" dataset. This limitation is caused by naming conventions for indexes"
" in Elasticsearch."
" https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-create-index.html"
Expand Down
4 changes: 2 additions & 2 deletions src/argilla/server/apis/v0/models/commons/params.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@

from argilla._constants import (
_OLD_WORKSPACE_HEADER_NAME,
DATASET_NAME_REGEX_PATTERN,
ES_INDEX_REGEX_PATTERN,
WORKSPACE_HEADER_NAME,
)
from argilla.server.security.model import WORKSPACE_NAME_PATTERN

DATASET_NAME_PATH_PARAM = Path(..., regex=DATASET_NAME_REGEX_PATTERN, description="The dataset name")
DATASET_NAME_PATH_PARAM = Path(..., regex=ES_INDEX_REGEX_PATTERN, description="The dataset name")


@dataclass
Expand Down
4 changes: 2 additions & 2 deletions src/argilla/server/apis/v0/models/datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

from pydantic import BaseModel, Field

from argilla._constants import DATASET_NAME_REGEX_PATTERN
from argilla._constants import ES_INDEX_REGEX_PATTERN
from argilla.server.commons.models import TaskType
from argilla.server.services.datasets import ServiceBaseDataset

Expand All @@ -43,7 +43,7 @@ class UpdateDatasetRequest(BaseModel):


class _BaseDatasetRequest(UpdateDatasetRequest):
name: str = Field(regex=DATASET_NAME_REGEX_PATTERN, description="The dataset name")
name: str = Field(regex=ES_INDEX_REGEX_PATTERN, description="The dataset name")


class CreateDatasetRequest(_BaseDatasetRequest):
Expand Down
4 changes: 2 additions & 2 deletions src/argilla/server/daos/models/datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@

from pydantic import BaseModel, Field, validator

from argilla._constants import DATASET_NAME_REGEX_PATTERN
from argilla._constants import ES_INDEX_REGEX_PATTERN
from argilla.server.commons.models import TaskType


class BaseDatasetDB(BaseModel):
name: str = Field(regex=DATASET_NAME_REGEX_PATTERN)
name: str = Field(regex=ES_INDEX_REGEX_PATTERN)
task: TaskType
owner: Optional[str] = Field(description="Deprecated. Use `workspace` instead. Will be removed in v1.5.0")
workspace: Optional[str] = None
Expand Down
6 changes: 3 additions & 3 deletions src/argilla/server/security/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

from pydantic import BaseModel, Field, root_validator, validator

from argilla._constants import DATASET_NAME_REGEX_PATTERN
from argilla._constants import ES_INDEX_REGEX_PATTERN
from argilla.server.errors import EntityNotFoundError

WORKSPACE_NAME_PATTERN = re.compile(r"^[a-zA-Z0-9][a-zA-Z0-9_\-]*$")
Expand All @@ -37,9 +37,9 @@ class User(BaseModel):

@validator("username")
def check_username(cls, value):
if not re.compile(DATASET_NAME_REGEX_PATTERN).match(value):
if not re.compile(ES_INDEX_REGEX_PATTERN).match(value):
raise ValueError(
"Wrong username. " f"The username {value} does not match the pattern {DATASET_NAME_REGEX_PATTERN}"
"Wrong username. " f"The username {value} does not match the pattern {ES_INDEX_REGEX_PATTERN}"
)
return value

Expand Down
5 changes: 5 additions & 0 deletions tests/client/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ def mock_get(*args, **kwargs):
monkeypatch.setattr(users_api, "whoami", mock_get)


def test_init_uppercase_workspace(mocked_client):
with pytest.raises(InputValueError):
api.init(workspace="UPPERCASE_WORKSPACE")


def test_init_correct(mock_response_200):
"""Testing correct default initialization
Expand Down

0 comments on commit d789fa1

Please sign in to comment.