betodealmeida commented on a change in pull request #14721:
URL: https://github.com/apache/superset/pull/14721#discussion_r637165291



##########
File path: superset/databases/commands/validate.py
##########
@@ -83,7 +83,8 @@ def run(self) -> None:
 
         # try to connect
         sqlalchemy_uri = engine_spec.build_sqlalchemy_uri(
-            self._properties["parameters"]  # type: ignore
+            self._properties["parameters"],  # type: ignore
+            self._properties.get("encrypted_extra", "{}"),

Review comment:
       We need to deserialize this:
   
   ```python
   serialized_encrypted_extra = self._properties.get("encrypted_extra", "{}")
   try:
       encrypted_extra = json.loads(serialized_encrypted_extra)
   except json.decoder.JSONDecodeError:
       encrypted_extra = {}
   ```
   
   Then:
   
   ```python
   sqlalchemy_uri = engine_spec.build_sqlalchemy_uri(
       self._properties["parameters"],
       encrypted_extra,
   )
   ```
   
   You can then pass `serialized_encrypted_extra` on line 95 below.

##########
File path: superset/databases/schemas.py
##########
@@ -246,6 +246,8 @@ def build_sqlalchemy_uri(
         the constructed SQLAlchemy URI to be passed.
         """
         parameters = data.pop("parameters", None)
+        encrypted_extra = data.get("encrypted_extra", None)

Review comment:
       Same here, we need to try to deserialize from JSON like above.

##########
File path: superset/db_engine_specs/bigquery.py
##########
@@ -38,6 +43,28 @@
     + "permission in project (?P<project>.+?)"
 )
 
+ma_plugin = MarshmallowPlugin()
+
+
+class EncryptedField(fields.String):
+    pass
+
+
+class BigQueryParametersSchema(Schema):
+    credentials_info = EncryptedField(description="credentials.json file for 
BigQuery")
+
+
+def encrypted_field_properties(self, field: Any, **_) -> Dict[str, Any]:  # 
type: ignore
+    ret = {}
+    if isinstance(field, EncryptedField):
+        if self.openapi_version.major > 2:
+            ret["x-encrypted-extra"] = True
+    return ret

Review comment:
       Let's move this and `EncryptedField` outside, since other specs will 
also use them. We can have them in `superset/databases/schemas.py`.

##########
File path: superset/db_engine_specs/bigquery.py
##########
@@ -38,6 +43,28 @@
     + "permission in project (?P<project>.+?)"
 )
 
+ma_plugin = MarshmallowPlugin()
+
+
+class EncryptedField(fields.String):
+    pass
+
+
+class BigQueryParametersSchema(Schema):
+    credentials_info = EncryptedField(description="credentials.json file for 
BigQuery")
+
+
+def encrypted_field_properties(self, field: Any, **_) -> Dict[str, Any]:  # 
type: ignore
+    ret = {}
+    if isinstance(field, EncryptedField):
+        if self.openapi_version.major > 2:
+            ret["x-encrypted-extra"] = True
+    return ret
+
+
+class BigQueryParametersType(TypedDict):
+    pass

Review comment:
       This now has `credentials_info`
   
   ```suggestion
       credentials_info: Dict[str, Any]
   ```

##########
File path: superset/db_engine_specs/bigquery.py
##########
@@ -282,3 +313,40 @@ def df_to_sql(
                 to_gbq_kwargs[key] = to_sql_kwargs[key]
 
         pandas_gbq.to_gbq(df, **to_gbq_kwargs)
+
+    @classmethod
+    def build_sqlalchemy_uri(
+        cls, _: BigQueryParametersType, encrypted_extra: Optional[Dict[str, 
str]] = None
+    ) -> str:
+        if encrypted_extra:
+            project_id = encrypted_extra.get("project_id")
+            return f"{cls.drivername}://{project_id}"
+
+        raise SupersetGenericDBErrorException(
+            message="Big Query encrypted_extra is not available",
+        )
+
+    @classmethod
+    def get_parameters_from_uri(cls, _: str) -> Any:
+        # BigQuery doesn't have parameters
+        return None

Review comment:
       Ideally:
   
   ```
   get_parameters_from_uri(build_sqlalchemy_uri(parameters)) == parameters
   ```
   
   Ie, this should be the opposite of `build_sqlalchemy_uri`.
   
   We need to return `credentials_info` here so that the edit form can show it 
to the user. This means that we need to modify `get_parameters_from_uri` to 
also receive `encrypted_extra`.

##########
File path: superset/databases/api.py
##########
@@ -909,11 +909,13 @@ def available(self) -> Response:
                 "preferred": engine_spec.engine in preferred_databases,
             }
 
-            if issubclass(engine_spec, BasicParametersMixin):
-                payload["parameters"] = engine_spec.parameters_json_schema()
+            if hasattr(engine_spec, "parameters_json_schema") or hasattr(

Review comment:
       This looks great!




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to