support specify user-id when create sample and alarm
The change of I9ba984f804aecee5b63e5a86e699733040dee245 enables
us to specify os-user-id in CLI, but it shadows the user-id when
creating sample and alarm, this patch fixes it as what we've done
for project-id.
Change-Id: Ic3ce5dc8e1aea49b3e41fe495be4921fbf85ea5a
ref-change: I0ce2416dccd61eb50584799e6df0b8c45d44cdda
(cherry picked from commit f72dc0c389)
			
			
This commit is contained in:
		 ZhiQiang Fan
					ZhiQiang Fan
				
			
				
					committed by
					
						 gordon chung
						gordon chung
					
				
			
			
				
	
			
			
			 gordon chung
						gordon chung
					
				
			
						parent
						
							94a6317128
						
					
				
				
					commit
					651350ffa6
				
			| @@ -1124,45 +1124,51 @@ class ShellEventListCommandTest(utils.BaseTestCase): | ||||
|  | ||||
| class ShellShadowedArgsTest(test_shell.ShellTestBase): | ||||
|  | ||||
|     def _test_project_id_alarm(self, command, args, method): | ||||
|     def _test_shadowed_args_alarm(self, command, args, method): | ||||
|         self.make_env(test_shell.FAKE_V2_ENV) | ||||
|         cli_args = [ | ||||
|             '--os-project-id', '0ba30185ddf44834914a0b859d244c56', | ||||
|             '--os-user-id', '85f59b3b17484ccb974c50596023bf8c', | ||||
|             '--debug', command, | ||||
|             '--project-id', 'the-project-id-i-want-to-set', | ||||
|             '--user-id', 'the-user-id-i-want-to-set', | ||||
|             '--name', 'project-id-test'] + args | ||||
|         with mock.patch.object(alarms.AlarmManager, method) as mocked: | ||||
|             base_shell.main(cli_args) | ||||
|         args, kwargs = mocked.call_args | ||||
|         self.assertEqual('the-project-id-i-want-to-set', | ||||
|                          kwargs.get('project_id')) | ||||
|         self.assertEqual('the-user-id-i-want-to-set', | ||||
|                          kwargs.get('user_id')) | ||||
|  | ||||
|     def test_project_id_threshold_alarm(self): | ||||
|     def test_shadowed_args_threshold_alarm(self): | ||||
|         cli_args = ['--meter-name', 'cpu', '--threshold', '90'] | ||||
|         self._test_project_id_alarm('alarm-create', cli_args, 'create') | ||||
|         self._test_project_id_alarm('alarm-threshold-create', | ||||
|                                     cli_args, 'create') | ||||
|         self._test_shadowed_args_alarm('alarm-create', cli_args, 'create') | ||||
|         self._test_shadowed_args_alarm('alarm-threshold-create', | ||||
|                                        cli_args, 'create') | ||||
|         cli_args += ['--alarm_id', '437b7ed0-3733-4054-a877-e9a297b8be85'] | ||||
|         self._test_project_id_alarm('alarm-update', cli_args, 'update') | ||||
|         self._test_project_id_alarm('alarm-threshold-update', | ||||
|                                     cli_args, 'update') | ||||
|         self._test_shadowed_args_alarm('alarm-update', cli_args, 'update') | ||||
|         self._test_shadowed_args_alarm('alarm-threshold-update', | ||||
|                                        cli_args, 'update') | ||||
|  | ||||
|     def test_project_id_combination_alarm(self): | ||||
|     def test_shadowed_args_combination_alarm(self): | ||||
|         cli_args = ['--alarm_ids', 'fb16a05a-669d-414e-8bbe-93aa381df6a8', | ||||
|                     '--alarm_ids', 'b189bcca-0a7b-49a9-a244-a927ac291881'] | ||||
|         self._test_project_id_alarm('alarm-combination-create', | ||||
|                                     cli_args, 'create') | ||||
|         self._test_shadowed_args_alarm('alarm-combination-create', | ||||
|                                        cli_args, 'create') | ||||
|         cli_args += ['--alarm_id', '437b7ed0-3733-4054-a877-e9a297b8be85'] | ||||
|         self._test_project_id_alarm('alarm-combination-update', | ||||
|                                     cli_args, 'update') | ||||
|         self._test_shadowed_args_alarm('alarm-combination-update', | ||||
|                                        cli_args, 'update') | ||||
|  | ||||
|     @mock.patch.object(samples.OldSampleManager, 'create') | ||||
|     def test_project_id_sample_create(self, mocked): | ||||
|     def test_shadowed_args_sample_create(self, mocked): | ||||
|         self.make_env(test_shell.FAKE_V2_ENV) | ||||
|         cli_args = [ | ||||
|             '--os-project-id', '0ba30185ddf44834914a0b859d244c56', | ||||
|             '--os-user-id', '85f59b3b17484ccb974c50596023bf8c', | ||||
|             '--debug', 'sample-create', | ||||
|             '--project-id', 'the-project-id-i-want-to-set', | ||||
|             '--user-id', 'the-user-id-i-want-to-set', | ||||
|             '--resource-id', 'b666633d-9bb6-4e05-89c0-ee5a8752fb0b', | ||||
|             '--meter-name', 'cpu', | ||||
|             '--meter-type', 'cumulative', | ||||
| @@ -1173,3 +1179,5 @@ class ShellShadowedArgsTest(test_shell.ShellTestBase): | ||||
|         args, kwargs = mocked.call_args | ||||
|         self.assertEqual('the-project-id-i-want-to-set', | ||||
|                          kwargs.get('project_id')) | ||||
|         self.assertEqual('the-user-id-i-want-to-set', | ||||
|                          kwargs.get('user_id')) | ||||
|   | ||||
| @@ -197,6 +197,7 @@ def _restore_shadowed_arg(shadowed, observed): | ||||
|            help='Tenant to associate with sample ' | ||||
|                 '(only settable by admin users).') | ||||
| @utils.arg('--user-id', metavar='<SAMPLE_USER_ID>', | ||||
|            dest='sample_user_id', | ||||
|            help='User to associate with sample ' | ||||
|                 '(only settable by admin users).') | ||||
| @utils.arg('-r', '--resource-id', metavar='<RESOURCE_ID>', required=True, | ||||
| @@ -215,6 +216,7 @@ def _restore_shadowed_arg(shadowed, observed): | ||||
| @utils.arg('--timestamp', metavar='<TIMESTAMP>', | ||||
|            help='The sample timestamp.') | ||||
| @_restore_shadowed_arg('project_id', 'sample_project_id') | ||||
| @_restore_shadowed_arg('user_id', 'sample_user_id') | ||||
| def do_sample_create(cc, args={}): | ||||
|     """Create a sample.""" | ||||
|     arg_to_field_mapping = { | ||||
| @@ -417,6 +419,7 @@ def common_alarm_arguments(create=False): | ||||
|                    help='Tenant to associate with alarm ' | ||||
|                    '(only settable by admin users).') | ||||
|         @utils.arg('--user-id', metavar='<ALARM_USER_ID>', | ||||
|                    dest='alarm_user_id', | ||||
|                    help='User to associate with alarm ' | ||||
|                    '(only settable by admin users).') | ||||
|         @utils.arg('--description', metavar='<DESCRIPTION>', | ||||
| @@ -576,6 +579,7 @@ def common_alarm_gnocchi_resources_arguments(create=False): | ||||
|            help=('True if actions should be repeatedly notified ' | ||||
|                  'while alarm remains in target state.')) | ||||
| @_restore_shadowed_arg('project_id', 'alarm_project_id') | ||||
| @_restore_shadowed_arg('user_id', 'alarm_user_id') | ||||
| def do_alarm_create(cc, args={}): | ||||
|     """Create a new alarm (Deprecated). Use alarm-threshold-create instead.""" | ||||
|     fields = dict(filter(lambda x: not (x[1] is None), vars(args).items())) | ||||
| @@ -657,6 +661,7 @@ def do_alarm_gnocchi_aggregation_by_resources_threshold_create(cc, args={}): | ||||
|            help=('True if actions should be repeatedly notified ' | ||||
|                  'while alarm remains in target state.')) | ||||
| @_restore_shadowed_arg('project_id', 'alarm_project_id') | ||||
| @_restore_shadowed_arg('user_id', 'alarm_user_id') | ||||
| def do_alarm_threshold_create(cc, args={}): | ||||
|     """Create a new alarm based on computed statistics.""" | ||||
|     fields = dict(filter(lambda x: not (x[1] is None), vars(args).items())) | ||||
| @@ -684,6 +689,7 @@ def do_alarm_threshold_create(cc, args={}): | ||||
|            help=('True if actions should be repeatedly notified ' | ||||
|                  'while alarm remains in target state.')) | ||||
| @_restore_shadowed_arg('project_id', 'alarm_project_id') | ||||
| @_restore_shadowed_arg('user_id', 'alarm_user_id') | ||||
| def do_alarm_combination_create(cc, args={}): | ||||
|     """Create a new alarm based on state of other alarms.""" | ||||
|     fields = dict(filter(lambda x: not (x[1] is None), vars(args).items())) | ||||
| @@ -726,6 +732,7 @@ def do_alarm_combination_create(cc, args={}): | ||||
|            help=('True if actions should be repeatedly notified ' | ||||
|                  'while alarm remains in target state.')) | ||||
| @_restore_shadowed_arg('project_id', 'alarm_project_id') | ||||
| @_restore_shadowed_arg('user_id', 'alarm_user_id') | ||||
| def do_alarm_update(cc, args={}): | ||||
|     """Update an existing alarm (Deprecated).""" | ||||
|     fields = dict(filter(lambda x: not (x[1] is None), vars(args).items())) | ||||
| @@ -778,6 +785,7 @@ def do_alarm_update(cc, args={}): | ||||
|            help=('True if actions should be repeatedly notified ' | ||||
|                  'while alarm remains in target state.')) | ||||
| @_restore_shadowed_arg('project_id', 'alarm_project_id') | ||||
| @_restore_shadowed_arg('user_id', 'alarm_user_id') | ||||
| def do_alarm_threshold_update(cc, args={}): | ||||
|     """Update an existing alarm based on computed statistics.""" | ||||
|     fields = dict(filter(lambda x: not (x[1] is None), vars(args).items())) | ||||
| @@ -897,6 +905,7 @@ def do_alarm_gnocchi_aggregation_by_resources_threshold_update(cc, args={}): | ||||
|            help=('True if actions should be repeatedly notified ' | ||||
|                  'while alarm remains in target state.')) | ||||
| @_restore_shadowed_arg('project_id', 'alarm_project_id') | ||||
| @_restore_shadowed_arg('user_id', 'alarm_user_id') | ||||
| def do_alarm_combination_update(cc, args={}): | ||||
|     """Update an existing alarm based on state of other alarms.""" | ||||
|     fields = dict(filter(lambda x: not (x[1] is None), vars(args).items())) | ||||
|   | ||||
		Reference in New Issue
	
	Block a user