|
14 | 14 |
|
15 | 15 | """Helpers for reading the Google Cloud SDK's configuration.""" |
16 | 16 |
|
| 17 | +import io |
17 | 18 | import os |
18 | 19 |
|
19 | 20 | import six |
|
32 | 33 | # The name of the file in the Cloud SDK config that contains default |
33 | 34 | # credentials. |
34 | 35 | _CREDENTIALS_FILENAME = 'application_default_credentials.json' |
35 | | -# The name of the file in the Cloud SDK config that contains the |
36 | | -# active configuration. |
37 | | -_ACTIVE_CONFIG_FILENAME = os.path.join( |
38 | | - 'configurations', 'config_default') |
39 | 36 | # The config section and key for the project ID in the cloud SDK config. |
40 | 37 | _PROJECT_CONFIG_SECTION = 'core' |
41 | 38 | _PROJECT_CONFIG_KEY = 'project' |
@@ -83,14 +80,49 @@ def get_application_default_credentials_path(): |
83 | 80 | return os.path.join(config_path, _CREDENTIALS_FILENAME) |
84 | 81 |
|
85 | 82 |
|
| 83 | +def _get_active_config(config_path): |
| 84 | + """Gets the active config for the Cloud SDK. |
| 85 | +
|
| 86 | + Args: |
| 87 | + config_path (str): The Cloud SDK's config path. |
| 88 | +
|
| 89 | + Returns: |
| 90 | + str: The active configuration name. |
| 91 | + """ |
| 92 | + active_config_filename = os.path.join(config_path, 'active_config') |
| 93 | + |
| 94 | + if not os.path.isfile(active_config_filename): |
| 95 | + return 'default' |
| 96 | + |
| 97 | + with io.open(active_config_filename, 'r', encoding='utf-8') as file_obj: |
| 98 | + active_config_name = file_obj.read().strip() |
| 99 | + |
| 100 | + return active_config_name |
| 101 | + |
| 102 | + |
| 103 | +def _get_config_file(config_path, config_name): |
| 104 | + """Returns the full path to a configuration's config file. |
| 105 | +
|
| 106 | + Args: |
| 107 | + config_path (str): The Cloud SDK's config path. |
| 108 | + config_name (str): The configuration name. |
| 109 | +
|
| 110 | + Returns: |
| 111 | + str: The config file path. |
| 112 | + """ |
| 113 | + return os.path.join( |
| 114 | + config_path, 'configurations', 'config_{}'.format(config_name)) |
| 115 | + |
| 116 | + |
86 | 117 | def get_project_id(): |
87 | 118 | """Gets the project ID from the Cloud SDK's configuration. |
88 | 119 |
|
89 | 120 | Returns: |
90 | 121 | Optional[str]: The project ID. |
91 | 122 | """ |
92 | 123 | config_path = get_config_path() |
93 | | - config_file = os.path.join(config_path, _ACTIVE_CONFIG_FILENAME) |
| 124 | + active_config = _get_active_config(config_path) |
| 125 | + config_file = _get_config_file(config_path, active_config) |
94 | 126 |
|
95 | 127 | if not os.path.isfile(config_file): |
96 | 128 | return None |
|
0 commit comments