def test_signal_with_body_as_input_and_delete_with_executions(self):
     tmpl = template_format.parse(workflow_template_full)
     stack = utils.parse_stack(
         tmpl, params={'parameters': {
             'use_request_body_as_input': 'true'
         }})
     rsrc_defns = stack.t.resource_definitions(stack)['create_vm']
     wf = workflow.Workflow('create_vm', rsrc_defns, stack)
     self.mistral.workflows.create.return_value = [
         FakeWorkflow('create_vm')
     ]
     scheduler.TaskRunner(wf.create)()
     details = {'flavor': '3'}
     execution = mock.Mock()
     execution.id = '12345'
     exec_manager = executions.ExecutionManager(wf.client('mistral'))
     self.mistral.executions.create.side_effect = (
         lambda *args, **kw: exec_manager.create(*args, **kw))
     self.patchobject(exec_manager, '_create', return_value=execution)
     scheduler.TaskRunner(wf.signal, details)()
     call_args = self.mistral.executions.create.call_args
     args, _ = call_args
     expected_args = ('{"image": "31d8eeaf-686e-4e95-bb27-765014b9f20b", '
                      '"name": "create_test_server", "flavor": "3"}')
     self.validate_json_inputs(args[1], expected_args)
     self.assertEqual({'executions': '12345'}, wf.data())
     # Updating the workflow changing "use_request_body_as_input" to
     # false and signaling again with the expected request body format.
     t = template_format.parse(workflow_updating_request_body_property)
     new_stack = utils.parse_stack(t)
     rsrc_defns = new_stack.t.resource_definitions(new_stack)
     self.mistral.workflows.update.return_value = [
         FakeWorkflow('test_stack-workflow-b5fiekdsa355')
     ]
     scheduler.TaskRunner(wf.update, rsrc_defns['create_vm'])()
     self.assertTrue(self.mistral.workflows.update.called)
     self.assertEqual((wf.UPDATE, wf.COMPLETE), wf.state)
     details = {'input': {'flavor': '4'}}
     execution = mock.Mock()
     execution.id = '54321'
     exec_manager = executions.ExecutionManager(wf.client('mistral'))
     self.mistral.executions.create.side_effect = (
         lambda *args, **kw: exec_manager.create(*args, **kw))
     self.patchobject(exec_manager, '_create', return_value=execution)
     scheduler.TaskRunner(wf.signal, details)()
     call_args = self.mistral.executions.create.call_args
     args, _ = call_args
     expected_args = ('{"image": "31d8eeaf-686e-4e95-bb27-765014b9f20b", '
                      '"name": "create_test_server", "flavor": "4"}')
     self.validate_json_inputs(args[1], expected_args)
     self.assertEqual(
         {
             'executions': '54321,12345',
             'name': 'test_stack-workflow-b5fiekdsa355'
         }, wf.data())
     scheduler.TaskRunner(wf.delete)()
     self.assertEqual(2, self.mistral.executions.delete.call_count)
     self.assertEqual((wf.DELETE, wf.COMPLETE), wf.state)
