Add keypairs panel

Change-Id: Ic4bb88c5090b062bbfc47d60738d64b8a92b3e82
This commit is contained in:
Zhenguo Niu 2017-05-08 10:36:01 +08:00
parent 1e363adb5f
commit 3333cf0194
14 changed files with 515 additions and 0 deletions

View File

@ -64,3 +64,54 @@ def server_delete(request, server_id):
"""
server_manager = moganclient(request).server
return server_manager.delete(server_id)
def keypair_list(request):
"""Retrieve a list of keypairs.
:param request: HTTP request.
:return: A list of keypairs.
"""
keypair_manager = moganclient(request).keypair
return keypair_manager.list()
def keypair_create(request, name):
"""Create a keypair.
:param request: HTTP request.
:param name: The name of the keypair.
"""
keypair_manager = moganclient(request).keypair
return keypair_manager.create(name=name)
def keypair_import(request, name, public_key):
"""Import a keypair.
:param request: HTTP request.
:param name: The name of the keypair.
:param public_key: The public key used to generate keypair.
"""
keypair_manager = moganclient(request).keypair
return keypair_manager.create(name=name, public_key=public_key)
def keypair_get(request, name):
"""Get a keypair.
:param request: HTTP request.
:param name: The name of the keypair.
"""
keypair_manager = moganclient(request).keypair
return keypair_manager.get(name)
def keypair_delete(request, name):
"""Delete a keypair.
:param request: HTTP request.
:param name: The name of the keypair.
"""
keypair_manager = moganclient(request).keypair
return keypair_manager.delete(name)

View File

View File

@ -0,0 +1,84 @@
# Copyright 2012 United States Government as represented by the
# Administrator of the National Aeronautics and Space Administration.
# All Rights Reserved.
#
# Copyright 2012 Nebula, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import re
from django.utils.translation import ugettext_lazy as _
from horizon import exceptions
from horizon import forms
from horizon import messages
from mogan_ui.api import mogan
NEW_LINES = re.compile(r"\r|\n")
KEYPAIR_NAME_REGEX = re.compile(r"^\w+(?:[- ]\w+)*$", re.UNICODE)
KEYPAIR_ERROR_MESSAGES = {
'invalid': _('Key pair name may only contain letters, '
'numbers, underscores, spaces, and hyphens '
'and may not be white space.')}
class CreateKeypair(forms.SelfHandlingForm):
name = forms.RegexField(max_length=255,
label=_("Key Pair Name"),
regex=KEYPAIR_NAME_REGEX,
error_messages=KEYPAIR_ERROR_MESSAGES)
def handle(self, request, data):
return True # We just redirect to the download view.
def clean(self):
cleaned_data = super(CreateKeypair, self).clean()
name = cleaned_data.get('name')
try:
keypairs = mogan.keypair_list(self.request)
except Exception:
exceptions.handle(self.request, ignore=True)
keypairs = []
if name in [keypair.name for keypair in keypairs]:
error_msg = _("The name is already in use.")
self._errors['name'] = self.error_class([error_msg])
return cleaned_data
class ImportKeypair(forms.SelfHandlingForm):
name = forms.RegexField(max_length=255,
label=_("Key Pair Name"),
regex=KEYPAIR_NAME_REGEX,
error_messages=KEYPAIR_ERROR_MESSAGES)
public_key = forms.CharField(label=_("Public Key"),
widget=forms.Textarea())
def handle(self, request, data):
try:
# Remove any new lines in the public key
data['public_key'] = NEW_LINES.sub("", data['public_key'])
keypair = mogan.keypair_import(request,
data['name'],
data['public_key'])
messages.success(request,
_('Successfully imported public key: %s')
% data['name'])
return keypair
except Exception:
exceptions.handle(request, ignore=True)
self.api_error(_('Unable to import key pair.'))
return False

View File

@ -0,0 +1,23 @@
# Copyright 2017 Cisco Systems, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from django.utils.translation import ugettext_lazy as _
import horizon
class KeyPairs(horizon.Panel):
name = _("Key Pairs")
slug = 'server_key_pairs'
permissions = ('openstack.services.baremetal_compute',)

View File

@ -0,0 +1,91 @@
# Copyright 2012 Nebula, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from django.utils.translation import ugettext_lazy as _
from django.utils.translation import ungettext_lazy
from horizon import tables
from mogan_ui.api import mogan
class DeleteKeyPairs(tables.DeleteAction):
help_text = _("Removing a key pair can leave OpenStack resources orphaned."
" You should not remove a key pair unless you are certain it"
" is not being used anywhere.")
@staticmethod
def action_present(count):
return ungettext_lazy(
u"Delete Server Key Pair",
u"Delete Server Key Pairs",
count
)
@staticmethod
def action_past(count):
return ungettext_lazy(
u"Deleted Server Key Pair",
u"Deleted Server Key Pairs",
count
)
def delete(self, request, obj_id):
mogan.keypair_delete(request, obj_id)
class ImportKeyPair(tables.LinkAction):
name = "import"
verbose_name = _("Import Server Key Pair")
url = "horizon:project:server_key_pairs:import"
classes = ("ajax-modal",)
icon = "upload"
class CreateKeyPair(tables.LinkAction):
name = "create"
verbose_name = _("Create Server Key Pair")
url = "horizon:project:server_key_pairs:create"
classes = ("ajax-modal",)
icon = "plus"
def allowed(self, request, keypair=None):
# TODO(zhenguo): Add quotas support
return True
class KeypairsFilterAction(tables.FilterAction):
def filter(self, table, keypairs, filter_string):
"""Naive case-insensitive search."""
query = filter_string.lower()
return [keypair for keypair in keypairs
if query in keypair.name.lower()]
class KeyPairsTable(tables.DataTable):
detail_link = "horizon:project:server_key_pairs:detail"
name = tables.Column("name", verbose_name=_("Key Pair Name"),
link=detail_link)
fingerprint = tables.Column("fingerprint", verbose_name=_("Fingerprint"))
def get_object_id(self, keypair):
return keypair.name
class Meta(object):
name = "keypairs"
verbose_name = _("Key Pairs")
table_actions = (CreateKeyPair, ImportKeyPair, DeleteKeyPairs,
KeypairsFilterAction,)
row_actions = (DeleteKeyPairs,)

View File

@ -0,0 +1,7 @@
{% extends "horizon/common/_modal_form.html" %}
{% load i18n %}
{% block modal-body-right %}
<p>{% trans "Key pairs are SSH credentials which are injected into images when they are launched. Creating a new key pair registers the public key and downloads the private key (a .pem file)." %}</p>
<p>{% trans "Protect and use the key as you would any normal SSH private key." %}</p>
{% endblock %}

View File

@ -0,0 +1,12 @@
{% extends "horizon/common/_modal_form.html" %}
{% load i18n %}
{% block modal-body-right %}
<p>{% trans "Key Pairs are how you login to your instance after it is launched." %}</p>
<p>{% trans "Choose a key pair name you will recognise and paste your SSH public key into the space provided." %}</p>
<p>{% trans "SSH key pairs can be generated with the ssh-keygen command:" %}</p>
<p><pre>ssh-keygen -t rsa -f cloud.key</pre></p>
<p>{% trans "This generates a pair of keys: a key you keep private (cloud.key) and a public key (cloud.key.pub). Paste the contents of the public key file here." %}</p>
<p>{% trans "After launching an instance, you login using the private key (the username might be different depending on the image you launched):" %}</p>
<p><pre>ssh -i cloud.key &lt;username&gt;@&lt;instance_ip&gt;</pre></p>
{% endblock %}

View File

@ -0,0 +1,8 @@
{% extends 'base.html' %}
{% load i18n %}
{% block title %}{{ page_title }}{% endblock %}
{% block main %}
{% include 'project/key_pairs/_create.html' %}
{% endblock %}

View File

@ -0,0 +1,31 @@
{% extends 'base.html' %}
{% load i18n sizeformat %}
{% block title %}{{ page_title }}{% endblock %}
{% block page_header %}
{% include "horizon/common/_detail_header.html" %}
{% endblock %}
{% block main %}
<div class="detail">
<dl class="dl-horizontal">
<dt>{% trans "Name" %}</dt>
<dd>{{ keypair.name|default:_("None") }}</dd>
<dt>{% trans "Fingerprint" %}</dt>
<dd>{{ keypair.fingerprint|default:_("None") }}</dd>
<dt>{% trans "Created" %}</dt>
<dd>{{ keypair.created_at|parse_isotime}}</dd>
{% if keypair.updated_at %}
<dt>{% trans "Updated" %}
<dd>{{ keypair.updated_at|parse_isotime}}</dd>
{% endif %}
<dt>{% trans "User ID" %}</dt>
<dd>{{ keypair.user_id|default:_("None") }}</dd>
<dt>{% trans "Public Key" %}</dt>
<dd>
<div class="key-text word-wrap">{{ keypair.public_key|default:_("None") }}</div>
</dd>
</dl>
</div>
{% endblock %}

View File

@ -0,0 +1,17 @@
{% extends 'base.html' %}
{% load i18n %}
{% block title %}{{ page_title }}{% endblock %}
{% block main %}
<div class="alert alert-info">
{% blocktrans %}The key pair &quot;{{ keypair_name }}&quot; should download automatically. If not, use the button below.{% endblocktrans %}
</div>
<a class="btn btn-default" href="{% url 'horizon:project:server_key_pairs:generate' keypair_name "regenerate" %}">
{% blocktrans %}Regenerate and download Key Pair &quot;{{ keypair_name }}&quot;{% endblocktrans %}
</a>
<script type="text/javascript" charset="utf-8">
$(document).ready(function() {
document.location = '{% url 'horizon:project:server_key_pairs:generate' keypair_name %}';
});
</script>
{% endblock %}

View File

@ -0,0 +1,7 @@
{% extends 'base.html' %}
{% load i18n %}
{% block title %}{{ page_title }}{% endblock %}
{% block main %}
{% include 'project/key_pairs/_import.html' %}
{% endblock %}

View File

@ -0,0 +1,34 @@
# Copyright 2012 United States Government as represented by the
# Administrator of the National Aeronautics and Space Administration.
# All Rights Reserved.
#
# Copyright 2012 Nebula, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from django.conf.urls import url
from mogan_ui.content.key_pairs import views
urlpatterns = [
url(r'^$', views.IndexView.as_view(), name='index'),
url(r'^create/$', views.CreateView.as_view(), name='create'),
url(r'^import/$', views.ImportView.as_view(), name='import'),
url(r'^(?P<keypair_name>[^/]+)/download/$', views.DownloadView.as_view(),
name='download'),
url(r'^(?P<keypair_name>[^/]+)/generate/$', views.GenerateView.as_view(),
name='generate'),
url(r'^(?P<keypair_name>[^/]+)/(?P<optional>[^/]+)/generate/$',
views.GenerateView.as_view(), name='generate'),
url(r'^(?P<keypair_name>[^/]+)/$', views.DetailView.as_view(),
name='detail'),
]

View File

@ -0,0 +1,129 @@
# Copyright 2016 Cisco Systems
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from django.core.urlresolvers import reverse
from django.core.urlresolvers import reverse_lazy
from django import http
from django.template.defaultfilters import slugify
from django.utils.decorators import method_decorator
from django.utils.translation import ugettext_lazy as _
from django.views.decorators.cache import cache_control
from django.views.decorators.cache import never_cache
from horizon import exceptions
from horizon import forms
from horizon import tables
from horizon.utils import memoized
from horizon import views
from mogan_ui.api import mogan
from mogan_ui.content.key_pairs import forms as key_pairs_forms
from mogan_ui.content.key_pairs import tables as key_pairs_tables
class IndexView(tables.DataTableView):
table_class = key_pairs_tables.KeyPairsTable
page_title = _("Server Key Pairs")
def get_data(self):
try:
keypairs = mogan.keypair_list(self.request)
except Exception:
keypairs = []
exceptions.handle(self.request,
_('Unable to retrieve key pair list.'))
return keypairs
class CreateView(forms.ModalFormView):
form_class = key_pairs_forms.CreateKeypair
template_name = 'project/server_key_pairs/create.html'
submit_url = reverse_lazy(
"horizon:project:server_key_pairs:create")
success_url = 'horizon:project:server_key_pairs:download'
submit_label = page_title = _("Create Server Key Pair")
cancel_url = reverse_lazy("horizon:project:server_key_pairs:index")
def get_success_url(self):
return reverse(self.success_url,
kwargs={"keypair_name": self.request.POST['name']})
class ImportView(forms.ModalFormView):
form_class = key_pairs_forms.ImportKeypair
template_name = 'project/server_key_pairs/import.html'
submit_url = reverse_lazy(
"horizon:project:server_key_pairs:import")
success_url = reverse_lazy('horizon:project:server_key_pairs:index')
submit_label = page_title = _("Import Server Key Pair")
def get_object_id(self, keypair):
return keypair.name
class DetailView(views.HorizonTemplateView):
template_name = 'project/server_key_pairs/detail.html'
page_title = _("Server Key Pair Details")
@memoized.memoized_method
def _get_data(self):
try:
keypair = mogan.keypair_get(self.request,
self.kwargs['keypair_name'])
except Exception:
redirect = reverse('horizon:project:server_key_pairs:index')
msg = _('Unable to retrieve details for keypair "%s".')\
% (self.kwargs['keypair_name'])
exceptions.handle(self.request, msg,
redirect=redirect)
return keypair
def get_context_data(self, **kwargs):
"""Gets the context data for keypair."""
context = super(DetailView, self).get_context_data(**kwargs)
context['keypair'] = self._get_data()
return context
class DownloadView(views.HorizonTemplateView):
template_name = 'project/server_key_pairs/download.html'
page_title = _("Download Server Key Pair")
def get_context_data(self, keypair_name=None):
return {'keypair_name': keypair_name}
class GenerateView(views.HorizonTemplateView):
# TODO(Itxaka): Remove cache_control in django >= 1.9
# https://code.djangoproject.com/ticket/13008
@method_decorator(cache_control(max_age=0, no_cache=True,
no_store=True, must_revalidate=True))
@method_decorator(never_cache)
def get(self, request, keypair_name=None, optional=None):
try:
if optional == "regenerate":
mogan.keypair_delete(request, keypair_name)
keypair = mogan.keypair_create(request, keypair_name)
except Exception:
redirect = reverse('horizon:project:server_key_pairs:index')
exceptions.handle(self.request,
_('Unable to create key pair: %(exc)s'),
redirect=redirect)
response = http.HttpResponse(content_type='application/binary')
response['Content-Disposition'] = ('attachment; filename=%s.pem'
% slugify(keypair.name))
response.write(keypair.private_key)
response['Content-Length'] = str(len(response.content))
return response

View File

@ -0,0 +1,21 @@
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
# The slug of the panel to be added to HORIZON_CONFIG. Required.
PANEL = 'server_key_pairs'
# The slug of the panel group the PANEL is associated with.
PANEL_GROUP = 'baremetal_compute'
# The slug of the dashboard the PANEL associated with. Required.
PANEL_DASHBOARD = 'project'
# Python panel class of the PANEL to be added.
ADD_PANEL = 'mogan_ui.content.key_pairs.panel.KeyPairs'