danarwix commented on a change in pull request #14639:
URL: https://github.com/apache/airflow/pull/14639#discussion_r591190438



##########
File path: airflow/providers/trino/hooks/trino.py
##########
@@ -0,0 +1,184 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+import os
+from typing import Any, Dict, Iterable, Optional
+
+import trino
+from trino.transaction import IsolationLevel
+
+from airflow import AirflowException
+from airflow.configuration import conf
+from airflow.hooks.dbapi import DbApiHook
+from airflow.models import Connection
+
+
+def _boolify(value):
+    if isinstance(value, bool):
+        return value
+    if isinstance(value, str):
+        if value.lower() == 'false':
+            return False
+        elif value.lower() == 'true':
+            return True
+    return value

Review comment:
       ```suggestion
   def _boolify(value):
         if value.lower() == 'false':
             return False
         elif value.lower() == 'true':
             return True
       return value
   ```

##########
File path: airflow/providers/trino/hooks/trino.py
##########
@@ -0,0 +1,184 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+import os
+from typing import Any, Dict, Iterable, Optional
+
+import trino
+from trino.transaction import IsolationLevel
+
+from airflow import AirflowException
+from airflow.configuration import conf
+from airflow.hooks.dbapi import DbApiHook
+from airflow.models import Connection
+
+
+def _boolify(value):
+    if isinstance(value, bool):
+        return value
+    if isinstance(value, str):
+        if value.lower() == 'false':
+            return False
+        elif value.lower() == 'true':
+            return True
+    return value
+
+
+class TrinoHook(DbApiHook):
+    """
+    Interact with Trino.
+
+    >>> th = TrinoHook()
+    >>> sql = "SELECT count(1) AS num FROM airflow.static_babynames"
+    >>> th.get_records(sql)
+    [[340698]]
+    """
+
+    conn_name_attr = 'trino_conn_id'
+    default_conn_name = 'trino_default'
+    conn_type = 'trino'
+    hook_name = 'Trino'
+
+    @staticmethod
+    def get_ui_field_behaviour() -> Dict[str, Any]:
+        """Returns custom field behaviour"""
+        return {
+            "hidden_fields": [],
+            "relabeling": {
+                'schema': 'Catalog',
+                'login': 'Username',
+            },
+        }
+
+    def get_conn(self) -> Connection:
+        """Returns a connection object"""
+        db = self.get_connection(getattr(self, self.conn_name_attr))
+        extra = db.extra_dejson
+        auth = None
+        if db.password and extra.get('auth') == 'kerberos':
+            raise AirflowException("Kerberos authorization doesn't support 
password.")
+        elif db.password:
+            auth = trino.auth.BasicAuthentication(db.login, db.password)
+        elif extra.get('auth') == 'kerberos':
+            auth = trino.auth.KerberosAuthentication(
+                config=extra.get('kerberos__config', 
os.environ.get('KRB5_CONFIG')),
+                service_name=extra.get('kerberos__service_name'),
+                
mutual_authentication=_boolify(extra.get('kerberos__mutual_authentication', 
False)),
+                
force_preemptive=_boolify(extra.get('kerberos__force_preemptive', False)),
+                hostname_override=extra.get('kerberos__hostname_override'),
+                sanitize_mutual_error_response=_boolify(
+                    extra.get('kerberos__sanitize_mutual_error_response', True)
+                ),
+                principal=extra.get('kerberos__principal', 
conf.get('kerberos', 'principal')),
+                delegate=_boolify(extra.get('kerberos__delegate', False)),
+                ca_bundle=extra.get('kerberos__ca_bundle'),
+            )

Review comment:
       Should be extracted into a `extract_auth` method in my opinion 😄 

##########
File path: airflow/providers/trino/hooks/trino.py
##########
@@ -0,0 +1,184 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+import os
+from typing import Any, Dict, Iterable, Optional
+
+import trino
+from trino.transaction import IsolationLevel
+
+from airflow import AirflowException
+from airflow.configuration import conf
+from airflow.hooks.dbapi import DbApiHook
+from airflow.models import Connection
+
+
+def _boolify(value):
+    if isinstance(value, bool):
+        return value
+    if isinstance(value, str):
+        if value.lower() == 'false':
+            return False
+        elif value.lower() == 'true':
+            return True
+    return value
+
+
+class TrinoHook(DbApiHook):
+    """
+    Interact with Trino.
+
+    >>> th = TrinoHook()
+    >>> sql = "SELECT count(1) AS num FROM airflow.static_babynames"
+    >>> th.get_records(sql)
+    [[340698]]
+    """
+
+    conn_name_attr = 'trino_conn_id'
+    default_conn_name = 'trino_default'
+    conn_type = 'trino'
+    hook_name = 'Trino'
+
+    @staticmethod
+    def get_ui_field_behaviour() -> Dict[str, Any]:
+        """Returns custom field behaviour"""
+        return {
+            "hidden_fields": [],
+            "relabeling": {
+                'schema': 'Catalog',
+                'login': 'Username',
+            },
+        }
+
+    def get_conn(self) -> Connection:
+        """Returns a connection object"""
+        db = self.get_connection(getattr(self, self.conn_name_attr))

Review comment:
       That line returns more than once.
   would suggest to extract it to a function




----------------------------------------------------------------
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]


Reply via email to