from __future__ import absolute_import

import json
import logging

from datetime import timedelta, datetime

import unityapiclient
from unityapiclient.client import UnityApiClient

from b2accessdeprovisioning.configparser import config
from b2accessdeprovisioning.user import User
from b2accessdeprovisioning.notifier import MailNotifier

logger = logging.getLogger(__name__)

b2access = UnityApiClient(
    config['api']['base_url'],
    auth=(config['api']['user'], config['api']['password']),
    cert_verify=config['api']['cert_verify'])

notifier = MailNotifier(
    host=config['notifications']['email']['host'],
    port=config['notifications']['email']['port'],
    use_tls=config['notifications']['email']['use_tls'],
    user=config['notifications']['email']['user'],
    password=config['notifications']['email']['password'])

if 'dry_run' in config:
    dry_run = config['dry_run']
else:
    dry_run = False

def main():
    groups = b2access.get_group()
    users = []
    for member_id in groups['members']:
        entity = b2access.get_entity(member_id)
        if entity['entityInformation']['state'] != 'disabled':
            continue
        if entity['entityInformation']['scheduledOperation'] == 'REMOVE':
            continue
        user = User(internal_id=member_id)
        users.append(user)
        for identity in entity['identities']:
            if identity['typeId'] == 'persistent':
                user.shared_id = identity['value']
                break

    for user in users:
        _remove_user_attrs(user)
        _schedule_user_removal(user)
        
    if users:
        _send_notification(users)


def _remove_user_attrs(user):
    attr_whitelist = config['attr_whitelist']

    attrs = b2access.get_entity_attrs(user.internal_id, effective=False)
    for attr in attrs:
        if ('name' in attr and attr['name'] not in attr_whitelist and
            attr['visibility'] == 'full'):
            if not dry_run:
                b2access.remove_entity_attr(user.internal_id, attr['name'])


def _schedule_user_removal(user):
    when = datetime.utcnow() + timedelta(days=config['retention_period'])
    if not dry_run:
        b2access.schedule_operation(user.internal_id, operation='REMOVE',
                                    when=when)


def _send_notification(users=[]):
    account_details = []
    for user in users:
        if user.shared_id is not None:
            account_details.append({'id': user.shared_id})
    if not account_details:
        return
    attachments = []
    attachment = {}
    attachment['filename'] = 'users.json'
    attachment['message'] = json.dumps(account_details, sort_keys=True,
                                       indent=4, separators=(',', ': '))
    attachments.append(attachment)
    if not dry_run:
        notifier.send(config['notifications']['email']['from'], 
                      config['notifications']['email']['to'],
                      config['notifications']['email']['subject'], 
                      config['notifications']['email']['intro_text'],
                      attachments)


if __name__ == "__main__":
    main()

@@ -1922,16 +1922,141 @@
 full'):%0A
+            logger.debug(%22removing attribute '%25s' from entity '%25s'%22,%0A                        attr%5B'name'%5D, user.internal_id)%0A
         
@@ -2067,32 +2067,32 @@
 if not dry_run:%0A
-
                 
@@ -2261,16 +2261,118 @@
 riod'%5D)%0A
+    logger.debug(%22scheduling removal of entity '%25s' at '%25s'%22,%0A                user.internal_id, when)%0A
     if n
@@ -2957,16 +2957,16 @@
  ': '))%0A
-
     atta
@@ -2992,16 +2992,363 @@
 chment)%0A
+    logger.debug(%22sending email notification from address '%25s' to '%25s' %22%0A        %22with subject '%25s' and attachment users.json:%5Cn%25s%22,%0A                config%5B'notifications'%5D%5B'email'%5D%5B'from'%5D,%0A                config%5B'notifications'%5D%5B'email'%5D%5B'to'%5D,%0A                config%5B'notifications'%5D%5B'email'%5D%5B'subject'%5D,%0A                attachment%5B'message'%5D)%0A
     if n

Add debug messages