Exemplo n.º 1
0
def register_policy_types(module):
    registered_count = 0
    mod_path = os.path.dirname(os.path.realpath(sys.modules[module.__name__].__file__))
    path = os.path.join(mod_path, 'policies/meta')

    files = []
    for ext in ALLOWED_EXTS:
        exp = '%s/*%s' % (path, ext)
        files += glob.glob(exp)

    for f in files:
        try:
            LOG.debug('Loading policy type from "%s".', f)
            content = loader.load_meta_file(f)
            policy_type_api = PolicyTypeAPI(**content)
            policy_type_db = PolicyTypeAPI.to_model(policy_type_api)

            try:
                existing_entry = PolicyType.get_by_ref(policy_type_db.ref)
                if existing_entry:
                    policy_type_db.id = existing_entry.id
            except StackStormDBObjectNotFoundError:
                LOG.debug('Policy type "%s" is not found. Creating new entry.',
                          policy_type_db.ref)

            policy_type_db = PolicyType.add_or_update(policy_type_db)
            extra = {'policy_type_db': policy_type_db}
            LOG.audit('Policy type "%s" is updated.', policy_type_db.ref, extra=extra)

            registered_count += 1
        except:
            LOG.exception('Unable to register policy type from "%s".', f)

    return registered_count
Exemplo n.º 2
0
def register_policy_types(module):
    registered_count = 0
    mod_path = os.path.dirname(os.path.realpath(sys.modules[module.__name__].__file__))
    path = '%s/policies/meta' % mod_path

    files = []
    for ext in ALLOWED_EXTS:
        exp = '%s/*%s' % (path, ext)
        files += glob.glob(exp)

    for f in files:
        try:
            LOG.debug('Loading policy type from "%s".', f)
            content = loader.load_meta_file(f)
            policy_type_api = PolicyTypeAPI(**content)
            policy_type_db = PolicyTypeAPI.to_model(policy_type_api)

            try:
                existing_entry = PolicyType.get_by_ref(policy_type_db.ref)
                if existing_entry:
                    policy_type_db.id = existing_entry.id
            except ValueError:
                LOG.debug('Policy type "%s" is not found. Creating new entry.',
                          policy_type_db.ref)

            policy_type_db = PolicyType.add_or_update(policy_type_db)
            extra = {'policy_type_db': policy_type_db}
            LOG.audit('Policy type "%s" is updated.', policy_type_db.ref, extra=extra)

            registered_count += 1
        except:
            LOG.exception('Unable to register policy type from "%s".', f)

    return registered_count
Exemplo n.º 3
0
    def setUpClass(cls):
        super(PolicyControllerTest, cls).setUpClass()

        for _, fixture in six.iteritems(FIXTURES['policytypes']):
            instance = PolicyTypeAPI(**fixture)
            PolicyType.add_or_update(PolicyTypeAPI.to_model(instance))

        for _, fixture in six.iteritems(FIXTURES['policies']):
            instance = PolicyAPI(**fixture)
            Policy.add_or_update(PolicyAPI.to_model(instance))
Exemplo n.º 4
0
    def test_register_policy_types(self):
        self.assertEqual(register_policy_types(st2tests), 2)

        type1 = PolicyType.get_by_ref("action.concurrency")
        self.assertEqual(type1.name, "concurrency")
        self.assertEqual(type1.resource_type, "action")

        type2 = PolicyType.get_by_ref("action.mock_policy_error")
        self.assertEqual(type2.name, "mock_policy_error")
        self.assertEqual(type2.resource_type, "action")
Exemplo n.º 5
0
    def test_register_policy_types(self):
        self.assertEqual(register_policy_types(st2tests), 2)

        type1 = PolicyType.get_by_ref("action.concurrency")
        self.assertEqual(type1.name, "concurrency")
        self.assertEqual(type1.resource_type, "action")

        type2 = PolicyType.get_by_ref("action.mock_policy_error")
        self.assertEqual(type2.name, "mock_policy_error")
        self.assertEqual(type2.resource_type, "action")
