# Buggy code
def set_cloud_subscription(cloud_name, subscription):
    if not _get_cloud(cloud_name):
        raise CloudNotRegisteredException(cloud_name)
    config = get_config_parser()
    config.read(CLOUD_CONFIG_FILE)
    if subscription:
        config.set(cloud_name, "subscription", subscription)
    else:
        config.remove_option(cloud_name, "subscription")
    if not os.path.isdir(GLOBAL_CONFIG_DIR):
        os.makedirs(GLOBAL_CONFIG_DIR)
    with open(CLOUD_CONFIG_FILE, "w") as configfile:
        config.write(configfile)


# 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 set_cloud_subscription(cloud_name, subscription):
    if not _get_cloud(cloud_name):
        raise CloudNotRegisteredException(cloud_name)
    config = get_config_parser()
    config.read(CLOUD_CONFIG_FILE)
    if subscription:
        try:
            config.add_section(cloud_name)
        except configparser.DuplicateSectionError:
            pass
        config.set(cloud_name, "subscription", subscription)
    else:
        try:
            config.remove_option(cloud_name, "subscription")
        except configparser.NoSectionError:
            pass
    if not os.path.isdir(GLOBAL_CONFIG_DIR):
        os.makedirs(GLOBAL_CONFIG_DIR)
    with open(CLOUD_CONFIG_FILE, "w") as configfile:
        config.write(configfile)