Example #1
0
    def create_policy(self, request, log, tenant_id, group_id):
        """Create a new policy.

        Receive application/json content for new policy creation.

        :param str tenant_id: A tenant id
        :param str group_id: A group id
        """
        content = json.loads(request.content.read())
        alarm_template_id = content.get('alarmTemplate')
        check_template_id = content.get('checkTemplate')
        policy_id = content.get('policyId')

        d = cass.create_policy(self._db, policy_id, group_id, alarm_template_id, check_template_id)

        # Trigger actions to create the alarm and checks on the MaaS side and set things up

        def serialize(policy):
            json_object = {
                'alarmTemplate': policy['alarmTemplate'],
                'checkTemplate': policy['checkTemplate'],
                'groupId': policy['groupId'],
                'links': [
                    {
                        'href': '{0}{1}'.format(request.URLPath().path, policy['policyId']),
                        'rel': 'self'
                    }
                ],
                'policyId': policy['policyId']
            }
            request.setHeader('Content-Type', 'application/json')
            request.setResponseCode(201)
            request.write(json.dumps(json_object))
            request.finish()
        return d.addCallback(serialize)
Example #2
0
    def test_create_policy(self):
        """Creates and returns a policy dict."""
        expected = {
            'policyId': 'policy-abc',
            'groupId': 'group-def',
            'alarmTemplate': 'alarmTemplate-ghi',
            'checkTemplate': 'checkTemplate-jkl'
        }

        def execute(query, data, consistency):
            if 'INSERT' in query:
                return defer.succeed(None)
            elif 'SELECT' in query:
                return defer.succeed([expected])

        self.client.execute.side_effect = execute

        d = cass.create_policy(self.client, expected['policyId'],
                               expected['groupId'], expected['alarmTemplate'],
                               expected['checkTemplate'])

        result = self.successResultOf(d)
        self.assertEqual(result, expected)

        calls = [
            mock.call(
                ' '.join([
                    'INSERT INTO policies',
                    '("policyId", "groupId", "alarmTemplate", "checkTemplate")',
                    'VALUES (:policyId, :groupId, :alarmTemplate, :checkTemplate);'
                ]), {
                    'alarmTemplate': 'alarmTemplate-ghi',
                    'checkTemplate': 'checkTemplate-jkl',
                    'policyId': 'policy-abc',
                    'groupId': 'group-def'
                }, 1),
            mock.call(
                'SELECT * FROM policies WHERE "policyId"=:policyId AND "groupId"=:groupId;',
                {
                    'policyId': 'policy-abc',
                    'groupId': 'group-def'
                }, 1)
        ]
        self.assertEqual(self.client.execute.mock_calls, calls)
Example #3
0
    def create_policy(self, request, log, tenant_id, group_id):
        """Create a new policy.

        Receive application/json content for new policy creation.

        :param str tenant_id: A tenant id
        :param str group_id: A group id
        """
        content = json.loads(request.content.read())
        alarm_template_id = content.get('alarmTemplate')
        check_template_id = content.get('checkTemplate')
        policy_id = content.get('policyId')

        d = cass.create_policy(self._db, policy_id, group_id,
                               alarm_template_id, check_template_id)

        # Trigger actions to create the alarm and checks on the MaaS side and set things up

        def serialize(policy):
            json_object = {
                'alarmTemplate':
                policy['alarmTemplate'],
                'checkTemplate':
                policy['checkTemplate'],
                'groupId':
                policy['groupId'],
                'links': [{
                    'href':
                    '{0}{1}'.format(request.URLPath().path,
                                    policy['policyId']),
                    'rel':
                    'self'
                }],
                'policyId':
                policy['policyId']
            }
            request.setHeader('Content-Type', 'application/json')
            request.setResponseCode(201)
            request.write(json.dumps(json_object))
            request.finish()

        return d.addCallback(serialize)
Example #4
0
    def test_create_policy(self):
        """Creates and returns a policy dict."""
        expected = {'policyId': 'policy-abc',
                    'groupId': 'group-def',
                    'alarmTemplate': 'alarmTemplate-ghi',
                    'checkTemplate': 'checkTemplate-jkl'}

        def execute(query, data, consistency):
            if 'INSERT' in query:
                return defer.succeed(None)
            elif 'SELECT' in query:
                return defer.succeed([expected])
        self.client.execute.side_effect = execute

        d = cass.create_policy(self.client, expected['policyId'],
                               expected['groupId'], expected['alarmTemplate'],
                               expected['checkTemplate'])

        result = self.successResultOf(d)
        self.assertEqual(result, expected)

        calls = [
            mock.call(
                ' '.join([
                    'INSERT INTO policies',
                    '("policyId", "groupId", "alarmTemplate", "checkTemplate")',
                    'VALUES (:policyId, :groupId, :alarmTemplate, :checkTemplate);']),
                {'alarmTemplate': 'alarmTemplate-ghi',
                 'checkTemplate': 'checkTemplate-jkl',
                 'policyId': 'policy-abc',
                 'groupId': 'group-def'},
                1),
            mock.call(
                'SELECT * FROM policies WHERE "policyId"=:policyId AND "groupId"=:groupId;',
                {'policyId': 'policy-abc', 'groupId': 'group-def'},
                1)
        ]
        self.assertEqual(self.client.execute.mock_calls, calls)