Skip to content

Commit 5ad1b79

Browse files
authored
Merge pull request #164 from natthan-pigoux/fix_init_iam_job
fix: change pydantic validator and root model
2 parents fa060ae + 371338f commit 5ad1b79

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

diracx/templates/tests/indigo-iam/init-iam/_init-iam.py.tpl

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import logging
66
import yaml
77

88
from typing import List, Dict, Optional
9-
from pydantic import BaseModel, parse_obj_as, validator
9+
from pydantic import BaseModel, field_validator, RootModel
1010

1111

1212
class User(BaseModel):
@@ -16,15 +16,17 @@ class User(BaseModel):
1616
given_name: Optional[str] = None
1717
family_name: Optional[str] = None
1818

19-
@validator('given_name', pre=True, always=True)
19+
@field_validator('given_name', mode='before')
20+
@classmethod
2021
def set_given_name(cls, v, values):
2122
# Use the username to set the given_name if it's not provided
22-
if v is None and 'username' in values:
23-
return values['username'].capitalize()
23+
if v is None and values.data and 'username' in values.data:
24+
return values.data['username'].capitalize()
2425
return v
2526

26-
@validator('family_name', pre=True, always=True)
27-
def set_family_name(cls, v, values):
27+
@field_validator('family_name', mode='before')
28+
@classmethod
29+
def set_family_name(cls, v):
2830
# Set the family_name to an empty string if it's not provided
2931
return v or ""
3032

@@ -38,10 +40,11 @@ class Client(BaseModel):
3840
scope: Optional[List[str]] = []
3941
redirect_uris: Optional[List[str]] = []
4042

41-
@validator('redirect_uris', pre=True, always=True)
43+
@field_validator('redirect_uris', mode='before')
44+
@classmethod
4245
def set_redirect_uris(cls, v, values):
4346
# redirect_uris is required if grant_types contains "authorization_code" or "device_code"
44-
grant_types = values.get('grant_types', [])
47+
grant_types = values.data.get('grant_types', []) if values.data else []
4548
if not v and ('authorization_code' in grant_types or \
4649
'urn:ietf:params:oauth:grant-type:device_code' in grant_types):
4750
raise ValueError("redirect_uris is required")
@@ -55,9 +58,8 @@ class InitialClient(Client):
5558
scope: Optional[List[str]] = ["scim:read", "scim:write", "iam:admin.read", "iam:admin.write"]
5659

5760

58-
class Group(BaseModel):
61+
class Group(RootModel[Dict[str, List[str]]]):
5962
"""Group to create."""
60-
__root__: Dict[str, List[str]]
6163

6264

6365
class Config(BaseModel):
@@ -76,7 +78,7 @@ def prepare_iam_instance(config_path):
7678
# Load and parse the configuration using Pydantic
7779
with open(config_path, 'r') as file:
7880
config_data = yaml.safe_load(file)
79-
config = parse_obj_as(Config, config_data)
81+
config = Config.model_validate(config_data)
8082
except FileNotFoundError:
8183
logging.error("Config file not found")
8284
raise RuntimeError("Config file not found")
@@ -126,7 +128,7 @@ def prepare_iam_instance(config_path):
126128
group_id = group_config["id"]
127129

128130
# Subgroups
129-
for subgroup_name, users in group_details.__root__.items():
131+
for subgroup_name, users in group_details.root.items():
130132
subgroup_config = _create_iam_subgroup(issuer, admin_access_token, group_name, group_id, subgroup_name)
131133
subgroup_id = subgroup_config["id"]
132134

0 commit comments

Comments
 (0)