agent: add generic import_template management command (#14630)

This commit is contained in:
Christophe Siraut 2019-01-10 21:34:51 +01:00
parent ea05d2abb8
commit 03a2aa8ed4
6 changed files with 604 additions and 0 deletions

View File

@ -0,0 +1,40 @@
# hobo - portal to configure and deploy applications
# Copyright (C) 2015 Entr'ouvert
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import os
from django.conf import settings
from django.core.management.base import BaseCommand, CommandError
from django.core.management import call_command, get_commands
class Command(BaseCommand):
def add_arguments(self, parser):
parser.add_argument('template_name', type=str)
parser.add_argument('--basepath', type=str)
def handle(self, *args, **kwargs):
app_name = settings.PROJECT_NAME
basepath = kwargs.get('basepath', '/var/lib/%s/skeletons')
template = '%s/%s.json' % (basepath, kwargs.get('template_name'))
if 'import_site' in get_commands():
if not os.path.isfile(template):
raise(CommandError('Template not found'))
call_command('import_site', template)

10
tests/utils.py Normal file
View File

@ -0,0 +1,10 @@
def byteify(input):
if isinstance(input, dict):
return {byteify(key): byteify(value)
for key, value in input.iteritems()}
elif isinstance(input, list):
return [byteify(element) for element in input]
elif isinstance(input, unicode):
return input.encode('utf-8')
else:
return input

View File

@ -0,0 +1,480 @@
{
"ous": [
{
"username_is_unique": false,
"validate_emails": false,
"uuid": "69b0a02cf58a4c71b1ae548f1375baff",
"default": true,
"description": "",
"email_is_unique": false,
"slug": "default",
"name": "Collectivit\u00e9 par d\u00e9faut"
}
],
"roles": [
{
"description": "",
"service": {
"ou": {
"slug": "default",
"uuid": "69b0a02cf58a4c71b1ae548f1375baff",
"name": "Collectivit\u00e9 par d\u00e9faut"
},
"slug": "hobo"
},
"name": "Administrateur de Hobo",
"attributes": [
{
"kind": "string",
"name": "is_superuser",
"value": "true"
}
],
"ou": {
"slug": "default",
"uuid": "69b0a02cf58a4c71b1ae548f1375baff",
"name": "Collectivit\u00e9 par d\u00e9faut"
},
"external_id": "",
"slug": "_a2-hobo-superuser",
"uuid": "25f33158b7e2449b9a5b00dbc57bf416"
},
{
"description": "",
"service": {
"ou": {
"slug": "default",
"uuid": "69b0a02cf58a4c71b1ae548f1375baff",
"name": "Collectivit\u00e9 par d\u00e9faut"
},
"slug": "portal"
},
"name": "Administrateur de Compte citoyen",
"attributes": [
{
"kind": "string",
"name": "is_superuser",
"value": "true"
}
],
"ou": {
"slug": "default",
"uuid": "69b0a02cf58a4c71b1ae548f1375baff",
"name": "Collectivit\u00e9 par d\u00e9faut"
},
"external_id": "",
"slug": "_a2-hobo-superuser",
"uuid": "84b3b1ba76e44bcdb4fd4437c448a981"
},
{
"description": "",
"service": {
"ou": {
"slug": "default",
"uuid": "69b0a02cf58a4c71b1ae548f1375baff",
"name": "Collectivit\u00e9 par d\u00e9faut"
},
"slug": "eservices"
},
"name": "Administrateur de D\u00e9marches",
"attributes": [
{
"kind": "string",
"name": "is_superuser",
"value": "true"
}
],
"ou": {
"slug": "default",
"uuid": "69b0a02cf58a4c71b1ae548f1375baff",
"name": "Collectivit\u00e9 par d\u00e9faut"
},
"external_id": "",
"slug": "_a2-hobo-superuser",
"uuid": "9054a61ccf684396b38189f1ca1ec087"
},
{
"description": "",
"service": {
"ou": {
"slug": "default",
"uuid": "69b0a02cf58a4c71b1ae548f1375baff",
"name": "Collectivit\u00e9 par d\u00e9faut"
},
"slug": "portal-agent"
},
"name": "Administrateur de Portail agent",
"attributes": [
{
"kind": "string",
"name": "is_superuser",
"value": "true"
}
],
"ou": {
"slug": "default",
"uuid": "69b0a02cf58a4c71b1ae548f1375baff",
"name": "Collectivit\u00e9 par d\u00e9faut"
},
"external_id": "",
"slug": "_a2-hobo-superuser",
"uuid": "e6e22e5c0ca04ac0bf3b50d88eafe6d5"
},
{
"description": "",
"service": {
"ou": {
"slug": "default",
"uuid": "69b0a02cf58a4c71b1ae548f1375baff",
"name": "Collectivit\u00e9 par d\u00e9faut"
},
"slug": "passerelle"
},
"name": "Administrateur de Passerelle",
"attributes": [
{
"kind": "string",
"name": "is_superuser",
"value": "true"
}
],
"ou": {
"slug": "default",
"uuid": "69b0a02cf58a4c71b1ae548f1375baff",
"name": "Collectivit\u00e9 par d\u00e9faut"
},
"external_id": "",
"slug": "_a2-hobo-superuser",
"uuid": "243f58712aa248e9b27aae669341c156"
},
{
"description": "",
"service": null,
"name": "Administrateur du r\u00f4le \u00ab\u00a0Debug eo\u00a0\u00bb",
"ou": {
"slug": "default",
"uuid": "69b0a02cf58a4c71b1ae548f1375baff",
"name": "Collectivit\u00e9 par d\u00e9faut"
},
"permissions": [
{
"operation": {
"slug": "view"
},
"ou": null,
"target_ct": {
"model": "contenttype",
"app_label": "contenttypes"
},
"target": {
"model": "user",
"app_label": "custom_user"
}
},
{
"operation": {
"slug": "admin"
},
"ou": {
"slug": "default",
"uuid": "69b0a02cf58a4c71b1ae548f1375baff",
"name": "Collectivit\u00e9 par d\u00e9faut"
},
"target_ct": {
"model": "role",
"app_label": "a2_rbac"
},
"target": {
"name": "Debug eo",
"ou": {
"slug": "default",
"uuid": "69b0a02cf58a4c71b1ae548f1375baff",
"name": "Collectivit\u00e9 par d\u00e9faut"
},
"uuid": "18e7bf78dc9a432396a99f32060052ec",
"service": null,
"slug": "debug-eo"
}
},
{
"operation": {
"slug": "change"
},
"ou": null,
"target_ct": {
"model": "role",
"app_label": "a2_rbac"
},
"target": {
"name": "Administrateur du r\u00f4le \u00ab\u00a0Debug eo\u00a0\u00bb",
"ou": {
"slug": "default",
"uuid": "69b0a02cf58a4c71b1ae548f1375baff",
"name": "Collectivit\u00e9 par d\u00e9faut"
},
"uuid": "3049444b35874b3b9a8377ad2f10b8b6",
"service": null,
"slug": "_a2-managers-of-role-debug-eo"
}
}
],
"external_id": "",
"slug": "_a2-managers-of-role-debug-eo",
"uuid": "3049444b35874b3b9a8377ad2f10b8b6"
},
{
"description": "",
"service": null,
"name": "Debug eo",
"attributes": [
{
"kind": "json",
"name": "emails_to_members",
"value": "false"
},
{
"kind": "json",
"name": "details",
"value": "\"\""
},
{
"kind": "json",
"name": "emails",
"value": "[]"
}
],
"ou": {
"slug": "default",
"uuid": "69b0a02cf58a4c71b1ae548f1375baff",
"name": "Collectivit\u00e9 par d\u00e9faut"
},
"external_id": "",
"slug": "debug-eo",
"uuid": "18e7bf78dc9a432396a99f32060052ec"
},
{
"description": "",
"service": null,
"name": "Administrateur",
"parents": [
{
"name": "Administrateur des entit\u00e9s",
"ou": null,
"uuid": "a1ff1b3da88f47cea91e344998dfdfbf",
"service": null,
"slug": "_a2-administrateur-des-entites"
},
{
"name": "Administrateur des r\u00f4les",
"ou": null,
"uuid": "8dd625b74cff40aa8531d7d72616550e",
"service": null,
"slug": "_a2-administrateur-des-roles"
},
{
"name": "Administrateur des utilisateurs",
"ou": null,
"uuid": "4ab5effedc404fb1bcba4d21ee89b719",
"service": null,
"slug": "_a2-administrateur-des-utilisateurs"
}
],
"ou": null,
"permissions": [
{
"operation": {
"slug": "change"
},
"ou": null,
"target_ct": {
"model": "role",
"app_label": "a2_rbac"
},
"target": {
"name": "Administrateur",
"ou": null,
"uuid": "81a8708382bb4e8ea12ed0e172aa48b9",
"service": null,
"slug": "_a2-manager"
}
}
],
"external_id": "",
"slug": "_a2-manager",
"uuid": "81a8708382bb4e8ea12ed0e172aa48b9"
},
{
"description": "",
"service": null,
"name": "Administrateur des entit\u00e9s",
"ou": null,
"permissions": [
{
"operation": {
"slug": "view"
},
"ou": null,
"target_ct": {
"model": "contenttype",
"app_label": "contenttypes"
},
"target": {
"model": "organizationalunit",
"app_label": "a2_rbac"
}
},
{
"operation": {
"slug": "admin"
},
"ou": null,
"target_ct": {
"model": "contenttype",
"app_label": "contenttypes"
},
"target": {
"model": "organizationalunit",
"app_label": "a2_rbac"
}
},
{
"operation": {
"slug": "search"
},
"ou": null,
"target_ct": {
"model": "contenttype",
"app_label": "contenttypes"
},
"target": {
"model": "organizationalunit",
"app_label": "a2_rbac"
}
}
],
"external_id": "",
"slug": "_a2-administrateur-des-entites",
"uuid": "a1ff1b3da88f47cea91e344998dfdfbf"
},
{
"description": "",
"service": null,
"name": "Administrateur des r\u00f4les",
"ou": null,
"permissions": [
{
"operation": {
"slug": "view"
},
"ou": null,
"target_ct": {
"model": "contenttype",
"app_label": "contenttypes"
},
"target": {
"model": "user",
"app_label": "custom_user"
}
},
{
"operation": {
"slug": "view"
},
"ou": null,
"target_ct": {
"model": "contenttype",
"app_label": "contenttypes"
},
"target": {
"model": "organizationalunit",
"app_label": "a2_rbac"
}
},
{
"operation": {
"slug": "admin"
},
"ou": null,
"target_ct": {
"model": "contenttype",
"app_label": "contenttypes"
},
"target": {
"model": "role",
"app_label": "a2_rbac"
}
},
{
"operation": {
"slug": "search"
},
"ou": null,
"target_ct": {
"model": "contenttype",
"app_label": "contenttypes"
},
"target": {
"model": "organizationalunit",
"app_label": "a2_rbac"
}
}
],
"external_id": "",
"slug": "_a2-administrateur-des-roles",
"uuid": "8dd625b74cff40aa8531d7d72616550e"
},
{
"description": "",
"service": null,
"name": "Administrateur des utilisateurs",
"ou": null,
"permissions": [
{
"operation": {
"slug": "view"
},
"ou": null,
"target_ct": {
"model": "contenttype",
"app_label": "contenttypes"
},
"target": {
"model": "organizationalunit",
"app_label": "a2_rbac"
}
},
{
"operation": {
"slug": "admin"
},
"ou": null,
"target_ct": {
"model": "contenttype",
"app_label": "contenttypes"
},
"target": {
"model": "user",
"app_label": "custom_user"
}
},
{
"operation": {
"slug": "search"
},
"ou": null,
"target_ct": {
"model": "contenttype",
"app_label": "contenttypes"
},
"target": {
"model": "organizationalunit",
"app_label": "a2_rbac"
}
}
],
"external_id": "",
"slug": "_a2-administrateur-des-utilisateurs",
"uuid": "4ab5effedc404fb1bcba4d21ee89b719"
}
]
}

View File

@ -6,6 +6,13 @@ import shutil
import json
import mock
from django.core.management import call_command
from django.db import connection
from hobo.multitenant.middleware import TenantMiddleware
from authentic2.data_transfer import export_site
os.sys.path.append('%s/tests' % os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from utils import byteify
pytestmark = pytest.mark.django_db
@ -436,3 +443,31 @@ def test_hobo_deploy(tenant_base, mocker, skeleton_dir):
assert Role.objects.filter(
ou=provider.ou, service__isnull=True,
name=u'Service état-civil').count() == 1
def test_import_template(db, tenant_base):
def with_uuid_removed(input):
if isinstance(input, dict):
for key in input.keys():
if key == 'uuid':
input.pop('uuid')
return {k: with_uuid_removed(v) for k, v in input.iteritems()}
elif isinstance(input, list):
return [with_uuid_removed(e) for e in input]
else:
return input
def with_lists_sorted(input):
if isinstance(input, dict):
return {k: with_lists_sorted(v) for k, v in input.iteritems()}
if isinstance(input, list):
return with_lists_sorted(input.sort())
else:
return input
call_command('create_tenant', 'authentic.example.net')
tenant = TenantMiddleware.get_tenant_by_hostname('authentic.example.net')
connection.set_tenant(tenant)
call_command('import_template', '--basepath=%s' % os.path.dirname(__file__), 'data_authentic_export_site')
content = open('%s/data_authentic_export_site.json' % os.path.dirname(__file__)).read()
assert byteify(with_lists_sorted(with_uuid_removed(export_site()))) == byteify(with_lists_sorted(with_uuid_removed(json.loads(content))))

View File

@ -0,0 +1,24 @@
{
"apiusers": [],
"resources": [
{
"log_level": "INFO",
"description": "",
"title": "Foo",
"access_rights": [],
"@type": "passerelle-resource",
"text_value_template": "",
"slug": "foo",
"resource_type": "jsondatastore.jsondatastore"
},
{
"log_level": "INFO",
"description": ".",
"title": "Bar",
"access_rights": [],
"@type": "passerelle-resource",
"text_value_template": "{{nom|safe}}",
"slug": "bar",
"resource_type": "jsondatastore.jsondatastore"
}]
}

View File

@ -1,10 +1,16 @@
import json
import os
import sys
from hobo.multitenant.middleware import TenantMiddleware
from django.core.management import call_command
from django.db import connection
import StringIO
from passerelle.utils import export_site
os.sys.path.append('%s/tests' % os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from utils import byteify
def test_deploy_specifics(db, tenant_base):
hobo_json = {
@ -42,3 +48,12 @@ def test_deploy_specifics(db, tenant_base):
sys.stdin = old_stdin
assert len(list(TenantMiddleware.get_tenants())) == 1
def test_import_template(db, tenant_base):
call_command('create_tenant', 'passerelle.example.net')
tenant = TenantMiddleware.get_tenant_by_hostname('passerelle.example.net')
connection.set_tenant(tenant)
call_command('import_template', '--basepath=%s' % os.path.dirname(__file__), 'data_passerelle_export_site')
content = open('%s/data_passerelle_export_site.json' % os.path.dirname(__file__)).read()
assert export_site() == byteify(json.loads(content))