tables.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import django_tables2 as tables
  2. from django_tables2.utils import Accessor
  3. from dcim.models import Interface
  4. from tenancy.tables import COL_TENANT
  5. from utilities.tables import BaseTable, ToggleColumn
  6. from .models import Cluster, ClusterGroup, ClusterType, VirtualMachine
  7. CLUSTERTYPE_ACTIONS = """
  8. <a href="{% url 'virtualization:clustertype_changelog' slug=record.slug %}" class="btn btn-default btn-xs" title="Change log">
  9. <i class="fa fa-history"></i>
  10. </a>
  11. {% if perms.virtualization.change_clustertype %}
  12. <a href="{% url 'virtualization:clustertype_edit' slug=record.slug %}?return_url={{ request.path }}" class="btn btn-xs btn-warning"><i class="glyphicon glyphicon-pencil" aria-hidden="true"></i></a>
  13. {% endif %}
  14. """
  15. CLUSTERGROUP_ACTIONS = """
  16. <a href="{% url 'virtualization:clustergroup_changelog' slug=record.slug %}" class="btn btn-default btn-xs" title="Change log">
  17. <i class="fa fa-history"></i>
  18. </a>
  19. {% if perms.virtualization.change_clustergroup %}
  20. <a href="{% url 'virtualization:clustergroup_edit' slug=record.slug %}?return_url={{ request.path }}" class="btn btn-xs btn-warning"><i class="glyphicon glyphicon-pencil" aria-hidden="true"></i></a>
  21. {% endif %}
  22. """
  23. VIRTUALMACHINE_STATUS = """
  24. <span class="label label-{{ record.get_status_class }}">{{ record.get_status_display }}</span>
  25. """
  26. VIRTUALMACHINE_ROLE = """
  27. {% if record.role %}<label class="label" style="background-color: #{{ record.role.color }}">{{ value }}</label>{% else %}&mdash;{% endif %}
  28. """
  29. VIRTUALMACHINE_PRIMARY_IP = """
  30. {{ record.primary_ip6.address.ip|default:"" }}
  31. {% if record.primary_ip6 and record.primary_ip4 %}<br />{% endif %}
  32. {{ record.primary_ip4.address.ip|default:"" }}
  33. """
  34. #
  35. # Cluster types
  36. #
  37. class ClusterTypeTable(BaseTable):
  38. pk = ToggleColumn()
  39. name = tables.LinkColumn()
  40. cluster_count = tables.Column(verbose_name='Clusters')
  41. actions = tables.TemplateColumn(
  42. template_code=CLUSTERTYPE_ACTIONS,
  43. attrs={'td': {'class': 'text-right noprint'}},
  44. verbose_name=''
  45. )
  46. class Meta(BaseTable.Meta):
  47. model = ClusterType
  48. fields = ('pk', 'name', 'cluster_count', 'actions')
  49. #
  50. # Cluster groups
  51. #
  52. class ClusterGroupTable(BaseTable):
  53. pk = ToggleColumn()
  54. name = tables.LinkColumn()
  55. cluster_count = tables.Column(verbose_name='Clusters')
  56. actions = tables.TemplateColumn(
  57. template_code=CLUSTERGROUP_ACTIONS,
  58. attrs={'td': {'class': 'text-right noprint'}},
  59. verbose_name=''
  60. )
  61. class Meta(BaseTable.Meta):
  62. model = ClusterGroup
  63. fields = ('pk', 'name', 'cluster_count', 'actions')
  64. #
  65. # Clusters
  66. #
  67. class ClusterTable(BaseTable):
  68. pk = ToggleColumn()
  69. name = tables.LinkColumn()
  70. tenant = tables.LinkColumn('tenancy:tenant', args=[Accessor('tenant.slug')], verbose_name='Tenant')
  71. site = tables.LinkColumn('dcim:site', args=[Accessor('site.slug')])
  72. device_count = tables.Column(accessor=Accessor('devices.count'), orderable=False, verbose_name='Devices')
  73. vm_count = tables.Column(accessor=Accessor('virtual_machines.count'), orderable=False, verbose_name='VMs')
  74. class Meta(BaseTable.Meta):
  75. model = Cluster
  76. fields = ('pk', 'name', 'type', 'group', 'tenant', 'site', 'device_count', 'vm_count')
  77. #
  78. # Virtual machines
  79. #
  80. class VirtualMachineTable(BaseTable):
  81. pk = ToggleColumn()
  82. name = tables.LinkColumn()
  83. status = tables.TemplateColumn(template_code=VIRTUALMACHINE_STATUS)
  84. cluster = tables.LinkColumn('virtualization:cluster', args=[Accessor('cluster.pk')])
  85. role = tables.TemplateColumn(VIRTUALMACHINE_ROLE)
  86. tenant = tables.TemplateColumn(template_code=COL_TENANT)
  87. class Meta(BaseTable.Meta):
  88. model = VirtualMachine
  89. fields = ('pk', 'name', 'status', 'cluster', 'role', 'tenant', 'vcpus', 'memory', 'disk')
  90. class VirtualMachineDetailTable(VirtualMachineTable):
  91. primary_ip = tables.TemplateColumn(
  92. orderable=False, verbose_name='IP Address', template_code=VIRTUALMACHINE_PRIMARY_IP
  93. )
  94. class Meta(BaseTable.Meta):
  95. model = VirtualMachine
  96. fields = ('pk', 'name', 'status', 'cluster', 'role', 'tenant', 'vcpus', 'memory', 'disk', 'primary_ip')
  97. #
  98. # VM components
  99. #
  100. class InterfaceTable(BaseTable):
  101. class Meta(BaseTable.Meta):
  102. model = Interface
  103. fields = ('name', 'enabled', 'description')