-
Notifications
You must be signed in to change notification settings - Fork 14.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding Google Deployment Manager Hook #9159
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
511e8a1
Add GoogleDeploymentManagerHook
SamWheating 3196f73
Apply suggestions from code review
SamWheating 5841e1b
Formatting fixes
SamWheating 2de6454
Fixing tests and formatting
SamWheating 058320e
Fix typo, add return value assertions to gdm_hook tests
SamWheating ea4aea3
Fetch all pages by default, refactor tests
SamWheating bd34ad3
Add test for handling failed deployment deletion
SamWheating File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
# | ||
# 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 typing import Any, Dict, List, Optional | ||
|
||
from googleapiclient.discovery import build | ||
|
||
from airflow.exceptions import AirflowException | ||
from airflow.providers.google.common.hooks.base_google import GoogleBaseHook | ||
|
||
|
||
class GoogleDeploymentManagerHook(GoogleBaseHook): # pylint: disable=abstract-method | ||
""" | ||
Interact with Google Cloud Deployment Manager using the Google Cloud Platform connection. | ||
This allows for scheduled and programatic inspection and deletion fo resources managed by GDM. | ||
""" | ||
|
||
def __init__(self, gcp_conn_id='google_cloud_default', delegate_to=None): | ||
super(GoogleDeploymentManagerHook, self).__init__(gcp_conn_id, delegate_to=delegate_to) | ||
|
||
def get_conn(self): | ||
""" | ||
Returns a Google Deployment Manager service object. | ||
|
||
:rtype: googleapiclient.discovery.Resource | ||
""" | ||
http_authorized = self._authorize() | ||
return build('deploymentmanager', 'v2', http=http_authorized, cache_discovery=False) | ||
|
||
@GoogleBaseHook.fallback_to_default_project_id | ||
def list_deployments(self, project_id: Optional[str] = None, # pylint: disable=too-many-arguments | ||
deployment_filter: Optional[str] = None, | ||
order_by: Optional[str] = None) -> List[Dict[str, Any]]: | ||
""" | ||
Lists deployments in a google cloud project. | ||
|
||
:param project_id: The project ID for this request. | ||
:type project_id: str | ||
:param deployment_filter: A filter expression which limits resources returned in the response. | ||
:type deployment_filter: str | ||
:param order_by: A field name to order by, ex: "creationTimestamp desc" | ||
:type order_by: Optional[str] | ||
:rtype: list | ||
""" | ||
deployments = [] # type: List[Dict] | ||
conn = self.get_conn() | ||
request = conn.deployments().list(project=project_id, # pylint: disable=no-member | ||
filter=deployment_filter, | ||
orderBy=order_by) | ||
|
||
while request is not None: | ||
response = request.execute(num_retries=self.num_retries) | ||
deployments.extend(response.get("deployments", [])) | ||
request = conn.deployments().list_next( # pylint: disable=no-member | ||
previous_request=request, previous_response=response | ||
) | ||
|
||
return deployments | ||
|
||
@GoogleBaseHook.fallback_to_default_project_id | ||
def delete_deployment(self, | ||
project_id: Optional[str], | ||
deployment: Optional[str] = None, | ||
delete_policy: Optional[str] = None): | ||
""" | ||
Deletes a deployment and all associated resources in a google cloud project. | ||
|
||
:param project_id: The project ID for this request. | ||
:type project_id: str | ||
:param deployment: The name of the deployment for this request. | ||
:type deployment: str | ||
:param delete_policy: Sets the policy to use for deleting resources. (ABANDON | DELETE) | ||
:type delete_policy: string | ||
|
||
:rtype: None | ||
""" | ||
conn = self.get_conn() | ||
request = conn.deployments().delete(project=project_id, # pylint: disable=no-member | ||
deployment=deployment, | ||
deletePolicy=delete_policy) | ||
resp = request.execute() | ||
if 'error' in resp.keys(): | ||
raise AirflowException('Errors deleting deployment: ', | ||
', '.join([err['message'] for err in resp['error']['errors']])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
# | ||
# 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 unittest import mock | ||
|
||
from airflow.exceptions import AirflowException | ||
from airflow.providers.google.cloud.hooks.gdm import GoogleDeploymentManagerHook | ||
|
||
|
||
def mock_init(self, gcp_conn_id, delegate_to=None): # pylint: disable=unused-argument | ||
pass | ||
|
||
|
||
TEST_PROJECT = 'my-project' | ||
TEST_DEPLOYMENT = 'my-deployment' | ||
|
||
|
||
class TestDeploymentManagerHook(unittest.TestCase): | ||
|
||
def setUp(self): | ||
with mock.patch( | ||
"airflow.providers.google.common.hooks.base_google.GoogleBaseHook.__init__", | ||
new=mock_init, | ||
): | ||
self.gdm_hook = GoogleDeploymentManagerHook(gcp_conn_id="test") | ||
|
||
@mock.patch("airflow.providers.google.cloud.hooks.gdm.GoogleDeploymentManagerHook.get_conn") | ||
def test_list_deployments(self, mock_get_conn): | ||
|
||
response1 = {'deployments': [{'id': 'deployment1', 'name': 'test-deploy1'}], 'pageToken': None} | ||
response2 = {'deployments': [{'id': 'deployment2', 'name': 'test-deploy2'}], 'pageToken': None} | ||
|
||
mock_get_conn.return_value.deployments.return_value.list.return_value.execute.return_value = response1 | ||
|
||
request_mock = mock.MagicMock() | ||
request_mock.execute.return_value = response2 | ||
mock_get_conn.return_value.deployments.return_value.list_next.side_effect = [ | ||
request_mock, | ||
None, | ||
] | ||
|
||
deployments = self.gdm_hook.list_deployments(project_id=TEST_PROJECT, | ||
deployment_filter='filter', | ||
order_by='name') | ||
|
||
mock_get_conn.assert_called_once_with() | ||
|
||
mock_get_conn.return_value.deployments.return_value.list.assert_called_once_with( | ||
project=TEST_PROJECT, | ||
filter='filter', | ||
orderBy='name', | ||
) | ||
|
||
self.assertEqual(mock_get_conn.return_value.deployments.return_value.list_next.call_count, 2) | ||
|
||
self.assertEqual(deployments, [{'id': 'deployment1', 'name': 'test-deploy1'}, | ||
{'id': 'deployment2', 'name': 'test-deploy2'}]) | ||
|
||
@mock.patch("airflow.providers.google.cloud.hooks.gdm.GoogleDeploymentManagerHook.get_conn") | ||
def test_delete_deployment(self, mock_get_conn): | ||
self.gdm_hook.delete_deployment(project_id=TEST_PROJECT, deployment=TEST_DEPLOYMENT) | ||
mock_get_conn.assert_called_once_with() | ||
mock_get_conn.return_value.deployments().delete.assert_called_once_with( | ||
project=TEST_PROJECT, | ||
deployment=TEST_DEPLOYMENT, | ||
deletePolicy=None | ||
) | ||
|
||
@mock.patch("airflow.providers.google.cloud.hooks.gdm.GoogleDeploymentManagerHook.get_conn") | ||
def test_delete_deployment_delete_fails(self, mock_get_conn): | ||
|
||
resp = {'error': {'errors': [{'message': 'error deleting things.', 'domain': 'global'}]}} | ||
|
||
mock_get_conn.return_value.deployments.return_value.delete.return_value.execute.return_value = resp | ||
|
||
with self.assertRaises(AirflowException): | ||
self.gdm_hook.delete_deployment(project_id=TEST_PROJECT, deployment=TEST_DEPLOYMENT) | ||
|
||
mock_get_conn.assert_called_once_with() | ||
mock_get_conn.return_value.deployments().delete.assert_called_once_with( | ||
project=TEST_PROJECT, | ||
deployment=TEST_DEPLOYMENT, | ||
deletePolicy=None | ||
) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a test when this operation fails?