Skip to content

Conversation

@mik-laj
Copy link
Member

@mik-laj mik-laj commented Aug 24, 2019

Currently project_id is read correctly only if project_id is configured in extra.

However, the default project_id can also be configured in other place.:

It makes much easier to configure the connection during development
Currently, I need to setup connection using following command:

export AIRFLOW_CONN_google_cloud_default='google-cloud-platform://?extra__google_cloud_platform__key_path=%2Fkeys%2Fsa.json&extra__google_cloud_platform__project=polidea-airflow'

but I would like to be able to configure the connection using the following command:

gcloud auth application-default login

or when I use a service account:

export GOOGLE_APPLICATION_CREDENTIALS=/keys/sa.json

CC: @ryanyuan


Make sure you have checked all steps below.

Jira

Description

Tests

  • My PR adds the following unit tests OR does not need testing for this extremely good reason:

Commits

  • My commits all reference Jira issues in their subject lines, and I have squashed multiple commits if they address the same issue. In addition, my commits follow the guidelines from "How to write a good git commit message":
    1. Subject is separated from body by a blank line
    2. Subject is limited to 50 characters (not including Jira issue reference)
    3. Subject does not end with a period
    4. Subject uses the imperative mood ("add", not "adding")
    5. Body wraps at 72 characters
    6. Body explains "what" and "why", not "how"

Documentation

  • In case of new functionality, my PR adds documentation that describes how to use it.
    • All the public functions and the classes in the PR contain docstrings that explain what it does
    • If you implement backwards incompatible changes, please leave a note in the Updating.md so we can assign it to a appropriate release

Code Quality

  • Passes flake8

@mik-laj mik-laj added the provider:google Google (including GCP) related issues label Aug 24, 2019
@mik-laj mik-laj force-pushed the AIRFLOW-5303 branch 2 times, most recently from 2466d16 to abddb0b Compare August 24, 2019 16:38
@mik-laj mik-laj changed the title [AIRFLOW-5303] Return project_id for other authorization methods [AIRFLOW-5303][WIP] Return project_id for other authorization methods Aug 24, 2019
@mik-laj mik-laj requested review from kaxil and potiuk August 24, 2019 20:15
@mik-laj mik-laj changed the title [AIRFLOW-5303][WIP] Return project_id for other authorization methods [AIRFLOW-5303] Return project_id for other authorization methods Aug 24, 2019
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if this piece of code should not be transferred to the _get_credentials_and_project_id method. Then this method will never return an invalid value and it will be easier to use it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated.

@mik-laj mik-laj requested a review from jmcarp August 25, 2019 10:41
@mik-laj mik-laj changed the title [AIRFLOW-5303] Return project_id for other authorization methods [AIRFLOW-5303] Use project_id from other authorization methods Aug 25, 2019
@mik-laj mik-laj changed the title [AIRFLOW-5303] Use project_id from other authorization methods [AIRFLOW-5303] Use project_id from gcp credentials Aug 25, 2019
@mik-laj mik-laj changed the title [AIRFLOW-5303] Use project_id from gcp credentials [AIRFLOW-5303] Use project_id from GCP credentials Aug 25, 2019
Copy link
Member

@kaxil kaxil left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM but would be good if we can add the test case mentioned in last comment

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a test case for this?

(1) Mock project_id as None from credential object & being overriden by project_id from connection?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a 8 tests. I am testing a lot of different cases now.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


@mock.patch( # type: ignore
MODULE_NAME + '.google.oauth2.service_account.Credentials.from_service_account_file',
**{'reutnr_value.project_id': "PROJECT_ID"}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
**{'reutnr_value.project_id': "PROJECT_ID"}
**{'return_value.project_id': "PROJECT_ID"}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. I also added missing assertion.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

}
result = self.instance._get_credentials_and_project_id()
mock_from_service_account_file.assert_called_once_with(service_account, scopes=self.instance.scopes)
self.assertEqual((mock_from_service_account_file.return_value, 'PROJECT_ID'), result)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(mock_from_service_account_file.return_value, 'PROJECT_ID') -> The first item is credential block?

Copy link
Member Author

@mik-laj mik-laj Aug 29, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. This is a credentials object because the from_service_account_info method is the Credentials class factory method. It's allways return Credentials object. This PR does not change the behavior of it.

All tests pass locally, but let's wait for Travis.
Screenshot 2019-08-30 at 00 15 30

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

provider:google Google (including GCP) related issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants