def test_set_active_subscription(self):
        storage_mock = {'subscriptions': None}
        profile = Profile(storage_mock)

        consolidated = Profile._normalize_properties(self.user1,
                                                     [self.subscription1],
                                                     False)
        profile._set_subscriptions(consolidated)

        consolidated = profile._normalize_properties(self.user2,
                                                     [self.subscription2],
                                                     False)
        profile._set_subscriptions(consolidated)

        self.assertTrue(storage_mock['subscriptions'][1]['isDefault'])

        profile.set_active_subscription(storage_mock['subscriptions'][0]['id'])
        self.assertFalse(storage_mock['subscriptions'][1]['isDefault'])
        self.assertTrue(storage_mock['subscriptions'][0]['isDefault'])
Ejemplo n.º 2
0
def _set_active_subscription(context):
    ''' Set the active subscription used by Profile '''
    from azure.cli.core._profile import Profile, _ENVIRONMENT_NAME, _SUBSCRIPTION_ID, _STATE
    profile = Profile()
    subscriptions = profile.load_cached_subscriptions()
    context_file_path = get_context_file_path(context['name'])
    context_config = configparser.SafeConfigParser()
    context_config.read(context_file_path)
    subscription_to_use = None
    try:
        subscription_to_use = context_config.get('context', 'default_subscription')
    except (configparser.NoSectionError, configparser.NoOptionError):
        # Fallback to finding the first subscription that is for this cloud
        for sub in subscriptions:
            if sub[_ENVIRONMENT_NAME] == context['cloud'] and sub[_STATE] == 'Enabled':
                subscription_to_use = sub[_SUBSCRIPTION_ID]
                break
    if subscription_to_use:
        profile.set_active_subscription(subscription_to_use)
Ejemplo n.º 3
0
def _set_active_subscription(cloud_name):
    from azure.cli.core._profile import (Profile, _ENVIRONMENT_NAME, _SUBSCRIPTION_ID,
                                         _STATE, _SUBSCRIPTION_NAME)
    profile = Profile()
    subscription_to_use = get_cloud_subscription(cloud_name) or \
                          next((s[_SUBSCRIPTION_ID] for s in profile.load_cached_subscriptions()  # noqa
                                if s[_STATE] == 'Enabled'),
                               None)
    if subscription_to_use:
        try:
            profile.set_active_subscription(subscription_to_use)
            sub = profile.get_subscription(subscription_to_use)
            logger.warning("Active subscription switched to '%s (%s)'.",
                           sub[_SUBSCRIPTION_NAME], sub[_SUBSCRIPTION_ID])
        except CLIError as e:
            logger.warning(e)
            logger.warning("Unable to automatically switch the active subscription. "
                           "Use 'az account set'.")
    else:
        logger.warning("Use 'az login' to log in to this cloud.")
        logger.warning("Use 'az account set' to set the active subscription.")
Ejemplo n.º 4
0
def set_active_subscription(subscription):
    """Set the current subscription"""
    if not id:
        raise CLIError('Please provide subscription id or unique name.')
    profile = Profile()
    profile.set_active_subscription(subscription)
Ejemplo n.º 5
0
def set_active_subscription(cmd, subscription):
    """Set the current subscription"""
    profile = Profile(cli_ctx=cmd.cli_ctx)
    if not id:
        raise CLIError('Please provide subscription id or unique name.')
    profile.set_active_subscription(subscription)
Ejemplo n.º 6
0
def set_active_subscription(subscription):
    '''Set the current subscription'''
    if not id:
        raise CLIError('Please provide subscription id or unique name.')
    profile = Profile()
    profile.set_active_subscription(subscription)
Ejemplo n.º 7
0
def set_active_subscription(subscription_name_or_id):
    '''Set the current subscription'''
    if not id:
        raise CLIError('Please provide subscription id or unique name.')
    profile = Profile()
    profile.set_active_subscription(subscription_name_or_id)