gopidesupavan commented on code in PR #39923:
URL: https://github.com/apache/airflow/pull/39923#discussion_r1621295814


##########
airflow/providers/amazon/aws/operators/glue.py:
##########
@@ -239,3 +243,253 @@ def on_kill(self):
             )
             if not response["SuccessfulSubmissions"]:
                 self.log.error("Failed to stop AWS Glue Job: %s. Run Id: %s", 
self.job_name, self._job_run_id)
+
+
+class GlueDataQualityOperator(AwsBaseOperator[GlueDataQualityHook]):
+    """
+    Creates a data quality ruleset with DQDL rules applied to a specified Glue 
table.
+
+    .. seealso::
+        For more information on how to use this operator, take a look at the 
guide:
+        :ref:`howto/operator:GlueDataQualityOperator`
+
+    :param name: A unique name for the data quality ruleset.
+    :param ruleset: A Data Quality Definition Language (DQDL) ruleset.
+        For more information, see the Glue developer guide.
+    :param description: A description of the data quality ruleset.
+    :param update_rule_set: To update existing ruleset, Set this flag to True. 
(default: False)
+    :param data_quality_ruleset_kwargs: Extra arguments for RuleSet.
+
+    :param aws_conn_id: The Airflow connection used for AWS credentials.
+        If this is ``None`` or empty then the default boto3 behaviour is used. 
If
+        running Airflow in a distributed manner and aws_conn_id is None or
+        empty, then default boto3 configuration would be used (and must be
+        maintained on each worker node).
+    :param region_name: AWS region_name. If not specified then the default 
boto3 behaviour is used.
+    :param verify: Whether or not to verify SSL certificates. See:
+        
https://boto3.amazonaws.com/v1/documentation/api/latest/reference/core/session.html
+    :param botocore_config: Configuration dictionary (key-values) for botocore 
client. See:
+        
https://botocore.amazonaws.com/v1/documentation/api/latest/reference/config.html
+    """
+
+    aws_hook_class = GlueDataQualityHook
+    template_fields: Sequence[str] = ("name", "ruleset", 
"data_quality_ruleset_kwargs")
+
+    template_fields_renderers = {
+        "data_quality_ruleset_kwargs": "json",
+    }
+    ui_color = "#ededed"
+
+    def __init__(
+        self,
+        *,
+        name: str,
+        ruleset: str,
+        description: str = "AWS Glue Data Quality Rule Set With Airflow",
+        update_rule_set: bool = False,
+        data_quality_ruleset_kwargs: dict | None = None,
+        aws_conn_id: str | None = "aws_default",
+        **kwargs,
+    ):
+        super().__init__(**kwargs)
+        self.name = name
+        self.ruleset = ruleset.strip()
+        self.description = description
+        self.update_rule_set = update_rule_set
+        self.data_quality_ruleset_kwargs = data_quality_ruleset_kwargs or {}
+        self.aws_conn_id = aws_conn_id
+
+    def validate_inputs(self) -> None:
+        if not self.ruleset.startswith("Rules") or not 
self.ruleset.endswith("]"):
+            raise AttributeError("RuleSet must starts with Rules = [ and ends 
with ]")
+
+        if self.data_quality_ruleset_kwargs.get("TargetTable"):
+            target_table = self.data_quality_ruleset_kwargs["TargetTable"]
+
+            if not target_table.get("TableName") or not 
target_table.get("DatabaseName"):
+                raise AttributeError("Target table must have DatabaseName and 
TableName")
+
+    def execute(self, context: Context):
+        self.validate_inputs()
+
+        config = {
+            "Name": self.name,
+            "Ruleset": self.ruleset,
+            "Description": self.description,
+            **self.data_quality_ruleset_kwargs,
+        }
+
+        if self.update_rule_set:
+            self.hook.update_glue_data_quality_ruleset(config)
+            self.log.info("AWS Glue data quality ruleset updated successfully")
+        else:
+            self.hook.create_glue_data_quality_ruleset(config)
+            self.log.info("AWS Glue data quality ruleset created successfully")
+
+
+class 
GlueDataQualityRuleSetEvaluationRunOperator(AwsBaseOperator[GlueDataQualityHook]):
+    """
+    Once you have a ruleset definition (either recommended or your own), you 
call this operation to evaluate the ruleset against a data source (Glue table).
+
+    .. seealso::
+        For more information on how to use this operator, take a look at the 
guide:
+        :ref:`howto/operator:GlueDataQualityRuleSetEvaluationRunOperator`
+
+    :param datasource: The data source (Glue table) associated with this run. 
(templated)
+    :param role: IAM role supplied for job execution. (templated)
+    :param rule_set_names: A list of ruleset names for evaluation. (templated)
+    :param number_of_workers: The number of G.1X workers to be used in the 
run. (default: 5)
+    :param timeout: The timeout for a run in minutes. This is the maximum time 
that a run can consume resources
+        before it is terminated and enters TIMEOUT status. (default: 2,880)
+    :param verify_result_status: Validate all the ruleset rules evaluation run 
results,
+        If any of the rule status is Fail or Error then an exception is 
thrown. (default: True)
+    :param show_results: Displays all the ruleset rules evaluation run 
results. (default: True)
+    :param rule_set_evaluation_run_kwargs: Extra arguments for evaluation run. 
(templated)
+    :param wait_for_completion: Whether to wait for job to stop. (default: 
True)
+    :param waiter_delay: Time in seconds to wait between status checks. 
(default: 60)
+    :param waiter_max_attempts: Maximum number of attempts to check for job 
completion. (default: 20)
+    :param deferrable: If True, the operator will wait asynchronously for the 
job to stop.
+        This implies waiting for completion. This mode requires aiobotocore 
module to be installed.
+        (default: False)
+
+    :param aws_conn_id: The Airflow connection used for AWS credentials.
+        If this is ``None`` or empty then the default boto3 behaviour is used. 
If
+        running Airflow in a distributed manner and aws_conn_id is None or
+        empty, then default boto3 configuration would be used (and must be
+        maintained on each worker node).
+    :param region_name: AWS region_name. If not specified then the default 
boto3 behaviour is used.
+    :param verify: Whether or not to verify SSL certificates. See:
+        
https://boto3.amazonaws.com/v1/documentation/api/latest/reference/core/session.html
+    :param botocore_config: Configuration dictionary (key-values) for botocore 
client. See:
+        
https://botocore.amazonaws.com/v1/documentation/api/latest/reference/config.html
+    """
+
+    aws_hook_class = GlueDataQualityHook
+
+    template_fields: Sequence[str] = (
+        "datasource",
+        "role",
+        "rule_set_names",
+        "rule_set_evaluation_run_kwargs",
+    )
+
+    template_fields_renderers = {"datasource": "json", 
"rule_set_evaluation_run_kwargs": "json"}
+
+    ui_color = "#ededed"
+
+    def __init__(
+        self,
+        *,
+        datasource: dict,
+        role: str,
+        rule_set_names: list[str],
+        number_of_workers: int = 5,
+        timeout: int = 2880,
+        verify_result_status: bool = True,
+        show_results: bool = True,
+        rule_set_evaluation_run_kwargs: dict[str, Any] | None = None,
+        wait_for_completion: bool = True,
+        waiter_delay: int = 60,
+        waiter_max_attempts: int = 20,
+        deferrable: bool = conf.getboolean("operators", "default_deferrable", 
fallback=False),
+        aws_conn_id: str | None = "aws_default",
+        **kwargs,
+    ):
+        super().__init__(**kwargs)
+        self.datasource = datasource
+        self.role = role
+        self.rule_set_names = rule_set_names
+        self.number_of_workers = number_of_workers
+        self.timeout = timeout
+        self.verify_result_status = verify_result_status
+        self.show_results = show_results
+        self.rule_set_evaluation_run_kwargs = rule_set_evaluation_run_kwargs 
or {}
+        self.wait_for_completion = wait_for_completion
+        self.waiter_delay = waiter_delay
+        self.waiter_max_attempts = waiter_max_attempts
+        self.deferrable = deferrable
+        self.aws_conn_id = aws_conn_id
+
+    def validate_inputs(self) -> None:
+        glue_table = self.datasource.get("GlueTable", {})
+
+        if not glue_table.get("DatabaseName") or not 
glue_table.get("TableName"):
+            raise AttributeError("DataSource glue table must have DatabaseName 
and TableName")
+
+        not_found_ruleset = []
+
+        for ruleset_name in self.rule_set_names:
+            if not self.hook.has_data_quality_ruleset(ruleset_name):
+                not_found_ruleset.append(ruleset_name)

Review Comment:
   updated the changes.



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

To unsubscribe, e-mail: [email protected]

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

Reply via email to