[GitHub] [airflow] nritholtz commented on a change in pull request #4903: [AIRFLOW-4069] Add Opsgenie Alert Hook and Operator

2019-04-05 Thread GitBox
nritholtz commented on a change in pull request #4903: [AIRFLOW-4069] Add 
Opsgenie Alert Hook and Operator
URL: https://github.com/apache/airflow/pull/4903#discussion_r272703256
 
 

 ##
 File path: tests/contrib/operators/test_opsgenie_alert_operator.py
 ##
 @@ -0,0 +1,124 @@
+# -*- 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 unittest
+
+from airflow import DAG, configuration
+
+from airflow.contrib.operators.opsgenie_alert_operator import 
OpsgenieAlertOperator
+from airflow.utils import timezone
+
+DEFAULT_DATE = timezone.datetime(2017, 1, 1)
+
+
+class TestOpsgenieAlertOperator(unittest.TestCase):
+_config = {
+'message': 'An example alert message',
+'alias': 'Life is too short for no alias',
+'description': 'Every alert needs a description',
+'responders': [
+{'id': '4513b7ea-3b91-438f-b7e4-e3e54af9147c', 'type': 'team'},
+{'name': 'NOC', 'type': 'team'},
+{'id': 'bb4d9938-c3c2-455d-aaab-727aa701c0d8', 'type': 'user'},
+{'username': 'trin...@opsgenie.com', 'type': 'user'},
+{'id': 'aee8a0de-c80f-4515-a232-501c0bc9d715', 'type': 
'escalation'},
+{'name': 'Nightwatch Escalation', 'type': 'escalation'},
+{'id': '80564037-1984-4f38-b98e-8a1f662df552', 'type': 'schedule'},
+{'name': 'First Responders Schedule', 'type': 'schedule'}
+],
+'visibleTo': [
+{'id': '4513b7ea-3b91-438f-b7e4-e3e54af9147c', 'type': 'team'},
+{'name': 'rocket_team', 'type': 'team'},
+{'id': 'bb4d9938-c3c2-455d-aaab-727aa701c0d8', 'type': 'user'},
+{'username': 'trin...@opsgenie.com', 'type': 'user'}
+],
+'actions': ['Restart', 'AnExampleAction'],
+'tags': ['OverwriteQuietHours', 'Critical'],
+'details': {'key1': 'value1', 'key2': 'value2'},
+'entity': 'An example entity',
+'source': 'Airflow',
+'priority': 'P1',
+'user': 'Jesse',
+'note': 'Write this down'
+}
+
+expected_payload_dict = {
+'message': _config['message'],
+'alias': _config['alias'],
+'description': _config['description'],
+'responders': _config['responders'],
+'visibleTo': _config['visibleTo'],
+'actions': _config['actions'],
+'tags': _config['tags'],
+'details': _config['details'],
+'entity': _config['entity'],
+'source': _config['source'],
+'priority': _config['priority'],
+'user': _config['user'],
+'note': _config['note']
+}
+
+def setUp(self):
+configuration.load_test_config()
+args = {
+'owner': 'airflow',
+'start_date': DEFAULT_DATE
+}
+self.dag = DAG('test_dag_id', default_args=args)
+
+def test_build_opsgenie_payload(self):
+# Given / When
+operator = OpsgenieAlertOperator(
+task_id='opsgenie_alert_job',
+dag=self.dag,
+**self._config
+)
+
+payload = operator._build_opsgenie_payload()
+
+# Then
+self.assertEqual(self.expected_payload_dict, payload)
+
+def test_execute(self):
 
 Review comment:
   Fixed.


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


[GitHub] [airflow] nritholtz commented on a change in pull request #4903: [AIRFLOW-4069] Add Opsgenie Alert Hook and Operator

2019-04-05 Thread GitBox
nritholtz commented on a change in pull request #4903: [AIRFLOW-4069] Add 
Opsgenie Alert Hook and Operator
URL: https://github.com/apache/airflow/pull/4903#discussion_r272703192
 
 

 ##
 File path: airflow/contrib/hooks/opsgenie_alert_hook.py
 ##
 @@ -0,0 +1,90 @@
