def begin_recovering_erred_service_settings(settings_uuid, transition_entity=None): settings = models.ServiceSettings.objects.get(uuid=settings_uuid) try: backend = settings.get_backend() is_active = backend.ping() except ServiceBackendNotImplemented: is_active = False if is_active: settings.set_in_sync() settings.error_message = '' settings.save() logger.info('Service settings %s successfully recovered.' % settings.name) try: spl_model = SupportedServices.get_service_models()[settings.type]['service_project_link'] erred_spls = spl_model.objects.filter(service__settings=settings, state=SynchronizationStates.ERRED) recover_erred_services.delay([spl.to_string() for spl in erred_spls]) except KeyError: logger.warning('Failed to recover service project links for settings %s', settings) else: settings.set_erred() settings.error_message = 'Failed to ping service settings %s' % settings.name settings.save() logger.info('Failed to recover service settings %s.' % settings.name)
def get_links(self): """ Get all service project links connected to current project """ return [ link for model in SupportedServices.get_service_models().values() for link in model['service_project_link'].objects.filter( project=self) ]
def _find_service(self): service_type = self.request.query_params.get('service_type') service_uuid = self.request.query_params.get('service_uuid') if not service_type or not service_uuid: return rows = SupportedServices.get_service_models() if service_type not in rows: return service_class = rows.get(service_type)['service'] try: return service_class.objects.get(uuid=service_uuid) except ObjectDoesNotExist: return None
def connect_customer_to_shared_service_settings(sender, instance, created=False, **kwargs): if not created: return customer = instance for shared_settings in ServiceSettings.objects.filter( shared=True, state=SynchronizationStates.IN_SYNC): service_model = SupportedServices.get_service_models()[ shared_settings.type]['service'] service_model.objects.create(customer=customer, settings=shared_settings, name=shared_settings.name, available_for_all=True)
def create_service_project_link(self, customer, project): service_type, models = SupportedServices.get_service_models().items( )[0] class ServiceFactory(factory.DjangoModelFactory): class Meta(object): model = models['service'] class ServiceProjectLinkFactory(factory.DjangoModelFactory): class Meta(object): model = models['service_project_link'] settings = structure_factories.ServiceSettingsFactory( customer=customer, type=service_type, shared=False) service = ServiceFactory(customer=customer, settings=settings) return ServiceProjectLinkFactory(service=service, project=project)
def connect_customer_to_shared_service_settings(sender, instance, created=False, **kwargs): if not created: return customer = instance for shared_settings in ServiceSettings.objects.filter(shared=True): try: service_model = SupportedServices.get_service_models()[ shared_settings.type]['service'] service_model.objects.create(customer=customer, settings=shared_settings, available_for_all=True) except KeyError: logger.warning("Unregistered service of type %s" % shared_settings.type)
def connect_shared_service_settings_to_customers(sender, instance, name, source, target, **kwargs): """ Connected service settings with all customers if they were created or become shared """ service_settings = instance if (target != SynchronizationStates.IN_SYNC or source not in (SynchronizationStates.ERRED, SynchronizationStates.CREATING) or not service_settings.shared): return service_model = SupportedServices.get_service_models()[ service_settings.type]['service'] for customer in Customer.objects.all(): if not service_model.objects.filter( customer=customer, settings=service_settings).exists(): service_model.objects.create(customer=customer, settings=service_settings, name=service_settings.name, available_for_all=True)
def create_resource(self, customer, project): service_type, models = SupportedServices.get_service_models().items()[0] class ServiceFactory(factory.DjangoModelFactory): class Meta(object): model = models['service'] class ServiceProjectLinkFactory(factory.DjangoModelFactory): class Meta(object): model = models['service_project_link'] class ResourceFactory(factory.DjangoModelFactory): class Meta(object): model = models['resources'][0] settings = factories.ServiceSettingsFactory(customer=customer, type=service_type) service = ServiceFactory(customer=customer, settings=settings) spl = ServiceProjectLinkFactory(service=service, project=project) resource = ResourceFactory(service_project_link=spl) return resource
def recover_erred_services(service_project_links=None): if service_project_links is not None: erred_spls = models.ServiceProjectLink.from_string(service_project_links) for spl in erred_spls: recover_erred_service.delay(spl.to_string(), is_iaas=spl._meta.app_label == 'iaas') else: for service_type, service in SupportedServices.get_service_models().items(): # TODO: Remove IaaS support (NC-645) is_iaas = service_type == SupportedServices.Types.IaaS query = Q(state=SynchronizationStates.ERRED) if is_iaas: query |= Q(cloud__state=SynchronizationStates.ERRED) else: query |= Q(service__settings__state=SynchronizationStates.ERRED) erred_spls = service['service_project_link'].objects.filter(query) for spl in erred_spls: recover_erred_service.delay(spl.to_string(), is_iaas=spl._meta.app_label == 'iaas')
def execute(self, service_settings): logger.debug( 'About to connect service settings "%s" to all available customers' % service_settings.name) if not service_settings.shared: raise ValueError('It is impossible to connect non-shared settings') service_model = SupportedServices.get_service_models()[ service_settings.type]['service'] with transaction.atomic(): for customer in models.Customer.objects.all(): defaults = {'available_for_all': True} service, _ = service_model.objects.get_or_create( customer=customer, settings=service_settings, defaults=defaults) service_project_link_model = service.projects.through for project in service.customer.projects.all(): service_project_link_model.objects.get_or_create( project=project, service=service) logger.info( 'Successfully connected service settings "%s" to all available customers' % service_settings.name)
def test_modify_suspended_services_and_resources(self): self.client.force_authenticate(user=self.user) for service_type, models in SupportedServices.get_service_models( ).items(): # XXX: quick fix for iaas cloud. Can be removed after iaas application refactoring. if service_type == -1: continue settings = factories.ServiceSettingsFactory(customer=self.customer, type=service_type, shared=True) class ServiceFactory(factory.DjangoModelFactory): class Meta(object): model = models['service'] class ServiceProjectLinkFactory(factory.DjangoModelFactory): class Meta(object): model = models['service_project_link'] if service_type == SupportedServices.Types.IaaS: service = ServiceFactory(customer=self.customer, state=SynchronizationStates.IN_SYNC) OpenStackSettings.objects.get_or_create( auth_url='http://example.com:5000/v2', defaults={ 'username': '******', 'password': '******', 'tenant_name': 'admin', }) else: service = models['service'].objects.create( customer=self.customer, settings=settings, name=settings.name, available_for_all=True) service_url = self._get_url( SupportedServices.get_detail_view_for_model(models['service']), uuid=service.uuid.hex) response = self.client.patch(service_url, {'name': 'new name'}) self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) response = self.client.delete(service_url) self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) for resource_model in models['resources']: if service_type == SupportedServices.Types.IaaS: continue class ResourceFactory(factory.DjangoModelFactory): class Meta(object): model = resource_model project = factories.ProjectFactory(customer=self.customer) spl = models['service_project_link'].objects.get( service=service, project=project) # XXX: Some resources can have more required fields and creation will fail. Lets just skip them. try: resource = ResourceFactory(service_project_link=spl) except: pass else: resource_url = self._get_url( SupportedServices.get_detail_view_for_model( resource_model), uuid=resource.uuid.hex) response = self.client.post(resource_url + 'start/') self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
def get_services(self): service_model = SupportedServices.get_service_models()[ self.type]['service'] return service_model.objects.filter(settings=self)
def test_auto_quotas_update(self): for service_type, models in SupportedServices.get_service_models( ).items(): settings = factories.ServiceSettingsFactory(customer=self.customer, type=service_type, shared=False) class ServiceFactory(factory.DjangoModelFactory): class Meta(object): model = models['service'] class ServiceProjectLinkFactory(factory.DjangoModelFactory): class Meta(object): model = models['service_project_link'] if service_type == SupportedServices.Types.IaaS: service = ServiceFactory(customer=self.customer, state=SynchronizationStates.IN_SYNC) OpenStackSettings.objects.get_or_create( auth_url='http://example.com:5000/v2', defaults={ 'username': '******', 'password': '******', 'tenant_name': 'admin', }) else: service = ServiceFactory(customer=self.customer, settings=settings) for resource_model in models['resources']: if not hasattr(resource_model, 'update_quota_usage'): continue class ResourceFactory(factory.DjangoModelFactory): class Meta(object): model = resource_model data = {'cores': 4, 'ram': 1024, 'disk': 20480} service_project_link = ServiceProjectLinkFactory( service=service, project=self.project) resource = ResourceFactory( service_project_link=service_project_link, cores=data['cores']) self.assertEqual( service_project_link.quotas.get(name='instances').usage, 1) self.assertEqual( service_project_link.quotas.get(name='vcpu').usage, data['cores']) self.assertEqual( service_project_link.quotas.get(name='ram').usage, 0) self.assertEqual( service_project_link.quotas.get(name='storage').usage, 0) resource.ram = data['ram'] resource.disk = data['disk'] resource.save() self.assertEqual( service_project_link.quotas.get(name='ram').usage, data['ram']) self.assertEqual( service_project_link.quotas.get(name='storage').usage, data['disk']) resource.delete() self.assertEqual( service_project_link.quotas.get(name='instances').usage, 0) self.assertEqual( service_project_link.quotas.get(name='vcpu').usage, 0) self.assertEqual( service_project_link.quotas.get(name='ram').usage, 0) self.assertEqual( service_project_link.quotas.get(name='storage').usage, 0)