|
19 | 19 | from dataclasses import dataclass |
20 | 20 | from typing import Dict, List |
21 | 21 |
|
22 | | -from pydantic import StrictStr |
| 22 | +from pydantic import StrictStr, SecretStr |
23 | 23 |
|
24 | 24 | from cli.command import Command |
25 | | -from cli.constants import StorageType, CatalogType, Subcommands, Arguments |
| 25 | +from cli.constants import StorageType, CatalogType, CatalogConnectionType, Subcommands, Arguments, AuthenticationType, \ |
| 26 | + ServiceIdentityType |
26 | 27 | from cli.options.option_tree import Argument |
27 | | -from polaris.management import ( |
28 | | - PolarisDefaultApi, |
29 | | - CreateCatalogRequest, |
30 | | - UpdateCatalogRequest, |
31 | | - StorageConfigInfo, |
32 | | - ExternalCatalog, |
33 | | - AwsStorageConfigInfo, |
34 | | - AzureStorageConfigInfo, |
35 | | - GcpStorageConfigInfo, |
36 | | - PolarisCatalog, |
37 | | - CatalogProperties, |
38 | | -) |
| 28 | +from polaris.management import PolarisDefaultApi, CreateCatalogRequest, UpdateCatalogRequest, \ |
| 29 | + StorageConfigInfo, ExternalCatalog, AwsStorageConfigInfo, AzureStorageConfigInfo, GcpStorageConfigInfo, \ |
| 30 | + PolarisCatalog, CatalogProperties, BearerAuthenticationParameters, \ |
| 31 | + OAuthClientCredentialsParameters, SigV4AuthenticationParameters, HadoopConnectionConfigInfo, \ |
| 32 | + IcebergRestConnectionConfigInfo, AwsIamServiceIdentityInfo |
39 | 33 |
|
40 | 34 |
|
41 | 35 | @dataclass |
@@ -68,19 +62,56 @@ class CatalogsCommand(Command): |
68 | 62 | properties: Dict[str, StrictStr] |
69 | 63 | set_properties: Dict[str, StrictStr] |
70 | 64 | remove_properties: List[str] |
| 65 | + hadoop_warehouse: str |
| 66 | + iceberg_remote_catalog_name: str |
| 67 | + catalog_connection_type: str |
| 68 | + catalog_authentication_type: str |
| 69 | + catalog_service_identity_type: str |
| 70 | + catalog_service_identity_iam_arn: str |
| 71 | + catalog_uri: str |
| 72 | + catalog_token_uri: str |
| 73 | + catalog_client_id: str |
| 74 | + catalog_client_secret: str |
| 75 | + catalog_client_scopes: List[str] |
| 76 | + catalog_bearer_token: str |
| 77 | + catalog_role_arn: str |
| 78 | + catalog_role_session_name: str |
| 79 | + catalog_external_id: str |
| 80 | + catalog_signing_region: str |
| 81 | + catalog_signing_name: str |
71 | 82 |
|
72 | 83 | def validate(self): |
73 | 84 | if self.catalogs_subcommand == Subcommands.CREATE: |
74 | | - if not self.storage_type: |
75 | | - raise Exception( |
76 | | - f"Missing required argument:" |
77 | | - f" {Argument.to_flag_name(Arguments.STORAGE_TYPE)}" |
78 | | - ) |
79 | | - if not self.default_base_location: |
80 | | - raise Exception( |
81 | | - f"Missing required argument:" |
82 | | - f" {Argument.to_flag_name(Arguments.DEFAULT_BASE_LOCATION)}" |
83 | | - ) |
| 85 | + if self.catalog_type != CatalogType.EXTERNAL.value: |
| 86 | + if not self.storage_type: |
| 87 | + raise Exception(f'Missing required argument:' |
| 88 | + f' {Argument.to_flag_name(Arguments.STORAGE_TYPE)}') |
| 89 | + if not self.default_base_location: |
| 90 | + raise Exception(f'Missing required argument:' |
| 91 | + f' {Argument.to_flag_name(Arguments.DEFAULT_BASE_LOCATION)}') |
| 92 | + else: |
| 93 | + if self.catalog_authentication_type == AuthenticationType.OAUTH.value: |
| 94 | + if not self.catalog_token_uri or not self.catalog_client_id \ |
| 95 | + or not self.catalog_client_secret or len(self.catalog_client_scopes) == 0: |
| 96 | + raise Exception(f"Authentication type 'OAUTH' requires" |
| 97 | + f" {Argument.to_flag_name(Arguments.CATALOG_TOKEN_URI)}," |
| 98 | + f" {Argument.to_flag_name(Arguments.CATALOG_CLIENT_ID)}," |
| 99 | + f" {Argument.to_flag_name(Arguments.CATALOG_CLIENT_SECRET)}," |
| 100 | + f" and at least one {Argument.to_flag_name(Arguments.CATALOG_CLIENT_SCOPE)}.") |
| 101 | + elif self.catalog_authentication_type == AuthenticationType.BEARER.value: |
| 102 | + if not self.catalog_bearer_token: |
| 103 | + raise Exception(f"Missing required argument for authentication type 'BEARER':" |
| 104 | + f" {Argument.to_flag_name(Arguments.CATALOG_BEARER_TOKEN)}") |
| 105 | + elif self.catalog_authentication_type == AuthenticationType.SIGV4.value: |
| 106 | + if not self.catalog_role_arn or not self.catalog_signing_region: |
| 107 | + raise Exception(f"Authentication type 'SIGV4 requires" |
| 108 | + f" {Argument.to_flag_name(Arguments.CATALOG_ROLE_ARN)}" |
| 109 | + f" and {Argument.to_flag_name(Arguments.CATALOG_SIGNING_REGION)}") |
| 110 | + |
| 111 | + if self.catalog_service_identity_type == ServiceIdentityType.AWS_IAM.value: |
| 112 | + if not self.catalog_service_identity_iam_arn: |
| 113 | + raise Exception(f"Missing required argument for service identity type 'AWS_IAM':" |
| 114 | + f" {Argument.to_flag_name(Arguments.CATALOG_SERVICE_IDENTITY_IAM_ARN)}") |
84 | 115 |
|
85 | 116 | if self.storage_type == StorageType.S3.value: |
86 | 117 | if not self.role_arn: |
@@ -166,31 +197,94 @@ def _build_storage_config_info(self): |
166 | 197 | ) |
167 | 198 | return config |
168 | 199 |
|
| 200 | + def _build_connection_config_info(self): |
| 201 | + if self.catalog_type != CatalogType.EXTERNAL.value: |
| 202 | + return None |
| 203 | + |
| 204 | + auth_params = None |
| 205 | + if self.catalog_authentication_type == AuthenticationType.OAUTH.value: |
| 206 | + auth_params = OAuthClientCredentialsParameters( |
| 207 | + authentication_type=self.catalog_authentication_type.upper(), |
| 208 | + token_uri=self.catalog_token_uri, |
| 209 | + client_id=self.catalog_client_id, |
| 210 | + client_secret=SecretStr(self.catalog_client_secret), |
| 211 | + scopes=self.catalog_client_scopes |
| 212 | + ) |
| 213 | + elif self.catalog_authentication_type == AuthenticationType.BEARER.value: |
| 214 | + auth_params = BearerAuthenticationParameters( |
| 215 | + authentication_type=self.catalog_authentication_type.upper(), |
| 216 | + bearer_token=SecretStr(self.catalog_bearer_token) |
| 217 | + ) |
| 218 | + elif self.catalog_authentication_type == AuthenticationType.SIGV4.value: |
| 219 | + auth_params = SigV4AuthenticationParameters( |
| 220 | + authentication_type=self.catalog_authentication_type.upper(), |
| 221 | + role_arn=self.catalog_role_arn, |
| 222 | + role_session_name=self.catalog_role_session_name, |
| 223 | + external_id=self.catalog_external_id, |
| 224 | + signing_region=self.catalog_signing_region, |
| 225 | + signing_name=self.catalog_signing_name, |
| 226 | + ) |
| 227 | + elif self.catalog_authentication_type is not None: |
| 228 | + raise Exception("Unknown authentication type:", self.catalog_authentication_type) |
| 229 | + |
| 230 | + service_identity = None |
| 231 | + if self.catalog_service_identity_type == ServiceIdentityType.AWS_IAM: |
| 232 | + service_identity = AwsIamServiceIdentityInfo( |
| 233 | + identity_type=self.catalog_service_identity_type.upper(), |
| 234 | + iam_arn=self.catalog_service_identity_iam_arn |
| 235 | + ) |
| 236 | + elif self.catalog_service_identity_type is not None: |
| 237 | + raise Exception("Unknown service identity type:", self.catalog_service_identity_type) |
| 238 | + |
| 239 | + config = None |
| 240 | + if self.catalog_connection_type == CatalogConnectionType.HADOOP.value: |
| 241 | + config = HadoopConnectionConfigInfo( |
| 242 | + connection_type=self.catalog_connection_type.upper(), |
| 243 | + uri=self.catalog_uri, |
| 244 | + authentication_parameters=auth_params, |
| 245 | + service_identity=service_identity, |
| 246 | + warehouse=self.hadoop_warehouse |
| 247 | + ) |
| 248 | + elif self.catalog_connection_type == CatalogConnectionType.ICEBERG.value: |
| 249 | + config = IcebergRestConnectionConfigInfo( |
| 250 | + connection_type=self.catalog_connection_type.upper().replace('-', '_'), |
| 251 | + uri=self.catalog_uri, |
| 252 | + authentication_parameters=auth_params, |
| 253 | + service_identity=service_identity, |
| 254 | + remote_catalog_name=self.iceberg_remote_catalog_name |
| 255 | + ) |
| 256 | + elif self.catalog_connection_type is not None: |
| 257 | + raise Exception("Unknown catalog connection type:", self.catalog_connection_type) |
| 258 | + return config |
| 259 | + |
169 | 260 | def execute(self, api: PolarisDefaultApi) -> None: |
170 | 261 | if self.catalogs_subcommand == Subcommands.CREATE: |
171 | | - config = self._build_storage_config_info() |
| 262 | + storage_config = self._build_storage_config_info() |
| 263 | + connection_config = self._build_connection_config_info() |
172 | 264 | if self.catalog_type == CatalogType.EXTERNAL.value: |
173 | 265 | request = CreateCatalogRequest( |
174 | 266 | catalog=ExternalCatalog( |
175 | 267 | type=self.catalog_type.upper(), |
176 | 268 | name=self.catalog_name, |
177 | | - storage_config_info=config, |
| 269 | + storage_config_info=storage_config, |
178 | 270 | properties=CatalogProperties( |
179 | 271 | default_base_location=self.default_base_location, |
180 | | - additional_properties=self.properties, |
| 272 | + additional_properties=self.properties |
181 | 273 | ), |
| 274 | + connection_config_info=connection_config |
182 | 275 | ) |
183 | 276 | ) |
184 | 277 | else: |
185 | 278 | request = CreateCatalogRequest( |
186 | 279 | catalog=PolarisCatalog( |
187 | 280 | type=self.catalog_type.upper(), |
188 | 281 | name=self.catalog_name, |
189 | | - storage_config_info=config, |
| 282 | + storage_config_info=storage_config, |
190 | 283 | properties=CatalogProperties( |
191 | 284 | default_base_location=self.default_base_location, |
192 | | - additional_properties=self.properties, |
| 285 | + additional_properties=self.properties |
193 | 286 | ), |
| 287 | + connection_config_info=connection_config |
194 | 288 | ) |
195 | 289 | ) |
196 | 290 | api.create_catalog(request) |
|
0 commit comments