feng-tao closed pull request #4289: [AIRFLOW-XXX] Fix Minor issues with Azure 
Cosmos Operator
URL: https://github.com/apache/incubator-airflow/pull/4289
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/airflow/contrib/example_dags/example_cosmosdb_sensor.py 
b/airflow/contrib/example_dags/example_azure_cosmosdb_sensor.py
similarity index 88%
rename from airflow/contrib/example_dags/example_cosmosdb_sensor.py
rename to airflow/contrib/example_dags/example_azure_cosmosdb_sensor.py
index a801d9f41b..dd0b83e811 100644
--- a/airflow/contrib/example_dags/example_cosmosdb_sensor.py
+++ b/airflow/contrib/example_dags/example_azure_cosmosdb_sensor.py
@@ -1,64 +1,64 @@
-# -*- coding: utf-8 -*-
-#
-# 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.
-
-"""
-This is only an example DAG to highlight usage of AzureCosmosDocumentSensor to 
detect
-if a document now exists.
-
-You can trigger this manually with `airflow trigger_dag 
example_cosmosdb_sensor`.
-
-*Note: Make sure that connection `azure_cosmos_default` is properly set before 
running
-this example.*
-"""
-
-from airflow import DAG
-from airflow.contrib.sensors.azure_cosmos_sensor import 
AzureCosmosDocumentSensor
-from airflow.contrib.operators.azure_cosmos_insertdocument_operator import 
AzureCosmosInsertDocumentOperator
-from airflow.utils import dates
-
-default_args = {
-    'owner': 'airflow',
-    'depends_on_past': False,
-    'start_date': dates.days_ago(2),
-    'email': ['[email protected]'],
-    'email_on_failure': False,
-    'email_on_retry': False
-}
-
-dag = DAG('example_cosmosdb_sensor', default_args=default_args)
-
-dag.doc_md = __doc__
-
-t1 = AzureCosmosDocumentSensor(
-    task_id='check_cosmos_file',
-    database_name='airflow_example_db',
-    collection_name='airflow_example_coll',
-    document_id='airflow_checkid',
-    azure_cosmos_conn_id='azure_cosmos_default',
-    dag=dag)
-
-t2 = AzureCosmosInsertDocumentOperator(
-    task_id='insert_cosmos_file',
-    dag=dag,
-    database_name='airflow_example_db',
-    collection_name='new-collection',
-    document={"id": "someuniqueid", "param1": "value1", "param2": "value2"},
-    azure_cosmos_conn_id='azure_cosmos_default')
-
-t2.set_upstream(t1)
+# -*- coding: utf-8 -*-
+#
+# 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.
+
+"""
+This is only an example DAG to highlight usage of AzureCosmosDocumentSensor to 
detect
+if a document now exists.
+
+You can trigger this manually with `airflow trigger_dag 
example_cosmosdb_sensor`.
+
+*Note: Make sure that connection `azure_cosmos_default` is properly set before 
running
+this example.*
+"""
+
+from airflow import DAG
+from airflow.contrib.sensors.azure_cosmos_sensor import 
AzureCosmosDocumentSensor
+from airflow.contrib.operators.azure_cosmos_operator import 
AzureCosmosInsertDocumentOperator
+from airflow.utils import dates
+
+default_args = {
+    'owner': 'airflow',
+    'depends_on_past': False,
+    'start_date': dates.days_ago(2),
+    'email': ['[email protected]'],
+    'email_on_failure': False,
+    'email_on_retry': False
+}
+
+dag = DAG('example_azure_cosmosdb_sensor', default_args=default_args)
+
+dag.doc_md = __doc__
+
+t1 = AzureCosmosDocumentSensor(
+    task_id='check_cosmos_file',
+    database_name='airflow_example_db',
+    collection_name='airflow_example_coll',
+    document_id='airflow_checkid',
+    azure_cosmos_conn_id='azure_cosmos_default',
+    dag=dag)
+
+t2 = AzureCosmosInsertDocumentOperator(
+    task_id='insert_cosmos_file',
+    dag=dag,
+    database_name='airflow_example_db',
+    collection_name='new-collection',
+    document={"id": "someuniqueid", "param1": "value1", "param2": "value2"},
+    azure_cosmos_conn_id='azure_cosmos_default')
+
+t1 >> t2
diff --git a/airflow/contrib/operators/azure_cosmos_insertdocument_operator.py 
b/airflow/contrib/operators/azure_cosmos_operator.py
similarity index 91%
rename from airflow/contrib/operators/azure_cosmos_insertdocument_operator.py
rename to airflow/contrib/operators/azure_cosmos_operator.py
index 930ff402d0..e0fbaf2dd2 100644
--- a/airflow/contrib/operators/azure_cosmos_insertdocument_operator.py
+++ b/airflow/contrib/operators/azure_cosmos_operator.py
@@ -1,69 +1,69 @@
-# -*- coding: utf-8 -*-
-#
-# 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 airflow.contrib.hooks.azure_cosmos_hook import AzureCosmosDBHook
-from airflow.models import BaseOperator
-from airflow.utils.decorators import apply_defaults
-
-
-class AzureCosmosInsertDocumentOperator(BaseOperator):
-    """
-    Inserts a new document into the specified Cosmos database and collection
-    It will create both the database and collection if they do not already 
exist
-
-    :param database_name: The name of the database. (templated)
-    :type database_name: str
-    :param collection_name: The name of the collection. (templated)
-    :type collection_name: str
-    :param document: The document to insert
-    :type document: json
-    :param azure_cosmos_conn_id: reference to a CosmosDB connection.
-    :type azure_cosmos_conn_id: str
-    """
-    template_fields = ('database_name', 'collection_name')
-    ui_color = '#e4f0e8'
-
-    @apply_defaults
-    def __init__(self,
-                 database_name,
-                 collection_name,
-                 document,
-                 azure_cosmos_conn_id='azure_cosmos_default',
-                 *args,
-                 **kwargs):
-        super(AzureCosmosInsertDocumentOperator, self).__init__(*args, 
**kwargs)
-        self.database_name = database_name
-        self.collection_name = collection_name
-        self.document = document
-        self.azure_cosmos_conn_id = azure_cosmos_conn_id
-
-    def execute(self, context):
-        # Create the hook
-        hook = 
AzureCosmosDBHook(azure_cosmos_conn_id=self.azure_cosmos_conn_id)
-
-        # Create the DB if it doesn't already exist
-        if (not hook.does_database_exist(self.database_name)):
-            hook.create_database(self.database_name)
-
-        # Create the collection as well
-        if (not hook.does_collection_exist(self.collection_name, 
self.database_name)):
-            hook.create_collection(self.collection_name, self.database_name)
-
-        # finally insert the document
-        hook.upsert_document(self.document, self.database_name, 
self.collection_name)
+# -*- coding: utf-8 -*-
+#
+# 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 airflow.contrib.hooks.azure_cosmos_hook import AzureCosmosDBHook
+from airflow.models import BaseOperator
+from airflow.utils.decorators import apply_defaults
+
+
+class AzureCosmosInsertDocumentOperator(BaseOperator):
+    """
+    Inserts a new document into the specified Cosmos database and collection
+    It will create both the database and collection if they do not already 
exist
+
+    :param database_name: The name of the database. (templated)
+    :type database_name: str
+    :param collection_name: The name of the collection. (templated)
+    :type collection_name: str
+    :param document: The document to insert
+    :type document: dict
+    :param azure_cosmos_conn_id: reference to a CosmosDB connection.
+    :type azure_cosmos_conn_id: str
+    """
+    template_fields = ('database_name', 'collection_name')
+    ui_color = '#e4f0e8'
+
+    @apply_defaults
+    def __init__(self,
+                 database_name,
+                 collection_name,
+                 document,
+                 azure_cosmos_conn_id='azure_cosmos_default',
+                 *args,
+                 **kwargs):
+        super(AzureCosmosInsertDocumentOperator, self).__init__(*args, 
**kwargs)
+        self.database_name = database_name
+        self.collection_name = collection_name
+        self.document = document
+        self.azure_cosmos_conn_id = azure_cosmos_conn_id
+
+    def execute(self, context):
+        # Create the hook
+        hook = 
AzureCosmosDBHook(azure_cosmos_conn_id=self.azure_cosmos_conn_id)
+
+        # Create the DB if it doesn't already exist
+        if not hook.does_database_exist(self.database_name):
+            hook.create_database(self.database_name)
+
+        # Create the collection as well
+        if not hook.does_collection_exist(self.collection_name, 
self.database_name):
+            hook.create_collection(self.collection_name, self.database_name)
+
+        # finally insert the document
+        hook.upsert_document(self.document, self.database_name, 
self.collection_name)
diff --git a/docs/code.rst b/docs/code.rst
index 968cdd600e..87633470c4 100644
--- a/docs/code.rst
+++ b/docs/code.rst
@@ -133,6 +133,7 @@ Operators
 .. autoclass:: 
