-
Notifications
You must be signed in to change notification settings - Fork 4
Added delete_engine_wait function that completes once an engine is completely deleted #129
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
8fefdc1
9a3fe7d
612288d
9ac5df6
8e83614
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -112,6 +112,7 @@ class Permission(str, Enum): | |
| "create_oauth_client", | ||
| "delete_database", | ||
| "delete_engine", | ||
| "delete_engine_wait", | ||
| "delete_model", | ||
| "disable_user", | ||
| "enable_user", | ||
|
|
@@ -134,8 +135,13 @@ class Permission(str, Enum): | |
| "list_oauth_clients", | ||
| "load_csv", | ||
| "update_user", | ||
| "ResourceNotFoundError", | ||
| ] | ||
|
|
||
| class ResourceNotFoundError(Exception): | ||
| """An error response, typically triggered by a 412 response (for update) or 404 (for get/post)""" | ||
| pass | ||
|
|
||
|
|
||
| # Context contains the state required to make rAI API calls. | ||
| class Context(rest.Context): | ||
|
|
@@ -221,11 +227,15 @@ def _get_resource(ctx: Context, path: str, key=None, **kwargs) -> Dict: | |
| url = _mkurl(ctx, path) | ||
| rsp = rest.get(ctx, url, **kwargs) | ||
| rsp = json.loads(rsp.read()) | ||
|
|
||
| if key: | ||
| rsp = rsp[key] | ||
| if rsp and isinstance(rsp, list): | ||
| assert len(rsp) == 1 | ||
|
|
||
| if isinstance(rsp, list): | ||
| if len(rsp) == 0: | ||
| raise ResourceNotFoundError(f"Resource not found at {url}") | ||
| return rsp[0] | ||
|
|
||
| return rsp | ||
|
|
||
|
|
||
|
|
@@ -356,8 +366,8 @@ def poll_with_specified_overhead( | |
| time.sleep(duration) | ||
|
|
||
|
|
||
| def is_engine_term_state(state: str) -> bool: | ||
| return state == "PROVISIONED" or ("FAILED" in state) | ||
| def is_engine_term_state(state: str, targetState: str) -> bool: | ||
|
||
| return state == targetState or ("FAILED" in state) | ||
|
|
||
|
|
||
| def create_engine(ctx: Context, engine: str, size: str = "XS", **kwargs): | ||
|
|
@@ -370,7 +380,7 @@ def create_engine(ctx: Context, engine: str, size: str = "XS", **kwargs): | |
| def create_engine_wait(ctx: Context, engine: str, size: str = "XS", **kwargs): | ||
| create_engine(ctx, engine, size, **kwargs) | ||
| poll_with_specified_overhead( | ||
| lambda: is_engine_term_state(get_engine(ctx, engine)["state"]), | ||
| lambda: is_engine_term_state(get_engine(ctx, engine)["state"], "PROVISIONED"), | ||
| overhead_rate=0.2, | ||
| timeout=30 * 60, | ||
| ) | ||
|
|
@@ -416,6 +426,18 @@ def delete_engine(ctx: Context, engine: str, **kwargs) -> Dict: | |
| return json.loads(rsp.read()) | ||
|
|
||
|
|
||
| def delete_engine_wait(ctx: Context, engine: str, **kwargs) -> bool: | ||
| rsp = delete_engine(ctx, engine, **kwargs) | ||
| rsp = rsp["status"] | ||
|
|
||
| while not is_engine_term_state(rsp["state"], "DELETED"): | ||
| try: | ||
| rsp = get_engine(ctx, engine) | ||
| except ResourceNotFoundError: | ||
| break | ||
| time.sleep(3) | ||
|
|
||
|
|
||
| def delete_user(ctx: Context, id: str, **kwargs) -> Dict: | ||
| url = _mkurl(ctx, f"{PATH_USER}/{id}") | ||
| rsp = rest.delete(ctx, url, None, **kwargs) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is it correct to say that historically an assertion error would be thrown instead of ResourceNotFound? (If so its good, just want to make sure we minor-version bump since surface area is changing)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nope, the assert was there to check if for some reason we get two resources with the same name. I don't think it was useful since the name is required to be unique for any resource. But even if we do get two resources with the same name for some reason the assert wouldn't do any good for the user as the user would just get confused.
The change is backward incompatible, previously the function would return an empty list if a requested resource not found. So any method like get_engine or get_database would return an empty list when we can't find a resource with the provided name. Ideally, our rest api should handle this and give 404 but since we don't have a rest api for it I emulate this behavior by throwing ResourceNotFoundError.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
got it, so we're replacing empty list with an error condition to match what a user of a rest api would expect (in this case that they get 1 resource and instead that resource doesn't exist)