From 33acf3c7da66685552d6bb743ca4dc002ee36852 Mon Sep 17 00:00:00 2001
From: Benjamin Bertrand <benjamin.bertrand@ess.eu>
Date: Wed, 4 Mar 2020 21:07:59 +0100
Subject: [PATCH] Introduced new device types for MTCA

- Allow to use "-" in device type name
- Add ALLOWED_SET_BOOT_PROFILE_DEVICE_TYPES variable
- Rename MicroTCA to MTCA-AMC

JIRA INFRA-1835 #action In Progress
---
 app/defaults.py                               |  5 ++-
 app/models.py                                 |  7 +++-
 app/network/views.py                          | 10 ++++-
 app/settings.py                               |  2 +
 app/templates/network/view_host.html          |  2 +-
 app/validators.py                             |  2 +-
 docs/network.rst                              |  2 +-
 ...1b0093a5e13_rename_microtca_to_mtca_amc.py | 38 +++++++++++++++++++
 8 files changed, 60 insertions(+), 8 deletions(-)
 create mode 100644 migrations/versions/91b0093a5e13_rename_microtca_to_mtca_amc.py

diff --git a/app/defaults.py b/app/defaults.py
index b140097..d4ebf2b 100644
--- a/app/defaults.py
+++ b/app/defaults.py
@@ -23,7 +23,10 @@ defaults = [
     models.DeviceType(name="PhysicalMachine"),
     models.DeviceType(name="VirtualMachine"),
     models.DeviceType(name="Network"),
-    models.DeviceType(name="MicroTCA"),
+    models.DeviceType(name="MTCA-AMC"),
+    models.DeviceType(name="MTCA-IOxOS"),
+    models.DeviceType(name="MTCA-MCH"),
+    models.DeviceType(name="MTCA-RTM"),
     models.DeviceType(name="VME"),
     models.DeviceType(name="PLC"),
 ]
diff --git a/app/models.py b/app/models.py
index 5c6d5c3..86685dc 100644
--- a/app/models.py
+++ b/app/models.py
@@ -300,14 +300,17 @@ class User(db.Model, UserMixin):
     def can_set_boot_profile(self, host):
         """Return True if the user can set the network boot profile
 
-        - host.device_type shall be PhysicalMachine
+        - host.device_type shall be in ALLOWED_SET_BOOT_PROFILE_DEVICE_TYPES
         - admin users can always set the profile
         - normal users must have access to the network
         - normal users can only set the boot profile if the host is in one of the allowed network scopes
         - LOGIN_DISABLED can be set to True to turn off authentication check when testing.
           In this case, this function always returns True.
         """
-        if str(host.device_type) != "PhysicalMachine":
+        if (
+            str(host.device_type)
+            not in current_app.config["ALLOWED_SET_BOOT_PROFILE_DEVICE_TYPES"]
+        ):
             return False
         if current_app.config.get("LOGIN_DISABLED") or self.is_admin:
             return True
diff --git a/app/network/views.py b/app/network/views.py
index 4af004e..951af4d 100644
--- a/app/network/views.py
+++ b/app/network/views.py
@@ -161,7 +161,10 @@ def view_host(name):
             f" Please rename it '{host.name}'.",
             "warning",
         )
-    if host.device_type.name == "PhysicalMachine":
+    if (
+        host.device_type.name
+        in current_app.config["ALLOWED_SET_BOOT_PROFILE_DEVICE_TYPES"]
+    ):
         form = BootProfileForm()
     elif host.device_type.name.startswith("Virtual"):
         form = CreateVMForm()
@@ -174,7 +177,10 @@ def view_host(name):
     else:
         form = None
     if form is not None and form.validate_on_submit():
