【问题标题】:Google cloud composer get airflow webserver_id谷歌云作曲家获取气流 webserver_id
【发布时间】:2020-06-29 14:36:52
【问题描述】:
我有一个 GCP 项目 my_project_id,其中包含一个作曲家实例 my_project_id_cmpsr_id。为了访问 Airflow REST API,我需要检索所谓的webserver_id。因此,GCP 气流网络服务器 url 的格式为 {webserver-id}.appspot.com,在 documentation 中指定
# This should be part of your webserver's URL:
# {tenant-project-id}.appspot.com
webserver_id = 'YOUR-TENANT-PROJECT'
是否可以通过project_id 和composer_id 检索fg8348538536e2df34-fd 之类的webserver_id?
【问题讨论】:
标签:
python
rest
google-cloud-platform
airflow
google-cloud-composer
【解决方案1】:
您可以通过在Google docs 上运行代码示例来获取 Python 所需的值,然后对其进行修改以返回 webserver_id 和 client_id:
#!/usr/bin/env python
# coding: utf-8
import google.auth
import google.auth.transport.requests
import requests
import six.moves.urllib.parse
project_id = getenv('GCP_PROJECT_ID')
location = getenv('LOCATION')
composer_environment = getenv('COMPOSER_ID')
def get_airflow_details():
# Authenticate with Google Cloud.
# See: https://cloud.google.com/docs/authentication/getting-started
credentials, _ = google.auth.default(
scopes=['https://www.googleapis.com/auth/cloud-platform'])
authed_session = google.auth.transport.requests.AuthorizedSession(
credentials)
environment_url = (
'https://composer.googleapis.com/v1beta1/projects/{}/locations/{}'
'/environments/{}').format(project_id, location, composer_environment)
composer_response = authed_session.request('GET', environment_url)
environment_data = composer_response.json()
airflow_uri = environment_data['config']['airflowUri']
# The Composer environment response does not include the IAP client ID.
# Make a second, unauthenticated HTTP request to the web server to get the
# redirect URI.
redirect_response = requests.get(airflow_uri, allow_redirects=False)
redirect_location = redirect_response.headers['location']
# Extract the client_id query parameter from the redirect.
parsed = six.moves.urllib.parse.urlparse(redirect_location)
query_string = six.moves.urllib.parse.parse_qs(parsed.query)
client_id = (query_string['client_id'][0])
return client_id, airflow_uri
def main():
get_airflow_details()
client_id = (get_airflow_details()[0])
airflow_uri = (get_airflow_details()[1])
print(client_id)
print(airflow_uri)
return 'Script has run without errors !!'
if (__name__ == "__main__"):
main()
【解决方案2】:
有可能,您可以转到您的 Airflow UI,而不是 Admin -> Configuration,然后搜索 base_url 键,这是您的 webserver-id(不包括 https:// 和 .appspot.com 部分)。
另一种方法是使用以下命令:
gcloud composer environments describe <ENVIRONMENT_NAME> --location <LOCATION>
您将能够看到config: -> airflowUri 变量。
希望对你有帮助。