1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2024-12-24 15:15:22 +00:00

fix secret types sync with libvirt library

This commit is contained in:
catborise 2020-06-04 12:44:47 +03:00
parent fe13e4dd66
commit fc2662bd91
2 changed files with 83 additions and 62 deletions

View file

@ -1,60 +1,61 @@
{% load i18n %}
{% if request.user.is_superuser %}
<a href="#AddSecret" type="button" class="btn btn-success float-right" data-toggle="modal">
<span class="fa fa-plus" aria-hidden="true"></span>
</a>
<a href="#AddSecret" type="button" class="btn btn-success float-right" data-toggle="modal">
<span class="fa fa-plus" aria-hidden="true"></span>
</a>
<!-- Modal Secret -->
<div class="modal fade" id="AddSecret" tabindex="-1" role="dialog" aria-labelledby="AddSecret" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
<h5 class="modal-title">{% trans "Create New Secret" %}</h5>
</div>
<div class="modal-body">
<form method="post" action="" role="form" aria-label="Create Secret form">{% csrf_token %}
<div class="form-group row">
<label class="col-sm-3 col-form-label">{% trans "Ephemeral" %}</label>
<div class="col-sm-6">
<select name="ephemeral" class="form-control">
<option value="no">{% trans "no" %}</option>
<option value="yes">{% trans "yes" %}</option>
</select>
</div>
<!-- Modal Secret -->
<div class="modal fade" id="AddSecret" tabindex="-1" role="dialog" aria-labelledby="AddSecret" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">{% trans "Create New Secret" %}</h5>
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
</div>
<div class="modal-body">
<form method="post" action="" role="form" aria-label="Create Secret form">{% csrf_token %}
<div class="form-group row">
<label class="col-sm-3 col-form-label">{% trans "Ephemeral" %}</label>
<div class="col-sm-6">
<select name="ephemeral" class="form-control">
<option value="no">{% trans "no" %}</option>
<option value="yes">{% trans "yes" %}</option>
</select>
</div>
<div class="form-group row">
<label class="col-sm-3 col-form-label">{% trans "Private" %}</label>
<div class="col-sm-6">
<select name="private" class="form-control">
<option value="no">{% trans "no" %}</option>
<option value="yes">{% trans "yes" %}</option>
</select>
</div>
</div>
<div class="form-group row">
<label class="col-sm-3 col-form-label">{% trans "Usage" %}</label>
<div class="col-sm-6">
<select name="usage_type" class="form-control">
<option value="ceph">{% trans "ceph" %}</option>
<option value="volume">{% trans "volume" %}</option>
<option value="iscsi">{% trans "iscsi" %}</option>
</select>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 col-form-label">{% trans "Data" %}</label>
<div class="col-sm-6">
<input type="text" name="data" class="form-control" value="" required pattern="[a-z0-9\. ]+"/>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">{% trans "Close" %}</button>
<button type="submit" class="btn btn-primary" name="create">{% trans "Create" %}</button>
</div>
</form>
</div> <!-- /.modal-content -->
</div> <!-- /.modal-dialog -->
</div><!-- /.modal -->
<div class="form-group row">
<label class="col-sm-3 col-form-label">{% trans "Private" %}</label>
<div class="col-sm-6">
<select name="private" class="form-control">
<option value="no">{% trans "no" %}</option>
<option value="yes">{% trans "yes" %}</option>
</select>
</div>
</div>
<div class="form-group row">
<label class="col-sm-3 col-form-label">{% trans "Usage" %}</label>
<div class="col-sm-6">
<select name="usage_type" class="form-control">
{% for key, usage_type in secret_usage_types.items %}
<option value="{{ usage_type }}">{{ usage_type }}</option>
{% endfor %}
</select>
</div>
</div>
<div class="form-group row">
<label class="col-sm-3 col-form-label">{% trans "Data" %}</label>
<div class="col-sm-6">
<input type="text" name="data" class="form-control" value="" required
pattern="[a-z0-9\. ]+" />
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">{% trans "Close" %}</button>
<button type="submit" class="btn btn-primary" name="create">{% trans "Create" %}</button>
</div>
</form>
</div> <!-- /.modal-content -->
</div> <!-- /.modal-dialog -->
</div><!-- /.modal -->
{% endif %}

View file

@ -1,12 +1,17 @@
from secrets.forms import AddSecret
from django.http import HttpResponseRedirect
from django.shortcuts import get_object_or_404, render
from django.urls import reverse
from libvirt import libvirtError
from libvirt import (VIR_SECRET_USAGE_TYPE_NONE,
VIR_SECRET_USAGE_TYPE_CEPH,
VIR_SECRET_USAGE_TYPE_TLS,
VIR_SECRET_USAGE_TYPE_VOLUME,
VIR_SECRET_USAGE_TYPE_ISCSI)
from admin.decorators import superuser_only
from computes.models import Compute
from secrets.forms import AddSecret
from vrtManager.secrets import wvmSecrets
@ -21,6 +26,14 @@ def secrets(request, compute_id):
secrets_all = []
error_messages = []
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",
}
try:
conn = wvmSecrets(compute.hostname, compute.login, compute.password, compute.type)
secrets = conn.get_secrets()
@ -28,21 +41,25 @@ def secrets(request, compute_id):
for uuid in secrets:
secrt = conn.get_secret(uuid)
try:
secret_value = conn.get_secret_value(uuid)
secrt_value = conn.get_secret_value(uuid)
except libvirtError as lib_err:
secret_value = None
secrt_value = None
secrets_all.append({
'usage': secrt.usageID(),
'uuid': secrt.UUIDString(),
'usageType': secrt.usageType(),
'value': secret_value
'usageType': secret_usage_types[secrt.usageType()],
'value': secrt_value
})
if request.method == 'POST':
if 'create' in request.POST:
form = AddSecret(request.POST)
if form.is_valid():
data = form.cleaned_data
conn.create_secret(data['ephemeral'], data['private'], data['usage_type'], data['data'])
conn.create_secret(
data['ephemeral'],
data['private'],
data['usage_type'],
data['data'])
return HttpResponseRedirect(request.get_full_path())
else:
for msg_err in form.errors.values():
@ -54,9 +71,12 @@ def secrets(request, compute_id):
if 'set_value' in request.POST:
uuid = request.POST.get('uuid', '')
value = request.POST.get('value', '')
conn.set_secret_value(uuid, value)
try:
conn.set_secret_value(uuid, value)
except Exception as err:
error_messages.append(err)
return HttpResponseRedirect(request.get_full_path())
except libvirtError as err:
error_messages.append(err)
return render(request, 'secrets.html', locals())
return render(request, 'secrets.html', locals())