Ejemplo n.º 1
0
    def apply_policies_to_server(self, tenant_id, group_id, server_id,
                                 entity_id):
        """ Apply policies to a new server """
        group = []
        d = cass.get_group_by_id(self._db, tenant_id, group_id)

        def get_policies(_group):
            group.append(_group)
            return cass.get_policies_by_group_id(self._db, group_id)

        d.addCallback(get_policies)

        def proc_policies(policies):
            deferreds = [
                self.add_policy_to_server(tenant_id, policy['policyId'],
                                          server_id, entity_id,
                                          policy['checkTemplate'],
                                          policy['alarmTemplate'],
                                          group[0]['notificationPlan'])
                for policy in policies
            ]
            return defer.gatherResults(deferreds, consumeErrors=False)

        d.addCallback(proc_policies)
        d.addCallback(lambda _: defer.succeed(None))
        return d
Ejemplo n.º 2
0
    def test_get_group_by_id_integrity_problems(self):
        """Raises an error if more than one group is found."""
        self.client.execute.return_value = defer.succeed(['group1', 'group2'])

        d = cass.get_group_by_id(self.client, '101010', 'group-abc')

        result = self.failureResultOf(d)
        self.assertTrue(result.check(cass.ExcessiveResultsError))
Ejemplo n.º 3
0
    def test_get_group_by_id_no_such_id(self):
        """Raises an error if no group is found."""
        self.client.execute.return_value = defer.succeed([])

        d = cass.get_group_by_id(self.client, '101010', 'group-abc')

        result = self.failureResultOf(d)
        self.assertTrue(result.check(cass.ResultNotFoundError))
Ejemplo n.º 4
0
    def test_get_group_by_id_integrity_problems(self):
        """Raises an error if more than one group is found."""
        self.client.execute.return_value = defer.succeed(['group1', 'group2'])

        d = cass.get_group_by_id(self.client, '101010', 'group-abc')

        result = self.failureResultOf(d)
        self.assertTrue(result.check(cass.ExcessiveResultsError))
Ejemplo n.º 5
0
    def test_get_group_by_id_no_such_id(self):
        """Raises an error if no group is found."""
        self.client.execute.return_value = defer.succeed([])

        d = cass.get_group_by_id(self.client, '101010', 'group-abc')

        result = self.failureResultOf(d)
        self.assertTrue(result.check(cass.ResultNotFoundError))
Ejemplo n.º 6
0
    def delete_group(self, tenant_id, group_id):
        d = cass.get_group_by_id(self._db, tenant_id, group_id)

        def remove_notification(group):
            maas_client = self._get_maas_client()
            return maas_client.remove_notification_and_plan(
                group['notification'], group['notificationPlan'])
        d.addCallback(remove_notification)

        def delete_group_from_db(_):
            return cass.delete_group(self._db, tenant_id, group_id)
        d.addCallback(delete_group_from_db)

        return d
Ejemplo n.º 7
0
    def delete_group(self, tenant_id, group_id):
        d = cass.get_group_by_id(self._db, tenant_id, group_id)

        def remove_notification(group):
            maas_client = self._get_maas_client()
            return maas_client.remove_notification_and_plan(
                group['notification'], group['notificationPlan'])

        d.addCallback(remove_notification)

        def delete_group_from_db(_):
            return cass.delete_group(self._db, tenant_id, group_id)

        d.addCallback(delete_group_from_db)

        return d
Ejemplo n.º 8
0
    def test_get_group_by_id(self):
        """Returns a single dict, rather than a single item list."""
        expected = {'groupId': 'group-abc',
                    'tenantId': '101010',
                    'notification': 'notification-ghi',
                    'notificationPlan': 'notificationPlan-jkl'}
        self.client.execute.return_value = defer.succeed([expected])

        d = cass.get_group_by_id(self.client, '101010', 'group-abc')

        result = self.successResultOf(d)
        self.assertEqual(result, expected)
        self.client.execute.assert_called_once_with(
            'SELECT * FROM groups WHERE "tenantId"=:tenantId AND "groupId"=:groupId;',
            {'tenantId': '101010', 'groupId': 'group-abc'},
            1)
Ejemplo n.º 9
0
    def test_get_group_by_id(self):
        """Returns a single dict, rather than a single item list."""
        expected = {
            'groupId': 'group-abc',
            'tenantId': '101010',
            'notification': 'notification-ghi',
            'notificationPlan': 'notificationPlan-jkl'
        }
        self.client.execute.return_value = defer.succeed([expected])

        d = cass.get_group_by_id(self.client, '101010', 'group-abc')

        result = self.successResultOf(d)
        self.assertEqual(result, expected)
        self.client.execute.assert_called_once_with(
            'SELECT * FROM groups WHERE "tenantId"=:tenantId AND "groupId"=:groupId;',
            {
                'tenantId': '101010',
                'groupId': 'group-abc'
            }, 1)
Ejemplo n.º 10
0
    def apply_policies_to_server(self, tenant_id, group_id, server_id, entity_id):
        """ Apply policies to a new server """
        group = []
        d = cass.get_group_by_id(self._db, tenant_id, group_id)

        def get_policies(_group):
            group.append(_group)
            return cass.get_policies_by_group_id(self._db, group_id)
        d.addCallback(get_policies)

        def proc_policies(policies):
            deferreds = [
                self.add_policy_to_server(
                    tenant_id, policy['policyId'], server_id, entity_id,
                    policy['checkTemplate'], policy['alarmTemplate'],
                    group[0]['notificationPlan'])
                for policy in policies
            ]
            return defer.gatherResults(deferreds, consumeErrors=False)
        d.addCallback(proc_policies)
        d.addCallback(lambda _: defer.succeed(None))
        return d