def test_tenant_quota_usages_with_id(self): tenant_id = 3 cinder.is_volume_service_enabled(IsA(http.HttpRequest)).AndReturn(True) api.base.is_service_enabled(IsA(http.HttpRequest), 'network').AndReturn(False) api.base.is_service_enabled(IsA(http.HttpRequest), 'compute') \ .MultipleTimes().AndReturn(True) api.nova.tenant_absolute_limits( IsA(http.HttpRequest), reserved=True, tenant_id=tenant_id).AndReturn(self.limits['absolute']) api.cinder.tenant_absolute_limits( IsA(http.HttpRequest), tenant_id).AndReturn(self.cinder_limits['absolute']) self.mox.ReplayAll() quota_usages = quotas.tenant_quota_usages(self.request, tenant_id=tenant_id) expected_output = self.get_usages_from_limits( with_volume=True, with_compute=True) # Compare internal structure of usages to expected. self.assertItemsEqual(expected_output, quota_usages.usages) # Compare available resources self.assertAvailableQuotasEqual(expected_output, quota_usages.usages)
def _test_tenant_quota_usages_with_target( self, targets, use_compute_call=True, use_flavor_list=False, use_cinder_call=False): tenant_id = self.request.user.tenant_id cinder.is_volume_service_enabled(IsA(http.HttpRequest)).AndReturn(True) api.base.is_service_enabled(IsA(http.HttpRequest), 'network') \ .AndReturn(False) api.base.is_service_enabled(IsA(http.HttpRequest), 'compute') \ .MultipleTimes().AndReturn(True) if use_compute_call: api.nova.tenant_absolute_limits( IsA(http.HttpRequest), reserved=True, tenant_id=tenant_id).AndReturn(self.limits['absolute']) if use_cinder_call: api.cinder.tenant_absolute_limits( IsA(http.HttpRequest), tenant_id).AndReturn(self.cinder_limits['absolute']) self.mox.ReplayAll() quota_usages = quotas.tenant_quota_usages(self.request, targets=targets) expected = self.get_usages_from_limits() expected = dict((k, v) for k, v in expected.items() if k in targets) # Compare internal structure of usages to expected. self.assertItemsEqual(expected, quota_usages.usages) # Compare available resources self.assertAvailableQuotasEqual(expected, quota_usages.usages)
def test_tenant_quota_usages_unlimited_quota(self): tenant_id = '1' inf_quota = self.quotas.first() inf_quota['ram'] = -1 cinder.is_volume_service_enabled( IsA(http.HttpRequest) ).AndReturn(True) api.base.is_service_enabled(IsA(http.HttpRequest), 'network').AndReturn(False) api.base.is_service_enabled(IsA(http.HttpRequest), 'compute').MultipleTimes().AndReturn(True) api.nova.tenant_absolute_limits( IsA(http.HttpRequest), reserved=True, tenant_id=tenant_id).AndReturn(self.limits['absolute']) api.cinder.tenant_absolute_limits( IsA(http.HttpRequest), tenant_id).AndReturn(self.cinder_limits['absolute']) self.mox.ReplayAll() quota_usages = quotas.tenant_quota_usages(self.request) expected_output = self.get_usages_from_limits() expected_output.update({'ram': {'available': float("inf"), 'used': 1024, 'quota': float("inf")}}) # Compare internal structure of usages to expected. self.assertItemsEqual(expected_output, quota_usages.usages)
def test_get_all_disabled_quotas(self): cinder.is_volume_service_enabled(IsA(http.HttpRequest)).AndReturn( False) api.base.is_service_enabled(IsA(http.HttpRequest), 'network').AndReturn(False) self.mox.ReplayAll() result_quotas = quotas.get_disabled_quotas(self.request) expected_quotas = list(quotas.CINDER_QUOTA_FIELDS) + \ list(quotas.NEUTRON_QUOTA_FIELDS) + list(quotas.NOVA_QUOTA_FIELDS) self.assertItemsEqual(result_quotas, expected_quotas)
def _test_tenant_quota_usages(self, nova_quotas_enabled=True, with_compute=True, with_volume=True): cinder.is_volume_service_enabled(IsA(http.HttpRequest)).AndReturn( with_volume) api.base.is_service_enabled(IsA(http.HttpRequest), 'network').AndReturn(False) api.base.is_service_enabled( IsA(http.HttpRequest), 'compute' ).MultipleTimes().AndReturn(with_compute) if with_compute: servers = [s for s in self.servers.list() if s.tenant_id == self.request.user.tenant_id] api.nova.flavor_list(IsA(http.HttpRequest)) \ .AndReturn(self.flavors.list()) api.network.floating_ip_supported(IsA(http.HttpRequest)) \ .AndReturn(True) api.network.tenant_floating_ip_list(IsA(http.HttpRequest)) \ .AndReturn(self.floating_ips.list()) search_opts = {'tenant_id': self.request.user.tenant_id} api.nova.server_list(IsA(http.HttpRequest), search_opts=search_opts, all_tenants=True) \ .AndReturn([servers, False]) if nova_quotas_enabled: api.nova.tenant_quota_get(IsA(http.HttpRequest), '1') \ .AndReturn(self.quotas.first()) if with_volume: opts = {'all_tenants': 1, 'project_id': self.request.user.tenant_id} cinder.volume_list(IsA(http.HttpRequest), opts) \ .AndReturn(self.volumes.list()) cinder.volume_snapshot_list(IsA(http.HttpRequest), opts) \ .AndReturn(self.cinder_volume_snapshots.list()) cinder.tenant_quota_get(IsA(http.HttpRequest), '1') \ .AndReturn(self.cinder_quotas.first()) self.mox.ReplayAll() quota_usages = quotas.tenant_quota_usages(self.request) expected_output = self.get_usages( nova_quotas_enabled=nova_quotas_enabled, with_volume=with_volume, with_compute=with_compute) # Compare internal structure of usages to expected. self.assertItemsEqual(expected_output, quota_usages.usages) # Compare available resources self.assertAvailableQuotasEqual(expected_output, quota_usages.usages)
def __init__(self, request, context, *args, **kwargs): self._init_images_cache() self.request = request self.context = context super(SetInstanceDetailsAction, self).__init__( request, context, *args, **kwargs) # Hide the device field if the hypervisor doesn't support it. if not nova.can_set_mount_point(): self.fields['device_name'].widget = forms.widgets.HiddenInput() source_type_choices = [ ('', _("Select source")), ("image_id", _("Boot from image")), ("instance_snapshot_id", _("Boot from snapshot")), ] if cinder.is_volume_service_enabled(request): source_type_choices.append(("volume_id", _("Boot from volume"))) try: if api.nova.extension_supported("BlockDeviceMappingV2Boot", request): source_type_choices.append( ("volume_image_id", _("Boot from image (creates a new volume)"))) except Exception: exceptions.handle(request, _('Unable to retrieve extensions ' 'information.')) source_type_choices.append( ("volume_snapshot_id", _("Boot from volume snapshot (creates a new volume)"))) self.fields['source_type'].choices = source_type_choices
def tenant_limit_usages(request): # TODO(licostan): This method shall be removed from Quota module. # ProjectUsage/BaseUsage maybe used instead on volume/image dashboards. limits = {} try: if base.is_service_enabled(request, 'compute'): limits.update(nova.tenant_absolute_limits(request, reserved=True)) except Exception: msg = _("Unable to retrieve compute limit information.") exceptions.handle(request, msg) if cinder.is_volume_service_enabled(request): try: limits.update(cinder.tenant_absolute_limits(request)) except cinder.cinder_exception.ClientException: msg = _("Unable to retrieve volume limit information.") exceptions.handle(request, msg) # TODO(amotoki): Support neutron quota details extensions # which returns limit/usage/reserved per resource. # Note that the data format is different from nova/cinder limit API. # https://developer.openstack.org/ # api-ref/network/v2/#quotas-details-extension-quota-details return limits
def get_disabled_quotas(request): # We no longer supports nova network, so we always disable # network related nova quota fields. disabled_quotas = set() # Cinder if not cinder.is_volume_service_enabled(request): disabled_quotas.update(CINDER_QUOTA_FIELDS) # Neutron if not base.is_service_enabled(request, 'network'): disabled_quotas.update(NEUTRON_QUOTA_FIELDS) else: if not neutron.is_extension_supported(request, 'security-group'): disabled_quotas.update(['security_group', 'security_group_rule']) if not neutron.is_router_enabled(request): disabled_quotas.update(['router', 'floatingip']) try: if not neutron.is_quotas_extension_supported(request): disabled_quotas.update(NEUTRON_QUOTA_FIELDS) except Exception: LOG.exception("There was an error checking if the Neutron " "quotas extension is enabled.") # Nova if not (base.is_service_enabled(request, 'compute') and nova.can_set_quotas()): disabled_quotas.update(NOVA_QUOTA_FIELDS) # There appear to be no glance quota fields currently return disabled_quotas
def _update_project_quota(self, request, data, project_id): disabled_quotas = quotas.get_disabled_quotas(request) # Update the project quotas. if api.base.is_service_enabled(request, 'compute'): nova_data = {key: data[key] for key in set(quotas.NOVA_QUOTA_FIELDS) - disabled_quotas} nova.tenant_quota_update(request, project_id, **nova_data) if cinder.is_volume_service_enabled(request): cinder_data = dict([(key, data[key]) for key in quotas.CINDER_QUOTA_FIELDS]) cinder.tenant_quota_update(request, project_id, **cinder_data) if api.base.is_service_enabled(request, 'network') and \ api.neutron.is_quotas_extension_supported(request): neutron_data = {} for key in quotas.NEUTRON_QUOTA_FIELDS: if key not in disabled_quotas: neutron_data[key] = data[key] api.neutron.tenant_quota_update(request, project_id, **neutron_data)
def tenant_limit_usages(request): # TODO(licostan): This method shall be removed from Quota module. # ProjectUsage/BaseUsage maybe used instead on volume/image dashboards. limits = {} try: if base.is_service_enabled(request, 'compute'): limits.update(nova.tenant_absolute_limits(request, reserved=True)) except Exception: msg = _("Unable to retrieve compute limit information.") exceptions.handle(request, msg) if cinder.is_volume_service_enabled(request): try: limits.update(cinder.tenant_absolute_limits(request)) volumes = cinder.volume_list(request) snapshots = cinder.volume_snapshot_list(request) # gigabytesUsed should be a total of volumes and snapshots vol_size = sum([getattr(volume, 'size', 0) for volume in volumes]) snap_size = sum([getattr(snap, 'size', 0) for snap in snapshots]) limits['gigabytesUsed'] = vol_size + snap_size limits['volumesUsed'] = len(volumes) limits['snapshotsUsed'] = len(snapshots) except cinder.cinder_exception.ClientException: msg = _("Unable to retrieve volume limit information.") exceptions.handle(request, msg) return limits
def get_disabled_quotas(request): disabled_quotas = set([]) # Nova if not nova.can_set_quotas(): disabled_quotas.update(NOVA_QUOTA_FIELDS) # Cinder if not cinder.is_volume_service_enabled(request): disabled_quotas.update(CINDER_QUOTA_FIELDS) # Neutron if not base.is_service_enabled(request, 'network'): disabled_quotas.update(NEUTRON_QUOTA_FIELDS) else: # Remove the nova network quotas disabled_quotas.update(['floating_ips', 'fixed_ips']) if neutron.is_extension_supported(request, 'security-group'): # If Neutron security group is supported, disable Nova quotas disabled_quotas.update(['security_groups', 'security_group_rules']) else: # If Nova security group is used, disable Neutron quotas disabled_quotas.update(['security_group', 'security_group_rule']) try: if not neutron.is_quotas_extension_supported(request): disabled_quotas.update(NEUTRON_QUOTA_FIELDS) except Exception: LOG.exception("There was an error checking if the Neutron " "quotas extension is enabled.") return disabled_quotas
def get_data(self): needs_gs = False if cinder.is_volume_service_enabled(self.request): try: marker, sort_dir = self._get_marker() snapshots, self._has_more_data, self._has_prev_data = \ cinder.volume_snapshot_list_paged( self.request, paginate=True, marker=marker, sort_dir=sort_dir, search_opts={'all_tenants': True}) volumes = cinder.volume_list( self.request, search_opts={'all_tenants': True}) volumes = dict((v.id, v) for v in volumes) except Exception: snapshots = [] volumes = {} exceptions.handle(self.request, _("Unable to retrieve " "volume snapshots.")) needs_gs = any(getattr(snapshot, 'group_snapshot_id', None) for snapshot in snapshots) if needs_gs: try: group_snapshots = cinder.group_snapshot_list( self.request, search_opts={'all_tenants': True}) group_snapshots = dict((gs.id, gs) for gs in group_snapshots) except Exception: group_snapshots = {} exceptions.handle(self.request, _("Unable to retrieve group snapshots.")) # Gather our tenants to correlate against volume IDs try: tenants, has_more = keystone.tenant_list(self.request) except Exception: tenants = [] msg = _('Unable to retrieve project ' 'information of volume snapshots.') exceptions.handle(self.request, msg) tenant_dict = dict((t.id, t) for t in tenants) for snapshot in snapshots: volume = volumes.get(snapshot.volume_id) if needs_gs: group_snapshot = group_snapshots.get( snapshot.group_snapshot_id) snapshot.group_snapshot = group_snapshot else: snapshot.group_snapshot = None tenant_id = snapshot.project_id tenant = tenant_dict.get(tenant_id, None) snapshot._volume = volume snapshot.tenant_name = getattr(tenant, "name", None) snapshot.host_name = getattr( volume, 'os-vol-host-attr:host', None) else: snapshots = [] return snapshots
def _test_tenant_quota_usages_with_target( self, targets, use_compute_call=True, use_flavor_list=False, use_cinder_call=False): cinder.is_volume_service_enabled(IsA(http.HttpRequest)).AndReturn(True) api.base.is_service_enabled(IsA(http.HttpRequest), 'network') \ .AndReturn(False) api.base.is_service_enabled(IsA(http.HttpRequest), 'compute') \ .MultipleTimes().AndReturn(True) if use_compute_call: servers = [s for s in self.servers.list() if s.tenant_id == self.request.user.tenant_id] if use_flavor_list: api.nova.flavor_list(IsA(http.HttpRequest)) \ .AndReturn(self.flavors.list()) search_opts = {'tenant_id': self.request.user.tenant_id} api.nova.server_list(IsA(http.HttpRequest), search_opts=search_opts) \ .AndReturn([servers, False]) api.nova.tenant_quota_get(IsA(http.HttpRequest), '1') \ .AndReturn(self.quotas.first()) if use_cinder_call: opts = {'all_tenants': 1, 'project_id': self.request.user.tenant_id} cinder.volume_list(IsA(http.HttpRequest), opts) \ .AndReturn(self.volumes.list()) cinder.volume_snapshot_list(IsA(http.HttpRequest), opts) \ .AndReturn(self.cinder_volume_snapshots.list()) cinder.tenant_quota_get(IsA(http.HttpRequest), '1') \ .AndReturn(self.cinder_quotas.first()) self.mox.ReplayAll() quota_usages = quotas.tenant_quota_usages(self.request, targets=targets) expected = self.get_usages() expected = dict((k, v) for k, v in expected.items() if k in targets) # Compare internal structure of usages to expected. self.assertItemsEqual(expected, quota_usages.usages) # Compare available resources self.assertAvailableQuotasEqual(expected, quota_usages.usages)
def test_tenant_quota_usages_unlimited_quota(self): inf_quota = self.quotas.first() inf_quota['ram'] = -1 servers = [s for s in self.servers.list() if s.tenant_id == self.request.user.tenant_id] cinder.is_volume_service_enabled( IsA(http.HttpRequest) ).AndReturn(True) api.base.is_service_enabled(IsA(http.HttpRequest), 'network').AndReturn(False) api.base.is_service_enabled(IsA(http.HttpRequest), 'compute').MultipleTimes().AndReturn(True) api.nova.flavor_list(IsA(http.HttpRequest)) \ .AndReturn(self.flavors.list()) api.nova.tenant_quota_get(IsA(http.HttpRequest), '1') \ .AndReturn(inf_quota) api.network.floating_ip_supported(IsA(http.HttpRequest)) \ .AndReturn(True) api.network.tenant_floating_ip_list(IsA(http.HttpRequest)) \ .AndReturn(self.floating_ips.list()) search_opts = {'tenant_id': self.request.user.tenant_id} api.nova.server_list(IsA(http.HttpRequest), search_opts=search_opts, all_tenants=True) \ .AndReturn([servers, False]) opts = {'all_tenants': 1, 'project_id': self.request.user.tenant_id} cinder.volume_list(IsA(http.HttpRequest), opts) \ .AndReturn(self.volumes.list()) cinder.volume_snapshot_list(IsA(http.HttpRequest), opts) \ .AndReturn(self.cinder_volume_snapshots.list()) cinder.tenant_quota_get(IsA(http.HttpRequest), '1') \ .AndReturn(self.cinder_quotas.first()) self.mox.ReplayAll() quota_usages = quotas.tenant_quota_usages(self.request) expected_output = self.get_usages() expected_output.update({'ram': {'available': float("inf"), 'used': 1024, 'quota': float("inf")}}) # Compare internal structure of usages to expected. self.assertItemsEqual(expected_output, quota_usages.usages)
def test_tenant_quota_usages_neutron_fip_disabled(self): servers = [s for s in self.servers.list() if s.tenant_id == self.request.user.tenant_id] cinder.is_volume_service_enabled( IsA(http.HttpRequest) ).AndReturn(True) api.base.is_service_enabled(IsA(http.HttpRequest), 'network').AndReturn(False) api.base.is_service_enabled(IsA(http.HttpRequest), 'compute').MultipleTimes().AndReturn(True) api.nova.flavor_list(IsA(http.HttpRequest)) \ .AndReturn(self.flavors.list()) api.nova.tenant_quota_get(IsA(http.HttpRequest), '1') \ .AndReturn(self.quotas.first()) api.network.floating_ip_supported(IsA(http.HttpRequest)) \ .AndReturn(False) search_opts = {'tenant_id': self.request.user.tenant_id} api.nova.server_list(IsA(http.HttpRequest), search_opts=search_opts, all_tenants=True) \ .AndReturn([servers, False]) opts = {'all_tenants': 1, 'project_id': self.request.user.tenant_id} cinder.volume_list(IsA(http.HttpRequest), opts) \ .AndReturn(self.volumes.list()) cinder.volume_snapshot_list(IsA(http.HttpRequest), opts) \ .AndReturn(self.cinder_volume_snapshots.list()) cinder.tenant_quota_get(IsA(http.HttpRequest), '1') \ .AndReturn(self.cinder_quotas.first()) self.mox.ReplayAll() quota_usages = quotas.tenant_quota_usages(self.request) expected_output = self.get_usages() expected_output['floating_ips']['used'] = 0 expected_output['floating_ips']['available'] = 1 # Compare internal structure of usages to expected. self.assertItemsEqual(expected_output, quota_usages.usages)
def handle(self, request, data): # Update the default quotas. # `fixed_ips` update for quota class is not supported by novaclient nova_data = { key: value for key, value in data.items() if key in quotas.NOVA_QUOTA_FIELDS and key != 'fixed_ips' } is_error_nova = False is_error_cinder = False is_volume_service_enabled = cinder.is_volume_service_enabled(request) # Update the default quotas for nova. try: nova.default_quota_update(request, **nova_data) except Exception: is_error_nova = True # Update the default quotas for cinder. if is_volume_service_enabled: cinder_data = { key: value for key, value in data.items() if key in quotas.CINDER_QUOTA_FIELDS } try: cinder.default_quota_update(request, **cinder_data) except Exception: is_error_cinder = True else: LOG.debug('Unable to update Cinder default quotas' ' because the Cinder volume service is disabled.') # Analyze errors (if any) to determine what success and error messages # to display to the user. if is_error_nova and not is_error_cinder: if is_volume_service_enabled: self.success_message = _('Default quotas updated for Cinder.') exceptions.handle(request, _('Unable to update default quotas' ' for Nova.')) else: return False elif is_error_cinder and not is_error_nova: self.success_message = _('Default quotas updated for Nova.') exceptions.handle(request, _('Unable to update default quotas for Cinder.')) elif is_error_nova and is_error_cinder: return False return True
def populate_volume_id_choices(self, request, context): volumes = [] try: if cinder.is_volume_service_enabled(request): available = api.cinder.VOLUME_STATE_AVAILABLE volumes = [self._get_volume_display_name(v) for v in cinder.volume_list(self.request, search_opts=dict(status=available, bootable=True))] except Exception: exceptions.handle(self.request, _('Unable to retrieve list of volumes.')) if volumes: volumes.insert(0, ("", _("Select Volume"))) else: volumes.insert(0, ("", _("No volumes available"))) return volumes
def populate_volume_snapshot_id_choices(self, request, context): snapshots = [] try: if cinder.is_volume_service_enabled(request): available = api.cinder.VOLUME_STATE_AVAILABLE snapshots = [self._get_volume_display_name(s) for s in cinder.volume_snapshot_list( self.request, search_opts=dict(status=available))] except Exception: exceptions.handle(self.request, _('Unable to retrieve list of volume ' 'snapshots.')) if snapshots: snapshots.insert(0, ("", _("Select Volume Snapshot"))) else: snapshots.insert(0, ("", _("No volume snapshots available"))) return snapshots
def get_data(self): snapshots = [] volumes = {} needs_gs = False if cinder.is_volume_service_enabled(self.request): try: marker, sort_dir = self._get_marker() snapshots, self._has_more_data, self._has_prev_data = \ cinder.volume_snapshot_list_paged( self.request, paginate=True, marker=marker, sort_dir=sort_dir) except Exception: exceptions.handle(self.request, _("Unable to retrieve volume snapshots.")) try: volumes = cinder.volume_list(self.request) volumes = dict((v.id, v) for v in volumes) except Exception: exceptions.handle(self.request, _("Unable to retrieve volumes.")) needs_gs = any(getattr(snapshot, 'group_snapshot_id', None) for snapshot in snapshots) if needs_gs: try: group_snapshots = cinder.group_snapshot_list(self.request) group_snapshots = dict((gs.id, gs) for gs in group_snapshots) except Exception: group_snapshots = {} exceptions.handle(self.request, _("Unable to retrieve group snapshots.")) for snapshot in snapshots: volume = volumes.get(snapshot.volume_id) setattr(snapshot, '_volume', volume) if needs_gs: group_snapshot = group_snapshots.get( snapshot.group_snapshot_id) snapshot.group_snapshot = group_snapshot else: snapshot.group_snapshot = None return snapshots
def tenant_limit_usages(request): # TODO(licostan): This method shall be removed from Quota module. # ProjectUsage/BaseUsage maybe used instead on volume/image dashboards. limits = {} try: if base.is_service_enabled(request, 'compute'): limits.update(nova.tenant_absolute_limits(request, reserved=True)) except Exception: msg = _("Unable to retrieve compute limit information.") exceptions.handle(request, msg) if cinder.is_volume_service_enabled(request): try: limits.update(cinder.tenant_absolute_limits(request)) except cinder.cinder_exception.ClientException: msg = _("Unable to retrieve volume limit information.") exceptions.handle(request, msg) return limits
def get_data(self): if cinder.is_volume_service_enabled(self.request): try: marker, sort_dir = self._get_marker() snapshots, self._has_more_data, self._has_prev_data = \ cinder.volume_snapshot_list_paged( self.request, paginate=True, marker=marker, sort_dir=sort_dir, search_opts={'all_tenants': True}) volumes = cinder.volume_list( self.request, search_opts={'all_tenants': True}) volumes = dict((v.id, v) for v in volumes) except Exception: snapshots = [] volumes = {} exceptions.handle(self.request, _("Unable to retrieve " "volume snapshots.")) # Gather our tenants to correlate against volume IDs try: tenants, has_more = keystone.tenant_list(self.request) except Exception: tenants = [] msg = _('Unable to retrieve volume project information.') exceptions.handle(self.request, msg) tenant_dict = dict([(t.id, t) for t in tenants]) for snapshot in snapshots: volume = volumes.get(snapshot.volume_id) tenant_id = getattr(volume, 'os-vol-tenant-attr:tenant_id', None) tenant = tenant_dict.get(tenant_id, None) snapshot._volume = volume snapshot.tenant_name = getattr(tenant, "name", None) snapshot.host_name = getattr( volume, 'os-vol-host-attr:host', None) else: snapshots = [] return sorted(snapshots, key=lambda snapshot: snapshot.tenant_name or '')
def get_disabled_quotas(request): disabled_quotas = set([]) # Cinder if not cinder.is_volume_service_enabled(request): disabled_quotas.update(CINDER_QUOTA_FIELDS) # Neutron if not base.is_service_enabled(request, 'network'): disabled_quotas.update(NEUTRON_QUOTA_FIELDS) else: # Remove the nova network quotas disabled_quotas.update(['floating_ips', 'fixed_ips']) if neutron.is_extension_supported(request, 'security-group'): # If Neutron security group is supported, disable Nova quotas disabled_quotas.update(['security_groups', 'security_group_rules']) else: # If Nova security group is used, disable Neutron quotas disabled_quotas.update(['security_group', 'security_group_rule']) if not neutron.is_router_enabled(request): disabled_quotas.update(['router', 'floatingip']) try: if not neutron.is_quotas_extension_supported(request): disabled_quotas.update(NEUTRON_QUOTA_FIELDS) except Exception: LOG.exception("There was an error checking if the Neutron " "quotas extension is enabled.") # Nova if not (base.is_service_enabled(request, 'compute') and nova.can_set_quotas()): disabled_quotas.update(NOVA_QUOTA_FIELDS) # The 'missing' quota fields are all nova (this is hardcoded in # dashboards.admin.defaults.workflows) disabled_quotas.update(MISSING_QUOTA_FIELDS) # There appear to be no glance quota fields currently return disabled_quotas
def get_data(self): snapshots = [] volumes = {} if cinder.is_volume_service_enabled(self.request): try: marker, sort_dir = self._get_marker() snapshots, self._has_more_data, self._has_prev_data = \ cinder.volume_snapshot_list_paged( self.request, paginate=True, marker=marker, sort_dir=sort_dir) volumes = cinder.volume_list(self.request) volumes = dict((v.id, v) for v in volumes) except Exception: exceptions.handle(self.request, _("Unable to retrieve " "volume snapshots.")) for snapshot in snapshots: volume = volumes.get(snapshot.volume_id) setattr(snapshot, '_volume', volume) return snapshots
def _update_project_quota(self, request, data, project_id): disabled_quotas = quotas.get_disabled_quotas(request) # Update the project quotas. if api.base.is_service_enabled(request, 'compute'): nova_data = {key: data[key] for key in quotas.NOVA_QUOTA_FIELDS - disabled_quotas} if nova_data: nova.tenant_quota_update(request, project_id, **nova_data) if cinder.is_volume_service_enabled(request): cinder_data = {key: data[key] for key in quotas.CINDER_QUOTA_FIELDS - disabled_quotas} if cinder_data: cinder.tenant_quota_update(request, project_id, **cinder_data) if (api.base.is_service_enabled(request, 'network') and api.neutron.is_quotas_extension_supported(request)): neutron_data = {key: data[key] for key in quotas.NEUTRON_QUOTA_FIELDS - disabled_quotas} if neutron_data: api.neutron.tenant_quota_update(request, project_id, **neutron_data)
def _test_tenant_quota_usages_neutron_with_target(self, targets): cinder.is_volume_service_enabled(IsA(http.HttpRequest)).AndReturn(True) api.base.is_service_enabled(IsA(http.HttpRequest), 'network') \ .AndReturn(True) api.neutron.is_extension_supported(IsA(http.HttpRequest), 'security-group').AndReturn(True) api.neutron.is_router_enabled(IsA(http.HttpRequest)).AndReturn(True) api.neutron.is_quotas_extension_supported(IsA(http.HttpRequest)) \ .AndReturn(True) api.base.is_service_enabled(IsA(http.HttpRequest), 'compute') \ .MultipleTimes().AndReturn(True) api.neutron.tenant_quota_get(IsA(http.HttpRequest), '1') \ .AndReturn(self.neutron_quotas.first()) if 'networks' in targets: api.neutron.network_list(IsA(http.HttpRequest), tenant_id=self.request.user.tenant_id) \ .AndReturn(self.networks.list()) if 'subnets' in targets: api.neutron.subnet_list(IsA(http.HttpRequest), tenant_id=self.request.user.tenant_id) \ .AndReturn(self.subnets.list()) if 'routers' in targets: api.neutron.router_list(IsA(http.HttpRequest), tenant_id=self.request.user.tenant_id) \ .AndReturn(self.routers.list()) if 'floating_ips' in targets: api.neutron.floating_ip_supported(IsA(http.HttpRequest)) \ .AndReturn(True) api.neutron.tenant_floating_ip_list(IsA(http.HttpRequest)) \ .AndReturn(self.floating_ips.list()) if 'security_groups' in targets: api.neutron.security_group_list(IsA(http.HttpRequest)) \ .AndReturn(self.security_groups.list()) self.mox.ReplayAll() quota_usages = quotas.tenant_quota_usages(self.request, targets=targets) network_used = len(self.networks.list()) subnet_used = len(self.subnets.list()) router_used = len(self.routers.list()) fip_used = len(self.floating_ips.list()) sg_used = len(self.security_groups.list()) expected = { 'networks': { 'used': network_used, 'quota': 10, 'available': 10 - network_used }, 'subnets': { 'used': subnet_used, 'quota': 10, 'available': 10 - subnet_used }, 'routers': { 'used': router_used, 'quota': 10, 'available': 10 - router_used }, 'security_groups': { 'used': sg_used, 'quota': 20, 'available': 20 - sg_used }, 'floating_ips': { 'used': fip_used, 'quota': 50, 'available': 50 - fip_used }, } expected = dict((k, v) for k, v in expected.items() if k in targets) # Compare internal structure of usages to expected. self.assertEqual(expected, quota_usages.usages) # Compare available resources self.assertAvailableQuotasEqual(expected, quota_usages.usages)
def allowed(self, request, volume=None): if volume and cinder.is_volume_service_enabled(request): return volume.status == "available" return False
def allowed(self, request): return cinder.is_volume_service_enabled(request)
def _test_tenant_quota_usages_neutron_with_target( self, targets): cinder.is_volume_service_enabled(IsA(http.HttpRequest)).AndReturn(True) api.base.is_service_enabled(IsA(http.HttpRequest), 'network') \ .AndReturn(True) api.neutron.is_extension_supported(IsA(http.HttpRequest), 'security-group').AndReturn(True) api.neutron.is_router_enabled(IsA(http.HttpRequest)).AndReturn(True) api.neutron.is_quotas_extension_supported(IsA(http.HttpRequest)) \ .AndReturn(True) api.base.is_service_enabled(IsA(http.HttpRequest), 'compute') \ .MultipleTimes().AndReturn(True) api.neutron.tenant_quota_get(IsA(http.HttpRequest), '1') \ .AndReturn(self.neutron_quotas.first()) if 'networks' in targets: api.neutron.network_list(IsA(http.HttpRequest), tenant_id=self.request.user.tenant_id) \ .AndReturn(self.networks.list()) if 'subnets' in targets: api.neutron.subnet_list(IsA(http.HttpRequest), tenant_id=self.request.user.tenant_id) \ .AndReturn(self.subnets.list()) if 'routers' in targets: api.neutron.router_list(IsA(http.HttpRequest), tenant_id=self.request.user.tenant_id) \ .AndReturn(self.routers.list()) if 'floating_ips' in targets: api.neutron.floating_ip_supported(IsA(http.HttpRequest)) \ .AndReturn(True) api.neutron.tenant_floating_ip_list(IsA(http.HttpRequest)) \ .AndReturn(self.floating_ips.list()) if 'security_groups' in targets: api.neutron.security_group_list(IsA(http.HttpRequest)) \ .AndReturn(self.security_groups.list()) self.mox.ReplayAll() quota_usages = quotas.tenant_quota_usages(self.request, targets=targets) network_used = len(self.networks.list()) subnet_used = len(self.subnets.list()) router_used = len(self.routers.list()) fip_used = len(self.floating_ips.list()) sg_used = len(self.security_groups.list()) expected = { 'networks': {'used': network_used, 'quota': 10, 'available': 10 - network_used}, 'subnets': {'used': subnet_used, 'quota': 10, 'available': 10 - subnet_used}, 'routers': {'used': router_used, 'quota': 10, 'available': 10 - router_used}, 'security_groups': {'used': sg_used, 'quota': 20, 'available': 20 - sg_used}, 'floating_ips': {'used': fip_used, 'quota': 50, 'available': 50 - fip_used}, } expected = dict((k, v) for k, v in expected.items() if k in targets) # Compare internal structure of usages to expected. self.assertEqual(expected, quota_usages.usages) # Compare available resources self.assertAvailableQuotasEqual(expected, quota_usages.usages)