Andrew Bogott has submitted this change and it was merged.

Change subject: Puppet Panel:  Actually populate the prefix panel with prefixes.
......................................................................


Puppet Panel:  Actually populate the prefix panel with prefixes.

Bug: T91990
Change-Id: I1a22aa6cad93da0bfe3c4f41748fd0a7566a1f61
---
M modules/openstack/files/liberty/horizon/puppettab/prefixpanel/plustab.py
M modules/openstack/files/liberty/horizon/puppettab/prefixpanel/prefixpanel.py
M modules/openstack/files/liberty/horizon/puppettab/puppet_config.py
M modules/openstack/files/liberty/horizon/puppettab/tab.py
M 
modules/openstack/files/liberty/horizon/puppettab/templates/puppet/plus_tab.html
5 files changed, 73 insertions(+), 43 deletions(-)

Approvals:
  Andrew Bogott: Looks good to me, approved
  jenkins-bot: Verified



diff --git 
a/modules/openstack/files/liberty/horizon/puppettab/prefixpanel/plustab.py 
b/modules/openstack/files/liberty/horizon/puppettab/prefixpanel/plustab.py
index f4fdc4f..73b1776 100644
--- a/modules/openstack/files/liberty/horizon/puppettab/prefixpanel/plustab.py
+++ b/modules/openstack/files/liberty/horizon/puppettab/prefixpanel/plustab.py
@@ -14,11 +14,15 @@
 #    under the License.
 
 import logging
+import re
 
 from django import template
 from django.template.loader import render_to_string
 from django.utils.translation import ugettext_lazy as _
 
+from wikimediapuppettab.puppet_config import puppet_config
+
+from horizon import exceptions
 from horizon import tabs
 
 logging.basicConfig()
@@ -39,12 +43,22 @@
         super(PlusTab, self).__init__(*args, **kwargs)
 
     def render(self):
-        LOG.warning("rendering, and logging is working")
         context = template.RequestContext(self.request)
         context['prefix_name'] = self.prefix_name
         return render_to_string(self.get_template_name(self.request),
                                 self.data, context_instance=context)
 
     def post(self, request, *args, **kwargs):
-        LOG.warning("We've got POST!")
+
+        pattern = re.compile("^[A-Za-z][A-Za-z0-9_-]*$")
+        if not pattern.match(request.POST["prefix_name"]):
+            raise exceptions.BadRequest('Prefix must begin with a '
+                                        'letter and contain only letters, '
+                                        'numbers, _ or -.')
+
         self.prefix_name = request.POST["prefix_name"]
+
+        # set an empty role list for this prefix, to force a
+        #  record creation on the back end.
+        config = puppet_config(self.prefix_name, self.tenant_id)
+        config.set_role_list([])
diff --git 
a/modules/openstack/files/liberty/horizon/puppettab/prefixpanel/prefixpanel.py 
b/modules/openstack/files/liberty/horizon/puppettab/prefixpanel/prefixpanel.py
index 4bb05b4..eb4e7dc 100644
--- 
a/modules/openstack/files/liberty/horizon/puppettab/prefixpanel/prefixpanel.py
+++ 
b/modules/openstack/files/liberty/horizon/puppettab/prefixpanel/prefixpanel.py
@@ -24,6 +24,7 @@
 
 from wikimediapuppettab.tab import PuppetTab
 from wikimediapuppettab.prefixpanel.plustab import PlusTab
+from wikimediapuppettab.puppet_config import puppet_config
 
 logging.basicConfig()
 LOG = logging.getLogger(__name__)
@@ -32,9 +33,6 @@
 class PrefixPuppetPanel(horizon.Panel):
     name = _("Prefix Puppet")
     slug = "prefixpuppet"
-
-    def handle(self, request, data):
-        LOG.warning('PrefixPuppetPanel is handling')
 
 
 class PrefixTabs(tabs.TabGroup):
@@ -47,42 +45,44 @@
         self.request = request
         self.kwargs = kwargs
         self._data = None
-        tab_instances = []
+        self.request = request
 
-        tenant_id = self.request.user.tenant_id
-
-        # demo #1
-        prefix = 'prefixone'
-        tab_instances.append(("puppet-%s" % prefix,
-                              PuppetTab(self,
-                                        request,
-                                        prefix=prefix,
-                                        tenant_id=tenant_id)))
-        # demo #2
-        prefix = 'prefixtwo'
-        tab_instances.append(("puppet-%s" % prefix,
-                              PuppetTab(self,
-                                        request,
-                                        prefix=prefix,
-                                        tenant_id=tenant_id)))
-        # + tab
-        tab_instances.append(('puppetprefixplus',
-                              PlusTab(self, request, tenant_id=tenant_id)))
-
-        self._tabs = OrderedDict(tab_instances)
+        self.tenant_id = self.request.user.tenant_id
+        self._tabs = OrderedDict(self.get_dynamic_tab_list())
         if self.sticky:
             self.attrs['data-sticky-tabs'] = 'sticky'
         if not self._set_active_tab():
             self.tabs_not_available()
 
-    def handle(self, request, data):
-        LOG.warning('PrefixTabs is handling')
+    def get_dynamic_tab_list(self):
+        prefixlist = puppet_config.get_prefixes(self.tenant_id)
+        LOG.warning("prefixlist: %s" % prefixlist)
+
+        tab_instances = []
+        # One tab per prefix
+        for prefix in prefixlist:
+            # exclude anything with a '.' as those are instance names
+            #  handled on a different UI
+            if '.' not in prefix:
+                tab_instances.append(("puppet-%s" % prefix,
+                                      PuppetTab(self,
+                                                self.request,
+                                                prefix=prefix,
+                                                tenant_id=self.tenant_id)))
+
+        # + tab
+        tab_instances.append(('puppetprefixplus',
+                              PlusTab(self, self.request,
+                                      tenant_id=self.tenant_id)))
+        return tab_instances
+
+    def load_tab_data(self):
+        # This ensures that the tab list is updated without
+        #  having to rebuild the whole object.
+        self._tabs = OrderedDict(self.get_dynamic_tab_list())
 
 
 class IndexView(tabs.TabbedTableView):
     tab_group_class = PrefixTabs
     template_name = 'project/puppet/prefix_panel.html'
     page_title = _("Prefix Puppet")
