/usr/lib/python2.7/dist-packages/manila_ui/dashboards/project/shares/__init__.py is in python-manila-ui 2.5.1-0.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 | # Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import functools
import sys
from django.utils.translation import ugettext_lazy as _
import horizon
from manila_ui.api import manila
from openstack_dashboard.api import base
from openstack_dashboard.dashboards.admin.defaults import tables as quota_tbl
from openstack_dashboard.dashboards.admin.defaults import views \
as default_views
from openstack_dashboard.dashboards.admin.defaults import workflows \
as default_workflows
from openstack_dashboard.dashboards.identity.projects import views \
as project_views
from openstack_dashboard.dashboards.identity.projects import workflows \
as project_workflows
from openstack_dashboard.dashboards.project.overview import views \
as overview_views
from openstack_dashboard.usage import base as usage_base
from openstack_dashboard.usage import quotas
def wrap(orig_func):
"""decorator to wrap an existing function
Modified post from http://downgra.de/2009/05/16/python-monkey-patching/
to work with functions
e.g.
@wrap(quotas.tenant_limit_usages)
def tenant_limit_usages(orig, self):
limits = orig(request)
limits['disksUsed'] = 100
return limits
the first parameter of the new function is the the original,
overwritten function ('orig').
"""
def outer(new_func):
@functools.wraps(orig_func)
def wrapper(*args, **kwargs):
return new_func(orig_func, *args, **kwargs)
# Replace the original function in the module with the wrapper
orig_module = sys.modules[orig_func.__module__]
setattr(orig_module, orig_func.__name__, wrapper)
return wrapper
return outer
# All public methods in openstack_dashboard.usage.quotas are hardcoded to
# only look at a fixed set of services (nova, cinder, etc.). In order to
# incorporate manila quotas and usage, all public functions must be
# monkey-patched to add that information in.
MANILA_QUOTA_FIELDS = (
"shares",
"snapshots",
"gigabytes",
"share_networks",
)
MANILA_QUOTA_NAMES = {
'shares': _('Shares'),
'share_networks': _('Shares Networks'),
}
quotas.QUOTA_FIELDS = quotas.QUOTA_FIELDS + MANILA_QUOTA_FIELDS
def _get_manila_disabled_quotas(request):
disabled_quotas = []
if not base.is_service_enabled(request, 'share'):
disabled_quotas.extend(MANILA_QUOTA_FIELDS)
return disabled_quotas
def _get_manila_quota_data(request, method_name, disabled_quotas=None,
tenant_id=None):
if not tenant_id:
tenant_id = request.user.tenant_id
if disabled_quotas is None:
disabled_quotas = _get_manila_disabled_quotas(request)
if 'shares' not in disabled_quotas:
return getattr(manila, method_name)(request, tenant_id)
else:
return None
@wrap(quotas.get_default_quota_data)
def get_default_quota_data(f, request, disabled_quotas=None, tenant_id=None):
qs = f(request, disabled_quotas, tenant_id)
manila_quota = _get_manila_quota_data(request, "default_quota_get",
disabled_quotas=disabled_quotas,
tenant_id=tenant_id)
if manila_quota:
qs.add(manila_quota)
return qs
@wrap(quotas.get_tenant_quota_data)
def get_tenant_quota_data(f, request, disabled_quotas=None, tenant_id=None):
qs = f(request, disabled_quotas, tenant_id)
manila_quota = _get_manila_quota_data(request, "tenant_quota_get",
disabled_quotas=disabled_quotas,
tenant_id=tenant_id)
if manila_quota:
qs.add(manila_quota)
return qs
@wrap(quotas.get_disabled_quotas)
def get_disabled_quotas(f, request):
disabled_quotas = f(request)
disabled_quotas.update(_get_manila_disabled_quotas(request))
return disabled_quotas
@wrap(quotas.tenant_quota_usages)
def tenant_quota_usages(f, request, tenant_id=None):
usages = f(request, tenant_id)
if 'shares' not in _get_manila_disabled_quotas(request):
shares = manila.share_list(request)
snapshots = manila.share_snapshot_list(request)
sn_l = manila.share_network_list(request)
gig_s = sum([int(v.size) for v in shares])
gig_ss = sum([int(v.size) for v in snapshots])
usages.tally('gigabytes', gig_s + gig_ss)
usages.tally('shares', len(shares))
usages.tally('snapshots', len(snapshots))
usages.tally('share_networks', len(sn_l))
return usages
@wrap(quotas.tenant_limit_usages)
def tenant_limit_usages(f, request):
limits = f(request)
if base.is_service_enabled(request, 'share'):
try:
limits.update(manila.tenant_absolute_limits(request))
shares = manila.share_list(request)
snapshots = manila.share_snapshot_list(request)
total_s_size = sum([getattr(share, 'size', 0) for share in shares])
total_ss_size = sum([getattr(ss, 'size', 0) for ss in snapshots])
limits['totalGigabytesUsed'] = total_s_size + total_ss_size
limits['totalSharesUsed'] = len(shares)
limits['totalSnapshotsUsed'] = len(snapshots)
except Exception:
msg = _("Unable to retrieve share limit information.")
horizon.exceptions.handle(request, msg)
return limits
@wrap(quota_tbl.get_quota_name)
def get_quota_name(f, quota):
if quota.name in MANILA_QUOTA_NAMES:
return MANILA_QUOTA_NAMES.get(quota.name)
else:
return f(quota)
#
# Add manila fields to Admin/Defaults/Update Defaults
#
class ManilaUpdateDefaultQuotaAction(
default_workflows.UpdateDefaultQuotasAction):
shares = horizon.forms.IntegerField(min_value=-1, label=_("Shares"))
share_networks = horizon.forms.IntegerField(min_value=-1,
label=_("Share Networks"))
class Meta(object):
name = _("Default Quota")
slug = 'update_default_quotas'
help_text = _("From here you can update the default quotas "
"(max limits).")
class ManilaUpdateDefaultQuotasStep(default_workflows.UpdateDefaultQuotasStep):
action_class = ManilaUpdateDefaultQuotaAction
contributes = quotas.QUOTA_FIELDS
class ManilaUpdateDefaultQuotas(default_workflows.UpdateDefaultQuotas):
default_steps = (ManilaUpdateDefaultQuotasStep,)
def handle(self, request, data):
try:
super(ManilaUpdateDefaultQuotas, self).handle(request, data)
if base.is_service_enabled(request, 'share'):
manila_data = dict([(key, data[key]) for key in
MANILA_QUOTA_FIELDS])
manila.default_quota_update(request, **manila_data)
except Exception:
horizon.exceptions.handle(request,
_('Unable to update default quotas.'))
return True
default_views.UpdateDefaultQuotasView.workflow_class = \
ManilaUpdateDefaultQuotas
#
# Add manila fields to Identity/Projects/Modify Quotas
#
class ManilaUpdateProjectQuotaAction(
project_workflows.UpdateProjectQuotaAction):
shares = horizon.forms.IntegerField(min_value=-1, label=_("Shares"))
share_networks = horizon.forms.IntegerField(min_value=-1,
label=_("Share Networks"))
class Meta(object):
name = _("Quota")
slug = 'update_quotas'
help_text = _("Set maximum quotas for the project.")
project_workflows.UpdateProjectQuota.action_class = \
ManilaUpdateProjectQuotaAction
project_workflows.UpdateProjectQuota.contributes = quotas.QUOTA_FIELDS
class ManilaUpdateProject(project_workflows.UpdateProject):
def handle(self, request, data):
try:
super(ManilaUpdateProject, self).handle(request, data)
if base.is_service_enabled(request, 'share'):
manila_data = dict([(key, data[key]) for key in
MANILA_QUOTA_FIELDS])
manila.tenant_quota_update(request,
data['project_id'],
**manila_data)
except Exception:
horizon.exceptions.handle(request,
_('Modified project information and '
'members, but unable to modify '
'project quotas.'))
return True
project_views.UpdateProjectView.workflow_class = ManilaUpdateProject
#
# Add manila fields to Identity/Projects/Create Project
#
class ManilaCreateProjectQuotaAction(
project_workflows.CreateProjectQuotaAction):
shares = horizon.forms.IntegerField(min_value=-1, label=_("Shares"))
share_networks = horizon.forms.IntegerField(min_value=-1,
label=_("Share Networks"))
class Meta(object):
name = _("Quota")
slug = 'create_quotas'
help_text = _("Set maximum quotas for the project.")
project_workflows.CreateProjectQuota.action_class = \
ManilaCreateProjectQuotaAction
project_workflows.CreateProjectQuota.contributes = quotas.QUOTA_FIELDS
class ManilaCreateProject(project_workflows.CreateProject):
def handle(self, request, data):
try:
super(ManilaCreateProject, self).handle(request, data)
if base.is_service_enabled(request, 'share'):
manila_data = dict([(key, data[key]) for key in
MANILA_QUOTA_FIELDS])
manila.tenant_quota_update(request,
self.object.id,
**manila_data)
except Exception:
horizon.exceptions.handle(request,
_('Unable to set project quotas.'))
return True
project_views.CreateProjectView.workflow_class = ManilaCreateProject
#
# Add extra pie charts to Project/Compute Overview
#
class ManilaUsage(usage_base.ProjectUsage):
def get_manila_limits(self):
"""Get share limits if manila is enabled."""
if not base.is_service_enabled(self.request, 'share'):
return
try:
self.limits.update(manila.tenant_absolute_limits(self.request))
except Exception:
msg = _("Unable to retrieve share limit information.")
horizon.exceptions.handle(self.request, msg)
return
def get_limits(self):
super(self.__class__, self).get_limits()
self.get_manila_limits()
def get_context_data(self, **kwargs):
context = super(self.__class__, self).get_context_data(**kwargs)
types = (
("totalSharesUsed", "maxTotalShares", _("Shares")),
("totalShareGigabytesUsed", "maxTotalShareGigabytes",
_("Share Storage")),
("totalShareSnapshotsUsed", "maxTotalShareSnapshots",
_("Share Snapshots")),
("totalSnapshotGigabytesUsed", "maxTotalSnapshotGigabytes",
_("Share Snapshots Storage")),
("totalShareNetworksUsed", "maxTotalShareNetworks",
_("Share Networks")),
)
for t in types:
if t[0] in self.usage.limits and t[1] in self.usage.limits:
context['charts'].append({
'name': t[2],
'used': self.usage.limits[t[0]],
'max': self.usage.limits[t[1]],
'text': False,
})
return context
overview_views.ProjectOverview.get_context_data = get_context_data
overview_views.ProjectOverview.usage_class = ManilaUsage
|