-        if host.device_type.name == "PhysicalMachine":
+        if (
+            host.device_type.name
+            in current_app.config["ALLOWED_SET_BOOT_PROFILE_DEVICE_TYPES"]
+        ):
             if not current_user.can_set_boot_profile(host):
                 flash(
                     f"You don't have the proper permissions to set the boot profile. Please contact an admin user.",
diff --git a/app/settings.py b/app/settings.py
index 1ea2832..2c193bd 100644
--- a/app/settings.py
+++ b/app/settings.py
@@ -85,6 +85,8 @@ CSENTRY_NETWORK_SCOPES_LDAP_GROUPS = {
 ALLOWED_VM_CREATION_NETWORK_SCOPES = ["LabNetworks"]
 # List of network scopes where users can set the boot profile for physical machines
 ALLOWED_SET_BOOT_PROFILE_NETWORK_SCOPES = ["LabNetworks"]
+# List of device types for which the boot profile can be set
+ALLOWED_SET_BOOT_PROFILE_DEVICE_TYPES = ["PhysicalMachine", "MTCA-AMC"]
 # List of existing boot profiles
 # Shall be kept in sync with the ics-ans-role-autoinstall Ansible role
 AUTOINSTALL_BOOT_PROFILES = ["localboot", "default", "cct", "LCR", "thinclient"]
diff --git a/app/templates/network/view_host.html b/app/templates/network/view_host.html
index 5032f43..d2f2712 100644
--- a/app/templates/network/view_host.html
+++ b/app/templates/network/view_host.html
@@ -83,7 +83,7 @@
           {{ submit_button_with_confirmation('Create ' + vm_type, 'Do you really want to create the ' + vm_type + ' ' + host.name + '?') }}
         </form>
       </div>
-    {% elif host.device_type.name == 'PhysicalMachine' %}
+    {% elif host.device_type.name in config.ALLOWED_SET_BOOT_PROFILE_DEVICE_TYPES %}
       <div class="col-sm-4">
         <form id="BootProfileForm" method="POST">
           {{ form.hidden_tag() }}
diff --git a/app/validators.py b/app/validators.py
index afdba7f..aad28d9 100644
--- a/app/validators.py
+++ b/app/validators.py
@@ -19,7 +19,7 @@ HOST_NAME_RE = re.compile(r"^[a-z0-9\-]{2,20}$")
 INTERFACE_NAME_RE = re.compile(r"^[a-z0-9\-]{2,25}$")
 VLAN_NAME_RE = re.compile(r"^[A-Za-z0-9\-]{3,25}$")
 MAC_ADDRESS_RE = re.compile(r"^(?:[0-9a-fA-F]{2}[:-]?){5}[0-9a-fA-F]{2}$")
-DEVICE_TYPE_RE = re.compile(r"^[A-Za-z0-9]{3,25}$")
+DEVICE_TYPE_RE = re.compile(r"^[A-Za-z0-9\-]{3,25}$")
 
 
 class NoValidateSelectField(SelectField):
diff --git a/docs/network.rst b/docs/network.rst
index a8fe881..6f7f0dc 100644
--- a/docs/network.rst
+++ b/docs/network.rst
@@ -186,7 +186,7 @@ Please contact an admin user if you don't have the proper permissions.
 Physical Machine installation
 -----------------------------
 
-From the *View host* page, you can set the boot profile of a Physical Machine. This can be used to perform a network installation:
+From the *View host* page, you can set the boot profile of a Physical Machine or MTCA-AMC. This can be used to perform a network installation:
 
 .. image:: _static/network/set_boot_profile.png
 
diff --git a/migrations/versions/91b0093a5e13_rename_microtca_to_mtca_amc.py b/migrations/versions/91b0093a5e13_rename_microtca_to_mtca_amc.py
new file mode 100644
index 0000000..91e702c
--- /dev/null
+++ b/migrations/versions/91b0093a5e13_rename_microtca_to_mtca_amc.py
@@ -0,0 +1,38 @@
+"""Rename MicroTCA to MTCA-AMC
+
+Revision ID: 91b0093a5e13
+Revises: b1eda5cb7d9d
+Create Date: 2020-03-05 12:55:35.804867
+
+"""
+from alembic import op
+import sqlalchemy as sa
+
+
+# revision identifiers, used by Alembic.
+revision = "91b0093a5e13"
+down_revision = "b1eda5cb7d9d"
+branch_labels = None
+depends_on = None
+
+
+def upgrade():
+    device_type = sa.sql.table(
+        "device_type", sa.sql.column("id"), sa.sql.column("name")
+    )
+    op.execute(
+        device_type.update()
+        .where(device_type.c.name == "MicroTCA")
+        .values(name="MTCA-AMC")
+    )
+
+
+def downgrade():
+    device_type = sa.sql.table(
+        "device_type", sa.sql.column("id"), sa.sql.column("name")
+    )
+    op.execute(
+        device_type.update()
+        .where(device_type.c.name == "MTCA-AMC")
+        .values(name="MicroTCA")
+    )
-- 
GitLab