Skip to content

Commit 4a3ed9c

Browse files
author
Alan Christie
committed
feat: Use of client Environment
Used client 1.13.3
1 parent 42ce511 commit 4a3ed9c

File tree

9 files changed

+58
-98
lines changed

9 files changed

+58
-98
lines changed

README.md

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,28 @@ set of examples so that users can create their own utilities.
1313
> Most tools need to be executed by a user with admin privileges.
1414
1515
## Usage
16-
You need to create a number of environment variables that
17-
control authentication and destination hosts. The template shell-script
18-
`setenv-template.sh` is a guide to what you need. Copy this to `setenv.sh`,
19-
edit it, and use that to conveniently set many of the variables for the tools: -
16+
The tools utilise the Pyhon client's `Environment` module, which expects
17+
you to create an `Envrionments` file - a YAML file that defines the
18+
variables used to connect to the corresponding installation. The environments
19+
file (typically `~/.squonk2/environmemnts`) allows you to creat variables
20+
for multiple installations identified by name.
2021

21-
source setenv.sh
22+
See the **Environment module** section of the [Squonk2 Python Client].
2223

2324
Using a python 3 virtual environment install the project requirements
2425
and then run the appropriate tool: -
2526

2627
pip install --upgrade pip
2728
pip install -r requirements.txt
2829

29-
./tools/delete-test-projects.py
30+
./tools/delete-test-projects.py dls-test
3031

3132
As a general style any tools that have destructive actions rely on the use of
3233
a `--do-it` option to prevent any accidental damage. Therefore, to _actually_
3334
delete Data Manager projects add `--do-it` to the `delete-test-projects`
3435
command: -
3536

36-
./tools/delete-test-projects.py --do-it
37+
./tools/delete-test-projects.py dls-test --do-it
3738

3839
All test tools use `argparse` so adding `--help` to the command will
3940
display the tool's help.
@@ -45,6 +46,8 @@ You should find the following tools in this repository: -
4546
- `delete-all-instances`
4647
- `delete-old-instances`
4748
- `delete-test-projects`
49+
- `load-er`
50+
- `save-er`
4851

4952
---
5053

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
im-squonk2-client >= 1.9.3, < 2.0.0
1+
im-squonk2-client >= 1.13.3, < 2.0.0
22
python-dateutil == 2.8.2
33
rich == 12.5.1
44
pyyaml == 6.0

tools/coins.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212
from rich.console import Console
1313
from squonk2.auth import Auth
1414
from squonk2.as_api import AsApi, AsApiRv
15-
16-
from common import Env, get_env
15+
from squonk2.environment import Environment
1716

1817
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
1918

@@ -26,16 +25,16 @@ def main(c_args: argparse.Namespace) -> None:
2625

2726
console = Console()
2827

29-
env: Optional[Env] = get_env()
30-
if not env:
31-
return
28+
_ = Environment.load()
29+
env: Environment = Environment(c_args.environment)
30+
AsApi.set_api_url(env.as_api)
3231

3332
token: str = Auth.get_access_token(
3433
keycloak_url=env.keycloak_url,
3534
keycloak_realm=env.keycloak_realm,
3635
keycloak_client_id=env.keycloak_as_client_id,
37-
username=env.keycloak_user,
38-
password=env.keycloak_user_password,
36+
username=env.admin_user,
37+
password=env.admin_password,
3938
)
4039

