~ubuntu-branches/ubuntu/trusty/horizon/trusty

« back to all changes in this revision

Viewing changes to openstack_dashboard/dashboards/project/instances/tables.py

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2014-03-06 16:53:28 UTC
  • mfrom: (1.1.37)
  • Revision ID: package-import@ubuntu.com-20140306165328-w2vgmtfriqlhp27m
Tags: 1:2014.1~b3-0ubuntu1
* New upstream milestone release.
* d/static/*: Refreshed assets for new upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 
18
18
import logging
19
19
 
 
20
from django.conf import settings
20
21
from django.core import urlresolvers
21
22
from django import shortcuts
22
23
from django import template
77
78
    data_type_singular = _("Instance")
78
79
    data_type_plural = _("Instances")
79
80
    classes = ('btn-danger', 'btn-terminate')
 
81
    policy_rules = (("compute", "compute:delete"),)
 
82
 
 
83
    def get_policy_target(self, request, datum=None):
 
84
        project_id = None
 
85
        if datum:
 
86
            project_id = getattr(datum, 'tenant_id', None)
 
87
        return {"project_id": project_id}
80
88
 
81
89
    def allowed(self, request, instance=None):
82
 
        return True
 
90
        """Allow terminate action if instance not currently being deleted."""
 
91
        return not is_deleting(instance)
83
92
 
84
93
    def action(self, request, obj_id):
85
94
        api.nova.server_delete(request, obj_id)
92
101
    data_type_singular = _("Instance")
93
102
    data_type_plural = _("Instances")
94
103
    classes = ('btn-danger', 'btn-reboot')
 
104
    policy_rules = (("compute", "compute:reboot"),)
 
105
 
 
106
    def get_policy_target(self, request, datum=None):
 
107
        project_id = None
 
108
        if datum:
 
109
            project_id = getattr(datum, 'tenant_id', None)
 
110
        return {"project_id": project_id}
95
111
 
96
112
    def allowed(self, request, instance=None):
97
113
        if instance is not None:
132
148
        self.paused = instance.status == "PAUSED"
133
149
        if self.paused:
134
150
            self.current_present_action = UNPAUSE
 
151
            policy = (("compute", "compute_extension:admin_actions:unpause"),)
135
152
        else:
136
153
            self.current_present_action = PAUSE
137
 
        return ((instance.status in ACTIVE_STATES or self.paused)
 
154
            policy = (("compute", "compute_extension:admin_actions:pause"),)
 
155
 
 
156
        has_permission = True
 
157
        policy_check = getattr(settings, "POLICY_CHECK_FUNCTION", None)
 
158
        if policy_check:
 
159
            has_permission = policy_check(policy, request,
 
160
                target={'project_id': getattr(instance, 'tenant_id', None)})
 
161
 
 
162
        return (has_permission
 
163
                and (instance.status in ACTIVE_STATES or self.paused)
138
164
                and not is_deleting(instance))
139
165
 
140
166
    def action(self, request, obj_id):
164
190
        self.suspended = instance.status == "SUSPENDED"
165
191
        if self.suspended:
166
192
            self.current_present_action = RESUME
 
193
            policy = (("compute", "compute_extension:admin_actions:resume"),)
167
194
        else:
168
195
            self.current_present_action = SUSPEND
169
 
        return ((instance.status in ACTIVE_STATES or self.suspended)
 
196
            policy = (("compute", "compute_extension:admin_actions:suspend"),)
 
197
 
 
198
        has_permission = True
 
199
        policy_check = getattr(settings, "POLICY_CHECK_FUNCTION", None)
 
200
        if policy_check:
 
201
            has_permission = policy_check(policy, request,
 
202
                target={'project_id': getattr(instance, 'tenant_id', None)})
 
203
 
 
204
        return (has_permission
 
205
                and (instance.status in ACTIVE_STATES or self.suspended)
170
206
                and not is_deleting(instance))
171
207
 
172
208
    def action(self, request, obj_id):
183
219
    verbose_name = _("Launch Instance")
184
220
    url = "horizon:project:instances:launch"
185
221
    classes = ("btn-launch", "ajax-modal")
 
222
    policy_rules = (("compute", "compute:create"),)
186
223
 
187
224
    def allowed(self, request, datum):
188
225
        try:
217
254
    verbose_name = _("Edit Instance")
218
255
    url = "horizon:project:instances:update"
219
256
    classes = ("ajax-modal", "btn-edit")
 
257
    policy_rules = (("compute", "compute:update"),)
 
258
 
 
259
    def get_policy_target(self, request, datum=None):
 
260
        project_id = None
 
261
        if datum:
 
262
            project_id = getattr(datum, 'tenant_id', None)
 
263
        return {"project_id": project_id}
220
264
 
221
265
    def get_link_url(self, project):
222
266
        return self._get_link_url(project, 'instance_info')
246
290
class CreateSnapshot(tables.LinkAction):
247
291
    name = "snapshot"
248
292
    verbose_name = _("Create Snapshot")
249
 
    url = "horizon:project:images_and_snapshots:snapshots:create"
 
293
    url = "horizon:project:images:snapshots:create"
250
294
    classes = ("ajax-modal", "btn-camera")
 
295
    policy_rules = (("compute", "compute:snapshot"),)
 
296
 
 
297
    def get_policy_target(self, request, datum=None):
 
298
        project_id = None
 
299
        if datum:
 
300
            project_id = getattr(datum, 'tenant_id', None)
 
301
        return {"project_id": project_id}
251
302
 
252
303
    def allowed(self, request, instance=None):
253
304
        return instance.status in SNAPSHOT_READY_STATES \
259
310
    verbose_name = _("Console")
260
311
    url = "horizon:project:instances:detail"
261
312
    classes = ("btn-console",)
 
313
    policy_rules = (("compute", "compute_extension:consoles"),)
 
314
 
 
315
    def get_policy_target(self, request, datum=None):
 
316
        project_id = None
 
317
        if datum:
 
318
            project_id = getattr(datum, 'tenant_id', None)
 
319
        return {"project_id": project_id}
262
320
 
263
321
    def allowed(self, request, instance=None):
264
322
        return instance.status in ACTIVE_STATES and not is_deleting(instance)
275
333
    verbose_name = _("View Log")
276
334
    url = "horizon:project:instances:detail"
277
335
    classes = ("btn-log",)
 
336
    policy_rules = (("compute", "compute_extension:console_output"),)
 
337
 
 
338
    def get_policy_target(self, request, datum=None):
 
339
        project_id = None
 
340
        if datum:
 
341
            project_id = getattr(datum, 'tenant_id', None)
 
342
        return {"project_id": project_id}
278
343
 
279
344
    def allowed(self, request, instance=None):
280
345
        return instance.status in ACTIVE_STATES and not is_deleting(instance)
291
356
    verbose_name = _("Resize Instance")
292
357
    url = "horizon:project:instances:resize"
293
358
    classes = ("ajax-modal", "btn-resize")
 
359
    policy_rules = (("compute", "compute:resize"),)
 
360
 
 
361
    def get_policy_target(self, request, datum=None):
 
362
        project_id = None
 
363
        if datum:
 
364
            project_id = getattr(datum, 'tenant_id', None)
 
365
        return {"project_id": project_id}
294
366
 
295
367
    def get_link_url(self, project):
296
368
        return self._get_link_url(project, 'flavor_choice')
310
382
    name = "confirm"
311
383
    verbose_name = _("Confirm Resize/Migrate")
312
384
    classes = ("btn-confirm", "btn-action-required")
 
385
    policy_rules = (("compute", "compute:confirm_resize"),)
 
386
 
 
387
    def get_policy_target(self, request, datum=None):
 
388
        project_id = None
 
389
        if datum:
 
390
            project_id = getattr(datum, 'tenant_id', None)
 
391
        return {"project_id": project_id}
313
392
 
314
393
    def allowed(self, request, instance):
315
394
        return instance.status == 'VERIFY_RESIZE'
322
401
    name = "revert"
323
402
    verbose_name = _("Revert Resize/Migrate")
324
403
    classes = ("btn-revert", "btn-action-required")
 
404
    policy_rules = (("compute", "compute:revert_resize"),)
 
405
 
 
406
    def get_policy_target(self, request, datum=None):
 
407
        project_id = None
 
408
        if datum:
 
409
            project_id = getattr(datum, 'tenant_id', None)
 
410
        return {"project_id": project_id}
325
411
 
326
412
    def allowed(self, request, instance):
327
413
        return instance.status == 'VERIFY_RESIZE'
335
421
    verbose_name = _("Rebuild Instance")
336
422
    classes = ("btn-rebuild", "ajax-modal")
337
423
    url = "horizon:project:instances:rebuild"
 
424
    policy_rules = (("compute", "compute:rebuild"),)
 
425
 
 
426
    def get_policy_target(self, request, datum=None):
 
427
        project_id = None
 
428
        if datum:
 
429
            project_id = getattr(datum, 'tenant_id', None)
 
430
        return {"project_id": project_id}
338
431
 
339
432
    def allowed(self, request, instance):
340
433
        return ((instance.status in ACTIVE_STATES
351
444
    verbose_name = _("Associate Floating IP")
352
445
    url = "horizon:project:access_and_security:floating_ips:associate"
353
446
    classes = ("ajax-modal", "btn-associate")
 
447
    policy_rules = (("compute", "network:associate_floating_ip"),)
 
448
 
 
449
    def get_policy_target(self, request, datum=None):
 
450
        project_id = None
 
451
        if datum:
 
452
            project_id = getattr(datum, 'tenant_id', None)
 
453
        return {"project_id": project_id}
354
454
 
355
455
    def allowed(self, request, instance):
356
456
        if api.network.floating_ip_simple_associate_supported(request):
370
470
    name = "associate-simple"
371
471
    verbose_name = _("Associate Floating IP")
372
472
    classes = ("btn-associate-simple",)
 
473
    policy_rules = (("compute", "network:associate_floating_ip"),)
 
474
 
 
475
    def get_policy_target(self, request, datum=None):
 
476
        project_id = None
 
477
        if datum:
 
478
            project_id = getattr(datum, 'tenant_id', None)
 
479
        return {"project_id": project_id}
373
480
 
374
481
    def allowed(self, request, instance):
375
482
        if not api.network.floating_ip_simple_associate_supported(request):
398
505
    name = "disassociate"
399
506
    verbose_name = _("Disassociate Floating IP")
400
507
    classes = ("btn-danger", "btn-disassociate",)
 
508
    policy_rules = (("compute", "network:disassociate_floating_ip"),)
 
509
 
 
510
    def get_policy_target(self, request, datum=None):
 
511
        project_id = None
 
512
        if datum:
 
513
            project_id = getattr(datum, 'tenant_id', None)
 
514
        return {"project_id": project_id}
401
515
 
402
516
    def allowed(self, request, instance):
403
517
        if not conf.HORIZON_CONFIG["simple_ip_management"]:
408
522
        try:
409
523
            # target_id is port_id for Neutron and instance_id for Nova Network
410
524
            # (Neutron API wrapper returns a 'portid_fixedip' string)
411
 
            target_id = api.network.floating_ip_target_get_by_instance(
412
 
                request, instance_id).split('_')[0]
 
525
            targets = api.network.floating_ip_target_list_by_instance(
 
526
                request, instance_id)
 
527
 
 
528
            target_ids = [t.split('_')[0] for t in targets]
413
529
 
414
530
            fips = [fip for fip in api.network.tenant_floating_ip_list(request)
415
 
                    if fip.port_id == target_id]
 
531
                    if fip.port_id in target_ids]
416
532
            # Removing multiple floating IPs at once doesn't work, so this pops
417
533
            # off the first one.
418
534
            if fips:
419
535
                fip = fips.pop()
420
536
                api.network.floating_ip_disassociate(request,
421
 
                                                     fip.id, target_id)
 
537
                                                     fip.id, fip.port_id)
422
538
                messages.success(request,
423
539
                                 _("Successfully disassociated "
424
540
                                   "floating IP: %s") % fip.ip)
472
588
    action_past = _("Started")
473
589
    data_type_singular = _("Instance")
474
590
    data_type_plural = _("Instances")
 
591
    policy_rules = (("compute", "compute:start"),)
 
592
 
 
593
    def get_policy_target(self, request, datum=None):
 
594
        project_id = None
 
595
        if datum:
 
596
            project_id = getattr(datum, 'tenant_id', None)
 
597
        return {"project_id": project_id}
475
598
 
476
599
    def allowed(self, request, instance):
477
600
        return instance.status in ("SHUTDOWN", "SHUTOFF", "CRASHED")
487
610
    data_type_singular = _("Instance")
488
611
    data_type_plural = _("Instances")
489
612
    classes = ('btn-danger',)
 
613
    policy_rules = (("compute", "compute:stop"),)
 
614
 
 
615
    def get_policy_target(self, request, datum=None):
 
616
        project_id = None
 
617
        if datum:
 
618
            project_id = getattr(datum, 'tenant_id', None)
 
619
        return {"project_id": project_id}
490
620
 
491
621
    def allowed(self, request, instance):
492
622
        return ((get_power_state(instance)