diff --git a/awscli/customizations/configure/set.py b/awscli/customizations/configure/set.py index 81a0d3d66618..270479a2f227 100644 --- a/awscli/customizations/configure/set.py +++ b/awscli/customizations/configure/set.py @@ -50,6 +50,13 @@ def _get_config_file(self, path): config_path = self._session.get_config_variable(path) return os.path.expanduser(config_path) + def _is_known_profile_name(self, name): + """Return whether name is a configured profile (mirrors configure get).""" + full_config = getattr(self._session, 'full_config', None) + if full_config is None: + return False + return name in full_config.get('profiles', {}) + def _run_main(self, args, parsed_globals): varname = args.varname value = args.value @@ -80,6 +87,14 @@ def _run_main(self, args, parsed_globals): varname = remaining[0] if len(remaining) == 2: value = {remaining[1]: value} + elif self._is_known_profile_name(parts[0]): + # Same syntax as configure get, e.g. + # emr-dev.emr.instance_profile + profile = parts[0] + remaining = parts[1:] + varname = remaining[0] + if len(remaining) == 2: + value = {remaining[1]: value} elif parts[0] not in PREDEFINED_SECTION_NAMES: if self._session.profile is not None: profile = self._session.profile diff --git a/tests/functional/configure/test_configure.py b/tests/functional/configure/test_configure.py index 83f9437c3423..e1b6055f9480 100644 --- a/tests/functional/configure/test_configure.py +++ b/tests/functional/configure/test_configure.py @@ -358,6 +358,28 @@ def test_get_nested_attribute(self): ) self.assertEqual(stdout, "") + def test_set_get_roundtrip_with_profile_name_prefix(self): + self.set_config_file_contents( + "[profile emr-dev]\n" + "region = us-west-1\n" + ) + self.run_cmd( + "configure set emr-dev.emr.instance_profile my_ip_emr", + ) + self.assertEqual( + "[profile emr-dev]\n" + "region = us-west-1\n" + "emr =\n" + " instance_profile = my_ip_emr\n", + self.get_config_file_contents(), + ) + # Reload the driver so configure get sees the updated config file. + self.driver = create_clidriver() + stdout, _, _ = self.run_cmd( + "configure get emr-dev.emr.instance_profile" + ) + self.assertEqual(stdout.strip(), "my_ip_emr") + def test_can_handle_empty_section(self): self.set_config_file_contents("[default]\n") self.run_cmd( diff --git a/tests/unit/customizations/configure/test_set.py b/tests/unit/customizations/configure/test_set.py index 2b8f5c0d7dfe..3813331a2797 100644 --- a/tests/unit/customizations/configure/test_set.py +++ b/tests/unit/customizations/configure/test_set.py @@ -70,6 +70,30 @@ def test_configure_set_command_dotted_with_profile(self): {'__section__': 'profile emr-dev', 'emr': {'instance_profile': 'my_ip_emr'}}, 'myconfigfile') + def test_configure_set_dotted_with_profile_name_prefix(self): + # Mirrors configure get syntax (test_dotted_get_with_profile). + self.session.full_config = {'profiles': {'emr-dev': { + 'emr': {'instance_profile': 'old_ip'}}}} + set_command = ConfigureSetCommand( + self.session, self.config_writer) + set_command( + args=['emr-dev.emr.instance_profile', 'my_ip_emr'], + parsed_globals=None) + self.config_writer.update_config.assert_called_with( + {'__section__': 'profile emr-dev', 'emr': + {'instance_profile': 'my_ip_emr'}}, 'myconfigfile') + + def test_configure_set_dotted_profile_name_two_part_varname(self): + self.session.full_config = {'profiles': {'emr-dev': { + 'region': 'us-west-1'}}} + set_command = ConfigureSetCommand( + self.session, self.config_writer) + set_command( + args=['emr-dev.region', 'us-west-2'], parsed_globals=None) + self.config_writer.update_config.assert_called_with( + {'__section__': 'profile emr-dev', 'region': 'us-west-2'}, + 'myconfigfile') + def test_configure_set_with_profile(self): self.session.profile = 'testing' set_command = ConfigureSetCommand(