Exemplo n.º 6
0
    def setUpClass(cls):
        super(PolicyControllerTest, cls).setUpClass()

        for _, fixture in six.iteritems(FIXTURES['policytypes']):
            instance = PolicyTypeAPI(**fixture)
            PolicyType.add_or_update(PolicyTypeAPI.to_model(instance))

        for _, fixture in six.iteritems(FIXTURES['policies']):
            instance = PolicyAPI(**fixture)
            Policy.add_or_update(PolicyAPI.to_model(instance))
Exemplo n.º 7
0
    def test_register_policy_types(self):
        self.assertEqual(register_policy_types(st2tests), 2)

        type1 = PolicyType.get_by_ref('action.concurrency')
        self.assertEqual(type1.name, 'concurrency')
        self.assertEqual(type1.resource_type, 'action')

        type2 = PolicyType.get_by_ref('action.mock_policy_error')
        self.assertEqual(type2.name, 'mock_policy_error')
        self.assertEqual(type2.resource_type, 'action')
Exemplo n.º 8
0
    def test_register_policy_types(self):
        self.assertEqual(register_policy_types(st2tests), 2)

        type1 = PolicyType.get_by_ref('action.concurrency')
        self.assertEqual(type1.name, 'concurrency')
        self.assertEqual(type1.resource_type, 'action')

        type2 = PolicyType.get_by_ref('action.mock_policy_error')
        self.assertEqual(type2.name, 'mock_policy_error')
        self.assertEqual(type2.resource_type, 'action')
Exemplo n.º 9
0
    def setUpClass(cls):
        super(PolicyControllerTestCase, cls).setUpClass()

        for _, fixture in six.iteritems(FIXTURES["policytypes"]):
            instance = PolicyTypeAPI(**fixture)
            PolicyType.add_or_update(PolicyTypeAPI.to_model(instance))

        cls.policy_dbs = []

        for _, fixture in six.iteritems(FIXTURES["policies"]):
            instance = PolicyAPI(**fixture)
            policy_db = Policy.add_or_update(PolicyAPI.to_model(instance))
            cls.policy_dbs.append(policy_db)
Exemplo n.º 10
0
    def validate(self):
        # Validate policy itself
        cleaned = super(PolicyAPI, self).validate()

        # Validate policy parameters
        # pylint: disable=no-member
        policy_type_db = PolicyType.get_by_ref(cleaned.policy_type)
        if not policy_type_db:
            raise ValueError('Referenced policy_type "%s" doesnt exist' %
                             (cleaned.policy_type))

        parameters_schema = policy_type_db.parameters
        parameters = getattr(cleaned, 'parameters', {})
        schema = util_schema.get_schema_for_resource_parameters(
            parameters_schema=parameters_schema)
        validator = util_schema.get_validator()
        cleaned_parameters = util_schema.validate(parameters,
                                                  schema,
                                                  validator,
                                                  use_default=True,
                                                  allow_default_none=True)

        cleaned.parameters = cleaned_parameters

        return cleaned
Exemplo n.º 11
0
    def setUpClass(cls):
        super(SchedulingPolicyTest, cls).setUpClass()

        # Register runners
        runners_registrar.register_runners()

        for _, fixture in six.iteritems(FIXTURES['actions']):
            instance = ActionAPI(**fixture)
            Action.add_or_update(ActionAPI.to_model(instance))

        for _, fixture in six.iteritems(FIXTURES['policytypes']):
            instance = PolicyTypeAPI(**fixture)
            PolicyType.add_or_update(PolicyTypeAPI.to_model(instance))

        for _, fixture in six.iteritems(FIXTURES['policies']):
            instance = PolicyAPI(**fixture)
            Policy.add_or_update(PolicyAPI.to_model(instance))
