From 7b1915a9bd06c4ad8bde7ce0a3f5d715a773671c Mon Sep 17 00:00:00 2001
From: Tanner Prestegard <tanner.prestegard@ligo.org>
Date: Tue, 18 Jun 2019 14:53:51 -0500
Subject: [PATCH] Create and populate group for robot accounts

---
 .../0044_create_robot_accounts_authgroup.py   | 38 +++++++++++++++++++
 .../0045_populate_robot_accounts_authgroup.py | 28 ++++++++++++++
 2 files changed, 66 insertions(+)
 create mode 100644 gracedb/ligoauth/migrations/0044_create_robot_accounts_authgroup.py
 create mode 100644 gracedb/ligoauth/migrations/0045_populate_robot_accounts_authgroup.py

diff --git a/gracedb/ligoauth/migrations/0044_create_robot_accounts_authgroup.py b/gracedb/ligoauth/migrations/0044_create_robot_accounts_authgroup.py
new file mode 100644
index 000000000..a47792b41
--- /dev/null
+++ b/gracedb/ligoauth/migrations/0044_create_robot_accounts_authgroup.py
@@ -0,0 +1,38 @@
+# -*- coding: utf-8 -*-
+# Generated by Django 1.11.20 on 2019-06-18 16:33
+from __future__ import unicode_literals
+
+from django.db import migrations
+
+AUTHGROUP_INFO = {
+    'name': 'robot_accounts',
+    'description': ('Shared robot accounts for LIGO/Virgo pipelines, '
+        'follow-up processes, etc.'),
+    'ldap_name': 'Communities:robot:GraceDBrobotCert',
+}
+
+
+def create_authgroup(apps, schema_editor):
+    AuthGroup = apps.get_model('ligoauth', 'AuthGroup')
+
+    # Create AuthGroup
+    AuthGroup.objects.create(**AUTHGROUP_INFO)
+
+
+def delete_authgroup(apps, schema_editor):
+    AuthGroup = apps.get_model('ligoauth', 'AuthGroup')
+
+    # Delete AuthGroup
+    ag = AuthGroup.objects.get(name=AUTHGROUP_INFO['name'])
+    ag.delete()
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('ligoauth', '0043_x509cert_delete_users_m2m_field'),
+    ]
+
+    operations = [
+        migrations.RunPython(create_authgroup, delete_authgroup),
+    ]
diff --git a/gracedb/ligoauth/migrations/0045_populate_robot_accounts_authgroup.py b/gracedb/ligoauth/migrations/0045_populate_robot_accounts_authgroup.py
new file mode 100644
index 000000000..e26d448e4
--- /dev/null
+++ b/gracedb/ligoauth/migrations/0045_populate_robot_accounts_authgroup.py
@@ -0,0 +1,28 @@
+# -*- coding: utf-8 -*-
+# Generated by Django 1.11.20 on 2019-06-18 16:42
+from __future__ import unicode_literals
+
+from django.db import migrations
+
+ROBOT_GROUP_NAME = 'robot_accounts'
+
+
+# Add current RobotUsers to robot_accounts AuthGroup
+def add_users(apps, schema_editor):
+    AuthGroup = apps.get_model('ligoauth', 'AuthGroup')
+    RobotUser = apps.get_model('ligoauth', 'RobotUser')
+
+    group = AuthGroup.objects.get(name=ROBOT_GROUP_NAME)
+    for robot in RobotUser.objects.all():
+        group.user_set.add(robot.user_ptr)
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('ligoauth', '0044_create_robot_accounts_authgroup'),
+    ]
+
+    operations = [
+        migrations.RunPython(add_users, migrations.RunPython.noop),
+    ]
-- 
GitLab