Exemple #2
0
    def __init__(self, auth_type='keystone', **kwargs):
        req = copy.deepcopy(kwargs)
        mistral_url = req.get('mistral_url')
        profile = req.get('profile')

        if mistral_url and not isinstance(mistral_url, six.string_types):
            raise RuntimeError('Mistral url should be a string.')

        auth_handler = auth.get_auth_handler(auth_type)
        auth_response = auth_handler.authenticate(req) or {}

        req.update(auth_response)

        mistral_url = auth_response.get('mistral_url') or mistral_url

        if not mistral_url:
            mistral_url = _DEFAULT_MISTRAL_URL

        if profile:
            osprofiler_profiler.init(profile)

        http_client = httpclient.HTTPClient(mistral_url, **req)

        # Create all resource managers.
        self.workbooks = workbooks.WorkbookManager(http_client)
        self.executions = executions.ExecutionManager(http_client)
        self.tasks = tasks.TaskManager(http_client)
        self.actions = actions.ActionManager(http_client)
        self.workflows = workflows.WorkflowManager(http_client)
        self.cron_triggers = cron_triggers.CronTriggerManager(http_client)
        self.environments = environments.EnvironmentManager(http_client)
        self.action_executions = action_executions.ActionExecutionManager(
            http_client)
        self.services = services.ServiceManager(http_client)
        self.members = members.MemberManager(http_client)
    def __init__(self, auth_type='keystone', **kwargs):
        # We get the session at this point, as some instances of session
        # objects might have mutexes that can't be deep-copied.
        session = kwargs.pop('session', None)
        req = copy.deepcopy(kwargs)
        mistral_url = req.get('mistral_url')
        profile = req.get('profile')

        if mistral_url and not isinstance(mistral_url, str):
            raise RuntimeError('Mistral url should be a string.')

        # If auth url was provided then we perform an authentication, otherwise
        # just ignore this step
        if req.get('auth_url') or req.get('target_auth_url'):
            auth_handler = auth.get_auth_handler(auth_type)
            auth_response = auth_handler.authenticate(req, session=session)
        else:
            auth_response = {}

        if session is None:
            # If the session was None and we're using keystone auth, it will be
            # created by the auth_handler.
            session = auth_response.pop('session', None)

        req.update(auth_response)

        mistral_url = auth_response.get('mistral_url') or mistral_url

        if not mistral_url:
            mistral_url = _DEFAULT_MISTRAL_URL

        if profile:
            osprofiler_profiler.init(profile)

        http_client = httpclient.HTTPClient(mistral_url,
                                            session=session,
                                            **req)

        # Create all resource managers.
        self.workbooks = workbooks.WorkbookManager(http_client)
        self.executions = executions.ExecutionManager(http_client)
        self.tasks = tasks.TaskManager(http_client)
        self.actions = actions.ActionManager(http_client)
        self.workflows = workflows.WorkflowManager(http_client)
        self.cron_triggers = cron_triggers.CronTriggerManager(http_client)
        self.event_triggers = event_triggers.EventTriggerManager(http_client)
        self.environments = environments.EnvironmentManager(http_client)
        self.action_executions = action_executions.ActionExecutionManager(
            http_client)
        self.services = services.ServiceManager(http_client)
        self.members = members.MemberManager(http_client)
        self.code_sources = code_sources.CodeSourceManager(http_client)
        self.dynamic_actions = dynamic_actions.DynamicActionManager(
            http_client)
Exemple #4
0
    def _get_workflow_result(self, exec_id):
        """
        Returns the workflow status and output. Mistral workflow status will be converted
        to st2 action status.
        :param exec_id: Mistral execution ID
        :type exec_id: ``str``
        :rtype: (``str``, ``dict``)
        """
        execution = executions.ExecutionManager(self._client).get(exec_id)

        output = jsonify.try_loads(execution.output) if execution.state in DONE_STATES else None

        return (execution.state, output)
    def __init__(self,
                 mistral_url=None,
                 username=None,
                 api_key=None,
                 project_name=None,
                 auth_url=None,
                 project_id=None,
                 endpoint_type='publicURL',
                 service_type='workflowv2',
                 auth_token=None,
                 user_id=None,
                 cacert=None,
                 insecure=False):

        if mistral_url and not isinstance(mistral_url, six.string_types):
            raise RuntimeError('Mistral url should be string')

        if auth_url:
            (mistral_url, auth_token, project_id,
             user_id) = (self.authenticate(mistral_url, username, api_key,
                                           project_name, auth_url, project_id,
                                           endpoint_type, service_type,
                                           auth_token, user_id, cacert,
                                           insecure))

        if not mistral_url:
            mistral_url = "http://localhost:8989/v2"

        self.http_client = httpclient.HTTPClient(mistral_url, auth_token,
                                                 project_id, user_id)

        # Create all resource managers.
        self.workbooks = workbooks.WorkbookManager(self)
        self.executions = executions.ExecutionManager(self)
        self.tasks = tasks.TaskManager(self)
        self.actions = actions.ActionManager(self)
        self.workflows = workflows.WorkflowManager(self)
        self.cron_triggers = cron_triggers.CronTriggerManager(self)
        self.environments = environments.EnvironmentManager(self)
        self.action_executions = action_executions.ActionExecutionManager(self)
        self.services = services.ServiceManager(self)