Exemplo n.º 12
0
    def setUpClass(cls):
        super(SchedulingPolicyTest, cls).setUpClass()

        # Register runners
        runners_registrar.register_runners()

        for _, fixture in six.iteritems(FIXTURES['actions']):
            instance = ActionAPI(**fixture)
            Action.add_or_update(ActionAPI.to_model(instance))

        for _, fixture in six.iteritems(FIXTURES['policytypes']):
            instance = PolicyTypeAPI(**fixture)
            PolicyType.add_or_update(PolicyTypeAPI.to_model(instance))

        for _, fixture in six.iteritems(FIXTURES['policies']):
            instance = PolicyAPI(**fixture)
            Policy.add_or_update(PolicyAPI.to_model(instance))
Exemplo n.º 13
0
    def setUpClass(cls):
        super(PolicyTypeControllerTestCase, cls).setUpClass()

        cls.policy_type_dbs = []

        for _, fixture in six.iteritems(FIXTURES['policytypes']):
            instance = PolicyTypeAPI(**fixture)
            policy_type_db = PolicyType.add_or_update(PolicyTypeAPI.to_model(instance))
            cls.policy_type_dbs.append(policy_type_db)
Exemplo n.º 14
0
    def setUpClass(cls):
        super(PolicyTest, cls).setUpClass()

        for _, fixture in six.iteritems(FIXTURES['runners']):
            instance = RunnerTypeAPI(**fixture)
            RunnerType.add_or_update(RunnerTypeAPI.to_model(instance))

        for _, fixture in six.iteritems(FIXTURES['actions']):
            instance = ActionAPI(**fixture)
            Action.add_or_update(ActionAPI.to_model(instance))

        for _, fixture in six.iteritems(FIXTURES['policytypes']):
            instance = PolicyTypeAPI(**fixture)
            PolicyType.add_or_update(PolicyTypeAPI.to_model(instance))

        for _, fixture in six.iteritems(FIXTURES['policies']):
            instance = PolicyAPI(**fixture)
            Policy.add_or_update(PolicyAPI.to_model(instance))
Exemplo n.º 15
0
    def setUpClass(cls):
        super(PolicyTest, cls).setUpClass()

        for _, fixture in six.iteritems(FIXTURES['runners']):
            instance = RunnerTypeAPI(**fixture)
            RunnerType.add_or_update(RunnerTypeAPI.to_model(instance))

        for _, fixture in six.iteritems(FIXTURES['actions']):
            instance = ActionAPI(**fixture)
            Action.add_or_update(ActionAPI.to_model(instance))

        for _, fixture in six.iteritems(FIXTURES['policytypes']):
            instance = PolicyTypeAPI(**fixture)
            PolicyType.add_or_update(PolicyTypeAPI.to_model(instance))

        for _, fixture in six.iteritems(FIXTURES['policies']):
            instance = PolicyAPI(**fixture)
            Policy.add_or_update(PolicyAPI.to_model(instance))
Exemplo n.º 16
0
    def test_get_by_ref(self):
        policy_db = Policy.get_by_ref("wolfpack.action-1.concurrency")
        self.assertIsNotNone(policy_db)
        self.assertEqual(policy_db.pack, "wolfpack")
        self.assertEqual(policy_db.name, "action-1.concurrency")

        policy_type_db = PolicyType.get_by_ref(policy_db.policy_type)
        self.assertIsNotNone(policy_type_db)
        self.assertEqual(policy_type_db.resource_type, "action")
        self.assertEqual(policy_type_db.name, "concurrency")
Exemplo n.º 17
0
    def test_get_by_ref(self):
        policy_db = Policy.get_by_ref("wolfpack.action-1.concurrency")
        self.assertIsNotNone(policy_db)
        self.assertEqual(policy_db.pack, "wolfpack")
        self.assertEqual(policy_db.name, "action-1.concurrency")

        policy_type_db = PolicyType.get_by_ref(policy_db.policy_type)
        self.assertIsNotNone(policy_type_db)
        self.assertEqual(policy_type_db.resource_type, "action")
        self.assertEqual(policy_type_db.name, "concurrency")
