1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2024-12-26 08:05:22 +00:00
webvirtcloud/virtsecrets/views.py

88 lines
2.9 KiB
Python
Raw Normal View History

from admin.decorators import superuser_only
2015-02-27 09:28:22 +00:00
from computes.models import Compute
2020-10-15 14:18:45 +00:00
from django.contrib import messages
from django.http import HttpResponseRedirect
from django.shortcuts import get_object_or_404, render
from libvirt import (
VIR_SECRET_USAGE_TYPE_CEPH,
VIR_SECRET_USAGE_TYPE_ISCSI,
VIR_SECRET_USAGE_TYPE_NONE,
VIR_SECRET_USAGE_TYPE_TLS,
VIR_SECRET_USAGE_TYPE_VOLUME,
libvirtError,
)
2021-06-15 06:42:55 +00:00
from vrtManager.virtsecrets import wvmSecrets
2015-02-27 08:53:51 +00:00
2022-11-02 13:05:41 +00:00
from virtsecrets.forms import AddSecret
2015-02-27 09:28:22 +00:00
@superuser_only
2015-02-27 09:28:22 +00:00
def secrets(request, compute_id):
"""
:param request:
:param compute_id:
2015-02-27 09:28:22 +00:00
:return:
"""
secrets_all = []
2015-04-02 12:39:40 +00:00
compute = get_object_or_404(Compute, pk=compute_id)
secret_usage_types = {
VIR_SECRET_USAGE_TYPE_NONE: "none",
VIR_SECRET_USAGE_TYPE_VOLUME: "volume",
VIR_SECRET_USAGE_TYPE_CEPH: "ceph",
VIR_SECRET_USAGE_TYPE_ISCSI: "iscsi",
VIR_SECRET_USAGE_TYPE_TLS: "tls",
}
2015-02-27 09:28:22 +00:00
try:
2022-11-02 05:54:35 +00:00
conn = wvmSecrets(
2022-11-02 13:05:41 +00:00
compute.hostname, compute.login, compute.password, compute.type
2022-11-02 05:54:35 +00:00
)
2015-02-27 09:28:22 +00:00
secrets = conn.get_secrets()
2015-02-27 09:28:22 +00:00
for uuid in secrets:
secrt = conn.get_secret(uuid)
try:
secrt_value = conn.get_secret_value(uuid)
except libvirtError:
secrt_value = None
secrets_all.append(
{
"usage": secrt.usageID(),
"uuid": secrt.UUIDString(),
"usageType": secret_usage_types[secrt.usageType()],
"value": secrt_value,
}
)
if request.method == "POST":
if "create" in request.POST:
2015-02-27 09:28:22 +00:00
form = AddSecret(request.POST)
if form.is_valid():
data = form.cleaned_data
conn.create_secret(
data["ephemeral"],
data["private"],
data["usage_type"],
data["data"],
2020-10-15 14:18:45 +00:00
)
2015-02-27 09:28:22 +00:00
return HttpResponseRedirect(request.get_full_path())
2015-03-10 14:24:10 +00:00
else:
for msg_err in form.errors.values():
2020-10-15 14:18:45 +00:00
messages.error(request, msg_err.as_text())
if "delete" in request.POST:
uuid = request.POST.get("uuid", "")
2015-02-27 09:28:22 +00:00
conn.delete_secret(uuid)
return HttpResponseRedirect(request.get_full_path())
if "set_value" in request.POST:
uuid = request.POST.get("uuid", "")
value = request.POST.get("value", "")
try:
conn.set_secret_value(uuid, value)
except Exception as err:
2020-10-15 14:18:45 +00:00
messages.error(request, err)
2015-02-27 09:28:22 +00:00
return HttpResponseRedirect(request.get_full_path())
except libvirtError as err:
2020-10-15 14:18:45 +00:00
messages.error(request, err)
2015-02-27 09:28:22 +00:00
return render(request, "secrets.html", locals())