airflow.contrib.operators.adls_to_gcs.AdlsToGoogleCloudStorageOperator
 .. autoclass:: airflow.contrib.operators.aws_athena_operator.AWSAthenaOperator
 .. autoclass:: airflow.contrib.operators.awsbatch_operator.AWSBatchOperator
+.. autoclass:: 
airflow.contrib.operators.azure_cosmos_operator.AzureCosmosInsertDocumentOperator
 .. autoclass:: 
airflow.contrib.operators.bigquery_check_operator.BigQueryCheckOperator
 .. autoclass:: 
airflow.contrib.operators.bigquery_check_operator.BigQueryValueCheckOperator
 .. autoclass:: 
airflow.contrib.operators.bigquery_check_operator.BigQueryIntervalCheckOperator
@@ -235,6 +236,7 @@ Sensors
 
 .. autoclass:: airflow.contrib.sensors.aws_athena_sensor.AthenaSensor
 .. autoclass:: 
airflow.contrib.sensors.aws_redshift_cluster_sensor.AwsRedshiftClusterSensor
+.. autoclass:: 
airflow.contrib.sensors.azure_cosmos_sensor.AzureCosmosDocumentSensor
 .. autoclass:: airflow.contrib.sensors.bash_sensor.BashSensor
 .. autoclass:: airflow.contrib.sensors.bigquery_sensor.BigQueryTableSensor
 .. autoclass:: 
