-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremove_confluence-users_from_guests.py
executable file
·72 lines (52 loc) · 1.99 KB
/
remove_confluence-users_from_guests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env python
import sys
import requests
from requests.auth import HTTPBasicAuth
import json
import yaml
import pdb
from pprint import pprint
from Confluence_apis import Confluence_cloud_api
import logging
# configure logging
logging.basicConfig(
level=logging.INFO,
format="[%(asctime)s] %(levelname)s\t[%(name)s.%(funcName)s:%(lineno)d] %(message)s",
datefmt="%d/%b/%Y %H:%M:%S",
stream=sys.stdout)
# user help message
usage = f"Usage: python3 {sys.argv[0]} <atlassian config yaml file> [<groups to convert, comma separated>]"
# get the arguments
try:
logging.debug("Fetching config filename.")
atlassian_config_filename = sys.argv[1]
except IndexError:
print(f"{usage}\n\nERROR: Atlassian config file argument missing")
sys.exit()
try:
logging.debug("Fetching group filter list")
group_filter_list = sys.argv[2].split(',')
except IndexError:
group_filter_list = []
# read the atlassian config file
logging.debug("Reading config file.")
with open(atlassian_config_filename, 'r') as file:
try:
config = yaml.safe_load(file)
except yaml.YAMLError as exc:
print(exc)
# create confluence api instance
logging.debug("Creating confluence api object")
confluence = Confluence_cloud_api(config)
# get groups
groups = confluence.get_groups()
# find the guest group
guest_group_id = [ group['id'] for group in groups if group['name'].startswith('confluence-guests-') ][0]
users_group_id = [ group['id'] for group in groups if group['name'] == 'confluence-users' ][0]
# get the guest group members
users = confluence.get_group_members(guest_group_id)
# for each member, make sure they are not a member of the confluence-users group as well
for i,user in enumerate(users):
print(f"{i}/{len(users)}\t{user['publicName']}")
confluence.remove_user_from_group(user['accountId'], users_group_id)
logging.info(f"Finished")