def get_serializer_class(self): from waldur_core.structure import SupportedServices if self.request.method == 'POST': resource_type = self.request.data.get('type') if resource_type == SupportedServices.get_name_for_model(models.Instance): return serializers.InstanceImportSerializer elif resource_type == SupportedServices.get_name_for_model(models.Volume): return serializers.VolumeImportSerializer return super(AmazonServiceViewSet, self).get_serializer_class()
def get_resources_for_import(self, resource_type=None): from waldur_core.structure import SupportedServices resources = [] if resource_type is None or resource_type == SupportedServices.get_name_for_model(models.Instance): resources.extend(self.get_instances_for_import()) if resource_type is None or resource_type == SupportedServices.get_name_for_model(models.Volume): resources.extend(self.get_volumes_for_import()) return resources
def _get_log_context(self, entity_name): context = super(ResourceMixin, self)._get_log_context(entity_name) # XXX: Add resource_full_name here, because event context does not support properties as fields context['resource_full_name'] = self.full_name context['resource_type'] = SupportedServices.get_name_for_model(self) return context
def get_limit(self, resource): nc_settings = getattr(settings, 'WALDUR_OPENSTACK', {}) limit_per_type = nc_settings.get('MAX_CONCURRENT_PROVISION', {}) model_name = SupportedServices.get_name_for_model(resource) return limit_per_type.get( model_name, super(LimitedPerTypeThrottleMixin, self).get_limit(resource) )
def get_fields(self): from waldur_core.structure import SupportedServices fields = super(AWSImportSerializerMixin, self).get_fields() resources = SupportedServices.get_service_resources(models.AWSService) choices = [SupportedServices.get_name_for_model(resource) for resource in resources] fields['type'] = serializers.ChoiceField(choices=choices, write_only=True) return fields
def to_instance(self, instance, region): from waldur_core.structure import SupportedServices manager = self._get_api(region.backend_id) # TODO: Connect volume with instance try: volumes = {v.id: v.size for v in manager.list_volumes(instance.id)} except Exception as e: reraise(e) for device in instance.extra['block_device_mapping']: vid = device['ebs']['volume_id'] if vid in volumes: device['ebs']['volume_size'] = volumes[vid] # libcloud is a funny buggy thing, put all required info here instance_type = self.get_size(instance.extra['instance_type'], manager) return { 'id': instance.id, 'name': instance.name or instance.uuid, 'cores': instance_type.extra.get('cpu', 1), 'ram': instance_type.ram, 'disk': self.gb2mb(sum(volumes.values())), 'created': dateparse.parse_datetime(instance.extra['launch_time']), 'region': region.uuid.hex, 'state': models.Instance.States.OK, 'public_ips': instance.public_ips, 'flavor_name': instance.extra.get('instance_type'), 'type': SupportedServices.get_name_for_model(models.Instance), 'runtime_state': instance.state }
def service_settings_description(): services = [] for cls in BaseServiceSerializer.__subclasses__(): if cls.Meta.model is NotImplemented: continue if not SupportedServices._is_active_model(cls.Meta.model): continue name = SupportedServices.get_name_for_model(cls.Meta.model) fields, extra_fields = get_fields(cls) services.append((name, {'fields': fields, 'extra_fields': extra_fields})) return {'services': sorted(services)}
def to_volume(self, volume): from waldur_core.structure import SupportedServices return { 'id': volume.id, 'name': volume.name, 'size': volume.size, 'created': volume.extra['create_time'], 'state': self._get_volume_state(volume.state), 'runtime_state': volume.state, 'type': SupportedServices.get_name_for_model(models.Volume), 'device': volume.extra['device'], 'instance_id': volume.extra['instance_id'], 'volume_type': volume.extra['volume_type'] }
def _get_log_context(self, entity_name): context = super(ResourceMixin, self)._get_log_context(entity_name) # XXX: Add resource_full_name here, because event context does not support properties as fields context['resource_full_name'] = self.full_name # required for lookups in ElasticSearch by the client context['resource_type'] = SupportedServices.get_name_for_model(self) # XXX: a hack for IaaS / PaaS / SaaS tags # XXX: should be moved to itacloud assembly if self.tags.filter(name='IaaS').exists(): context['resource_delivery_model'] = 'IaaS' elif self.tags.filter(name='PaaS').exists(): context['resource_delivery_model'] = 'PaaS' elif self.tags.filter(name='SaaS').exists(): context['resource_delivery_model'] = 'SaaS' return context
def get_resources_for_import(self): cur_droplets = models.Droplet.objects.all().values_list('backend_id', flat=True) statuses = ('active', 'off') droplets = self.get_all_droplets() return [ { 'id': droplet.id, 'name': droplet.name, 'created_at': droplet.created_at, 'cores': droplet.vcpus, 'ram': droplet.memory, 'disk': self.gb2mb(droplet.disk), 'flavor_name': droplet.size_slug, 'resource_type': SupportedServices.get_name_for_model(models.Droplet), } for droplet in droplets if str(droplet.id) not in cur_droplets and droplet.status in statuses ]
def _get_log_context(self, entity_name): context = super(Service, self)._get_log_context(entity_name) context['service_type'] = SupportedServices.get_name_for_model(self) return context
def get_scope_type(self, item): return SupportedServices.get_name_for_model( item.content_type.model_class())
def filter_by_logged_object(self): return { 'resource_uuid': self.uuid.hex, 'resource_type': SupportedServices.get_name_for_model(self) }
def get_scope_type(self, item): return SupportedServices.get_name_for_model(openstack_models.Tenant)
def full_name(self): return '%s %s' % (SupportedServices.get_name_for_model(self).replace('.', ' '), self.name)
def resource_type(self): cls = self.resource_content_type.model_class() if cls: return SupportedServices.get_name_for_model(cls)
def get_resource_type(self, obj): if obj.resource: return SupportedServices.get_name_for_model(obj.resource_content_type.model_class())
def get_resource_type(self, obj): if isinstance(obj.resource, structure_models.ResourceMixin): return SupportedServices.get_name_for_model(obj.resource_content_type.model_class())
def get_scope_type(cls): return SupportedServices.get_name_for_model(cls)
def get_resource_type(self, obj): if obj.is_resource_estimate(): return SupportedServices.get_name_for_model( obj.content_type.model_class())