19
19
20
20
def get_client_id (project_id , location , composer_environment ):
21
21
# [START composer_get_environment_client_id]
22
+ # This script is intended to be used with Composer 1 environments
23
+ # In Composer 2, the Airflow Webserver is not in the tenant project
24
+ # so there is no tenant client ID
25
+ # See https://cloud.google.com/composer/docs/composer-2/environment-architecture
26
+ # for more details
22
27
import google .auth
23
28
import google .auth .transport .requests
24
29
import requests
@@ -27,45 +32,52 @@ def get_client_id(project_id, location, composer_environment):
27
32
# Authenticate with Google Cloud.
28
33
# See: https://cloud.google.com/docs/authentication/getting-started
29
34
credentials , _ = google .auth .default (
30
- scopes = [' https://www.googleapis.com/auth/cloud-platform' ])
31
- authed_session = google . auth . transport . requests . AuthorizedSession (
32
- credentials )
35
+ scopes = [" https://www.googleapis.com/auth/cloud-platform" ]
36
+ )
37
+ authed_session = google . auth . transport . requests . AuthorizedSession ( credentials )
33
38
34
39
# project_id = 'YOUR_PROJECT_ID'
35
40
# location = 'us-central1'
36
41
# composer_environment = 'YOUR_COMPOSER_ENVIRONMENT_NAME'
37
42
38
43
environment_url = (
39
- 'https://composer.googleapis.com/v1beta1/projects/{}/locations/{}'
40
- '/environments/{}' ).format (project_id , location , composer_environment )
41
- composer_response = authed_session .request ('GET' , environment_url )
44
+ "https://composer.googleapis.com/v1beta1/projects/{}/locations/{}"
45
+ "/environments/{}"
46
+ ).format (project_id , location , composer_environment )
47
+ composer_response = authed_session .request ("GET" , environment_url )
42
48
environment_data = composer_response .json ()
43
- airflow_uri = environment_data ['config' ]['airflowUri' ]
49
+ composer_version = environment_data ["config" ]["softwareConfig" ]["imageVersion" ]
50
+ if "composer-1" not in composer_version :
51
+ version_error = ("This script is intended to be used with Composer 1 environments. "
52
+ "In Composer 2, the Airflow Webserver is not in the tenant project, "
53
+ "so there is no tenant client ID. "
54
+ "See https://cloud.google.com/composer/docs/composer-2/environment-architecture for more details." )
55
+ raise (RuntimeError (version_error ))
56
+ airflow_uri = environment_data ["config" ]["airflowUri" ]
44
57
45
58
# The Composer environment response does not include the IAP client ID.
46
59
# Make a second, unauthenticated HTTP request to the web server to get the
47
60
# redirect URI.
48
61
redirect_response = requests .get (airflow_uri , allow_redirects = False )
49
- redirect_location = redirect_response .headers [' location' ]
62
+ redirect_location = redirect_response .headers [" location" ]
50
63
51
64
# Extract the client_id query parameter from the redirect.
52
65
parsed = six .moves .urllib .parse .urlparse (redirect_location )
53
66
query_string = six .moves .urllib .parse .parse_qs (parsed .query )
54
- print (query_string [' client_id' ][0 ])
67
+ print (query_string [" client_id" ][0 ])
55
68
# [END composer_get_environment_client_id]
56
69
57
70
58
71
# Usage: python get_client_id.py your_project_id your_region your_environment_name
59
- if __name__ == ' __main__' :
72
+ if __name__ == " __main__" :
60
73
parser = argparse .ArgumentParser (
61
- description = __doc__ ,
62
- formatter_class = argparse .RawDescriptionHelpFormatter )
63
- parser .add_argument ('project_id' , help = 'Your Project ID.' )
74
+ description = __doc__ , formatter_class = argparse .RawDescriptionHelpFormatter
75
+ )
76
+ parser .add_argument ("project_id" , help = "Your Project ID." )
77
+ parser .add_argument ("location" , help = "Region of the Cloud Composer environment." )
64
78
parser .add_argument (
65
- 'location' , help = 'Region of the Cloud Composer environment.' )
66
- parser .add_argument (
67
- 'composer_environment' , help = 'Name of the Cloud Composer environment.' )
79
+ "composer_environment" , help = "Name of the Cloud Composer environment."
80
+ )
68
81
69
82
args = parser .parse_args ()
70
- get_client_id (
71
- args .project_id , args .location , args .composer_environment )
83
+ get_client_id (args .project_id , args .location , args .composer_environment )
0 commit comments