kaxil commented on code in PR #65968: URL: https://github.com/apache/airflow/pull/65968#discussion_r3150410013
########## providers/amazon/tests/system/amazon/aws/example_s3vectors.py: ########## @@ -0,0 +1,54 @@ +# 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. +from __future__ import annotations + +from datetime import datetime + +from airflow.providers.amazon.aws.operators.s3vectors import S3VectorsCreateVectorBucketOperator +from airflow.providers.common.compat.sdk import DAG + +from system.amazon.aws.utils import ENV_ID_KEY, SystemTestContextBuilder + +DAG_ID = "example_s3vectors" + +sys_test_context_task = SystemTestContextBuilder().build() + +with DAG( + dag_id=DAG_ID, + schedule=None, + start_date=datetime(2024, 1, 1), + catchup=False, +) as dag: + test_context = sys_test_context_task() + env_id = test_context[ENV_ID_KEY] + + # [START howto_operator_s3vectors_create_vector_bucket] + create_vector_bucket = S3VectorsCreateVectorBucketOperator( + task_id="create_vector_bucket", + vector_bucket_name=f"{env_id}-test-vectors", + ) + # [END howto_operator_s3vectors_create_vector_bucket] + + test_context >> create_vector_bucket Review Comment: System test creates a bucket but never deletes it. Each CI run leaks one, and re-runs will fail because of the idempotency issue above. `example_neptune.py` shows the pattern: a `delete_cluster` task chained after the create, with `trigger_rule=ALL_DONE` so teardown runs even when an upstream task fails. You'll want a `S3VectorsDeleteVectorBucketOperator` (or a thin `@task` wrapper calling the boto3 client directly) for that. ########## providers/amazon/src/airflow/providers/amazon/aws/operators/s3vectors.py: ########## @@ -0,0 +1,76 @@ +# 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. +"""Amazon S3 Vectors operators.""" + +from __future__ import annotations + +from typing import TYPE_CHECKING, Any + +from airflow.providers.amazon.aws.hooks.base_aws import AwsBaseHook +from airflow.providers.amazon.aws.operators.base_aws import AwsBaseOperator + +if TYPE_CHECKING: + from airflow.sdk import Context + + +class S3VectorsCreateVectorBucketOperator(AwsBaseOperator[AwsBaseHook]): + """ + Create an Amazon S3 Vectors vector bucket. + + .. seealso:: + For more information on how to use this operator, take a look at the guide: + :ref:`howto/operator:S3VectorsCreateVectorBucketOperator` + + :param vector_bucket_name: The name of the vector bucket to create (3-63 chars). + :param encryption_configuration: Optional encryption config dict with keys + ``sseType`` (``AES256`` or ``aws:kms``) and optionally ``kmsKeyArn``. + :param tags: Optional dict of tags to apply to the vector bucket. + """ + + aws_hook_class = AwsBaseHook + template_fields: tuple[str, ...] = ( + *AwsBaseOperator.template_fields, + "vector_bucket_name", + ) + + def __init__( + self, + *, + vector_bucket_name: str, + encryption_configuration: dict[str, Any] | None = None, + tags: dict[str, str] | None = None, + **kwargs, + ) -> None: + super().__init__(**kwargs) + self.vector_bucket_name = vector_bucket_name + self.encryption_configuration = encryption_configuration + self.tags = tags + + @property + def _hook_parameters(self) -> dict[str, Any]: + return {**super()._hook_parameters, "client_type": "s3vectors"} + + def execute(self, context: Context) -> str: + kwargs: dict[str, Any] = {"vectorBucketName": self.vector_bucket_name} + if self.encryption_configuration is not None: + kwargs["encryptionConfiguration"] = self.encryption_configuration + if self.tags is not None: + kwargs["tags"] = self.tags + response = self.hook.conn.create_vector_bucket(**kwargs) Review Comment: `create_vector_bucket` raises `ConflictException` if the bucket already exists, so this operator is not idempotent. Compare with `S3CreateBucketOperator.execute` which calls `check_for_bucket()` first and no-ops otherwise. Worth either catching `ClientError` for `ConflictException` (and returning the existing ARN), or adding an `if_exists: Literal["fail", "skip"] = "fail"` parameter from day one so you don't break BC when you add it later. ########## providers/amazon/tests/unit/amazon/aws/operators/test_s3vectors.py: ########## @@ -0,0 +1,69 @@ +# 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. +from __future__ import annotations + +from unittest.mock import MagicMock + +from airflow.providers.amazon.aws.operators.s3vectors import S3VectorsCreateVectorBucketOperator + +BUCKET_NAME = "test-vector-bucket" +BUCKET_ARN = "arn:aws:s3vectors:us-east-1:123456789012:bucket/test-vector-bucket" + + +class TestS3VectorsCreateVectorBucketOperator: + def test_execute(self): + op = S3VectorsCreateVectorBucketOperator( + task_id="create_vector_bucket", + vector_bucket_name=BUCKET_NAME, + ) + mock_conn = MagicMock() + mock_conn.create_vector_bucket.return_value = {"vectorBucketArn": BUCKET_ARN} + op.hook.conn = mock_conn + + result = op.execute({}) + + mock_conn.create_vector_bucket.assert_called_once_with(vectorBucketName=BUCKET_NAME) + assert result == BUCKET_ARN + + def test_execute_with_encryption(self): + encryption = {"sseType": "aws:kms", "kmsKeyArn": "arn:aws:kms:us-east-1:123456789012:key/abc"} + op = S3VectorsCreateVectorBucketOperator( + task_id="create_vector_bucket", + vector_bucket_name=BUCKET_NAME, + encryption_configuration=encryption, + tags={"env": "test"}, + ) + mock_conn = MagicMock() + mock_conn.create_vector_bucket.return_value = {"vectorBucketArn": BUCKET_ARN} + op.hook.conn = mock_conn + + result = op.execute({}) + + mock_conn.create_vector_bucket.assert_called_once_with( + vectorBucketName=BUCKET_NAME, + encryptionConfiguration=encryption, + tags={"env": "test"}, + ) + assert result == BUCKET_ARN + + def test_template_fields(self): Review Comment: Other AWS operator tests use `validate_template_fields(op)` from `unit.amazon.aws.utils.test_template_fields` instead of asserting individual field membership. It catches things like missing `template_fields_renderers` entries that this assert won't. Also, `aws_conn_id` is included automatically via `aws_template_fields()`, so that line is trivially true. ########## providers/amazon/docs/operators/s3vectors/s3vectors.rst: ########## @@ -0,0 +1,42 @@ + .. Licensed to the Apache Software Foundation (ASF) under one Review Comment: Single-page guides are flat under `docs/operators/` (see `lambda.rst`, `neptune.rst`, `bedrock.rst`). The directory pattern with same-name `.rst` is only used for multi-page groups like `s3/`, `redshift/`, `emr/` that have an `index.rst`. Suggest moving this to `docs/operators/s3vectors.rst` and updating the `index.rst` entry accordingly. -- 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]