Exemple #6
0
 def test_signal_and_delete_with_executions(self):
     tmpl = template_format.parse(workflow_template_full)
     stack = utils.parse_stack(tmpl)
     rsrc_defns = stack.t.resource_definitions(stack)['create_vm']
     wf = workflow.Workflow('create_vm', rsrc_defns, stack)
     self.mistral.workflows.create.return_value = [
         FakeWorkflow('create_vm')]
     scheduler.TaskRunner(wf.create)()
     details = {'input': {'flavor': '3'}}
     execution = mock.Mock()
     execution.id = '12345'
     # Invoke the real create method (bug 1453539)
     exec_manager = executions.ExecutionManager(wf.client('mistral'))
     self.mistral.executions.create.side_effect = (
         lambda *args, **kw: exec_manager.create(*args, **kw))
     self.patchobject(exec_manager, '_create', return_value=execution)
     scheduler.TaskRunner(wf.signal, details)()
     self.assertEqual({'executions': '12345'}, wf.data())
     scheduler.TaskRunner(wf.delete)()
     self.assertEqual(1, self.mistral.executions.delete.call_count)
     self.assertEqual((wf.DELETE, wf.COMPLETE), wf.state)
Exemple #7
0
def data(TEST):
    # MistralActions
    TEST.mistralclient_actions = test_data_utils.TestDataContainer()
    action_1 = actions.Action(
        actions.ActionManager(None),
        {'name': 'a',
         'is_system': True,
         'input': 'param1',
         'description': 'my cool action',
         'tags': ['test'],
         'created_at': '1',
         'updated_at': '1'
         }
    )
    TEST.mistralclient_actions.add(action_1)

    # MistralExecutions
    TEST.mistralclient_executions = test_data_utils.TestDataContainer()
    execution_1 = executions.Execution(
        executions.ExecutionManager(None),
        {'id': '123',
         'workflow_name': 'my_wf',
         'description': '',
         'state': 'RUNNING',
         'input': {
             'person': {
                 'first_name': 'John',
                 'last_name': 'Doe'
             }
         }}
    )
    TEST.mistralclient_executions.add(execution_1)

    # Tasks
    TEST.mistralclient_tasks = test_data_utils.TestDataContainer()
    task_1 = tasks.Task(
        tasks.TaskManager(None),
        {'id': '1',
         'workflow_execution_id': '123',
         'name': 'my_task',
         'workflow_name': 'my_wf',
         'state': 'RUNNING',
         'type': 'ACTION',
         'tags': ['deployment', 'demo'],
         'result': {'some': 'result'}})
    TEST.mistralclient_tasks.add(task_1)

    # Workbooks
    TEST.mistralclient_workbooks = test_data_utils.TestDataContainer()
    workbook_1 = workbooks.Workbook(
        workbooks.WorkbookManager(None),
        {'name': 'a',
         'tags': ['a', 'b'],
         'created_at': '1',
         'updated_at': '1',
         'definition': WB_DEF}
    )
    TEST.mistralclient_workbooks.add(workbook_1)

    # Workflows
    TEST.mistralclient_workflows = test_data_utils.TestDataContainer()
    workflow_1 = workflows.Workflow(
        workflows.WorkflowManager(None),
        {'name': 'a',
         'tags': ['a', 'b'],
         'input': 'param',
         'created_at': '1',
         'updated_at': '1',
         'definition': WF_DEF}
    )
    TEST.mistralclient_workflows.add(workflow_1)

    # MistralActionsExecutions
    TEST.mistralclient_action_executions = test_data_utils.TestDataContainer()
    action_executions_1 = action_executions.ActionExecution(
        action_executions.ActionExecutionManager(None),
        {'id': '1',
         'name': 'a',
         'tags': ['a', 'b'],
         'workflow_name': 'my work flow',
         'task_execution_id': '1',
         'task_name': 'b',
         'description': '',
         'created_at': '1',
         'updated_at': '1',
         'accepted': True,
         'state': 'RUNNING'
         }
    )
    TEST.mistralclient_action_executions.add(action_executions_1)

    # MistralCronTriggers
    TEST.mistralclient_cron_triggers = test_data_utils.TestDataContainer()
    cron_triggers_1 = cron_triggers.CronTrigger(
        cron_triggers.CronTriggerManager(None),
        {'id': '1',
         'name': 'a',
         'workflow_name': 'my work flow',
         'pattern': '',
         'next_execution_time': '',
         'remaining_executions': '',
         'first_execution_time': '',
         'created_at': '1',
         'updated_at': '1'
         })
    TEST.mistralclient_cron_triggers.add(cron_triggers_1)
