コード例 #1
0
    def is_admin_user(self, ctx, param):
        """
        :param param: instance of type "IsAdminUserParam" -> structure:
           parameter "username" of type "Username"
        :returns: multiple set - (1) parameter "is_admin" of type "Boolean",
           (2) parameter "error" of type "Error" -> structure: parameter
           "message" of String, parameter "type" of String, parameter "code"
           of String, parameter "info" of unspecified object
        """
        # ctx is the context object
        # return variables are: is_admin, error
        #BEGIN is_admin_user
        param, error = Validation.validate_is_admin_user(param, ctx)
        if error:
            return None, error

        # This uses the admin user stored in the model...
        model = UIServiceModel(auth_url=self.auth_url,
                               admin_users=self.admin_users,
                               token=ctx['token'],
                               username=ctx['user_id'],
                               db_config=self.db_config)
        is_admin = model.is_admin_user(param['username'])
        return [is_admin, None]
        #END is_admin_user

        # At some point might do deeper type checking...
        if not isinstance(is_admin, int):
            raise ValueError('Method is_admin_user return value ' +
                             'is_admin is not type int as required.')
        if not isinstance(error, dict):
            raise ValueError('Method is_admin_user return value ' +
                             'error is not type dict as required.')
        # return the results
        return [is_admin, error]
コード例 #2
0
    def test_set_alert(self):
        a_random_string, alert_id = self.add_random_alert()
        alert = self.get_alert(alert_id)
        alert_id = alert['id']
        another_random_string = self.generate_random_string()
        input = {
            'alert': {
                'id': alert_id,
                'title': 'title_' + another_random_string,
                'message': 'message_' + another_random_string,
                'status': 'status_' + another_random_string,
                'start_at': UIServiceModel.iso_to_iso('2019-01-01T00:00:00Z'),
                'end_at': UIServiceModel.iso_to_iso('2019-01-02T00:00:00Z'),
            }
        }
        ret, err = self.getImpl().set_alert(self.getContext(), input)
        self.assertIsNotNone(ret)
        self.assertIsNone(err)

        alert2 = self.get_alert(alert_id)
        self.assertEquals(alert2['title'], input['alert']['title'])
        self.assertEquals(alert2['message'], input['alert']['message'])

        self.assertNotEquals(alert2['title'], alert['title'])
        self.assertNotEquals(alert2['message'], alert['message'])
コード例 #3
0
    def delete_alert(self, ctx, id):
        """
        :param id: instance of type "AlertID"
        :returns: multiple set - (1) parameter "result" of type
           "DeleteAlertResult" -> structure: parameter "id" of type
           "AlertID", (2) parameter "error" of type "Error" -> structure:
           parameter "message" of String, parameter "type" of String,
           parameter "code" of String, parameter "info" of unspecified object
        """
        # ctx is the context object
        # return variables are: result, error
        #BEGIN delete_alert
        model = UIServiceModel(auth_url=self.auth_url,
                               admin_users=self.admin_users,
                               token=ctx['token'],
                               username=ctx['user_id'],
                               db_config=self.db_config)
        model.delete_alert(id)
        result = {'id': id}
        return [result, None]
        #END delete_alert

        # At some point might do deeper type checking...
        if not isinstance(result, dict):
            raise ValueError('Method delete_alert return value ' +
                             'result is not type dict as required.')
        if not isinstance(error, dict):
            raise ValueError('Method delete_alert return value ' +
                             'error is not type dict as required.')
        # return the results
        return [result, error]
コード例 #4
0
    def test_update_alert(self):
        a_random_string, alert_id = self.add_random_alert()
        alert = self.get_alert(alert_id)
        alert_id = alert['id']

        another_random_string = self.generate_random_string()
        test_table = [{
            'field': 'title',
            'value': 'title_' + another_random_string
        }, {
            'field': 'message',
            'value': 'message_' + another_random_string
        }, {
            'field':
            'start_at',
            'value':
            UIServiceModel.iso_to_iso('2019-01-02T00:00:00Z')
        }, {
            'field':
            'end_at',
            'value':
            UIServiceModel.iso_to_iso('2019-01-03T00:00:00Z')
        }]

        for test in test_table:
            field_name = test['field']
            input = {'alert': {'id': alert_id}}
            input['alert'][field_name] = test['value']

            ret, err = self.getImpl().update_alert(self.getContext(), input)
            self.assertIsNotNone(ret)
            self.assertIsNone(err)

            alert2 = self.get_alert(alert_id)
            self.assertEquals(alert2[field_name], input['alert'][field_name])
