1818
1919logger = get_logger (__name__ )
2020
21+ # This default cloud name. If you use DummyCli(random_config_dir=True), use get_cloud_config_file() instead.
2122CLOUD_CONFIG_FILE = os .path .join (GLOBAL_CONFIG_DIR , 'clouds.config' )
2223
24+
25+ def get_cloud_config_file (cli_ctx = None ):
26+ return os .path .join (cli_ctx .config .config_dir , 'clouds.config' ) if cli_ctx else CLOUD_CONFIG_FILE
27+
28+
2329# Add names of clouds that don't allow telemetry data collection here such as some air-gapped clouds.
2430CLOUDS_FORBIDDING_TELEMETRY = ['USSec' , 'USNat' ]
2531
@@ -511,10 +517,10 @@ def get_clouds(cli_ctx):
511517 for c in KNOWN_CLOUDS :
512518 _config_add_cloud (config , c )
513519 try :
514- config .read (CLOUD_CONFIG_FILE )
520+ config .read (get_cloud_config_file ( cli_ctx ) )
515521 except configparser .MissingSectionHeaderError :
516- os .remove (CLOUD_CONFIG_FILE )
517- logger .warning ("'%s' is in bad format and has been removed." , CLOUD_CONFIG_FILE )
522+ os .remove (get_cloud_config_file ( cli_ctx ) )
523+ logger .warning ("'%s' is in bad format and has been removed." , get_cloud_config_file ( cli_ctx ) )
518524 for section in config .sections ():
519525 c = Cloud (section )
520526 for option in config .options (section ):
@@ -563,9 +569,9 @@ def get_active_cloud(cli_ctx=None):
563569 return get_cloud (cli_ctx , default_cloud_name )
564570
565571
566- def get_cloud_subscription (cloud_name ):
572+ def get_cloud_subscription (cloud_name , cli_ctx = None ):
567573 config = configparser .ConfigParser ()
568- config .read (CLOUD_CONFIG_FILE )
574+ config .read (get_cloud_config_file ( cli_ctx ) )
569575 try :
570576 return config .get (cloud_name , 'subscription' )
571577 except (configparser .NoOptionError , configparser .NoSectionError ):
@@ -576,7 +582,7 @@ def set_cloud_subscription(cli_ctx, cloud_name, subscription):
576582 if not _get_cloud (cli_ctx , cloud_name ):
577583 raise CloudNotRegisteredException (cloud_name )
578584 config = configparser .ConfigParser ()
579- config .read (CLOUD_CONFIG_FILE )
585+ config .read (get_cloud_config_file ( cli_ctx ) )
580586 if subscription :
581587 try :
582588 config .add_section (cloud_name )
@@ -590,15 +596,15 @@ def set_cloud_subscription(cli_ctx, cloud_name, subscription):
590596 pass
591597 if not os .path .isdir (GLOBAL_CONFIG_DIR ):
592598 os .makedirs (GLOBAL_CONFIG_DIR )
593- with open (CLOUD_CONFIG_FILE , 'w' ) as configfile :
599+ with open (get_cloud_config_file ( cli_ctx ) , 'w' ) as configfile :
594600 config .write (configfile )
595601
596602
597603def _set_active_subscription (cli_ctx , cloud_name ):
598604 from azure .cli .core ._profile import (Profile , _ENVIRONMENT_NAME , _SUBSCRIPTION_ID ,
599605 _STATE , _SUBSCRIPTION_NAME )
600606 profile = Profile (cli_ctx = cli_ctx )
601- subscription_to_use = get_cloud_subscription (cloud_name ) or \
607+ subscription_to_use = get_cloud_subscription (cloud_name , cli_ctx ) or \
602608 next ((s [_SUBSCRIPTION_ID ] for s in profile .load_cached_subscriptions () # noqa
603609 if s [_STATE ] == 'Enabled' ),
604610 None )
@@ -644,20 +650,20 @@ def _config_add_cloud(config, cloud, overwrite=False):
644650 config .set (cloud .name , 'suffix_{}' .format (k ), v )
645651
646652
647- def _save_cloud (cloud , overwrite = False ):
653+ def _save_cloud (cloud , overwrite = False , cli_ctx = None ):
648654 config = configparser .ConfigParser ()
649- config .read (CLOUD_CONFIG_FILE )
655+ config .read (get_cloud_config_file ( cli_ctx ) )
650656 _config_add_cloud (config , cloud , overwrite = overwrite )
651657 if not os .path .isdir (GLOBAL_CONFIG_DIR ):
652658 os .makedirs (GLOBAL_CONFIG_DIR )
653- with open (CLOUD_CONFIG_FILE , 'w' ) as configfile :
659+ with open (get_cloud_config_file ( cli_ctx ) , 'w' ) as configfile :
654660 config .write (configfile )
655661
656662
657663def add_cloud (cli_ctx , cloud ):
658664 if _get_cloud (cli_ctx , cloud .name ):
659665 raise CloudAlreadyRegisteredException (cloud .name )
660- _save_cloud (cloud )
666+ _save_cloud (cloud , cli_ctx = cli_ctx )
661667
662668
663669def update_cloud (cli_ctx , cloud ):
@@ -677,9 +683,9 @@ def remove_cloud(cli_ctx, cloud_name):
677683 raise CannotUnregisterCloudException ("The cloud '{}' cannot be unregistered "
678684 "as it's not a custom cloud." .format (cloud_name ))
679685 config = configparser .ConfigParser ()
680- config .read (CLOUD_CONFIG_FILE )
686+ config .read (get_cloud_config_file ( cli_ctx ) )
681687 config .remove_section (cloud_name )
682- with open (CLOUD_CONFIG_FILE , 'w' ) as configfile :
688+ with open (get_cloud_config_file ( cli_ctx ) , 'w' ) as configfile :
683689 config .write (configfile )
684690
685691
0 commit comments