forked from apache/polaris
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SNOW-1527866: [3/x] Improvements to the Polaris CLI (apache#21)
<!-- Please describe your change here and remove this comment --> This PR covers various improvements to the [recently merged](https://github.com/snowflakedb/managed-pull/7) Polaris CLI. Changes here include: * Added the `namespaces` command for creating, listing, and dropping namespaces with the CLI * Refactored error handling to reduce the proliferation of string literals * Added support for the `FILE` storage type * Added end-to-end regression tests for the CLI * Various usability and bug fixes * Support for the `CLIENT_ID` and `CLIENT_SECRET` environment variables * CLI documentation ## Pre-review checklist - [ ] I attest that this change meets the bar for low risk without security requirements as defined in the [Accelerated Risk Assessment Criteria](https://developer-handbook.m1.us-west-2.aws.app.snowflake.com/docs/reference/security-review/accelerated-risk-assessment/#eligibility) and I have taken the [Risk Assessment Training in Workday](https://wd5.myworkday.com/snowflake/learning/course/6c613806284a1001f111fedf3e4e0000). - Checking this checkbox is mandatory if using the [Accelerated Risk Assessment](https://developer-handbook.m1.us-west-2.aws.app.snowflake.com/docs/reference/security-review/accelerated-risk-assessment/) to risk assess the changes in this Pull Request. - If this change does not meet the bar for low risk without security requirements (as confirmed by the peer reviewers of this pull request) then a [formal Risk Assessment](https://developer-handbook.m1.us-west-2.aws.app.snowflake.com/docs/reference/security-review/risk-assessment/) must be completed. Please note that a formal Risk Assessment will require you to spend extra time performing a security review for this change. Please account for this extra time earlier rather than later to avoid unnecessary delays in the release process. - [ ] This change has code coverage for the new code added
- Loading branch information
1 parent
bb54423
commit e06642e
Showing
19 changed files
with
1,844 additions
and
157 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import json | ||
import re | ||
from dataclasses import dataclass | ||
from typing import Dict, Optional, List | ||
|
||
from pydantic import StrictStr | ||
|
||
from cli.command import Command | ||
from cli.constants import Subcommands, Arguments, UNIT_SEPARATOR | ||
from cli.options.option_tree import Argument | ||
from polaris.catalog import IcebergCatalogAPI, CreateNamespaceRequest, ApiClient, Configuration | ||
from polaris.catalog.exceptions import NotFoundException | ||
from polaris.management import PolarisDefaultApi | ||
|
||
|
||
@dataclass | ||
class NamespacesCommand(Command): | ||
""" | ||
A Command implementation to represent `polaris namespaces`. The instance attributes correspond to parameters | ||
that can be provided to various subcommands | ||
Example commands: | ||
* ./polaris namespaces create --catalog my_schema my_namespace | ||
* ./polaris namespaces list --catalog my_catalog | ||
* ./polaris namespaces delete --catalog my_catalog my_namespace.inner | ||
""" | ||
|
||
namespaces_subcommand: str | ||
catalog: str | ||
namespace: List[StrictStr] | ||
parent: List[StrictStr] | ||
location: str | ||
properties: Optional[Dict[str, StrictStr]] | ||
|
||
def validate(self): | ||
if not self.catalog: | ||
raise Exception(f'Missing required argument:' | ||
f' {Argument.to_flag_name(Arguments.CATALOG)}') | ||
|
||
def _get_catalog_api(self, api: PolarisDefaultApi): | ||
""" | ||
Convert a management API to a catalog API | ||
""" | ||
catalog_host = re.match(r'(http://[^/]+)', api.api_client.configuration.host).group(1) | ||
configuration = Configuration( | ||
host=f'{catalog_host}/api/catalog', | ||
username=api.api_client.configuration.username, | ||
password=api.api_client.configuration.password, | ||
access_token=api.api_client.configuration.access_token, | ||
) | ||
return IcebergCatalogAPI(ApiClient(configuration)) | ||
|
||
def execute(self, api: PolarisDefaultApi) -> None: | ||
catalog_api = self._get_catalog_api(api) | ||
if self.namespaces_subcommand == Subcommands.CREATE: | ||
properties = self.properties or {} | ||
if self.location: | ||
properties = {**properties, Arguments.LOCATION: self.location} | ||
request = CreateNamespaceRequest( | ||
namespace=self.namespace, | ||
properties=self.properties | ||
) | ||
catalog_api.create_namespace( | ||
prefix=self.catalog, | ||
create_namespace_request=request) | ||
elif self.namespaces_subcommand == Subcommands.LIST: | ||
if self.parent is not None: | ||
result = catalog_api.list_namespaces(prefix=self.catalog, parent=UNIT_SEPARATOR.join(self.parent)) | ||
else: | ||
result = catalog_api.list_namespaces(prefix=self.catalog) | ||
for namespace in result.namespaces: | ||
print(json.dumps({"namespace": '.'.join(namespace)})) | ||
elif self.namespaces_subcommand == Subcommands.DELETE: | ||
catalog_api.drop_namespace(prefix=self.catalog, namespace=UNIT_SEPARATOR.join(self.namespace)) | ||
elif self.namespaces_subcommand == Subcommands.GET: | ||
catalog_api.namespace_exists(prefix=self.catalog, namespace=UNIT_SEPARATOR.join(self.namespace)) | ||
print(json.dumps({"namespace": '.'.join(self.namespace)})) | ||
else: | ||
raise Exception(f"{self.namespaces_subcommand} is not supported in the CLI") |
Oops, something went wrong.