コード例 #5
0
    def search_alerts(self, ctx, query):
        """
        :param query: instance of type "AlertQuery" (typedef structure {
           string path; string op; string value; } SearchField; typedef
           structure { string op; list<SearchField> args; }
           SearchSubExpression; typedef structure { SearchField field;
           SearchSubExpression expression; } SearchArg; typedef structure {
           string op; list<SearchArg> args; } SearchExpression;) ->
           structure: parameter "query" of type "SearchExpression" (I give up
           ...) -> unspecified object, parameter "paging" of type
           "PagingSpec" (search_alerts) -> structure: parameter "start" of
           Long, parameter "limit" of Long, parameter "sorting" of list of
           type "SortSpec" -> structure: parameter "field" of String,
           parameter "is_descending" of type "Boolean"
        :returns: multiple set - (1) parameter "result" of type
           "SearchAlertsResult" -> structure: parameter "alerts" of list of
           type "Alert" -> structure: parameter "id" of type "AlertID",
           parameter "start_at" of type "Timestamp" (BASE Types), parameter
           "end_at" of type "Timestamp" (BASE Types), parameter "type" of
           type "AlertType", parameter "title" of String, parameter "message"
           of String, parameter "status" of type "AlertStatus", parameter
           "created_at" of type "Timestamp" (BASE Types), parameter
           "created_by" of String, parameter "updated_at" of type "Timestamp"
           (BASE Types), parameter "updated_by" of String, (2) parameter
           "error" of type "Error" -> structure: parameter "message" of
           String, parameter "type" of String, parameter "code" of String,
           parameter "info" of unspecified object
        """
        # ctx is the context object
        # return variables are: result, error
        #BEGIN search_alerts
        query2, error = Validation.validate_search_alerts_parameter(query, ctx)
        if error:
            return None, error

        model = UIServiceModel(auth_url=self.auth_url,
                               admin_users=self.admin_users,
                               token=ctx['token'],
                               username=ctx['user_id'],
                               db_config=self.db_config)

        result, error = model.search_alerts(query2)
        if error:
            return None, error

        return [{'alerts': result}, None]

        #END search_alerts

        # At some point might do deeper type checking...
        if not isinstance(result, dict):
            raise ValueError('Method search_alerts return value ' +
                             'result is not type dict as required.')
        if not isinstance(error, dict):
            raise ValueError('Method search_alerts return value ' +
                             'error is not type dict as required.')
        # return the results
        return [result, error]
コード例 #6
0
    def get_alert(self, ctx, param):
        """
        :param param: instance of type "GetAlertParam" (get_alert) ->
           structure: parameter "id" of type "AlertID"
        :returns: multiple set - (1) parameter "alert" of type "Alert" ->
           structure: parameter "id" of type "AlertID", parameter "start_at"
           of type "Timestamp" (BASE Types), parameter "end_at" of type
           "Timestamp" (BASE Types), parameter "type" of type "AlertType",
           parameter "title" of String, parameter "message" of String,
           parameter "status" of type "AlertStatus", parameter "created_at"
           of type "Timestamp" (BASE Types), parameter "created_by" of
           String, parameter "updated_at" of type "Timestamp" (BASE Types),
           parameter "updated_by" of String, (2) parameter "error" of type
           "Error" -> structure: parameter "message" of String, parameter
           "type" of String, parameter "code" of String, parameter "info" of
           unspecified object
        """
        # ctx is the context object
        # return variables are: alert, error
        #BEGIN get_alert
        input, error = Validation.validate_get_alert_parameter(param, ctx)
        if error:
            return None, error

        model = UIServiceModel(auth_url=self.auth_url,
                               admin_users=self.admin_users,
                               token=ctx['token'],
                               username=ctx['user_id'],
                               db_config=self.db_config)
        alert, error = model.get_alert(input['id'])
        return [alert, error]
        #END get_alert

        # At some point might do deeper type checking...
        if not isinstance(alert, dict):
            raise ValueError('Method get_alert return value ' +
                             'alert is not type dict as required.')
        if not isinstance(error, dict):
            raise ValueError('Method get_alert return value ' +
                             'error is not type dict as required.')
        # return the results
        return [alert, error]
コード例 #7
0
    def search_alerts_summary(self, ctx, query):
        """
        :param query: instance of type "SearchExpression" (I give up ...) ->
           unspecified object
        :returns: multiple set - (1) parameter "result" of type
           "SearchAlertsSummaryResult" -> structure: parameter "statuses" of
           mapping from String to Long, (2) parameter "error" of type "Error"
           -> structure: parameter "message" of String, parameter "type" of
           String, parameter "code" of String, parameter "info" of
           unspecified object
        """
        # ctx is the context object
        # return variables are: result, error
        #BEGIN search_alerts_summary
        query2, error = Validation.validate_search_alerts_summary_parameter(
            query, ctx)
        if error:
            return None, error

        model = UIServiceModel(auth_url=self.auth_url,
                               admin_users=self.admin_users,
                               token=ctx['token'],
                               username=ctx['user_id'],
                               db_config=self.db_config)

        result, error = model.search_alerts_summary(query2)
        if error:
            return None, error

        return [{'alerts_summary': result}, None]
        #END search_alerts_summary

        # At some point might do deeper type checking...
        if not isinstance(result, dict):
            raise ValueError('Method search_alerts_summary return value ' +
                             'result is not type dict as required.')
        if not isinstance(error, dict):
            raise ValueError('Method search_alerts_summary return value ' +
                             'error is not type dict as required.')
        # return the results
        return [result, error]
