mik-laj commented on a change in pull request #6123: [AIRFLOW-5502][depends on 
AIRFLOW-5498, AIRFLOW-5499] Move GCP base hook to core
URL: https://github.com/apache/airflow/pull/6123#discussion_r325936782
 
 

 ##########
 File path: airflow/contrib/utils/gcp_field_sanitizer.py
 ##########
 @@ -16,150 +16,16 @@
 # KIND, either express or implied.  See the License for the
 # specific language governing permissions and limitations
 # under the License.
-"""Sanitizer for body fields sent via GCP API.
-
-The sanitizer removes fields specified from the body.
-
-Context
--------
-In some cases where GCP operation requires modification of existing resources 
(such
-as instances or instance templates) we need to sanitize body of the resources 
returned
-via GCP APIs. This is in the case when we retrieve information from GCP first,
-modify the body and either update the existing resource or create a new one 
with the
-modified body. Usually when you retrieve resource from GCP you get some extra 
fields which
-are Output-only, and we need to delete those fields if we want to use
-the body as input for subsequent create/insert type operation.
-
-
-Field specification
--------------------
-
-Specification of fields is an array of strings which denote names of fields to 
be removed.
-The field can be either direct field name to remove from the body or the full
-specification of the path you should delete - separated with '.'
-
-
->>> FIELDS_TO_SANITIZE = [
->>>    "kind",
->>>    "properties.disks.kind",
->>>    "properties.metadata.kind",
->>>]
->>> body = {
->>>     "kind": "compute#instanceTemplate",
->>>     "name": "instance",
->>>     "properties": {
->>>         "disks": [
->>>             {
->>>                 "name": "a",
->>>                 "kind": "compute#attachedDisk",
->>>                 "type": "PERSISTENT",
->>>                 "mode": "READ_WRITE",
->>>             },
->>>             {
->>>                 "name": "b",
->>>                 "kind": "compute#attachedDisk",
->>>                 "type": "PERSISTENT",
->>>                 "mode": "READ_WRITE",
->>>             }
->>>         ],
->>>         "metadata": {
->>>             "kind": "compute#metadata",
->>>             "fingerprint": "GDPUYxlwHe4="
->>>         },
->>>     }
->>> }
->>> sanitizer=GcpBodyFieldSanitizer(FIELDS_TO_SANITIZE)
->>> SANITIZED_BODY = sanitizer.sanitize(body)
->>> json.dumps(SANITIZED_BODY, indent=2)
-{
-    "name":  "instance",
-    "properties": {
-        "disks": [
-            {
-                "name": "a",
-                "type": "PERSISTENT",
-                "mode": "READ_WRITE",
-            },
-            {
-                "name": "b",
-                "type": "PERSISTENT",
-                "mode": "READ_WRITE",
-            }
-        ],
-        "metadata": {
-            "fingerprint": "GDPUYxlwHe4="
-        },
-    }
-}
-
-Note that the components of the path can be either dictionaries or arrays of 
dictionaries.
-In case  they are dictionaries, subsequent component names key of the field, 
in case of
-arrays - the sanitizer iterates through all dictionaries in the array and 
searches
-components in all elements of the array.
+"""
+This module is deprecated. Please use `airflow.gcp.utils.field_sanitizer`.
 """
 
-from typing import List
-
-from airflow import LoggingMixin, AirflowException
-
-
-class GcpFieldSanitizerException(AirflowException):
-    """Thrown when sanitizer finds unexpected field type in the path
-    (other than dict or array).
-    """
-
-
-class GcpBodyFieldSanitizer(LoggingMixin):
-    """Sanitizes the body according to specification.
-
-    :param sanitize_specs: array of strings that specifies which fields to 
remove
-    :type sanitize_specs: list[str]
-
-    """
-    def __init__(self, sanitize_specs: List[str]) -> None:
-        super().__init__()
-        self._sanitize_specs = sanitize_specs
+import warnings
 
-    def _sanitize(self, dictionary, remaining_field_spec, current_path):
-        field_split = remaining_field_spec.split(".", 1)
-        if len(field_split) == 1:  # pylint: disable=too-many-nested-blocks
-            field_name = field_split[0]
-            if field_name in dictionary:
-                self.log.info("Deleted %s [%s]", field_name, current_path)
-                del dictionary[field_name]
-            else:
-                self.log.debug(
-                    "The field %s is missing in %s at the path %s.", 
field_name, dictionary, current_path
-                )
-        else:
-            field_name = field_split[0]
-            remaining_path = field_split[1]
-            child = dictionary.get(field_name)
-            if child is None:
-                self.log.debug(
-                    "The field %s is missing in %s at the path %s. ", 
field_name, dictionary, current_path
-                )
-            elif isinstance(child, dict):
-                self._sanitize(child, remaining_path, "{}.{}".format(
-                    current_path, field_name))
-            elif isinstance(child, list):
-                for index, elem in enumerate(child):
-                    if not isinstance(elem, dict):
-                        self.log.warning(
-                            "The field %s element at index %s is of wrong 
type. "
-                            "It should be dict and is %s. Skipping it.",
-                            current_path, index, elem)
-                    self._sanitize(elem, remaining_path, "{}.{}[{}]".format(
-                        current_path, field_name, index))
-            else:
-                self.log.warning(
-                    "The field %s is of wrong type. It should be dict or list 
and it is %s. Skipping it.",
-                    current_path, child
-                )
+# pylint: disable=unused-import
+from airflow.gcp.utils.field_sanitizer import GcpBodyFieldSanitizer, 
GcpFieldSanitizerException  # noqa
 
-    def sanitize(self, body):
-        """
-        Sanitizes the body according to specification.
-        """
-        for elem in self._sanitize_specs:
-            self._sanitize(body, elem, "")
+warnings.warn(
+    "This module is deprecated. Please use 
`airflow.gcp.utils.field_sanitizer`.",
+    DeprecationWarning
 
 Review comment:
   Can you add stacklevel here?

----------------------------------------------------------------
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:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to