Skip to content

Commit

Permalink
ensure_workspace
Browse files Browse the repository at this point in the history
  • Loading branch information
epwalsh committed Dec 8, 2021
1 parent b679ab1 commit 1e49982
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed

- Fixed bug with `Config.save()`.
- Fixed bug with `create_image()` where it would fail if workspace doesn't exist yet.

## [v0.2.0](https://github.com/allenai/beaker-py/releases/tag/v0.2.0) - 2021-12-07

Expand Down
17 changes: 17 additions & 0 deletions beaker/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,16 @@ def get_workspace(self, workspace: Optional[str] = None) -> Dict[str, Any]:
exceptions_for_status={404: WorkspaceNotFound(workspace_name)},
).json()

def ensure_workspace(self, workspace: str):
"""
Ensure the given workspace exists.
"""
try:
self.get_workspace(workspace)
except WorkspaceNotFound:
org, name = workspace.split("/")
self.request("workspaces", method="POST", data={"name": name, "org": org})

def get_experiment(self, exp_id: str) -> Dict[str, Any]:
"""
Get info about an experiment.
Expand Down Expand Up @@ -134,6 +144,8 @@ def create_experiment(
workspace_name = workspace or self.config.default_workspace
if workspace_name is None:
raise ValueError("'workspace' argument required")
# Ensure workspace exists.
self.ensure_workspace(workspace_name)
return self.request(
f"workspaces/{urllib.parse.quote(workspace_name, safe='')}/experiments",
method="POST",
Expand Down Expand Up @@ -229,6 +241,11 @@ def create_image(
"""
workspace = workspace or self.config.default_workspace
if workspace is None:
raise ValueError("'workspace' argument required")

# Ensure workspace exists.
self.ensure_workspace(workspace)

# Get local Docker image object.
image = self.docker.images.get(image_tag)
Expand Down

0 comments on commit 1e49982

Please sign in to comment.