Exemple #8
0
    def __init__(self, mistral_url=None, username=None, api_key=None,
                 project_name=None, auth_url=None, project_id=None,
                 endpoint_type='publicURL', service_type='workflowv2',
                 auth_token=None, user_id=None, cacert=None, insecure=False,
                 profile=None, auth_type=auth_types.KEYSTONE, client_id=None,
                 client_secret=None, target_username=None, target_api_key=None,
                 target_project_name=None, target_auth_url=None,
                 target_project_id=None, target_auth_token=None,
                 target_user_id=None, target_cacert=None,
                 target_insecure=False, **kwargs):

        if mistral_url and not isinstance(mistral_url, six.string_types):
            raise RuntimeError('Mistral url should be a string.')

        if auth_url:
            if auth_type == auth_types.KEYSTONE:
                (mistral_url, auth_token, project_id, user_id) = (
                    keystone.authenticate(
                        mistral_url,
                        username,
                        api_key,
                        project_name,
                        auth_url,
                        project_id,
                        endpoint_type,
                        service_type,
                        auth_token,
                        user_id,
                        cacert,
                        insecure
                    )
                )
            elif auth_type == auth_types.KEYCLOAK_OIDC:
                auth_token = keycloak.authenticate(
                    auth_url,
                    client_id,
                    client_secret,
                    project_name,
                    username,
                    api_key,
                    auth_token,
                    cacert,
                    insecure
                )

                # In case of KeyCloak OpenID Connect we can treat project
                # name and id in the same way because KeyCloak realm is
                # essentially a different OpenID Connect Issuer which in
                # KeyCloak is represented just as a URL path component
                # (see http://openid.net/specs/openid-connect-core-1_0.html).
                project_id = project_name
            else:
                raise RuntimeError(
                    'Invalid authentication type [value=%s, valid_values=%s]'
                    % (auth_type, auth_types.ALL)
                )

        if not mistral_url:
            mistral_url = _DEFAULT_MISTRAL_URL

        if profile:
            osprofiler_profiler.init(profile)

        if target_auth_url:
            keystone.authenticate(
                mistral_url,
                target_username,
                target_api_key,
                target_project_name,
                target_auth_url,
                target_project_id,
                endpoint_type,
                service_type,
                target_auth_token,
                target_user_id,
                target_cacert,
                target_insecure
            )

        http_client = httpclient.HTTPClient(
            mistral_url,
            auth_token,
            project_id,
            user_id,
            cacert=cacert,
            insecure=insecure,
            target_token=target_auth_token,
            target_auth_uri=target_auth_url,
            **kwargs
        )

        # Create all resource managers.
        self.workbooks = workbooks.WorkbookManager(http_client)
        self.executions = executions.ExecutionManager(http_client)
        self.tasks = tasks.TaskManager(http_client)
        self.actions = actions.ActionManager(http_client)
        self.workflows = workflows.WorkflowManager(http_client)
        self.cron_triggers = cron_triggers.CronTriggerManager(http_client)
        self.environments = environments.EnvironmentManager(http_client)
        self.action_executions = action_executions.ActionExecutionManager(
            http_client)
        self.services = services.ServiceManager(http_client)
        self.members = members.MemberManager(http_client)