Exemplo n.º 18
0
    def setUp(self):
        EventletTestCase.setUpClass()
        DbTestCase.setUpClass()

        for _, fixture in six.iteritems(FIXTURES["runners"]):
            instance = RunnerTypeAPI(**fixture)
            RunnerType.add_or_update(RunnerTypeAPI.to_model(instance))

        for _, fixture in six.iteritems(FIXTURES["actions"]):
            instance = ActionAPI(**fixture)
            Action.add_or_update(ActionAPI.to_model(instance))

        for _, fixture in six.iteritems(FIXTURES["policytypes"]):
            instance = PolicyTypeAPI(**fixture)
            PolicyType.add_or_update(PolicyTypeAPI.to_model(instance))

        for _, fixture in six.iteritems(FIXTURES["policies"]):
            instance = PolicyAPI(**fixture)
            Policy.add_or_update(PolicyAPI.to_model(instance))
Exemplo n.º 19
0
    def test_get_by_ref(self):
        policy_db = Policy.get_by_ref('wolfpack.action-1.concurrency')
        self.assertIsNotNone(policy_db)
        self.assertEqual(policy_db.pack, 'wolfpack')
        self.assertEqual(policy_db.name, 'action-1.concurrency')

        policy_type_db = PolicyType.get_by_ref(policy_db.policy_type)
        self.assertIsNotNone(policy_type_db)
        self.assertEqual(policy_type_db.resource_type, 'action')
        self.assertEqual(policy_type_db.name, 'concurrency')
    def test_get_by_ref(self):
        policy_db = Policy.get_by_ref('wolfpack.action-1.concurrency')
        self.assertIsNotNone(policy_db)
        self.assertEqual(policy_db.pack, 'wolfpack')
        self.assertEqual(policy_db.name, 'action-1.concurrency')

        policy_type_db = PolicyType.get_by_ref(policy_db.policy_type)
        self.assertIsNotNone(policy_type_db)
        self.assertEqual(policy_type_db.resource_type, 'action')
        self.assertEqual(policy_type_db.name, 'concurrency')
Exemplo n.º 21
0
    def setUp(self):
        EventletTestCase.setUpClass()
        DbTestCase.setUpClass()

        for _, fixture in six.iteritems(FIXTURES['runners']):
            instance = RunnerTypeAPI(**fixture)
            RunnerType.add_or_update(RunnerTypeAPI.to_model(instance))

        for _, fixture in six.iteritems(FIXTURES['actions']):
            instance = ActionAPI(**fixture)
            Action.add_or_update(ActionAPI.to_model(instance))

        for _, fixture in six.iteritems(FIXTURES['policytypes']):
            instance = PolicyTypeAPI(**fixture)
            PolicyType.add_or_update(PolicyTypeAPI.to_model(instance))

        for _, fixture in six.iteritems(FIXTURES['policies']):
            instance = PolicyAPI(**fixture)
            Policy.add_or_update(PolicyAPI.to_model(instance))
Exemplo n.º 22
0
    def validate(self):
        # Validate policy itself
        cleaned = super(PolicyAPI, self).validate()

        # Validate policy parameters
        policy_type_db = PolicyType.get_by_ref(cleaned.policy_type)
        if not policy_type_db:
            raise ValueError('Referenced policy_type "%s" doesnt exist' % (cleaned.policy_type))

        parameters_schema = policy_type_db.parameters
        parameters = getattr(cleaned, 'parameters', {})
        schema = util_schema.get_schema_for_resource_parameters(
            parameters_schema=parameters_schema)
        validator = util_schema.get_validator()
        cleaned_parameters = util_schema.validate(parameters, schema, validator, use_default=True,
                                                  allow_default_none=True)

        cleaned.parameters = cleaned_parameters

        return cleaned