-
-    def handle(self, request, data):
-        LOG.warning('IndexView is handling')
diff --git a/modules/openstack/files/liberty/horizon/puppettab/puppet_config.py 
b/modules/openstack/files/liberty/horizon/puppettab/puppet_config.py
index bc209db..0fce354 100644
--- a/modules/openstack/files/liberty/horizon/puppettab/puppet_config.py
+++ b/modules/openstack/files/liberty/horizon/puppettab/puppet_config.py
@@ -59,6 +59,8 @@
             self.hiera_raw = yaml.safe_load(req.text)['hiera']
 
         hiera_yaml = yaml.safe_load(self.hiera_raw)
+        if not hiera_yaml:
+            hiera_yaml = {}
         self.role_dict = {}
 
         # Find the hiera lines that assign params to applied roles.
@@ -154,3 +156,16 @@
                             headers={'Content-Type': 'application/x-yaml'})
         req.raise_for_status()
         self.refresh()
+
+    @staticmethod
+    def get_prefixes(tenant_id):
+        apiurl = getattr(settings,
+                         "PUPPET_CONFIG_BACKEND",
+                         "http://labcontrol1001.wikimedia.org:8100/v1";)
+        prefixurl = "%s/%s/prefix" % (apiurl, tenant_id)
+        req = requests.get(prefixurl, verify=False)
+        if req.status_code == 404:
+            return []
+        else:
+            req.raise_for_status()
+        return yaml.safe_load(req.text)['prefixes']
diff --git a/modules/openstack/files/liberty/horizon/puppettab/tab.py 
b/modules/openstack/files/liberty/horizon/puppettab/tab.py
index 24d4902..9f880ac 100644
--- a/modules/openstack/files/liberty/horizon/puppettab/tab.py
+++ b/modules/openstack/files/liberty/horizon/puppettab/tab.py
@@ -48,7 +48,7 @@
             self.tenant_id = kwargs['tenant_id']
             del kwargs['tenant_id']
 
-        if self.tenant_id and self.prefix:
+        if hasattr(self, 'tenant_id') and hasattr(self, 'prefix'):
             self.caption = _("These puppet settings will affect all VMs in the"
                              " %s project whose names begin with \'%s\'") % (
                 self.tenant_id, self.prefix)
@@ -63,12 +63,11 @@
             self.prefix = "%s.%s.%s" % (instance.name,
                                         instance.tenant_id, tld)
             self.tenant_id = instance.tenant_id
-            self.caption = _("These puppet settings will affect all VMs"
-                             " in the %s project") % (self.tenant_id,
-                                                      self.prefix)
-        elif 'prefix' in self.tab_group.kwargs:
-            self.prefix = self.tab_group.kwargs['prefix']
+        elif 'tenant_id' in self.tab_group.kwargs:
             self.tenant_id = self.tab_group.kwargs['tenant_id']
+            self.prefix = self.tab_group.kwargs['prefix']
+            self.caption = _("These puppet settings will affect all VMs"
+                             " in the %s project") % self.tenant_id
 
         self.config = puppet_config(self.prefix, self.tenant_id)
 
@@ -77,10 +76,10 @@
         context['prefix'] = self.prefix
         context['config'] = self.config
 
-        if 'caption' in self.tab_group.kwargs:
-            context['caption'] = self.tab_group.kwargs['caption']
-        elif self.caption:
+        if hasattr(self, 'caption'):
             context['caption'] = self.caption
+        elif 'caption' in self.tab_group.kwargs:
+            context['caption'] = self.tab_group.kwargs['caption']
 
         url = "horizon:project:puppet:edithiera"
         kwargs = {
diff --git 
a/modules/openstack/files/liberty/horizon/puppettab/templates/puppet/plus_tab.html
 
b/modules/openstack/files/liberty/horizon/puppettab/templates/puppet/plus_tab.html
index a855907..f5338a3 100644
--- 
a/modules/openstack/files/liberty/horizon/puppettab/templates/puppet/plus_tab.html
+++ 
b/modules/openstack/files/liberty/horizon/puppettab/templates/puppet/plus_tab.html
@@ -3,11 +3,13 @@
 {% if prefix_name %}
 Name: {{ prefix_name }}
 {% else %}
+<br>
+{% trans "A prefix must start with a letter and consist of only letters, 
numbers, - and _." %}
 <form action="" method="POST">
     {% csrf_token %}
     <input type="hidden" name="action" value="puppetprefixplus__undefined">
-    <input type="text" name="prefix_name" placeholder="prefix name:"><br>
-    <input type="submit" value="New prefix"><br>
+    <input type="text" name="prefix_name" placeholder="prefix name:" 
pattern="^[A-Za-z][A-Za-z0-9-_]*$">
+    <input type="submit" value="Add prefix"><br>
 </form>
 {% endif %}
 

-- 
To view, visit https://gerrit.wikimedia.org/r/310718
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I1a22aa6cad93da0bfe3c4f41748fd0a7566a1f61
Gerrit-PatchSet: 4
Gerrit-Project: operations/puppet
Gerrit-Branch: production
Gerrit-Owner: Andrew Bogott <abog...@wikimedia.org>
Gerrit-Reviewer: Andrew Bogott <abog...@wikimedia.org>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to