コード例 #8
0
    def get_active_alerts(self, ctx):
        """
        get_active_alerts
        :returns: multiple set - (1) parameter "alerts" of list of type
           "Alert" -> structure: parameter "id" of type "AlertID", parameter
           "start_at" of type "Timestamp" (BASE Types), parameter "end_at" of
           type "Timestamp" (BASE Types), parameter "type" of type
           "AlertType", parameter "title" of String, parameter "message" of
           String, parameter "status" of type "AlertStatus", parameter
           "created_at" of type "Timestamp" (BASE Types), parameter
           "created_by" of String, parameter "updated_at" of type "Timestamp"
           (BASE Types), parameter "updated_by" of String, (2) parameter
           "error" of type "Error" -> structure: parameter "message" of
           String, parameter "type" of String, parameter "code" of String,
           parameter "info" of unspecified object
        """
        # ctx is the context object
        # return variables are: alerts, error
        #BEGIN get_active_alerts
        # print('getting active alerts... %s, %s, %s, %s, %s' % (self.auth_url, self.admin_users, ctx['token'], ctx['user_id'], self.db_config))
        model = UIServiceModel(auth_url=self.auth_url,
                               admin_users=self.admin_users,
                               token=ctx['token'],
                               username=ctx['user_id'],
                               db_config=self.db_config)
        # print('getting active alerts...')
        alerts, error = model.get_active_alerts()
        return [alerts, error]
        #END get_active_alerts

        # At some point might do deeper type checking...
        if not isinstance(alerts, list):
            raise ValueError('Method get_active_alerts return value ' +
                             'alerts is not type list as required.')
        if not isinstance(error, dict):
            raise ValueError('Method get_active_alerts return value ' +
                             'error is not type dict as required.')
        # return the results
        return [alerts, error]
コード例 #9
0
    def set_alert(self, ctx, alert_param):
        """
        :param alert_param: instance of type "UpdateAlertParams" (update
           alert) -> structure: parameter "alert" of type "Alert" ->
           structure: parameter "id" of type "AlertID", parameter "start_at"
           of type "Timestamp" (BASE Types), parameter "end_at" of type
           "Timestamp" (BASE Types), parameter "type" of type "AlertType",
           parameter "title" of String, parameter "message" of String,
           parameter "status" of type "AlertStatus", parameter "created_at"
           of type "Timestamp" (BASE Types), parameter "created_by" of
           String, parameter "updated_at" of type "Timestamp" (BASE Types),
           parameter "updated_by" of String
        :returns: multiple set - (1) parameter "success" of type "Boolean",
           (2) parameter "error" of type "Error" -> structure: parameter
           "message" of String, parameter "type" of String, parameter "code"
           of String, parameter "info" of unspecified object
        """
        # ctx is the context object
        # return variables are: success, error
        #BEGIN set_alert
        model = UIServiceModel(auth_url=self.auth_url,
                               admin_users=self.admin_users,
                               token=ctx['token'],
                               username=ctx['user_id'],
                               db_config=self.db_config)
        model.set_alert(alert_param['alert'])
        success = True
        return [success, None]
        #END set_alert

        # At some point might do deeper type checking...
        if not isinstance(success, int):
            raise ValueError('Method set_alert return value ' +
                             'success is not type int as required.')
        if not isinstance(error, dict):
            raise ValueError('Method set_alert return value ' +
                             'error is not type dict as required.')
        # return the results
        return [success, error]
コード例 #10
0
    def add_random_alert(self, include_end_at=True):
        a_random_string = self.generate_random_string()
        if include_end_at:
            end_at = UIServiceModel.iso_to_iso('2018-01-02T00:00:00Z')
        else:
            end_at = None
        input = {
            'alert': {
                'title': 'title_' + a_random_string,
                'message': 'message_' + a_random_string,
                'start_at': UIServiceModel.iso_to_iso('2018-01-01T00:00:00Z'),
                'end_at': end_at,
                'status': 'published'
            }
        }

        ret, err = self.getImpl().add_alert(self.getContext(), input)
        self.assertIsNone(err)
        self.assertIsNotNone(ret)
        self.assertIsInstance(ret, dict)
        self.assertIn('id', ret)
        return a_random_string, ret['id']
コード例 #11
0
    def test_model_no_token(self):
        auth_url = self.cfg['auth-url']
        admin_users = self.cfg['admin-users']
        token = self.ctx['token']
        username = self.ctx['user_id']
        config = Validation.validate_config(self.cfg)
        db_config = config['mongo']

        try:
            uis = UIServiceModel(auth_url, None, username, admin_users,
                                 db_config)
            uis.delete_alert('abc')
            self.assertTrue(False)
        except Exception as ex:
            self.assertTrue(True)

        try:
            uis = UIServiceModel(auth_url, token, 'not_an_admin', admin_users,
                                 db_config)
            uis.delete_alert('abc')
            self.assertTrue(False)
        except Exception as ex:
            self.assertTrue(True)