Skip to content
Draft
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/azure-cli-core/azure/cli/core/cloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ def _add_starting_dot(suffix):
def _get_arm_endpoint(arm_dict, is_suffix=False):
def _get_processed_arm_endpoint(name, add_dot=False, fallback_value=None):
if is_suffix:
return (_add_starting_dot(arm_dict['suffixes'][name]) if add_dot else arm_dict['suffixes'][name]) if name in arm_dict['suffixes'] else fallback_value
return (_add_starting_dot(arm_dict['suffixes'][name]) if add_dot else arm_dict['suffixes'][name]) if 'suffixes' in arm_dict and name in arm_dict['suffixes'] else fallback_value
Copy link
Member

@jiasli jiasli Jun 10, 2025

Choose a reason for hiding this comment

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

If arm_dict does contain suffixes but arm_dict['suffixes'] is None, name in arm_dict['suffixes'] will fail:

arm_dict = {'suffixes': None}
name = 'test'
print('suffixes' in arm_dict and name in arm_dict['suffixes'])

Output:

Traceback (most recent call last):
  File "D:\cli\testproj\main.py", line 3, in <module>
    print('suffixes' in arm_dict and name in arm_dict['suffixes'])
                                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: argument of type 'NoneType' is not iterable

It is hard to enumerate all possibilities for an undocumented API.

Copy link
Member Author

@evelyn-ys evelyn-ys Jun 10, 2025

Choose a reason for hiding this comment

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

Nice catch! Updated to use if name in arm_dict.get('suffixes', {}) now

return arm_dict[name] if name in arm_dict else fallback_value
return _get_processed_arm_endpoint

Expand Down
7 changes: 6 additions & 1 deletion src/azure-cli/azure/cli/command_modules/cloud/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,12 @@ def _populate_from_metadata_endpoint(arm_endpoint, session=None):
response = session.get(metadata_endpoint)
if response.status_code == 200:
metadata = response.json()
return _arm_to_cli_mapper(metadata)
if isinstance(metadata, dict) and metadata:
return _arm_to_cli_mapper(metadata)
elif isinstance(metadata, list) and metadata:
return _arm_to_cli_mapper(metadata[0])
msg = 'Response body does not contain valid json. Response content: {}'.format(str(metadata))
raise CLIError(error_msg_fmt.format(msg))
msg = 'Server returned status code {} for {}'.format(response.status_code, metadata_endpoint)
raise CLIError(error_msg_fmt.format(msg))
except (requests.exceptions.ConnectionError, requests.exceptions.HTTPError) as err:
Expand Down
Loading