Add missing --public option to workbook api
				
					
				
			Handle `scope` parameter in workbook api with `private` as default value Add `--public` option to workbook create/update commands Add `Scope` column to the output of workbook commands Change-Id: I5c9b107127afd55757fe9e815eff5b30e48fccac Closes-Bug: 1782790
This commit is contained in:
		| @@ -3,6 +3,13 @@ version: '2.0' | |||||||
|  |  | ||||||
| name: wb | name: wb | ||||||
|  |  | ||||||
|  | actions: | ||||||
|  |   ac1: | ||||||
|  |     input: | ||||||
|  |       - str1 | ||||||
|  |       - str2 | ||||||
|  |     base: std.echo output="<% $.str1 %><% $.str2 %>" | ||||||
|  |  | ||||||
| workflows: | workflows: | ||||||
|   wf1: |   wf1: | ||||||
|     type: direct |     type: direct | ||||||
|   | |||||||
| @@ -25,7 +25,7 @@ class Workbook(base.Resource): | |||||||
| class WorkbookManager(base.ResourceManager): | class WorkbookManager(base.ResourceManager): | ||||||
|     resource_class = Workbook |     resource_class = Workbook | ||||||
|  |  | ||||||
|     def create(self, definition): |     def create(self, definition, scope='private'): | ||||||
|         self._ensure_not_empty(definition=definition) |         self._ensure_not_empty(definition=definition) | ||||||
|  |  | ||||||
|         # If the specified definition is actually a file, read in the |         # If the specified definition is actually a file, read in the | ||||||
| @@ -34,7 +34,7 @@ class WorkbookManager(base.ResourceManager): | |||||||
|  |  | ||||||
|         try: |         try: | ||||||
|             resp = self.http_client.post( |             resp = self.http_client.post( | ||||||
|                 '/workbooks', |                 '/workbooks?scope=%s' % scope, | ||||||
|                 definition, |                 definition, | ||||||
|                 headers={'content-type': 'text/plain'} |                 headers={'content-type': 'text/plain'} | ||||||
|             ) |             ) | ||||||
| @@ -46,7 +46,7 @@ class WorkbookManager(base.ResourceManager): | |||||||
|  |  | ||||||
|         return self.resource_class(self, base.extract_json(resp, None)) |         return self.resource_class(self, base.extract_json(resp, None)) | ||||||
|  |  | ||||||
|     def update(self, definition): |     def update(self, definition, scope='private'): | ||||||
|         self._ensure_not_empty(definition=definition) |         self._ensure_not_empty(definition=definition) | ||||||
|  |  | ||||||
|         # If the specified definition is actually a file, read in the |         # If the specified definition is actually a file, read in the | ||||||
| @@ -55,7 +55,7 @@ class WorkbookManager(base.ResourceManager): | |||||||
|  |  | ||||||
|         try: |         try: | ||||||
|             resp = self.http_client.put( |             resp = self.http_client.put( | ||||||
|                 '/workbooks', |                 '/workbooks?scope=%s' % scope, | ||||||
|                 definition, |                 definition, | ||||||
|                 headers={'content-type': 'text/plain'} |                 headers={'content-type': 'text/plain'} | ||||||
|             ) |             ) | ||||||
|   | |||||||
| @@ -25,6 +25,7 @@ def format(workbook=None): | |||||||
|     columns = ( |     columns = ( | ||||||
|         'Name', |         'Name', | ||||||
|         'Tags', |         'Tags', | ||||||
|  |         'Scope', | ||||||
|         'Created at', |         'Created at', | ||||||
|         'Updated at' |         'Updated at' | ||||||
|     ) |     ) | ||||||
| @@ -33,6 +34,7 @@ def format(workbook=None): | |||||||
|         data = ( |         data = ( | ||||||
|             workbook.name, |             workbook.name, | ||||||
|             base.wrap(', '.join(workbook.tags or '')) or '<none>', |             base.wrap(', '.join(workbook.tags or '')) or '<none>', | ||||||
|  |             workbook.scope, | ||||||
|             workbook.created_at, |             workbook.created_at, | ||||||
|         ) |         ) | ||||||
|  |  | ||||||
| @@ -89,13 +91,21 @@ class Create(command.ShowOne): | |||||||
|             type=argparse.FileType('r'), |             type=argparse.FileType('r'), | ||||||
|             help='Workbook definition file' |             help='Workbook definition file' | ||||||
|         ) |         ) | ||||||
|  |         parser.add_argument( | ||||||
|  |             '--public', | ||||||
|  |             action='store_true', | ||||||
|  |             help='With this flag workbook will be marked as "public".' | ||||||
|  |         ) | ||||||
|  |  | ||||||
|         return parser |         return parser | ||||||
|  |  | ||||||
|     def take_action(self, parsed_args): |     def take_action(self, parsed_args): | ||||||
|  |         scope = 'public' if parsed_args.public else 'private' | ||||||
|  |  | ||||||
|         mistral_client = self.app.client_manager.workflow_engine |         mistral_client = self.app.client_manager.workflow_engine | ||||||
|         workbook = mistral_client.workbooks.create( |         workbook = mistral_client.workbooks.create( | ||||||
|             parsed_args.definition.read() |             parsed_args.definition.read(), | ||||||
|  |             scope=scope | ||||||
|         ) |         ) | ||||||
|  |  | ||||||
|         return format(workbook) |         return format(workbook) | ||||||
| @@ -132,13 +142,21 @@ class Update(command.ShowOne): | |||||||
|             type=argparse.FileType('r'), |             type=argparse.FileType('r'), | ||||||
|             help='Workbook definition file' |             help='Workbook definition file' | ||||||
|         ) |         ) | ||||||
|  |         parser.add_argument( | ||||||
|  |             '--public', | ||||||
|  |             action='store_true', | ||||||
|  |             help='With this flag workbook will be marked as "public".' | ||||||
|  |         ) | ||||||
|  |  | ||||||
|         return parser |         return parser | ||||||
|  |  | ||||||
|     def take_action(self, parsed_args): |     def take_action(self, parsed_args): | ||||||
|  |         scope = 'public' if parsed_args.public else 'private' | ||||||
|  |  | ||||||
|         mistral_client = self.app.client_manager.workflow_engine |         mistral_client = self.app.client_manager.workflow_engine | ||||||
|         workbook = mistral_client.workbooks.update( |         workbook = mistral_client.workbooks.update( | ||||||
|             parsed_args.definition.read() |             parsed_args.definition.read(), | ||||||
|  |             scope=scope | ||||||
|         ) |         ) | ||||||
|  |  | ||||||
|         return format(workbook) |         return format(workbook) | ||||||
|   | |||||||
| @@ -111,11 +111,16 @@ class MistralClientTestBase(base.MistralCLIAuth, base.MistralCLIAltAuth): | |||||||
|         else: |         else: | ||||||
|             return self.mistral_alt_user(cmd, params) |             return self.mistral_alt_user(cmd, params) | ||||||
|  |  | ||||||
|     def workbook_create(self, wb_def, admin=True): |     def workbook_create(self, wb_def, admin=True, scope='private'): | ||||||
|  |         params = '{0}'.format(wb_def) | ||||||
|  |  | ||||||
|  |         if scope == 'public': | ||||||
|  |             params += ' --public' | ||||||
|  |  | ||||||
|         wb = self.mistral_cli( |         wb = self.mistral_cli( | ||||||
|             admin, |             admin, | ||||||
|             'workbook-create', |             'workbook-create', | ||||||
|             params='{0}'.format(wb_def) |             params=params | ||||||
|         ) |         ) | ||||||
|  |  | ||||||
|         wb_name = self.get_field_value(wb, "Name") |         wb_name = self.get_field_value(wb, "Name") | ||||||
|   | |||||||
| @@ -103,6 +103,32 @@ class WorkbookIsolationCLITests(base_v2.MistralClientTestBase): | |||||||
|             params=name |             params=name | ||||||
|         ) |         ) | ||||||
|  |  | ||||||
|  |     def test_create_public_workbook(self): | ||||||
|  |         wb = self.workbook_create(self.wb_def, scope='public') | ||||||
|  |         name = self.get_field_value(wb, "Name") | ||||||
|  |  | ||||||
|  |         same_wb = self.mistral_alt_user( | ||||||
|  |             "workbook-get", | ||||||
|  |             params=name | ||||||
|  |         ) | ||||||
|  |  | ||||||
|  |         self.assertEqual( | ||||||
|  |             name, | ||||||
|  |             self.get_field_value(same_wb, "Name") | ||||||
|  |         ) | ||||||
|  |  | ||||||
|  |         # The workflows should be public too | ||||||
|  |         self.mistral_alt_user( | ||||||
|  |             "workflow-get", | ||||||
|  |             params="wb.wf1" | ||||||
|  |         ) | ||||||
|  |  | ||||||
|  |         # The actions should be public too | ||||||
|  |         self.mistral_alt_user( | ||||||
|  |             "action-get", | ||||||
|  |             params="wb.ac1" | ||||||
|  |         ) | ||||||
|  |  | ||||||
|     def test_delete_wb_from_another_tenant(self): |     def test_delete_wb_from_another_tenant(self): | ||||||
|         wb = self.workbook_create(self.wb_def) |         wb = self.workbook_create(self.wb_def) | ||||||
|         name = self.get_field_value(wb, "Name") |         name = self.get_field_value(wb, "Name") | ||||||
|   | |||||||
| @@ -23,8 +23,9 @@ from mistralclient.tests.unit import base | |||||||
| WORKBOOK_DICT = { | WORKBOOK_DICT = { | ||||||
|     'name': 'a', |     'name': 'a', | ||||||
|     'tags': ['a', 'b'], |     'tags': ['a', 'b'], | ||||||
|  |     'scope': 'private', | ||||||
|     'created_at': '1', |     'created_at': '1', | ||||||
|     'updated_at': '1' |     'updated_at': '1', | ||||||
| } | } | ||||||
|  |  | ||||||
|  |  | ||||||
| @@ -54,7 +55,26 @@ class TestCLIWorkbooksV2(base.BaseCommandTest): | |||||||
|  |  | ||||||
|         result = self.call(workbook_cmd.Create, app_args=['wb.yaml']) |         result = self.call(workbook_cmd.Create, app_args=['wb.yaml']) | ||||||
|  |  | ||||||
|         self.assertEqual(('a', 'a, b', '1', '1'), result[1]) |         self.assertEqual(('a', 'a, b', 'private', '1', '1'), result[1]) | ||||||
|  |  | ||||||
|  |     @mock.patch('argparse.open', create=True) | ||||||
|  |     def test_create_public(self, mock_open): | ||||||
|  |         wb_public_dict = WORKBOOK_DICT.copy() | ||||||
|  |         wb_public_dict['scope'] = 'public' | ||||||
|  |         workbook_public = workbooks.Workbook(mock, wb_public_dict) | ||||||
|  |         self.client.workbooks.create.return_value = workbook_public | ||||||
|  |  | ||||||
|  |         result = self.call( | ||||||
|  |             workbook_cmd.Create, | ||||||
|  |             app_args=['wb.yaml', '--public'] | ||||||
|  |         ) | ||||||
|  |  | ||||||
|  |         self.assertEqual(('a', 'a, b', 'public', '1', '1'), result[1]) | ||||||
|  |  | ||||||
|  |         self.assertEqual( | ||||||
|  |             'public', | ||||||
|  |             self.client.workbooks.create.call_args[1]['scope'] | ||||||
|  |         ) | ||||||
|  |  | ||||||
|     @mock.patch('argparse.open', create=True) |     @mock.patch('argparse.open', create=True) | ||||||
|     def test_update(self, mock_open): |     def test_update(self, mock_open): | ||||||
| @@ -62,21 +82,40 @@ class TestCLIWorkbooksV2(base.BaseCommandTest): | |||||||
|  |  | ||||||
|         result = self.call(workbook_cmd.Update, app_args=['definition']) |         result = self.call(workbook_cmd.Update, app_args=['definition']) | ||||||
|  |  | ||||||
|         self.assertEqual(('a', 'a, b', '1', '1'), result[1]) |         self.assertEqual(('a', 'a, b', 'private', '1', '1'), result[1]) | ||||||
|  |  | ||||||
|  |     @mock.patch('argparse.open', create=True) | ||||||
|  |     def test_update_public(self, mock_open): | ||||||
|  |         wb_public_dict = WORKBOOK_DICT.copy() | ||||||
|  |         wb_public_dict['scope'] = 'public' | ||||||
|  |         workbook_public = workbooks.Workbook(mock, wb_public_dict) | ||||||
|  |         self.client.workbooks.update.return_value = workbook_public | ||||||
|  |  | ||||||
|  |         result = self.call( | ||||||
|  |             workbook_cmd.Update, | ||||||
|  |             app_args=['definition', '--public'] | ||||||
|  |         ) | ||||||
|  |  | ||||||
|  |         self.assertEqual(('a', 'a, b', 'public', '1', '1'), result[1]) | ||||||
|  |  | ||||||
|  |         self.assertEqual( | ||||||
|  |             'public', | ||||||
|  |             self.client.workbooks.update.call_args[1]['scope'] | ||||||
|  |         ) | ||||||
|  |  | ||||||
|     def test_list(self): |     def test_list(self): | ||||||
|         self.client.workbooks.list.return_value = [WORKBOOK] |         self.client.workbooks.list.return_value = [WORKBOOK] | ||||||
|  |  | ||||||
|         result = self.call(workbook_cmd.List) |         result = self.call(workbook_cmd.List) | ||||||
|  |  | ||||||
|         self.assertEqual([('a', 'a, b', '1', '1')], result[1]) |         self.assertEqual([('a', 'a, b', 'private', '1', '1')], result[1]) | ||||||
|  |  | ||||||
|     def test_get(self): |     def test_get(self): | ||||||
|         self.client.workbooks.get.return_value = WORKBOOK |         self.client.workbooks.get.return_value = WORKBOOK | ||||||
|  |  | ||||||
|         result = self.call(workbook_cmd.Get, app_args=['name']) |         result = self.call(workbook_cmd.Get, app_args=['name']) | ||||||
|  |  | ||||||
|         self.assertEqual(('a', 'a, b', '1', '1'), result[1]) |         self.assertEqual(('a', 'a, b', 'private', '1', '1'), result[1]) | ||||||
|  |  | ||||||
|     def test_delete(self): |     def test_delete(self): | ||||||
|         self.call(workbook_cmd.Delete, app_args=['name']) |         self.call(workbook_cmd.Delete, app_args=['name']) | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Kevin Pouget
					Kevin Pouget