forked from wrosenauer/oxcloud-provisioning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
forwarder.py
executable file
·121 lines (104 loc) · 4.96 KB
/
forwarder.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
115
116
117
118
119
120
121
#!/usr/bin/python3
# Copyright (C) 2022 OX Software GmbH
# Wolfgang Rosenauer
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import argparse
import requests
import settings
def main():
parser = argparse.ArgumentParser(
description='Manage OX Cloud mailforwarders.')
subparsers = parser.add_subparsers(title="subcommands",
description="valid subcommands",
required=True,
help="Subcommand help")
parser_create = subparsers.add_parser("create", help="Create a forwarder")
parser_create.add_argument(
"-c", "--context", help="Context ID or name the forward should be created in.", required=True)
parser_create.add_argument(
"-a", "--alias", help="The alias/forward address to be created.", required=True)
parser_create.add_argument(
"-t", "--target", help="The target address(es) (comma separted if multiple)", required=True)
parser_create.set_defaults(func=create)
parser_update = subparsers.add_parser("update", help="Update a forwarder")
parser_update.add_argument(
"-c", "--context", help="Context ID or name the forward should be created in.", required=True)
parser_update.add_argument(
"-a", "--alias", help="The alias/forward address to be created.", required=True)
parser_update.add_argument(
"-t", "--target", help="The target address(es) (comma separted if multiple)", required=True)
parser_update.set_defaults(func=update_target)
parser_delete = subparsers.add_parser("delete", help="Delete a forwarder")
parser_delete.add_argument(
"-c", "--context", help="Context ID or name", required=True)
parser_delete.add_argument(
"-a", "--alias", help="The alias/forward address to be deleted.")
parser_delete.add_argument(
"--all", help="Deletes all forwarders from context.", default=False, action='store_true')
parser_delete.set_defaults(func=delete)
parser_list = subparsers.add_parser("list", help="List forwarders")
parser_list.add_argument(
"-c", "--context", help="Context id or name", required=True)
parser_list.set_defaults(func=list)
args = parser.parse_args()
try:
cid = int(args.context)
args.context = cid
except:
args.context = settings.getCreds()["login"] + "_" + args.context
args.func(args)
def create(args):
data = args.target.split(",")
r = requests.post(settings.getRestHost()+"api/oxaas/v1/admin/forwards/"+str(
args.context)+"/"+str(args.alias), auth=(settings.getRestCreds()), json=data, verify=settings.getVerifyTls())
if r.status_code == 201:
print("Created forwarder", args.alias,
"to", args.target, "in context", args.context)
else:
if r.status_code == 204:
print("Forwarder already exists!")
else:
print("Failed to create forwarder!")
r.raise_for_status()
def update_target(args):
data = args.target.split(",")
r = requests.post(settings.getRestHost()+"api/oxaas/v1/admin/forwards/"+str(
args.context)+"/"+str(args.alias), auth=(settings.getRestCreds()), json=data, verify=settings.getVerifyTls())
if r.status_code == 201:
print("Updated forwarder", args.alias,
"to", args.target, "in context", args.context)
else:
print("Failed to update forwarder!")
r.raise_for_status()
def delete(args):
if args.alias is not None:
r = requests.delete(settings.getRestHost()+"api/oxaas/v1/admin/forwards/"+str(args.context)+"/"+str(args.alias),
auth=(settings.getRestCreds()), verify=settings.getVerifyTls())
else:
if args.all:
r = requests.delete(settings.getRestHost()+"api/oxaas/v1/admin/forwards/"+str(args.context),
auth=(settings.getRestCreds()), verify=settings.getVerifyTls())
if r.ok:
if args.all:
print("Deleted all forwarders from context")
else:
print("Deleted forwarder", args.alias)
else:
r.raise_for_status()
def list(args):
r = requests.get(settings.getRestHost()+"api/oxaas/v1/admin/forwards/"+str(args.context),
auth=(settings.getRestCreds()), verify=settings.getVerifyTls())
print(r.json())
if __name__ == "__main__":
main()