airflow.contrib.sensors.cassandra_record_sensor.CassandraRecordSensor
@@ -417,6 +419,7 @@ Community contributed hooks
 .. autoclass:: airflow.contrib.hooks.aws_hook.AwsHook
 .. autoclass:: airflow.contrib.hooks.aws_lambda_hook.AwsLambdaHook
 .. autoclass:: airflow.contrib.hooks.aws_sns_hook.AwsSnsHook
+.. autoclass:: airflow.contrib.hooks.azure_cosmos_hook.AzureCosmosDBHook
 .. autoclass:: airflow.contrib.hooks.azure_data_lake_hook.AzureDataLakeHook
 .. autoclass:: airflow.contrib.hooks.azure_fileshare_hook.AzureFileShareHook
 .. autoclass:: airflow.contrib.hooks.bigquery_hook.BigQueryHook
@@ -473,3 +476,4 @@ Community-contributed executors
 '''''''''''''''''''''''''''''''
 
 .. autoclass:: airflow.contrib.executors.mesos_executor.MesosExecutor
+.. autoclass:: airflow.contrib.executors.kubernetes_executor.KubernetesExecutor
diff --git a/docs/integration.rst b/docs/integration.rst
index e1b945b815..dfa001256a 100644
--- a/docs/integration.rst
+++ b/docs/integration.rst
@@ -162,7 +162,7 @@ Airflow can be configured to read and write task logs in 
Azure Blob Storage.
 See :ref:`write-logs-azure`.
 
 Azure CosmosDB 
-'''''''''''''''''' 
+''''''''''''''
  
 AzureCosmosDBHook communicates via the Azure Cosmos library. Make sure that a 
 Airflow connection of type `azure_cosmos` exists. Authorization can be done by 
