def setUp(self): init_args = [] self.init_args = init_args class NewInstanceSerializer(structure_test_serializers.NewInstanceSerializer): def __init__(self, *args, **kwargs): init_args.extend([self, args, kwargs]) super(NewInstanceSerializer, self).__init__(*args, **kwargs) class Meta(structure_test_serializers.NewInstanceSerializer.Meta): fields = ( structure_test_serializers.NewInstanceSerializer.Meta.fields + ('cores',) ) class TestNewInstanceViewSet(structure_test_views.TestNewInstanceViewSet): serializer_class = NewInstanceSerializer class TestNewInstanceCreateProcessor(utils.TestCreateProcessor): viewset = TestNewInstanceViewSet fields = ['name', 'cores'] manager.register( offering_type='TEST_TYPE', create_resource_processor=TestNewInstanceCreateProcessor, ) self.service_settings = structure_factories.ServiceSettingsFactory(type='Test') self.fixture = fixtures.ProjectFixture() service = structure_factories.TestServiceFactory( settings=self.service_settings, customer=self.fixture.customer ) structure_factories.TestServiceProjectLinkFactory( service=service, project=self.fixture.project ) self.offering = factories.OfferingFactory( state=models.Offering.States.ACTIVE, type='TEST_TYPE', scope=self.service_settings, ) structure_models.Project.add_quota_field( name='test_cpu_count', quota_field=TotalQuotaField( target_models=[test_models.TestNewInstance], path_to_scope='service_project_link.project', target_field='cores', ), ) quotas_models.Quota.objects.update_or_create( name='test_cpu_count', content_type=ContentType.objects.get_for_model(self.fixture.project), object_id=self.fixture.project.id, defaults={'limit': 1}, )
def ready(self): from waldur_core.quotas.fields import QuotaField, TotalQuotaField from waldur_core.quotas.models import Quota from waldur_core.structure.models import ( ServiceSettings, Project, Customer, SharedServiceSettings, ) from waldur_core.structure import SupportedServices from waldur_openstack.openstack.models import Tenant from .backend import OpenStackTenantBackend from . import handlers, models SupportedServices.register_backend(OpenStackTenantBackend) # Initialize service settings quotas based on tenant. for quota in Tenant.get_quotas_fields(): ServiceSettings.add_quota_field( name=quota.name, quota_field=QuotaField( is_backend=True, default_limit=quota.default_limit, creation_condition=lambda service_settings: service_settings.type == OpenStackTenantConfig.service_name, ), ) signals.post_save.connect( handlers.sync_private_settings_quotas_with_tenant_quotas, sender=Quota, dispatch_uid='openstack_tenant.handlers.sync_private_settings_quotas_with_tenant_quotas', ) signals.post_save.connect( handlers.propagate_volume_type_quotas_from_tenant_to_private_service_settings, sender=Quota, dispatch_uid='openstack_tenant.handlers.propagate_volume_type_quotas_from_tenant_to_private_service_settings', ) signals.post_delete.connect( handlers.delete_volume_type_quotas_from_private_service_settings, sender=Quota, dispatch_uid='openstack_tenant.handlers.delete_volume_type_quotas_from_private_service_settings', ) Project.add_quota_field( name='os_cpu_count', quota_field=TotalQuotaField( target_models=[models.Instance], path_to_scope='service_project_link.project', target_field='cores', ), ) Project.add_quota_field( name='os_ram_size', quota_field=TotalQuotaField( target_models=[models.Instance], path_to_scope='service_project_link.project', target_field='ram', ), ) Project.add_quota_field( name='os_storage_size', quota_field=TotalQuotaField( target_models=[models.Volume, models.Snapshot], path_to_scope='service_project_link.project', target_field='size', ), ) Customer.add_quota_field( name='os_cpu_count', quota_field=TotalQuotaField( target_models=[models.Instance], path_to_scope='service_project_link.project.customer', target_field='cores', ), ) Customer.add_quota_field( name='os_ram_size', quota_field=TotalQuotaField( target_models=[models.Instance], path_to_scope='service_project_link.project.customer', target_field='ram', ), ) Customer.add_quota_field( name='os_storage_size', quota_field=TotalQuotaField( target_models=[models.Volume, models.Snapshot], path_to_scope='service_project_link.project.customer', target_field='size', ), ) for Resource in (models.Instance, models.Volume, models.Snapshot): name = Resource.__name__.lower() signals.post_save.connect( handlers.log_action, sender=Resource, dispatch_uid='openstack_tenant.handlers.log_%s_action' % name, ) for handler in handlers.resource_handlers: model = handler.resource_model name = model.__name__.lower() fsm_signals.post_transition.connect( handler.create_handler, sender=model, dispatch_uid='openstack_tenant.handlers.create_%s' % name, ) fsm_signals.post_transition.connect( handler.update_handler, sender=model, dispatch_uid='openstack_tenant.handlers.update_%s' % name, ) signals.post_save.connect( handler.import_handler, sender=model, dispatch_uid='openstack_tenant.handlers.import_%s' % name, ) signals.post_delete.connect( handler.delete_handler, sender=model, dispatch_uid='openstack_tenant.handlers.delete_%s' % name, ) signals.post_save.connect( handlers.log_backup_schedule_creation, sender=models.BackupSchedule, dispatch_uid='openstack_tenant.handlers.log_backup_schedule_creation', ) signals.post_save.connect( handlers.log_backup_schedule_action, sender=models.BackupSchedule, dispatch_uid='openstack_tenant.handlers.log_backup_schedule_action', ) signals.pre_delete.connect( handlers.log_backup_schedule_deletion, sender=models.BackupSchedule, dispatch_uid='openstack_tenant.handlers.log_backup_schedule_deletion', ) signals.post_save.connect( handlers.log_snapshot_schedule_creation, sender=models.SnapshotSchedule, dispatch_uid='openstack_tenant.handlers.log_snapshot_schedule_creation', ) signals.post_save.connect( handlers.log_snapshot_schedule_action, sender=models.SnapshotSchedule, dispatch_uid='openstack_tenant.handlers.log_snapshot_schedule_action', ) signals.pre_delete.connect( handlers.log_snapshot_schedule_deletion, sender=models.SnapshotSchedule, dispatch_uid='openstack_tenant.handlers.log_snapshot_schedule_deletion', ) signals.post_save.connect( handlers.update_service_settings_credentials, sender=Tenant, dispatch_uid='openstack_tenant.handlers.update_service_settings_credentials', ) signals.post_save.connect( handlers.update_service_settings, sender=Tenant, dispatch_uid='openstack_tenant.handlers.update_service_settings', ) signals.m2m_changed.connect( handlers.sync_certificates_between_openstack_service_with_openstacktenant_service, sender=ServiceSettings.certifications.through, dispatch_uid='openstack_tenant.handlers.' 'sync_certificates_between_openstack_service_with_openstacktenant_service', ) signals.post_save.connect( handlers.copy_certifications_from_openstack_service_to_openstacktenant_service, sender=ServiceSettings, dispatch_uid='openstack_tenant.handlers.' 'copy_certifications_from_openstack_service_to_openstacktenant_service', ) signals.post_save.connect( handlers.copy_flavor_exclude_regex_to_openstacktenant_service_settings, sender=ServiceSettings, dispatch_uid='openstack_tenant.handlers.' 'copy_flavor_exclude_regex_to_openstacktenant_service_settings', ) for model in (SharedServiceSettings, ServiceSettings): signals.post_save.connect( handlers.copy_config_drive_to_openstacktenant_service_settings, sender=model, dispatch_uid='openstack_tenant.handlers.' 'copy_config_drive_to_openstacktenant_service_settings_%s' % model.__class__, ) signals.post_save.connect( handlers.create_service_from_tenant, sender=Tenant, dispatch_uid='openstack_tenant.handlers.create_service_from_tenant', )