-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathreset-localhost-projects.py
114 lines (91 loc) · 3.35 KB
/
reset-localhost-projects.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import os
import argparse
from getpass import getpass
import requests
def main():
user = CommandLineParser().parse()
API(user).parse()
class API:
def __init__(self, user):
self.target = "http://localhost:8080/"
self.user = user
self.header = self._create_header()
def parse(self):
projects = self._parse_projects()
for project in projects:
if project != "lifecycle":
self._delete_project(project)
users = self._parse_users()
for user in users:
if user != self.user:
self._delete_user(user)
def _parse_projects(self):
projects = []
localhost_project_dict = self._get_projects()
for project in localhost_project_dict:
projects.append(project["name"])
return projects
def _parse_users(self):
users = []
localhost_user_dict = self._get_users()
for user in localhost_user_dict:
users.append(user["email"])
return users
@staticmethod
def _create_header():
token = getpass("Token: ")
return {"Authorization": "Bearer " + token}
def _delete_project(self, project_name):
response = requests.delete(f"{self.target}access/projects/{project_name}", headers=self.header)
response.raise_for_status()
def _delete_user(self, user_email):
response = requests.delete(f"{self.target}access/users/{user_email}", headers=self.header)
response.raise_for_status()
def _get_projects(self):
response = requests.get(f"{self.target}access/projects", headers=self.header).json()
return response
def _get_users(self):
response = requests.get(f"{self.target}access/users", headers=self.header).json()
return response
class CommandLineParser:
def __init__(self):
self.parser = CommandLineInterface()
def parse(self):
su = self._parse_su()
return su
def _parse_su(self):
return self.parser.get_argument("user")
class CommandLineInterface:
def __init__(self):
parser = self._create_argument_parser()
self.arguments = parser.parse_args()
@staticmethod
def _create_argument_parser():
"""
Creation of the Command Line Arguments, and whenever they should be displayed as
"required" or not.
"""
parser = argparse.ArgumentParser(
prog="Reset localhost projects and users",
description="Utilitarian script for developers that want to reset their localhost instance to just Project "
"'Lifecycle' and a singular user if they supply -u. Does not affect Profiles."
)
required = parser.add_argument_group("Required arguments")
required.add_argument(
"-u",
"--user",
type=str,
required=True,
help="The single user that is registered on the localhost that needs to remain."
)
return parser
def get_argument(self, argument_key):
"""
Small getter function for CLI, including some error handling.
"""
if argument_key in self.arguments:
return getattr(self.arguments, argument_key)
else:
raise KeyError(f"Argument {argument_key} invalid, not found in CLI arguments.")
if __name__ == "__main__":
main()