xinbinhuang commented on a change in pull request #8888:
URL: https://github.com/apache/airflow/pull/8888#discussion_r426192919
##########
File path: tests/test_utils/amazon_system_helpers.py
##########
@@ -92,3 +135,310 @@ def create_emr_default_roles(cls) -> None:
"""
cmd = ["aws", "emr", "create-default-roles"]
cls.execute_with_ctx(cmd)
+
+ @staticmethod
+ def create_ecr_repository(aws_conn_id: str,
+ repository_name: str) -> str:
+ """
+ Create repository in ecr with given name
+
+ :param aws_conn_id: id of the aws connection to use when creating
boto3 client/resource
+ :type aws_conn_id: str
+ :param repository_name: name of the repository to create in aws ecr
+ :type repository_name: str
+ :return: uri of the created repository
+ uri format:
`registry_id`.dkr.ecr.`region`.amazonaws.com/`repository_name`
+ :rtype: str
+ """
+ hook = AwsBaseHook(
+ aws_conn_id=aws_conn_id,
+ client_type="ecr",
+ )
+ response = hook.conn.create_repository(
+ repositoryName=repository_name,
+ imageTagMutability="MUTABLE",
+ imageScanningConfiguration={
+ "scanOnPush": False
+ },
+ )
+ return response["repository"]["repositoryUri"]
+
+ @staticmethod
+ def delete_ecr_repository(aws_conn_id: str,
+ repository_name: str,
+ registry_id: str) -> None:
+ """
+ Delete repository in ecr with given name
+
+ :param aws_conn_id: id of the aws connection to use when creating
boto3 client/resource
+ :type aws_conn_id: str
+ :param repository_name: name of the repository to delete in aws ecr
+ :type repository_name: str
+ :param registry_id: aws account id associated with the registry that
contains the repository to delete
+ :type registry_id: str
+ """
+ hook = AwsBaseHook(
+ aws_conn_id=aws_conn_id,
+ client_type="ecr",
+ )
+ hook.conn.delete_repository(
+ registryId=registry_id,
+ repositoryName=repository_name,
+ force=True, # also delete images
+ )
+
+ @staticmethod
+ def is_ecr_repository_exists(aws_conn_id: str,
+ repository_name: str,
+ registry_id: str) -> Tuple[bool, str]:
+ """
+ Check whether given repository exists in ecr
+
+ :param aws_conn_id: id of the aws connection to use when creating
boto3 client/resource
+ :type aws_conn_id: str
+ :param repository_name: name of the repository to check in aws ecr
+ :type repository_name: str
+ :param registry_id: aws account id associated with the registry that
contains the repository to delete
+ :type registry_id: str
+ :return: True and uri of the found repository if given repository
exists in ecr,
+ else False and empty string
+ :rtype: Tuple[bool, str]
+ """
+ hook = AwsBaseHook(
+ aws_conn_id=aws_conn_id,
+ client_type="ecr",
+ )
+
+ try:
+ response = hook.conn.describe_repositories(
+ registryId=registry_id,
+ repositoryNames=[repository_name],
+ )
+ except hook.conn.exceptions.RepositoryNotFoundException:
+ # return False if repository does not exist
+ return False, ""
+
+ # return True if repository exists
+ return True, response["repositories"][0]["repositoryUri"]
Review comment:
I suggested instead of check if a repository exists -
`is_ecr_repository_exists`. You just return None if the repository not exists
and name the function as `get_repository_uri`
----------------------------------------------------------------
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]