-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcreate_space_bank.py
More file actions
76 lines (60 loc) · 2.38 KB
/
Copy pathcreate_space_bank.py
File metadata and controls
76 lines (60 loc) · 2.38 KB
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
"""Make sure the bank (aka Space) named by OBP_ENTITY_SPACE_ID exists, creating it if not.
Bank level dynamic entities can only be created inside an existing bank. This
checks for the bank and, if it is missing, creates it with POST /obp/v6.0.0/banks,
which needs the Role CanCreateBank. With OBP_ENTITY_SPACE_ID empty (system level
entities) there is nothing to do.
Usage:
python3 create_space_bank.py [--full-name "OGCR ..."]
Exits 0 if the bank exists or was created (or no space is configured), 1 otherwise.
"""
import argparse
import sys
import requests
from obp_client import token, obp_host, session
from obp_space import SPACE_ID
def get_bank(bank_id):
"""Return the bank JSON, or None if it does not exist. Raises on other errors."""
response = session.get(f"{obp_host}/obp/v6.0.0/banks/{bank_id}",
headers={"Authorization": f"DirectLogin token={token}"}, timeout=30)
if response.status_code == 404:
return None
if response.status_code != 200:
raise RuntimeError(f"GET bank '{bank_id}' failed ({response.status_code}): {response.text}")
return response.json()
def create_bank(bank_id, full_name):
body = {
"bank_id": bank_id,
"bank_code": bank_id,
"full_name": full_name,
"logo": "",
"website": "",
"bank_routings": [{"scheme": "OBP", "address": bank_id}],
}
response = session.post(f"{obp_host}/obp/v6.0.0/banks", json=body,
headers={"Authorization": f"DirectLogin token={token}"}, timeout=30)
if response.status_code not in (200, 201):
raise RuntimeError(f"Creating bank '{bank_id}' failed ({response.status_code}): {response.text}")
return response.json()
def main():
parser = argparse.ArgumentParser(description="Create the OBP_ENTITY_SPACE_ID bank if it does not exist.")
parser.add_argument("--full-name", default=None,
help="Full name for a newly created bank (default: 'OGCR <bank id>')")
args = parser.parse_args()
if not SPACE_ID:
print("OBP_ENTITY_SPACE_ID is empty: entities are system level, no bank needed")
return 0
if not token:
print("✗ DirectLogin failed; cannot check the bank")
return 1
try:
if get_bank(SPACE_ID):
print(f"✓ Bank '{SPACE_ID}' already exists")
return 0
create_bank(SPACE_ID, args.full_name or f"OGCR {SPACE_ID}")
print(f"✓ Created bank '{SPACE_ID}'")
return 0
except (RuntimeError, requests.exceptions.RequestException) as e:
print(f"✗ {e}")
return 1
if __name__ == "__main__":
sys.exit(main())