I have the following classes:
class ResourceType(str, Enum):
MANAGEMENT_GROUP = "Microsoft.Management/managementGroups"
SUBSCRIPTION = "/subscriptions"
RESOURCE_GROUP = "Microsoft.Resources/resourceGroups"
@dataclass_json
@dataclass
class Resource:
id: str = None
displayName: Optional[str] = None
name: str = None
type: str = None
custom_role_definitions: List[RBACRole] = field(default_factory=list)
children: Optional[List['Resource']] = field(default_factory=list)
@dataclass_json
@dataclass
class ResourceGroup(Resource):
type: ResourceType = field(default=ResourceType.RESOURCE_GROUP)
location: str = None
tags: Optional[Dict[str, str]] = field(default_factory=dict)
properties: Optional[Dict[str, str]] = field(default_factory=dict)
@dataclass_json
@dataclass
class Subscription(Resource):
type: ResourceType = field(default=ResourceType.SUBSCRIPTION)
@dataclass_json
@dataclass
class ManagementGroup(Resource):
type: ResourceType = field(default=ResourceType.MANAGEMENT_GROUP)
The problem is with the nesting. When I’m reading a json file I’m losing all the children of type ResourceGroup fields (location, tags and properties). Also, I have problem when using ManagementGroup().from_dict(dict_data) since all the children are considered Resource.
I’ve tried using:
def __post_init__(self):
super().__post_init__()
if self.children:
for i, child in enumerate(self.children):
if child.type == ResourceType.SUBSCRIPTION:
self.children[i] = Subscription(**child.to_dict())
elif child.type == ResourceType.MANAGEMENT_GROUP:
self.children[i] = ManagementGroup(**child.to_dict())