From a5bb31b95347229e400099565bedd3f6a3785c9a Mon Sep 17 00:00:00 2001 From: Sylvain Desbureaux Date: Tue, 7 Apr 2020 12:02:22 +0200 Subject: [Contrib] Authorize choice of subcomponents Instead of forcing installation of all contrib components, make the installation of these components enabled with a toggle, so each person can choose to install a subset, all or none. Issue-ID: OOM-2352 Signed-off-by: Sylvain Desbureaux Change-Id: Ie112fe1f1864587b9ac69f18967a3c28d16bdbbe --- .../resources/config/startup_scripts/00_users.py | 26 +++++++++ .../resources/config/startup_scripts/10_groups.py | 19 ++++++ .../config/startup_scripts/20_custom_fields.py | 68 ++++++++++++++++++++++ 3 files changed, 113 insertions(+) create mode 100755 kubernetes/contrib/components/netbox/charts/netbox-app/resources/config/startup_scripts/00_users.py create mode 100755 kubernetes/contrib/components/netbox/charts/netbox-app/resources/config/startup_scripts/10_groups.py create mode 100755 kubernetes/contrib/components/netbox/charts/netbox-app/resources/config/startup_scripts/20_custom_fields.py (limited to 'kubernetes/contrib/components/netbox/charts/netbox-app/resources/config/startup_scripts') diff --git a/kubernetes/contrib/components/netbox/charts/netbox-app/resources/config/startup_scripts/00_users.py b/kubernetes/contrib/components/netbox/charts/netbox-app/resources/config/startup_scripts/00_users.py new file mode 100755 index 0000000000..7626058357 --- /dev/null +++ b/kubernetes/contrib/components/netbox/charts/netbox-app/resources/config/startup_scripts/00_users.py @@ -0,0 +1,26 @@ +from django.contrib.auth.models import Group, User +from users.models import Token + +from ruamel.yaml import YAML + +with open('/opt/netbox/initializers/users.yml', 'r') as stream: + yaml=YAML(typ='safe') + users = yaml.load(stream) + + if users is not None: + for username, user_details in users.items(): + if not User.objects.filter(username=username): + user = User.objects.create_user( + username = username, + password = user_details.get('password', 0) or User.objects.make_random_password, + is_staff = user_details.get('is_staff', 0) or false, + is_superuser = user_details.get('is_superuser', 0) or false, + is_active = user_details.get('is_active', 0) or true, + first_name = user_details.get('first_name', 0), + last_name = user_details.get('last_name', 0), + email = user_details.get('email', 0)) + + print("👤 Created user ",username) + + if user_details.get('api_token', 0): + Token.objects.create(user=user, key=user_details['api_token']) \ No newline at end of file diff --git a/kubernetes/contrib/components/netbox/charts/netbox-app/resources/config/startup_scripts/10_groups.py b/kubernetes/contrib/components/netbox/charts/netbox-app/resources/config/startup_scripts/10_groups.py new file mode 100755 index 0000000000..7932874704 --- /dev/null +++ b/kubernetes/contrib/components/netbox/charts/netbox-app/resources/config/startup_scripts/10_groups.py @@ -0,0 +1,19 @@ +from django.contrib.auth.models import Group, User +from ruamel.yaml import YAML + +with open('/opt/netbox/initializers/groups.yml', 'r') as stream: + yaml=YAML(typ='safe') + groups = yaml.load(stream) + + if groups is not None: + for groupname, group_details in groups.items(): + group, created = Group.objects.get_or_create(name=groupname) + + if created: + print("👥 Created group", groupname) + + for username in group_details['users']: + user = User.objects.get(username=username) + + if user: + user.groups.add(group) diff --git a/kubernetes/contrib/components/netbox/charts/netbox-app/resources/config/startup_scripts/20_custom_fields.py b/kubernetes/contrib/components/netbox/charts/netbox-app/resources/config/startup_scripts/20_custom_fields.py new file mode 100755 index 0000000000..5c40e37bf2 --- /dev/null +++ b/kubernetes/contrib/components/netbox/charts/netbox-app/resources/config/startup_scripts/20_custom_fields.py @@ -0,0 +1,68 @@ +from extras.constants import CF_TYPE_TEXT, CF_TYPE_INTEGER, CF_TYPE_BOOLEAN, CF_TYPE_DATE, CF_TYPE_URL, CF_TYPE_SELECT +from extras.models import CustomField, CustomFieldChoice + +from ruamel.yaml import YAML + +text_to_fields = { + 'boolean': CF_TYPE_BOOLEAN, + 'date': CF_TYPE_DATE, + 'integer': CF_TYPE_INTEGER, + 'selection': CF_TYPE_SELECT, + 'text': CF_TYPE_TEXT, + 'url': CF_TYPE_URL, +} + +def get_class_for_class_path(class_path): + import importlib + from django.contrib.contenttypes.models import ContentType + + module_name, class_name = class_path.rsplit(".", 1) + module = importlib.import_module(module_name) + clazz = getattr(module, class_name) + return ContentType.objects.get_for_model(clazz) + +with open('/opt/netbox/initializers/custom_fields.yml', 'r') as stream: + yaml = YAML(typ='safe') + customfields = yaml.load(stream) + + if customfields is not None: + for cf_name, cf_details in customfields.items(): + custom_field, created = CustomField.objects.get_or_create(name = cf_name) + + if created: + if cf_details.get('default', 0): + custom_field.default = cf_details['default'] + + if cf_details.get('description', 0): + custom_field.description = cf_details['description'] + + if cf_details.get('filterable', 0): + custom_field.is_filterables = cf_details['filterable'] + + if cf_details.get('label', 0): + custom_field.label = cf_details['label'] + + for object_type in cf_details.get('on_objects', []): + custom_field.obj_type.add(get_class_for_class_path(object_type)) + + if cf_details.get('required', 0): + custom_field.required = cf_details['required'] + + if cf_details.get('type', 0): + custom_field.type = text_to_fields[cf_details['type']] + + if cf_details.get('weight', 0): + custom_field.weight = cf_details['weight'] + + custom_field.save() + + for choice_details in cf_details.get('choices', []): + choice = CustomFieldChoice.objects.create( + field=custom_field, + value=choice_details['value']) + + if choice_details.get('weight', 0): + choice.weight = choice_details['weight'] + choice.save() + + print("🔧 Created custom field", cf_name) -- cgit 1.2.3-korg