Skip to content

Commit

Permalink
feat: configuration management
Browse files Browse the repository at this point in the history
Signed-off-by: Rupanshi Jain <[email protected]>
  • Loading branch information
rdotjain committed Aug 26, 2022
1 parent decdbbe commit 4f6605b
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 6 deletions.
1 change: 0 additions & 1 deletion zezere/default.conf
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ debug = no
allowed_hosts = localhost, localhost.localdomain
secure_cookie = yes
# auth_method = local, oidc
# ov_base_url =

[oidc.rp]
# client_id =
Expand Down
20 changes: 20 additions & 0 deletions zezere/migrations/0013_alter_device_mac_address.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Generated by Django 4.0.4 on 2022-04-20 06:28

import django.core.validators
from django.db import migrations, models
import zezere.models


class Migration(migrations.Migration):

dependencies = [
('zezere', '0012_auto_20200323_1130'),
]

operations = [
migrations.AlterField(
model_name='device',
name='mac_address',
field=models.CharField(max_length=20, unique=True, validators=[django.core.validators.RegexValidator('^([0-9A-F]{2}[:]){5}([0-9A-F]{2})$'), zezere.models.validator_disallow_blacklisted_mac], verbose_name='Device MAC Address'),
),
]
25 changes: 25 additions & 0 deletions zezere/migrations/0014_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Generated by Django 4.0.4 on 2022-08-14 06:22

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('zezere', '0013_alter_device_mac_address'),
]

operations = [
migrations.CreateModel(
name='Config',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('auth_token', models.CharField(default='', max_length=255)),
('ov_base_url', models.URLField(default='', max_length=255)),
('owner', models.ForeignKey(blank=True, default=None, null=True, on_delete=django.db.models.deletion.PROTECT, to=settings.AUTH_USER_MODEL)),
],
),
]
11 changes: 11 additions & 0 deletions zezere/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,3 +208,14 @@ class UnownedDeviceSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Device
fields = ["mac_address"]


class Config(models.Model):
owner = models.ForeignKey(
User, on_delete=models.PROTECT, default=None, blank=True, null=True
)
auth_token = models.CharField(max_length=255, default="")
ov_base_url = models.URLField(max_length=255, default="")

def __str__(self):
return "configuration for %s" % self.owner
2 changes: 0 additions & 2 deletions zezere/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,5 +170,3 @@
messages.WARNING: "alert-warning",
messages.ERROR: "alert-danger",
}

OV_BASE_URL = get("global", "ov_base_url", "OV_BASE_URL")
25 changes: 25 additions & 0 deletions zezere/templates/portal/configure.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{% extends "./master.html" %}

{% load rules %}

{% block title %}Add Voucher{% endblock %}

{% block content %}

{% if messages %}
{% for message in messages %}
<div{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</div>
{% endfor %}
{% endif %}

<h3>Your Configuration:</h3>
<form method="POST" action="/portal/configure/">
{% csrf_token %}
Enter the Authentication token: <br>
<input type="text" name="auth_token" value="{{ auth_token }}" /> <br> <br>
Enter your OV Management Server URL: <br>
<input type="text" name="ov_base_url" value="{{ ov_base_url }}" /> <br> <br>
<input type="submit" class="btn btn-primary btn-sm" value="Done"> <br>
</form>

{% endblock %}
3 changes: 3 additions & 0 deletions zezere/templates/portal/master.html
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@
<li class="{% if nbar == 'ov' %}active{% endif %}">
<a href="/portal/ov">Add Ownership Voucher</a>
</li>
<li class="{% if nbar == 'configure' %}active{% endif %}">
<a href="/portal/configure">Configure</a>
</li>
{% else %}
<li>
<a href="/accounts/login">Log In</a>
Expand Down
1 change: 1 addition & 0 deletions zezere/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
path("portal/ov/", views_portal.ov, name="portal_ov"),
path("portal/ov/add/", views_portal.add_ov, name="portal_ov_add"),
# path("portal/ov/delete/", views_portal.remove_ov, name="portal_ov_remove"),
path("portal/configure/", views_portal.configure, name="portal_configure"),
# API
path("api/", include(router.urls), name="apis"),
path(
Expand Down
37 changes: 34 additions & 3 deletions zezere/views_portal.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
from rules.contrib.views import permission_required
from ipware import get_client_ip

from zezere.models import Device, RunRequest, device_getter, SSHKey
from zezere.settings import OV_BASE_URL
from zezere.models import Device, RunRequest, device_getter, SSHKey, Config


@login_required
Expand Down Expand Up @@ -126,6 +125,9 @@ def ov(request):
@login_required
@require_POST
def add_ov(request):
OV_BASE_URL = Config.objects.get(owner=request.user).ov_base_url
AUTH_TOKEN = Config.objects.get(owner=request.user).auth_token

payload = None
no_of_vouchers = request.POST["no_of_vouchers"]

Expand All @@ -146,7 +148,7 @@ def add_ov(request):
headers = {
"X-Number-Of-Vouchers": no_of_vouchers,
"Content-Type": content_type,
"Authorization": "Bearer",
"Authorization": f"Bearer {AUTH_TOKEN}",
}

try:
Expand All @@ -162,3 +164,32 @@ def add_ov(request):
messages.error(request, "Error adding ownership voucher: {}".format(error_code))

return redirect("portal_ov")


@login_required
def configure(request):
if request.method == "GET":
config = Config.objects.filter(owner=request.user).first()
auth_token = config.auth_token if config else ""
ov_base_url = config.ov_base_url if config else ""
return render(
request,
"portal/configure.html",
{"auth_token": auth_token, "ov_base_url": ov_base_url},
)
elif request.method == "POST":
auth_token = request.POST.get("auth_token")
ov_base_url = request.POST.get("ov_base_url")
owner = request.user
if not auth_token or not ov_base_url:
messages.error(request, "Please fill in all fields")
return redirect("portal_configure")

config = Config.objects.filter(owner=owner).first()
if config is None:
config = Config(owner=owner)
config.auth_token = auth_token
config.ov_base_url = ov_base_url
config.save()
messages.success(request, "Configuration saved")
return redirect("portal_configure")

0 comments on commit 4f6605b

Please sign in to comment.