4140
# Get the product details.
@@ -187,6 +186,7 @@ def _calculate_adjusted_coins(total_coins: Decimal,
187186
prog="coins",
188187
description="Calculates a Product's Coin Charges (actual and predicted)"
189188
)
189+
parser.add_argument('environment', type=str, help='The environment name')
190190
parser.add_argument('product', type=str, help='The Product UUID')
191191
parser.add_argument(
192192
'--verbose',

tools/common.py

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -15,48 +15,3 @@
1515
"dmit-user-d",
1616
"dmit-user-admin",
1717
]
18-
19-
# A namedtuple for the environment.
20-
# It contains vales, extracted from the following variables: -
21-
# - SQUONK2_DMAPI_URL
22-
# - SQUONK2_ASAPI_URL
23-
# - SQUONK2_KEYCLOAK_URL
24-
# - SQUONK2_KEYCLOAK_REALM
25-
# - SQUONK2_KEYCLOAK_AS_CLIENT_ID
26-
# - SQUONK2_KEYCLOAK_DM_CLIENT_ID
27-
# - SQUONK2_KEYCLOAK_USER
28-
# - SQUONK2_KEYCLOAK_USER_PASSWORD
29-
Env: namedtuple = namedtuple(
30-
"Env",
31-
[
32-
"dmapi_url",
33-
"asapi_url",
34-
"keycloak_url",
35-
"keycloak_realm",
36-
"keycloak_as_client_id",
37-
"keycloak_dm_client_id",
38-
"keycloak_user",
39-
"keycloak_user_password",
40-
],
41-
)
42-
43-
44-
def get_env() -> Optional[Env]:
45-
"""Get the environment variables.
46-
All must be defined.
47-
"""
48-
env: Optional[Env] = None
49-
try:
50-
env: Env = Env(
51-
dmapi_url=os.environ["SQUONK2_DMAPI_URL"],
52-
asapi_url=os.environ["SQUONK2_ASAPI_URL"],
53-
keycloak_url=os.environ["SQUONK2_KEYCLOAK_URL"],
54-
keycloak_realm=os.environ["SQUONK2_KEYCLOAK_REALM"],
55-
keycloak_as_client_id=os.environ["SQUONK2_KEYCLOAK_AS_CLIENT_ID"],
56-
keycloak_dm_client_id=os.environ["SQUONK2_KEYCLOAK_DM_CLIENT_ID"],
57-
keycloak_user=os.environ["SQUONK2_KEYCLOAK_USER"],
58-
keycloak_user_password=os.environ["SQUONK2_KEYCLOAK_USER_PASSWORD"],
59-
)
60-
except KeyError as e:
61-
print(f"ERROR: You need to define {e}")
62-
return env

tools/delete-all-instances.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,25 @@
1414
from typing import Dict, List, Optional, Tuple
1515
import urllib3
1616

17-
from common import Env, get_env
18-
1917
from squonk2.auth import Auth
2018
from squonk2.dm_api import DmApi, DmApiRv
19+
from squonk2.environment import Environment
2120

2221
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
2322

2423

2524
def main(c_args: argparse.Namespace) -> None:
2625

27-
env: Optional[Env] = get_env()
28-
if not env:
29-
return
26+
_ = Environment.load()
27+
env: Environment = Environment(c_args.environment)
28+
DmApi.set_api_url(env.dm_api)
3029

3130
token: str = Auth.get_access_token(
3231
keycloak_url=env.keycloak_url,
3332
keycloak_realm=env.keycloak_realm,
3433
keycloak_client_id=env.keycloak_dm_client_id,
35-
username=env.keycloak_user,
36-
password=env.keycloak_user_password,
34+
username=env.admin_user,
35+
password=env.admin_password,
3736
)
3837

