Skip to content

Commit

Permalink
Add Beaker.workspace.list() method
Browse files Browse the repository at this point in the history
  • Loading branch information
epwalsh committed Apr 11, 2022
1 parent 4f4a0c4 commit 6c035ea
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added `Beaker.workspace.datasets()`.
- Added `Beaker.workspace.experiments()`.
- Added `Beaker.workspace.images()`.
- Added `Beaker.workspace.list()`.

### Removed

Expand Down
25 changes: 15 additions & 10 deletions beaker/data_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -671,16 +671,6 @@ def _validate_datetime(cls, v: Optional[datetime]) -> Optional[datetime]:
return v


class DatasetsPage(BaseModel):
data: List[Dataset]
next_cursor: Optional[str] = None


class ExperimentsPage(BaseModel):
data: List[Experiment]
next_cursor: Optional[str] = None


class DatasetStorageInfo(BaseModel):
id: str
created: Optional[datetime] = None
Expand Down Expand Up @@ -736,3 +726,18 @@ def _validate_datetime(cls, v: Optional[datetime]) -> Optional[datetime]:
class ImagesPage(BaseModel):
data: List[Image]
next_cursor: Optional[str] = None


class DatasetsPage(BaseModel):
data: List[Dataset]
next_cursor: Optional[str] = None


class ExperimentsPage(BaseModel):
data: List[Experiment]
next_cursor: Optional[str] = None


class WorkspacePage(BaseModel):
data: List[Workspace]
next_cursor: Optional[str] = None
52 changes: 50 additions & 2 deletions beaker/services/workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,62 @@ def ensure(self, workspace: str):
raise ValueError(f"Invalided workspace name '{workspace}'")
self.request("workspaces", method="POST", data={"name": name, "org": org})

def list(self, match: Optional[str] = None) -> List[Workspace]:
def list(
self,
org: Union[str, Organization],
author: Optional[Union[str, Account]] = None,
match: Optional[str] = None,
archived: Optional[bool] = None,
limit: Optional[int] = None,
) -> List[Workspace]:
"""
List workspaces belonging to your account.
List workspaces belonging to an organization.
:param org: The organization name or object.
:param author: Only list workspaces authored by this account.
:param match: Only include workspaces matching the text.
:param archived: Only include/exclude archived workspaces.
:param limit: Limit the number of workspaces returned.
:raises OrganizationNotFound: If the organization doesn't exist.
:raises AccountNotFound: If the author account doesn't exist.
:raises HTTPError: Any other HTTP exception that can occur.
"""
organization_id = (
org.id if isinstance(org, Organization) else self.beaker.organization.get(org).id
)
workspaces: List[Workspace] = []
cursor: Optional[str] = None
query: Dict[str, str] = {"org": organization_id}
if author is not None:
query["author"] = (
author.name if isinstance(author, Account) else self.beaker.account.get(author).name
)
if match is not None:
query["q"] = match
if archived is not None:
query["archived"] = str(archived).lower()
if limit:
query["limit"] = str(limit)

while True:
query["cursor"] = cursor or ""
page = WorkspacePage.from_json(
self.request(
"workspaces",
method="GET",
query=query,
).json()
)
workspaces.extend(page.data)
cursor = page.next_cursor
if not cursor:
break
if limit and len(workspaces) >= limit:
workspaces = workspaces[:limit]
break

return workspaces

def images(
self,
Expand Down
5 changes: 5 additions & 0 deletions tests/workspace_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,8 @@ def test_workspace_experiments(client: Beaker, hello_world_experiment_name: str)
def test_workspace_images(client: Beaker, hello_world_image_name: str):
images = client.workspace.images(match=hello_world_image_name)
assert images


def test_workspace_list(client: Beaker, workspace_name: str):
workspaces = client.workspace.list("ai2", match=workspace_name.split("/")[1])
assert workspaces

0 comments on commit 6c035ea

Please sign in to comment.