apiVersion: batch/v1
kind: Job
metadata:
  name: hub-import-database-job
spec:
  #ttlSecondsAfterFinished: 60  # Job will be deleted 60 seconds after completion
  template:
    spec:
      initContainers:
      - name: wait-for-mongodb-before-starup
        image: busybox
        command:  ["sh", "-c", "until nc -z mongodb.mongodb 27017 > /dev/null; do echo Waiting for master.; sleep 2; done;"]
      - name: copy-config
        image: busybox
        command: ['sh', '-c', 'echo "$IMPORT_FILE" > /data/hub-import.js']
        env:
        - name: IMPORT_FILE
          valueFrom:
            configMapKeyRef:
              name: hub-import-configmap
              key: hub-import.js
        volumeMounts:
        - name: hub-import
          mountPath: /data
      containers:
      - name: init-container
        image: mongo:latest
        command: ['sh', '-c', 'mongosh --host mongodb.mongodb --username root --password yourpassword --authenticationDatabase admin --db Kerberos --file /data/hub-import.js']
        volumeMounts:
        - name: hub-import
          mountPath: /data
      restartPolicy: Never
      volumes:
      - name: hub-import
        emptyDir: {}
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: hub-import-configmap
data:
    hub-import.js: |
        conn = db.getMongo();
        db = conn.getDB('Kerberos');

        // This will create a user with the following credentials.
        // username: example-user
        // password: example-password
        db.users.updateOne(
            { '_id': ObjectId('57e1011e3178aa6c5cc774d1') },
            {
                $set: {
                'username': 'example-user',
                'email': 'example-user@email.com',
                'password': '$2a$10$jwLcD/.UT/1WLK7ct1XuHewI3GQXwW3zerPhCCs7QDrReEuIHbVYi',
                'role': 'owner',
                'google2fa_enabled': false,
                'timezone': 'Europe/Brussels',
                'isActive': NumberLong(1),
                'registerToken': '',
                'updated_at': ISODate('2020-06-14T05:01:35.000Z'),
                'created_at': ISODate('2016-09-20T09:27:58.811Z'),
                'amazon_secret_access_key': 'K6rRLBI1xxxCk3C1H',
                'amazon_access_key_id': 'AKIAxxxxxxG5Q',
                'card_brand': 'Visa',
                'card_last_four': '0000',
                'card_status': 'ok',
                'card_status_message': null
                }
            },
            { upsert: true }
        );


        // This will create an application with the following credentials, which
        // can be used to authenticate with the admin panel.
        // username: example-application
        // password: example-password
        db.users.updateOne(
            { '_id': ObjectId('57e1011e3178aa6c5cc774d2') },
            {
                $set: {
                'username': 'example-application',
                'email': 'example-application@email.com',
                'password': '$2a$10$jwLcD/.UT/1WLK7ct1XuHewI3GQXwW3zerPhCCs7QDrReEuIHbVYi',
                'role': 'application',
                'google2fa_enabled': false,
                'timezone': 'Europe/Brussels',
                'isActive': NumberLong(1),
                'registerToken': '',
                'updated_at': ISODate('2020-06-14T05:01:35.000Z'),
                'created_at': ISODate('2016-09-20T09:27:58.811Z')
                }
            },
            { upsert: true }
        );

        // This will create a subscription for the previously created user.
        // Subscriptions allow you to modify specific permissions and features for a user.
        // As you'll see below you can specify specific threshold for each subscription.
        db.subscriptions.updateOne(
            { '_id': ObjectId('57e1011e3178aa6c5cc774d1') },
            {
                $set: {
                'name': 'default',
                'stripe_id': 'sub_9ECyjjMz3R7etK',
                'stripe_plan': 'enterprise',
                'quantity': 1,
                'trial_ends_at': null,
                'ends_at': null,
                'user_id': '57e1011e3178aa6c5cc774d1',
                'updated_at': ISODate('2021-04-27T09:45:30.169Z'),
                'created_at': ISODate('2016-09-20T09:35:03.448Z'),
                'stripe_status': 'active'
                }
            },
            { upsert: true }
        );

        // This will create a settings object for the subscription.
        // and will allow you to modify the settings for the subscription.
        // - Level: a number that represents the feature level of the subscription (which features are available)
        // - UploadLimit: the maximum number of uploads allowed per day (upload of a single video).
        // - VideoLimit: the maximum number of videos allowed per day.
        // - Usage: the maximum storage per dag (expessed in megabytes).
        // - AnalysisLimit: the maximum number of analysis allowed per day (number of recordings processed with machine learning).
        // - DayLimit: the maximum number of days the subscription is active.
        db.settings.updateOne(
            { '_id': ObjectId('5a72c509e17699d18ada9154') },
            {
                $set: {
                'key': 'plan',
                'map': {
                    'basic': {
                    'level': NumberInt(1),
                    'uploadLimit': NumberInt(100),
                    'videoLimit': NumberInt(100),
                    'usage': NumberInt(500),
                    'analysisLimit': NumberInt(0),
                    'dayLimit': NumberInt(3)
                    },
                    'premium': {
                    'level': NumberInt(2),
                    'uploadLimit': NumberInt(500),
                    'videoLimit': NumberInt(500),
                    'usage': NumberInt(1000),
                    'analysisLimit': NumberInt(0),
                    'dayLimit': NumberInt(7)
                    },
                    'gold': {
                    'level': NumberInt(3),
                    'uploadLimit': NumberInt(1000),
                    'videoLimit': NumberInt(1000),
                    'usage': NumberInt(3000),
                    'analysisLimit': NumberInt(1000),
                    'dayLimit': NumberInt(30)
                    },
                    'business': {
                    'level': NumberInt(4),
                    'uploadLimit': NumberInt(99999999),
                    'videoLimit': NumberInt(99999999),
                    'usage': NumberInt(10000),
                    'analysisLimit': NumberInt(1000),
                    'dayLimit': NumberInt(30)
                    },
                    'enterprise': {
                    'level': NumberInt(5),
                    'uploadLimit': NumberInt(99999999),
                    'videoLimit': NumberInt(99999999),
                    'usage': NumberInt(99999999),
                    'analysisLimit': NumberInt(5000),
                    'dayLimit': NumberInt(30)
                    }
                }
                }
            },
            { upsert: true }
        );

        // By modifying the following settings you can change the classification options.
        // This will modify the classification options in the media page
        db.settings.updateOne(
            { '_id': ObjectId('63f346ec64011a574161cf99') },
            {
                $set: {
                'key': 'classifications',
                'map': {
                    'objects': [
                    {
                        'text': 'Car',
                        'value': 'car',
                        'icon': 'car'
                    },
                    {
                        'text': 'Person',
                        'value': 'pedestrian',
                        'icon': 'pedestrian'
                    }
                    ]
                }
                }
            },
            { upsert: true }
        );

        db.settings.updateOne(
            { '_id': ObjectId('5a43fa12d885eb7da57046b3') },
            {
                $set: {
                'key': 'sequence',
                'map': {
                    'timeBetween': NumberInt(60)
                }
                }
            },
            { upsert: true }
        );

        db.settings.updateOne(
            { '_id': ObjectId('5a4d3a6bd885eb7da5e6b297') },
            {
                $set: {
                'key': 'throttler',
                'map': {
                    'waitingTime': NumberInt(60)
                }
                }
            },
            { upsert: true }
            );

        db.settings.updateOne(
            { '_id': ObjectId('5a53d0a0d885eb7da53ed5a6') },
            {
                $set: {
                'key': 'analysis',
                'map': {
                    'waitingTime': NumberInt(15)
                }
                }
            },
            { upsert: true }
        );