3938
# The collection of instances
@@ -94,6 +93,7 @@ def main(c_args: argparse.Namespace) -> None:
9493
# Build a command-line parser and parse it...
9594
parser = argparse.ArgumentParser(
9695
description='Delete All DM Project Instances')
96+
parser.add_argument('environment', type=str, help='The environment name')
9797
parser.add_argument(
9898
'--do-it',
9999
help='Set to actually delete, if not set the instances are listed',

tools/delete-old-instances.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,23 @@
1515
from dateutil.parser import parse
1616
from squonk2.auth import Auth
1717
from squonk2.dm_api import DmApi, DmApiRv
18-
19-
from common import Env, get_env
18+
from squonk2.environment import Environment
2019

2120
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
2221

2322

2423
def main(c_args: argparse.Namespace) -> None:
2524

26-
env: Optional[Env] = get_env()
27-
if not env:
28-
return
25+
_ = Environment.load()
26+
env: Environment = Environment(c_args.environment)
27+
DmApi.set_api_url(env.dm_api)
2928

3029
token: str = Auth.get_access_token(
3130
keycloak_url=env.keycloak_url,
3231
keycloak_realm=env.keycloak_realm,
3332
keycloak_client_id=env.keycloak_dm_client_id,
34-
username=env.keycloak_user,
35-
password=env.keycloak_user_password,
33+
username=env.admin_user,
34+
password=env.admin_password,
3635
)
3736

3837
# To see everything we need to become admin...
@@ -88,6 +87,7 @@ def main(c_args: argparse.Namespace) -> None:
8887
# Build a command-line parser and parse it...
8988
parser = argparse.ArgumentParser(
9089
description='Delete All Old DM Project Instances')
90+
parser.add_argument('environment', type=str, help='The environment name')
9191
parser.add_argument(
9292
'--age',
9393
nargs='?',

tools/delete-test-projects.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,27 @@
77
from typing import Any, Dict, List, Optional
88
import urllib3
99

10-
from common import Env, get_env, TEST_UNIT, TEST_USER_NAMES
11-
1210
from squonk2.auth import Auth
1311
from squonk2.dm_api import DmApi, DmApiRv
12+
from squonk2.environment import Environment
13+
14+
from common import TEST_UNIT, TEST_USER_NAMES
1415

1516
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
1617

1718

1819
def main(c_args: argparse.Namespace) -> None:
19-
"""Main function."""
20-
env: Optional[Env] = get_env()
21-
if not env:
22-
return
20+
21+
_ = Environment.load()
22+
env: Environment = Environment(c_args.environment)
23+
DmApi.set_api_url(env.dm_api)
2324

2425
token: str = Auth.get_access_token(
2526
keycloak_url=env.keycloak_url,
2627
keycloak_realm=env.keycloak_realm,
2728
keycloak_client_id=env.keycloak_dm_client_id,
28-
username=env.keycloak_user,
29-
password=env.keycloak_user_password,
29+
username=env.admin_user,
30+
password=env.admin_password,
3031
)
3132

3233
ret_val: DmApiRv = DmApi.get_available_projects(token)
@@ -85,6 +86,7 @@ def main(c_args: argparse.Namespace) -> None:
8586
parser = argparse.ArgumentParser(
8687
description="Delete all Projects owned by test users"
8788
)
89+
parser.add_argument('environment', type=str, help='The environment name')
8890
parser.add_argument(
8991
"--do-it",
9092
help="Set to actually delete, if not set the projects are listed",

tools/load-er.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@
1010
from rich.console import Console
1111
from squonk2.auth import Auth
1212
from squonk2.dm_api import DmApi, DmApiRv
13+
from squonk2.environment import Environment
1314
import yaml
1415

15-
from common import Env, get_env
16-
1716
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
1817

1918

@@ -22,16 +21,16 @@ def main(c_args: argparse.Namespace) -> None:
2221

2322
console = Console()
2423

25-
env: Optional[Env] = get_env()
26-
if not env:
27-
return
24+
_ = Environment.load()
25+
env: Environment = Environment(c_args.environment)
26+
DmApi.set_api_url(env.dm_api)
2827

2928
token: str = Auth.get_access_token(
3029
keycloak_url=env.keycloak_url,
3130
keycloak_realm=env.keycloak_realm,
3231
keycloak_client_id=env.keycloak_dm_client_id,
33-
username=env.keycloak_user,
34-
password=env.keycloak_user_password,
32+
username=env.admin_user,
33+
password=env.admin_password,
3534
)
3635

3736
# Just read the list from the chosen file
@@ -56,6 +55,7 @@ def main(c_args: argparse.Namespace) -> None:
5655
prog="load-er",
5756
description="Loads exchange rates (from a YAML file)"
5857
)
58+
parser.add_argument('environment', type=str, help='The environment name')
5959
parser.add_argument('file', type=str, help='The source file')
6060
args: argparse.Namespace = parser.parse_args()
6161

tools/save-er.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,34 @@
55
import argparse
66
from pathlib import Path
77
import sys
8-
from typing import Optional
98
import urllib3
109

1110
from rich.console import Console
1211
from squonk2.auth import Auth
1312
from squonk2.dm_api import DmApi, DmApiRv
13+
from squonk2.environment import Environment
1414
import yaml
1515

16-
from common import Env, get_env
17-
1816
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
1917

2018

2119
def main(c_args: argparse.Namespace) -> None:
2220
"""Main function."""
2321

24-
env: Optional[Env] = get_env()
25-
if not env:
26-
return
27-
2822
console = Console()
2923

24+
_ = Environment.load()
25+
env: Environment = Environment(c_args.environment)
26+
DmApi.set_api_url(env.dm_api)
27+
3028
token: str = Auth.get_access_token(
3129
keycloak_url=env.keycloak_url,
3230
keycloak_realm=env.keycloak_realm,
3331
keycloak_client_id=env.keycloak_dm_client_id,
34-
username=env.keycloak_user,
35-
password=env.keycloak_user_password,
32+
username=env.admin_user,
33+
password=env.admin_password,
3634
)
35+
console.log(token)
3736

3837
er_rv: DmApiRv = DmApi.get_job_exchange_rates(token)
3938
if not er_rv.success:
@@ -75,6 +74,7 @@ def main(c_args: argparse.Namespace) -> None:
7574
prog="save-er",
7675
description="Saves existing exchange rates (to a YAML file)"
7776
)
77+
parser.add_argument('environment', type=str, help='The environment name')
7878
parser.add_argument('file', type=str, help='The destination file')
7979
args: argparse.Namespace = parser.parse_args()
8080

0 commit comments

Comments
 (0)