+# -*- 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 requests
+
+from airflow.hooks.http_hook import HttpHook
+from airflow import AirflowException
+
+
+class OpsgenieAlertHook(HttpHook):
+"""
+This hook allows you to post alerts to Opsgenie.
+Accepts a connection that has an Opsgenie API key as the connection's 
password.
+This hook sets the domain to conn_id.host, and if not set will default
+to ``https://api.opsgenie.com``.
+
+Each Opsgenie API key can be pre-configured to a team integration.
+You can override these defaults in this hook.
+
+:param opsgenie_conn_id: The name of the Opsgenie connection to use
+:type opsgenie_conn_id: str
+:param payload: Opsgenie API Create Alert payload values
+See https://docs.opsgenie.com/docs/alert-api#section-create-alert
+:type payload: dict
+
+"""
+def __init__(self,
+ opsgenie_conn_id='opsgenie_default',
+ payload={},
+ proxy=None,
 
 Review comment:
   Fixed, and also removed `proxy` which I left behind by mistake.


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


[GitHub] [airflow] nritholtz commented on a change in pull request #4903: [AIRFLOW-4069] Add Opsgenie Alert Hook and Operator

2019-04-05 Thread GitBox
nritholtz commented on a change in pull request #4903: [AIRFLOW-4069] Add 
Opsgenie Alert Hook and Operator
URL: https://github.com/apache/airflow/pull/4903#discussion_r272635684
 
 

 ##
 File path: airflow/contrib/hooks/opsgenie_alert_hook.py
 ##
 @@ -0,0 +1,68 @@
+# -*- 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
+
+from airflow.hooks.http_hook import HttpHook
+
+
+class OpsgenieAlertHook(HttpHook):
+"""
+This hook allows you to post alerts to Opsgenie.
+Accepts a connection that has an Opsgenie API key as the connection's 
password.
+Each Opsgenie API key can be pre-configured to a team integration.
+You can override these defaults in this hook.
+
+:param http_conn_id: Http connection ID with host as 
"https://api.opsgenie.com/;
+  and Opsgenie API key as the connection's password
+  (e.g. "eb243592-faa2-4ba2-a551q-1afdf565c889")
+:type http_conn_id: str
+:param payload: Opsgenie API Create Alert payload values
+See 
https://docs.opsgenie.com/docs/alert-api#section-create-alert
+:type payload: dict
+:param proxy: Proxy to use to make the Opsgenie Alert API call
+:type proxy: str
+"""
+def __init__(self,
+ http_conn_id,
+ payload={},
+ proxy=None,
+ *args,
+ **kwargs
+ ):
+super(OpsgenieAlertHook, self).__init__(*args, **kwargs)
+self.http_conn_id = http_conn_id
+self.payload = payload
+self.proxy = proxy
+
+def execute(self):
+"""
+Remote Popen (actually execute the Opsgenie Alert call)
+"""
+proxies = {}
+if self.proxy:
+# we only need https proxy for Opsgenie, as the endpoint is https
+proxies = {'https': self.proxy}
+api_key = self.get_connection(self.http_conn_id).password
+return self.run(endpoint='v2/alerts',
+data=json.dumps(self.payload, sort_keys=True),
 
 Review comment:
   I believe I originally tried that and had inconsistent failures, but I made 
the suggested change, and hopefully I was mistaken.


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


[GitHub] [airflow] nritholtz commented on a change in pull request #4903: [AIRFLOW-4069] Add Opsgenie Alert Hook and Operator

2019-04-05 Thread GitBox
nritholtz commented on a change in pull request #4903: [AIRFLOW-4069] Add 
Opsgenie Alert Hook and Operator
URL: https://github.com/apache/airflow/pull/4903#discussion_r272635439
 
 

 ##
 File path: airflow/contrib/hooks/opsgenie_alert_hook.py
 ##
 @@ -0,0 +1,67 @@
+# -*- 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
+
+from airflow.hooks.http_hook import HttpHook
+
+
+class OpsgenieAlertHook(HttpHook):
+"""
+This hook allows you to post alerts to Opsgenie.
+Accepts a connection that has an Opsgenie API key as the connection's 
password.
+Each Opsgenie API key can be pre-configured to a team integration.
+You can override these defaults in this hook.
+
+:param opsgenie_conn_id: Http connection ID with host as 
"https://api.opsgenie.com/;
+  and Opsgenie API key as the connection's password
+  (e.g. "eb243592-faa2-4ba2-a551q-1afdf565c889")
 
 Review comment:
   Fixed.


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


[GitHub] [airflow] nritholtz commented on a change in pull request #4903: [AIRFLOW-4069] Add Opsgenie Alert Hook and Operator

2019-04-05 Thread GitBox
nritholtz commented on a change in pull request #4903: [AIRFLOW-4069] Add 
Opsgenie Alert Hook and Operator
URL: https://github.com/apache/airflow/pull/4903#discussion_r272635342
 
 

 ##
 File path: airflow/contrib/hooks/opsgenie_alert_hook.py
 ##
 @@ -0,0 +1,67 @@
+# -*- 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
+
+from airflow.hooks.http_hook import HttpHook
+
+
+class OpsgenieAlertHook(HttpHook):
+"""
+This hook allows you to post alerts to Opsgenie.
+Accepts a connection that has an Opsgenie API key as the connection's 
password.
+Each Opsgenie API key can be pre-configured to a team integration.
+You can override these defaults in this hook.
+
+:param opsgenie_conn_id: Http connection ID with host as 
"https://api.opsgenie.com/;
+  and Opsgenie API key as the connection's password
+  (e.g. "eb243592-faa2-4ba2-a551q-1afdf565c889")
+:type opsgenie_conn_id: str
+:param payload: Opsgenie API Create Alert payload values
+See 
https://docs.opsgenie.com/docs/alert-api#section-create-alert
+:type payload: dict
+:param proxy: Proxy to use to make the Opsgenie Alert API call
+:type proxy: str
+"""
+def __init__(self,
+ opsgenie_conn_id,
+ payload={},
+ proxy=None,
+ *args,
+ **kwargs
+ ):
+super(OpsgenieAlertHook, self).__init__(http_conn_id=opsgenie_conn_id, 
*args, **kwargs)
+self.payload = payload
+self.proxy = proxy
+
+def execute(self):
+"""
+Remote Popen (actually execute the Opsgenie Alert call)
+"""
+proxies = {}
+if self.proxy:
+# we only need https proxy for Opsgenie, as the endpoint is https
+proxies = {'https': self.proxy}
 
 Review comment:
   Removed.


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


[GitHub] [airflow] nritholtz commented on a change in pull request #4903: [AIRFLOW-4069] Add Opsgenie Alert Hook and Operator

2019-04-05 Thread GitBox
nritholtz commented on a change in pull request #4903: [AIRFLOW-4069] Add 
Opsgenie Alert Hook and Operator
URL: https://github.com/apache/airflow/pull/4903#discussion_r272635182
 
 

 ##
 File path: tests/contrib/hooks/test_opsgenie_alert_hook.py
 ##
 @@ -0,0 +1,142 @@
+# -*- 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 unittest
+import requests_mock
+import json
+
+from airflow import configuration
+from airflow.models.connection import Connection
+from airflow.utils import db
+from airflow.contrib.hooks.opsgenie_alert_hook import OpsgenieAlertHook
+
+try:
+from unittest import mock
+except ImportError:
+try:
+import mock
+except ImportError:
+mock = None
 
 Review comment:
   Removed.


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


[GitHub] [airflow] nritholtz commented on a change in pull request #4903: [AIRFLOW-4069] Add Opsgenie Alert Hook and Operator

2019-04-05 Thread GitBox
nritholtz commented on a change in pull request #4903: [AIRFLOW-4069] Add 
Opsgenie Alert Hook and Operator
URL: https://github.com/apache/airflow/pull/4903#discussion_r272635296
 
 

 ##
 File path: airflow/contrib/hooks/opsgenie_alert_hook.py
 ##
 @@ -0,0 +1,67 @@
+# -*- 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
+
+from airflow.hooks.http_hook import HttpHook
+
+
+class OpsgenieAlertHook(HttpHook):
+"""
+This hook allows you to post alerts to Opsgenie.
+Accepts a connection that has an Opsgenie API key as the connection's 
password.
+Each Opsgenie API key can be pre-configured to a team integration.
+You can override these defaults in this hook.
+
+:param opsgenie_conn_id: Http connection ID with host as 
"https://api.opsgenie.com/;
 
 Review comment:
   Implemented suggestion.


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


[GitHub] [airflow] nritholtz commented on a change in pull request #4903: [AIRFLOW-4069] Add Opsgenie Alert Hook and Operator

2019-04-05 Thread GitBox
nritholtz commented on a change in pull request #4903: [AIRFLOW-4069] Add 
Opsgenie Alert Hook and Operator
URL: https://github.com/apache/airflow/pull/4903#discussion_r272635027
 
 

 ##
 File path: airflow/contrib/hooks/opsgenie_alert_hook.py
 ##
 @@ -0,0 +1,67 @@
+# -*- 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
+
+from airflow.hooks.http_hook import HttpHook
+
+
+class OpsgenieAlertHook(HttpHook):
+"""
+This hook allows you to post alerts to Opsgenie.
+Accepts a connection that has an Opsgenie API key as the connection's 
password.
+Each Opsgenie API key can be pre-configured to a team integration.
+You can override these defaults in this hook.
+
+:param opsgenie_conn_id: Http connection ID with host as 
"https://api.opsgenie.com/;
+  and Opsgenie API key as the connection's password
+  (e.g. "eb243592-faa2-4ba2-a551q-1afdf565c889")
+:type opsgenie_conn_id: str
+:param payload: Opsgenie API Create Alert payload values
+See 
https://docs.opsgenie.com/docs/alert-api#section-create-alert
+:type payload: dict
+:param proxy: Proxy to use to make the Opsgenie Alert API call
+:type proxy: str
+"""
+def __init__(self,
+ opsgenie_conn_id,
+ payload={},
+ proxy=None,
+ *args,
+ **kwargs
+ ):
+super(OpsgenieAlertHook, self).__init__(http_conn_id=opsgenie_conn_id, 
*args, **kwargs)
+self.payload = payload
+self.proxy = proxy
+
+def execute(self):
+"""
+Remote Popen (actually execute the Opsgenie Alert call)
 
 Review comment:
   Fixed.


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


[GitHub] [airflow] nritholtz commented on a change in pull request #4903: [AIRFLOW-4069] Add Opsgenie Alert Hook and Operator

2019-04-05 Thread GitBox
nritholtz commented on a change in pull request #4903: [AIRFLOW-4069] Add 
Opsgenie Alert Hook and Operator
URL: https://github.com/apache/airflow/pull/4903#discussion_r272635106
 
 

 ##
 File path: airflow/contrib/operators/opsgenie_alert_operator.py
 ##
 @@ -0,0 +1,139 @@
+# -*- 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.opsgenie_alert_hook import OpsgenieAlertHook
+from airflow.models import BaseOperator
+from airflow.utils.decorators import apply_defaults
+
+
+class OpsgenieAlertOperator(BaseOperator):
+"""
+This operator allows you to post alerts to Opsgenie.
+Accepts a connection that has an Opsgenie API key as the connection's 
password.
+
+Each Opsgenie API key can be pre-configured to a team integration.
+You can override these defaults in this operator.
+
+:param opsgenie_conn_id: Http connection ID, defaults to 
'opsgenie_default',
+  with host as "https://api.opsgenie.com/;
+  and Opsgenie API key as the connection's password
+  (e.g. "eb243592-faa2-4ba2-a551q-1afdf565c889")
+:type opsgenie_conn_id: str
+:param message: The Message of the Opsgenie alert
+:type message: str
+:param alias: Client-defined identifier of the alert
+:type alias: str
+:param description: Description field of the alert
+:type description: str
+:param responders: Teams, users, escalations and schedules that
+  the alert will be routed to send notifications.
+:type responders: list[dict]
+:param visibleTo: Teams and users that the alert will become visible
+  to without sending any notification.
+:type visibleTo: list[dict]
+:param actions: Custom actions that will be available for the alert.
+:type actions: list[str]
+:param tags: Tags of the alert.
+:type tags: list[str]
+:param details: Map of key-value pairs to use as custom properties of the 
alert.
+:type details: dict
+:param entity: Entity field of the alert that is
+generally used to specify which domain alert is related to.
+:type entity: str
+:param source: Source field of the alert. Default value is
+IP address of the incoming request.
+:type source: str
+:param priority: Priority level of the alert. Default value is P3.
+:type priority: str
+:param user: Display name of the request owner.
+:type user: str
+:param note: Additional note that will be added while creating the alert.
+:type note: str
+:param proxy: Proxy to use to make the Opsgenie Alert API call
+:type proxy: str
+"""
+
+@apply_defaults
+def __init__(self,
+ message,
+ opsgenie_conn_id='opsgenie_default',
+ alias=None,
+ description=None,
+ responders=None,
+ visibleTo=None,
+ actions=None,
+ tags=None,
+ details=None,
+ entity=None,
+ source=None,
+ priority=None,
+ user=None,
+ note=None,
+ proxy=None,
+ *args,
+ **kwargs
+ ):
+super(OpsgenieAlertOperator, self).__init__(*args, **kwargs)
+
+self.message = message
+self.opsgenie_conn_id = opsgenie_conn_id
+self.alias = alias
 
 Review comment:
   Added `template_fields`


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


[GitHub] [airflow] nritholtz commented on a change in pull request #4903: [AIRFLOW-4069] Add Opsgenie Alert Hook and Operator

2019-03-26 Thread GitBox
nritholtz commented on a change in pull request #4903: [AIRFLOW-4069] Add 
Opsgenie Alert Hook and Operator
URL: https://github.com/apache/airflow/pull/4903#discussion_r269092660
 
 

 ##
 File path: airflow/contrib/hooks/opsgenie_alert_hook.py
 ##
 @@ -0,0 +1,68 @@
+# -*- 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
+
+from airflow.hooks.http_hook import HttpHook
+
+
+class OpsgenieAlertHook(HttpHook):
+"""
+This hook allows you to post alerts to Opsgenie.
+Accepts a connection that has an Opsgenie API key as the connection's 
password.
+Each Opsgenie API key can be pre-configured to a team integration.
+You can override these defaults in this hook.
+
+:param http_conn_id: Http connection ID with host as 
"https://api.opsgenie.com/;
+  and Opsgenie API key as the connection's password
+  (e.g. "eb243592-faa2-4ba2-a551q-1afdf565c889")
+:type http_conn_id: str
+:param payload: Opsgenie API Create Alert payload values
+See 
https://docs.opsgenie.com/docs/alert-api#section-create-alert
+:type payload: dict
+:param proxy: Proxy to use to make the Opsgenie Alert API call
+:type proxy: str
+"""
+def __init__(self,
+ http_conn_id,
+ payload={},
+ proxy=None,
+ *args,
+ **kwargs
+ ):
+super(OpsgenieAlertHook, self).__init__(*args, **kwargs)
 
 Review comment:
   Thanks for the heads up, fixed in latest push.


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


[GitHub] [airflow] nritholtz commented on a change in pull request #4903: [AIRFLOW-4069] Add Opsgenie Alert Hook and Operator

2019-03-26 Thread GitBox
nritholtz commented on a change in pull request #4903: [AIRFLOW-4069] Add 
Opsgenie Alert Hook and Operator
URL: https://github.com/apache/airflow/pull/4903#discussion_r269092608
 
 

 ##
 File path: airflow/contrib/hooks/opsgenie_alert_hook.py
 ##
 @@ -0,0 +1,68 @@
+# -*- 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
+
+from airflow.hooks.http_hook import HttpHook
+
+
+class OpsgenieAlertHook(HttpHook):
+"""
+This hook allows you to post alerts to Opsgenie.
+Accepts a connection that has an Opsgenie API key as the connection's 
password.
+Each Opsgenie API key can be pre-configured to a team integration.
+You can override these defaults in this hook.
+
+:param http_conn_id: Http connection ID with host as 
"https://api.opsgenie.com/;
+  and Opsgenie API key as the connection's password
+  (e.g. "eb243592-faa2-4ba2-a551q-1afdf565c889")
+:type http_conn_id: str
+:param payload: Opsgenie API Create Alert payload values
+See 
https://docs.opsgenie.com/docs/alert-api#section-create-alert
+:type payload: dict
+:param proxy: Proxy to use to make the Opsgenie Alert API call
+:type proxy: str
+"""
+def __init__(self,
+ http_conn_id,
 
 Review comment:
   Thanks for the heads up, fixed in latest push.


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


[GitHub] [airflow] nritholtz commented on a change in pull request #4903: [AIRFLOW-4069] Add Opsgenie Alert Hook and Operator

2019-03-26 Thread GitBox
nritholtz commented on a change in pull request #4903: [AIRFLOW-4069] Add 
Opsgenie Alert Hook and Operator
URL: https://github.com/apache/airflow/pull/4903#discussion_r269086441
 
 

 ##
 File path: airflow/contrib/operators/opsgenie_alert_operator.py
 ##
 @@ -0,0 +1,139 @@
+# -*- 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.opsgenie_alert_hook import OpsgenieAlertHook
+from airflow.models import BaseOperator
+from airflow.utils.decorators import apply_defaults
+
+
+class OpsgenieAlertOperator(BaseOperator):
+"""
+This operator allows you to post alerts to Opsgenie.
+Accepts a connection that has an Opsgenie API key as the connection's 
password.
+
+Each Opsgenie API key can be pre-configured to a team integration.
+You can override these defaults in this operator.
+
+:param http_conn_id: Http connection ID, defaults to 'opsgenie_default',
+  with host as "https://api.opsgenie.com/;
+  and Opsgenie API key as the connection's password
+  (e.g. "eb243592-faa2-4ba2-a551q-1afdf565c889")
+:type http_conn_id: str
+:param message: The Message of the Opsgenie alert
+:type message: str
+:param alias: Client-defined identifier of the alert
+:type alias: str
+:param description: Description field of the alert
+:type description: str
+:param responders: Teams, users, escalations and schedules that
+  the alert will be routed to send notifications.
+:type responders: list[dict]
+:param visibleTo: Teams and users that the alert will become visible
+  to without sending any notification.
+:type visibleTo: list[dict]
+:param actions: Custom actions that will be available for the alert.
+:type actions: list[str]
+:param tags: Tags of the alert.
+:type tags: list[str]
+:param details: Map of key-value pairs to use as custom properties of the 
alert.
+:type details: dict
+:param entity: Entity field of the alert that is
+generally used to specify which domain alert is related to.
+:type entity: str
+:param source: Source field of the alert. Default value is
+IP address of the incoming request.
+:type source: str
+:param priority: Priority level of the alert. Default value is P3.
+:type priority: str
+:param user: Display name of the request owner.
+:type user: str
+:param note: Additional note that will be added while creating the alert.
+:type note: str
+:param proxy: Proxy to use to make the Opsgenie Alert API call
+:type proxy: str
+"""
+
+@apply_defaults
+def __init__(self,
+ message,
+ http_conn_id='opsgenie_default',
+ alias=None,
+ description=None,
+ responders=None,
+ visibleTo=None,
+ actions=None,
+ tags=None,
+ details=None,
+ entity=None,
+ source=None,
+ priority=None,
+ user=None,
+ note=None,
+ proxy=None,
+ *args,
+ **kwargs
+ ):
+super(OpsgenieAlertOperator, self).__init__(*args, **kwargs)
+
+self.message = message
+self.http_conn_id = http_conn_id
+self.alias = alias
+self.description = description
+self.responders = responders
+self.visibleTo = visibleTo
+self.actions = actions
+self.tags = tags
+self.details = details
+self.entity = entity
+self.source = source
+self.priority = priority
+self.user = user
+self.note = note
+self.proxy = proxy
+self.hook = None
+
+def _build_opsgenie_payload(self):
 
 Review comment:
   This is in the operator as per the suggestion from 
https://github.com/apache/airflow/pull/4903#discussion_r264925228. 
   
   > ... maybe add a payload parameter to the constructor instead of copying 
the data? The user will provide data in the appropriate format. The 

[GitHub] [airflow] nritholtz commented on a change in pull request #4903: [AIRFLOW-4069] Add Opsgenie Alert Hook and Operator

2019-03-26 Thread GitBox
nritholtz commented on a change in pull request #4903: [AIRFLOW-4069] Add 
Opsgenie Alert Hook and Operator
URL: https://github.com/apache/airflow/pull/4903#discussion_r269084930
 
 

 ##
 File path: airflow/contrib/hooks/opsgenie_alert_hook.py
 ##
 @@ -0,0 +1,68 @@
+# -*- 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
+
+from airflow.hooks.http_hook import HttpHook
+
+
+class OpsgenieAlertHook(HttpHook):
+"""
+This hook allows you to post alerts to Opsgenie.
+Accepts a connection that has an Opsgenie API key as the connection's 
password.
+Each Opsgenie API key can be pre-configured to a team integration.
+You can override these defaults in this hook.
+
+:param http_conn_id: Http connection ID with host as 
"https://api.opsgenie.com/;
+  and Opsgenie API key as the connection's password
+  (e.g. "eb243592-faa2-4ba2-a551q-1afdf565c889")
+:type http_conn_id: str
+:param payload: Opsgenie API Create Alert payload values
+See 
https://docs.opsgenie.com/docs/alert-api#section-create-alert
+:type payload: dict
+:param proxy: Proxy to use to make the Opsgenie Alert API call
+:type proxy: str
+"""
+def __init__(self,
+ http_conn_id,
+ payload={},
+ proxy=None,
+ *args,
+ **kwargs
+ ):
+super(OpsgenieAlertHook, self).__init__(*args, **kwargs)
+self.http_conn_id = http_conn_id
+self.payload = payload
+self.proxy = proxy
+
+def execute(self):
+"""
+Remote Popen (actually execute the Opsgenie Alert call)
+"""
+proxies = {}
+if self.proxy:
+# we only need https proxy for Opsgenie, as the endpoint is https
+proxies = {'https': self.proxy}
+api_key = self.get_connection(self.http_conn_id).password
+return self.run(endpoint='v2/alerts',
+data=json.dumps(self.payload, sort_keys=True),
 
 Review comment:
   It's needed to ensure the `test_opsgenie_alert_hook` passes consistently.
   
   The test is `self.assertEqual(resp.request.body, 
json.dumps(self._expected_payload_dict, sort_keys=True))` and before I added 
`sort_keys` to both, since it's a string assertion, it would fail dependent on 
how the keys were loaded.


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


[GitHub] [airflow] nritholtz commented on a change in pull request #4903: [AIRFLOW-4069] Add Opsgenie Alert Hook and Operator

2019-03-20 Thread GitBox
nritholtz commented on a change in pull request #4903: [AIRFLOW-4069] Add 
Opsgenie Alert Hook and Operator
URL: https://github.com/apache/airflow/pull/4903#discussion_r267347821
 
 

 ##
 File path: airflow/contrib/operators/opsgenie_alert_operator.py
 ##
 @@ -0,0 +1,142 @@
+# -*- 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.opsgenie_alert_hook import OpsgenieAlertHook
+from airflow.exceptions import AirflowException
+from airflow.models import BaseOperator
+from airflow.utils.decorators import apply_defaults
+
+
+class OpsgenieAlertOperator(BaseOperator):
+"""
+This operator allows you to post alerts to Opsgenie.
+Accepts a connection that has an Opsgenie API key as the connection's 
password.
+
+Each Opsgenie API key can be pre-configured to a team integration.
+You can override these defaults in this operator.
+
+:param http_conn_id: Http connection ID with host as 
"https://api.opsgenie.com/;
+  and Opsgenie API key as the connection's password
+  (e.g. "eb243592-faa2-4ba2-a551q-1afdf565c889")
+:type http_conn_id: str
+:param message: The Message of the Opsgenie alert
+:type message: str
+:param alias: Client-defined identifier of the alert
+:type alias: str
+:param description: Description field of the alert
+:type description: str
+:param responders: Teams, users, escalations and schedules that
+  the alert will be routed to send notifications.
+:type responders: list[dict]
+:param visibleTo: Teams and users that the alert will become visible
+  to without sending any notification.
+:type visibleTo: list[dict]
+:param actions: Custom actions that will be available for the alert.
+:type actions: list[str]
+:param tags: Tags of the alert.
+:type tags: list[str]
+:param details: Map of key-value pairs to use as custom properties of the 
alert.
+:type details: dict
+:param entity: Entity field of the alert that is
+generally used to specify which domain alert is related to.
+:type entity: str
+:param source: Source field of the alert. Default value is
+IP address of the incoming request.
+:type source: str
+:param priority: Priority level of the alert. Default value is P3.
+:type priority: str
+:param user: Display name of the request owner.
+:type user: str
+:param note: Additional note that will be added while creating the alert.
+:type note: str
+:param proxy: Proxy to use to make the Opsgenie Alert API call
+:type proxy: str
+"""
+
+@apply_defaults
+def __init__(self,
+ http_conn_id=None,
 
 Review comment:
   Added.


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


[GitHub] [airflow] nritholtz commented on a change in pull request #4903: [AIRFLOW-4069] Add Opsgenie Alert Hook and Operator

2019-03-14 Thread GitBox
nritholtz commented on a change in pull request #4903: [AIRFLOW-4069] Add 
Opsgenie Alert Hook and Operator
URL: https://github.com/apache/airflow/pull/4903#discussion_r265565571
 
 

 ##
 File path: airflow/contrib/hooks/opsgenie_alert_hook.py
 ##
 @@ -0,0 +1,83 @@
+# -*- 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
+
+from airflow.hooks.http_hook import HttpHook
+from airflow.exceptions import AirflowException
+
+
+class OpsgenieAlertHook(HttpHook):
+"""
+This hook allows you to post alerts to Opsgenie.
+Accepts a connection that has an Opsgenie API key as the connection's 
password.
+Each Opsgenie API key can be pre-configured to a team integration.
+You can override these defaults in this hook.
+
+:param http_conn_id: Http connection ID with host as 
"https://api.opsgenie.com/;
+  and Opsgenie API key as the connection's password
+  (e.g. "eb243592-faa2-4ba2-a551q-1afdf565c889")
+:type http_conn_id: str
+:param payload: Opsgenie API Create Alert payload values
+See 
https://docs.opsgenie.com/docs/alert-api#section-create-alert
+:type payload: dict
+:param proxy: Proxy to use to make the Opsgenie Alert API call
+:type proxy: str
+"""
+def __init__(self,
+ http_conn_id=None,
+ payload={},
+ proxy=None,
+ *args,
+ **kwargs
+ ):
+super(OpsgenieAlertHook, self).__init__(*args, **kwargs)
+self.http_conn_id = http_conn_id
+self.api_key = None
+self.payload = payload
+self.proxy = proxy
+
+def _get_api_key(self, http_conn_id):
 
 Review comment:
   Fixed as per suggestion


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


[GitHub] [airflow] nritholtz commented on a change in pull request #4903: [AIRFLOW-4069] Add Opsgenie Alert Hook and Operator

2019-03-14 Thread GitBox
nritholtz commented on a change in pull request #4903: [AIRFLOW-4069] Add 
Opsgenie Alert Hook and Operator
URL: https://github.com/apache/airflow/pull/4903#discussion_r265564941
 
 

 ##
 File path: airflow/contrib/hooks/opsgenie_alert_hook.py
 ##
 @@ -0,0 +1,83 @@
+# -*- 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
+
+from airflow.hooks.http_hook import HttpHook
+from airflow.exceptions import AirflowException
+
+
+class OpsgenieAlertHook(HttpHook):
+"""
+This hook allows you to post alerts to Opsgenie.
+Accepts a connection that has an Opsgenie API key as the connection's 
password.
+Each Opsgenie API key can be pre-configured to a team integration.
+You can override these defaults in this hook.
+
+:param http_conn_id: Http connection ID with host as 
"https://api.opsgenie.com/;
+  and Opsgenie API key as the connection's password
+  (e.g. "eb243592-faa2-4ba2-a551q-1afdf565c889")
+:type http_conn_id: str
+:param payload: Opsgenie API Create Alert payload values
+See 
https://docs.opsgenie.com/docs/alert-api#section-create-alert
+:type payload: dict
+:param proxy: Proxy to use to make the Opsgenie Alert API call
+:type proxy: str
+"""
+def __init__(self,
+ http_conn_id=None,
+ payload={},
+ proxy=None,
+ *args,
+ **kwargs
+ ):
+super(OpsgenieAlertHook, self).__init__(*args, **kwargs)
+self.http_conn_id = http_conn_id
+self.api_key = None
+self.payload = payload
+self.proxy = proxy
+
+def _get_api_key(self, http_conn_id):
+"""
+Given a conn_id, return the api_key to use
+:param http_conn_id: The conn_id provided
+:type http_conn_id: str
+:return: api_key (str) to use
+"""
+if http_conn_id:
+conn = self.get_connection(http_conn_id)
+return conn.password
+else:
+raise AirflowException('Cannot get api_key: No valid conn_id '
+   'supplied')
+
+def execute(self):
+"""
+Remote Popen (actually execute the Opsgenie Alert call)
+"""
+proxies = {}
+if self.proxy:
+# we only need https proxy for Opsgenie, as the endpoint is https
+proxies = {'https': self.proxy}
+self.api_key = self.api_key or self._get_api_key(self.http_conn_id)
 
 Review comment:
   In order to facilitate this PR, and also since I wasn't sure if you meant 
`cached_property` as in the package, which would introduce another dependency, 
I just removed this 'caching' functionality entirely.
   
   It is very possible that this may never happen anyways, since the majority 
of the use cases for this hook will most likely be through the operator, and 
even the small subset of users using the hook may never call execute more than 
once.


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


[GitHub] [airflow] nritholtz commented on a change in pull request #4903: [AIRFLOW-4069] Add Opsgenie Alert Hook and Operator

2019-03-12 Thread GitBox
nritholtz commented on a change in pull request #4903: [AIRFLOW-4069] Add 
Opsgenie Alert Hook and Operator
URL: https://github.com/apache/airflow/pull/4903#discussion_r264961711
 
 

 ##
 File path: airflow/contrib/hooks/opsgenie_alert_hook.py
 ##
 @@ -0,0 +1,83 @@
+# -*- 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
+
+from airflow.hooks.http_hook import HttpHook
+from airflow.exceptions import AirflowException
+
+
+class OpsgenieAlertHook(HttpHook):
+"""
+This hook allows you to post alerts to Opsgenie.
+Accepts a connection that has an Opsgenie API key as the connection's 
password.
+Each Opsgenie API key can be pre-configured to a team integration.
+You can override these defaults in this hook.
+
+:param http_conn_id: Http connection ID with host as 
"https://api.opsgenie.com/;
+  and Opsgenie API key as the connection's password
+  (e.g. "eb243592-faa2-4ba2-a551q-1afdf565c889")
+:type http_conn_id: str
+:param payload: Opsgenie API Create Alert payload values
+See 
https://docs.opsgenie.com/docs/alert-api#section-create-alert
+:type payload: dict
+:param proxy: Proxy to use to make the Opsgenie Alert API call
+:type proxy: str
+"""
+def __init__(self,
+ http_conn_id=None,
+ payload={},
+ proxy=None,
+ *args,
+ **kwargs
+ ):
+super(OpsgenieAlertHook, self).__init__(*args, **kwargs)
+self.http_conn_id = http_conn_id
+self.api_key = None
+self.payload = payload
+self.proxy = proxy
+
+def _get_api_key(self, http_conn_id):
 
 Review comment:
   My original intention of the method was when I originally had ability to 
explicitly pass the key. However, at this point, I see the current state of 
this method as better traceability for scenarios where the `http_connection_id` 
is not set or errors (e.g. connection doesn't exist).
   
   However, if you strongly believe in this change, I would make the change 
with the additional change of making `http_conn_id` a required parameter to the 
hook's constructor.
   


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


[GitHub] [airflow] nritholtz commented on a change in pull request #4903: [AIRFLOW-4069] Add Opsgenie Alert Hook and Operator

2019-03-12 Thread GitBox
nritholtz commented on a change in pull request #4903: [AIRFLOW-4069] Add 
Opsgenie Alert Hook and Operator
URL: https://github.com/apache/airflow/pull/4903#discussion_r264960756
 
 

 ##
 File path: airflow/contrib/hooks/opsgenie_alert_hook.py
 ##
 @@ -0,0 +1,83 @@
+# -*- 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
+
+from airflow.hooks.http_hook import HttpHook
+from airflow.exceptions import AirflowException
+
+
+class OpsgenieAlertHook(HttpHook):
+"""
+This hook allows you to post alerts to Opsgenie.
+Accepts a connection that has an Opsgenie API key as the connection's 
password.
+Each Opsgenie API key can be pre-configured to a team integration.
+You can override these defaults in this hook.
+
+:param http_conn_id: Http connection ID with host as 
"https://api.opsgenie.com/;
+  and Opsgenie API key as the connection's password
+  (e.g. "eb243592-faa2-4ba2-a551q-1afdf565c889")
+:type http_conn_id: str
+:param payload: Opsgenie API Create Alert payload values
+See 
https://docs.opsgenie.com/docs/alert-api#section-create-alert
+:type payload: dict
+:param proxy: Proxy to use to make the Opsgenie Alert API call
+:type proxy: str
+"""
+def __init__(self,
+ http_conn_id=None,
+ payload={},
+ proxy=None,
+ *args,
+ **kwargs
+ ):
+super(OpsgenieAlertHook, self).__init__(*args, **kwargs)
+self.http_conn_id = http_conn_id
+self.api_key = None
+self.payload = payload
+self.proxy = proxy
+
+def _get_api_key(self, http_conn_id):
+"""
+Given a conn_id, return the api_key to use
+:param http_conn_id: The conn_id provided
+:type http_conn_id: str
+:return: api_key (str) to use
+"""
+if http_conn_id:
+conn = self.get_connection(http_conn_id)
+return conn.password
+else:
+raise AirflowException('Cannot get api_key: No valid conn_id '
+   'supplied')
+
+def execute(self):
+"""
+Remote Popen (actually execute the Opsgenie Alert call)
+"""
+proxies = {}
+if self.proxy:
+# we only need https proxy for Opsgenie, as the endpoint is https
+proxies = {'https': self.proxy}
+self.api_key = self.api_key or self._get_api_key(self.http_conn_id)
 
 Review comment:
   In this case, I wasn't necessarily thinking of during the constructor but 
rather per `execute`.
   
   For example, a use case is instantiating a single hook object and then 
calling `execute` more than once. The first call to `execute` will do the 
`get_connection`, subsequent calls will not.


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