# Buggy code
def get_clouds():
    clouds = []
    # load the config again as it may have changed
    config = get_config_parser()
    config.read(CLOUD_CONFIG_FILE)
    for section in config.sections():
        c = Cloud(section)
        for option in config.options(section):
            if option == "profile":
                c.profile = config.get(section, option)
            if option.startswith("endpoint_"):
                setattr(
                    c.endpoints,
                    option.replace("endpoint_", ""),
                    config.get(section, option),
                )
            elif option.startswith("suffix_"):
                setattr(
                    c.suffixes,
                    option.replace("suffix_", ""),
                    config.get(section, option),
                )
        if c.profile is None:
            # If profile isn't set, use latest
            setattr(c, "profile", "latest")
        if c.profile not in API_PROFILES:
            raise CLIError(
                "Profile {} does not exist or is not supported.".format(c.profile)
            )
        if not c.endpoints.has_endpoint_set(
            "management"
        ) and c.endpoints.has_endpoint_set("resource_manager"):
            # If management endpoint not set, use resource manager endpoint
            c.endpoints.management = c.endpoints.resource_manager
        clouds.append(c)
    active_cloud_name = get_active_cloud_name()
    for c in clouds:
        if c.name == active_cloud_name:
            c.is_active = True
            break
    return clouds


# Traceback (CloudNotRegisteredException)
ERROR: The cloud 'AzureCloud' is not registered.
Traceback (most recent call last):
File "/root/lib/azure-cli/local/lib/python2.7/site-packages/azure/cli/main.py", line 36, in main
cmd_result = APPLICATION.execute(args)
File "/root/lib/azure-cli/local/lib/python2.7/site-packages/azure/cli/core/application.py", line 203, in execute
result = expanded_arg.func(params)
File "/root/lib/azure-cli/local/lib/python2.7/site-packages/azure/cli/core/commands/__init__.py", line 278, in __call__
return self.handler(*args, **kwargs)
File "/root/lib/azure-cli/local/lib/python2.7/site-packages/azure/cli/core/commands/__init__.py", line 473, in _execute_command
reraise(*sys.exc_info())
File "/root/lib/azure-cli/local/lib/python2.7/site-packages/azure/cli/core/commands/__init__.py", line 450, in _execute_command
result = op(client, **kwargs) if client else op(**kwargs)
File "/root/lib/azure-cli/local/lib/python2.7/site-packages/azure/cli/command_modules/resource/custom.py", line 229, in deploy_arm_template
deployment_name, parameters, mode, no_wait=no_wait)
File "/root/lib/azure-cli/local/lib/python2.7/site-packages/azure/cli/command_modules/resource/custom.py", line 333, in _deploy_arm_template_core
smc = get_mgmt_service_client(ResourceType.MGMT_RESOURCE_RESOURCES)
File "/root/lib/azure-cli/local/lib/python2.7/site-packages/azure/cli/core/commands/client_factory.py", line 32, in get_mgmt_service_client
api_version=api_version, **kwargs)
File "/root/lib/azure-cli/local/lib/python2.7/site-packages/azure/cli/core/commands/client_factory.py", line 66, in _get_mgmt_service_client
cred, subscription_id, _ = profile.get_login_credentials(subscription_id=subscription_id)
File "/root/lib/azure-cli/local/lib/python2.7/site-packages/azure/cli/core/_profile.py", line 314, in get_login_credentials
account = self.get_subscription(subscription_id)
File "/root/lib/azure-cli/local/lib/python2.7/site-packages/azure/cli/core/_profile.py", line 291, in get_subscription
subscriptions = self.load_cached_subscriptions()
File "/root/lib/azure-cli/local/lib/python2.7/site-packages/azure/cli/core/_profile.py", line 276, in load_cached_subscriptions
active_cloud = get_active_cloud()
File "/root/lib/azure-cli/local/lib/python2.7/site-packages/azure/cli/core/cloud.py", line 283, in get_active_cloud
return get_cloud(get_active_cloud_name())
File "/root/lib/azure-cli/local/lib/python2.7/site-packages/azure/cli/core/cloud.py", line 278, in get_cloud
raise CloudNotRegisteredException(cloud_name)
CloudNotRegisteredException: The cloud 'AzureCloud' is not registered.

# Fixed code
def get_clouds():
    clouds = []
    config = get_config_parser()
    # Start off with known clouds and apply config file on top of current config
    for c in KNOWN_CLOUDS:
        _config_add_cloud(config, c)
    config.read(CLOUD_CONFIG_FILE)
    for section in config.sections():
        c = Cloud(section)
        for option in config.options(section):
            if option == "profile":
                c.profile = config.get(section, option)
            if option.startswith("endpoint_"):
                setattr(
                    c.endpoints,
                    option.replace("endpoint_", ""),
                    config.get(section, option),
                )
            elif option.startswith("suffix_"):
                setattr(
                    c.suffixes,
                    option.replace("suffix_", ""),
                    config.get(section, option),
                )
        if c.profile is None:
            # If profile isn't set, use latest
            setattr(c, "profile", "latest")
        if c.profile not in API_PROFILES:
            raise CLIError(
                "Profile {} does not exist or is not supported.".format(c.profile)
            )
        if not c.endpoints.has_endpoint_set(
            "management"
        ) and c.endpoints.has_endpoint_set("resource_manager"):
            # If management endpoint not set, use resource manager endpoint
            c.endpoints.management = c.endpoints.resource_manager
        clouds.append(c)
    active_cloud_name = get_active_cloud_name()
    for c in clouds:
        if c.name == active_cloud_name:
            c.is_active = True
            break
    return clouds