supplying a 
@@ -173,20 +173,20 @@ default database and collection to use (see connection 
`azure_cosmos_default` fo
 - :ref:`AzureCosmosInsertDocumentOperator`: Simple operator to insert document 
into CosmosDB. 
 - :ref:`AzureCosmosDocumentSensor`: Simple sensor to detect document existence 
in CosmosDB. 
 
-.. AzureCosmosDBHook: 
- 
+.. _AzureCosmosDBHook:
+
 AzureCosmosDBHook 
-""""""""" 
+"""""""""""""""""
  
 .. autoclass:: airflow.contrib.hooks.azure_cosmos_hook.AzureCosmosDBHook 
  
 AzureCosmosInsertDocumentOperator 
-""""""""" 
+"""""""""""""""""""""""""""""""""
  
-.. autoclass:: 
airflow.contrib.operators.azure_cosmos_insertdocument_operator.AzureCosmosInsertDocumentOperator
 
+.. autoclass:: 
airflow.contrib.operators.azure_cosmos_operator.AzureCosmosInsertDocumentOperator
  
 AzureCosmosDocumentSensor 
-""""""""" 
+"""""""""""""""""""""""""
  
 .. autoclass:: 
airflow.contrib.sensors.azure_cosmos_sensor.AzureCosmosDocumentSensor 
 
diff --git 
a/tests/contrib/operators/test_azure_cosmos_insertdocument_operator.py 
b/tests/contrib/operators/test_azure_cosmos_insertdocument_operator.py
index 26099d0cb3..e6e1abe374 100644
--- a/tests/contrib/operators/test_azure_cosmos_insertdocument_operator.py
+++ b/tests/contrib/operators/test_azure_cosmos_insertdocument_operator.py
@@ -1,84 +1,84 @@
-# -*- coding: utf-8 -*-
-#
-# 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 json
-import unittest
-import uuid
-
-from airflow.contrib.operators.azure_cosmos_insertdocument_operator import 
AzureCosmosInsertDocumentOperator
-
-from airflow import configuration
-from airflow import models
-from airflow.utils import db
-
-try:
-    from unittest import mock
-
-except ImportError:
-    try:
-        import mock
-    except ImportError:
-        mock = None
-
-
-class TestAzureCosmosDbHook(unittest.TestCase):
-
-    # Set up an environment to test with
-    def setUp(self):
-        # set up some test variables
-        self.test_end_point = 'https://test_endpoint:443'
-        self.test_master_key = 'magic_test_key'
-        self.test_database_name = 'test_database_name'
-        self.test_collection_name = 'test_collection_name'
-        configuration.load_test_config()
-        db.merge_conn(
-            models.Connection(
-                conn_id='azure_cosmos_test_key_id',
-                conn_type='azure_cosmos',
-                login=self.test_end_point,
-                password=self.test_master_key,
-                extra=json.dumps({'database_name': self.test_database_name,
-                                  'collection_name': 
self.test_collection_name})
-            )
-        )
-
-    @mock.patch('azure.cosmos.cosmos_client.CosmosClient')
-    def test_insert_document(self, cosmos_mock):
-        test_id = str(uuid.uuid4())
-        cosmos_mock.return_value.CreateItem.return_value = {'id': test_id}
-        self.cosmos = AzureCosmosInsertDocumentOperator(
-            database_name=self.test_database_name,
-            collection_name=self.test_collection_name,
-            document={'id': test_id, 'data': 'sometestdata'},
-            azure_cosmos_conn_id='azure_cosmos_test_key_id',
-            task_id='azure_cosmos_sensor')
-
-        expected_calls = [mock.call().CreateItem(
-            'dbs/' + self.test_database_name + '/colls/' + 
self.test_collection_name,
-            {'data': 'sometestdata', 'id': test_id})]
-
-        self.cosmos.execute(None)
-        cosmos_mock.assert_any_call(self.test_end_point, {'masterKey': 
self.test_master_key})
-        cosmos_mock.assert_has_calls(expected_calls)
-
-
-if __name__ == '__main__':
-    unittest.main()
+# -*- coding: utf-8 -*-
+#
+# 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 json
+import unittest
+import uuid
+
+from airflow.contrib.operators.azure_cosmos_operator import 
AzureCosmosInsertDocumentOperator
+
+from airflow import configuration
+from airflow import models
+from airflow.utils import db
+
+try:
+    from unittest import mock
+
+except ImportError:
+    try:
+        import mock
+    except ImportError:
+        mock = None
+
+
+class TestAzureCosmosDbHook(unittest.TestCase):
+
+    # Set up an environment to test with
+    def setUp(self):
+        # set up some test variables
+        self.test_end_point = 'https://test_endpoint:443'
+        self.test_master_key = 'magic_test_key'
+        self.test_database_name = 'test_database_name'
+        self.test_collection_name = 'test_collection_name'
+        configuration.load_test_config()
+        db.merge_conn(
+            models.Connection(
+                conn_id='azure_cosmos_test_key_id',
+                conn_type='azure_cosmos',
+                login=self.test_end_point,
+                password=self.test_master_key,
+                extra=json.dumps({'database_name': self.test_database_name,
+                                  'collection_name': 
self.test_collection_name})
+            )
+        )
+
+    @mock.patch('azure.cosmos.cosmos_client.CosmosClient')
+    def test_insert_document(self, cosmos_mock):
+        test_id = str(uuid.uuid4())
+        cosmos_mock.return_value.CreateItem.return_value = {'id': test_id}
+        self.cosmos = AzureCosmosInsertDocumentOperator(
+            database_name=self.test_database_name,
+            collection_name=self.test_collection_name,
+            document={'id': test_id, 'data': 'sometestdata'},
+            azure_cosmos_conn_id='azure_cosmos_test_key_id',
+            task_id='azure_cosmos_sensor')
+
+        expected_calls = [mock.call().CreateItem(
+            'dbs/' + self.test_database_name + '/colls/' + 
self.test_collection_name,
+            {'data': 'sometestdata', 'id': test_id})]
+
+        self.cosmos.execute(None)
+        cosmos_mock.assert_any_call(self.test_end_point, {'masterKey': 
self.test_master_key})
+        cosmos_mock.assert_has_calls(expected_calls)
+
+
+if __name__ == '__main__':
+    unittest.main()


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to