@@ -41,55 +41,59 @@ class ManagedIdentity(UserDict):
4141 }
4242
4343 @classmethod
44- def system_assigned (cls ):
45- """Construct a system-assigned managed identity.
46-
47- The outcome is equivalent to::
48-
49- {"ManagedIdentityIdType": "SystemAssigned", "Id": None}
50- """
51- return ManagedIdentity (id_type = cls .SYSTEM_ASSIGNED )
44+ def is_managed_identity (cls , unknown ):
45+ return isinstance (unknown , dict ) and cls .ID_TYPE in unknown
5246
5347 @classmethod
5448 def is_system_assigned (cls , unknown ):
5549 return isinstance (unknown , dict ) and unknown .get (cls .ID_TYPE ) == cls .SYSTEM_ASSIGNED
5650
5751 @classmethod
58- def user_assigned_client_id (cls , identifier ):
59- """Construct a ``ManagedIdentity`` instance from a user-assigned client id.
52+ def is_user_assigned (cls , unknown ):
53+ return (
54+ isinstance (unknown , dict )
55+ and unknown .get (cls .ID_TYPE ) in cls ._types_mapping
56+ and unknown .get (cls .ID ))
6057
61- The outcome is equivalent to::
58+ def __init__ (self , identifier = None , id_type = None ):
59+ # Undocumented. Use subclasses instead.
60+ super (ManagedIdentity , self ).__init__ ({
61+ self .ID_TYPE : id_type ,
62+ self .ID : identifier ,
63+ })
6264
63- {"ManagedIdentityIdType": "ClientId", "Id": "foo"}
64- """
65- return ManagedIdentity (identifier = identifier , id_type = cls .CLIENT_ID )
6665
67- @classmethod
68- def user_assigned_resource_id (cls , identifier ):
69- """Construct a ``ManagedIdentity`` instance from a user-assigned resource id.
66+ class SystemAssignedManagedIdentity (ManagedIdentity ):
67+ """Construct a system-assigned managed identity, which is equivalent to:
68+ ``{"ManagedIdentityIdType": "SystemAssigned", "Id": None}``
69+ """
70+ def __init__ (self ):
71+ super (SystemAssignedManagedIdentity , self ).__init__ (id_type = self .SYSTEM_ASSIGNED )
7072
71- The outcome is equivalent to::
7273
73- {"ManagedIdentityIdType": "ResourceId", "Id": "foo"}
74- """
75- return ManagedIdentity (identifier = identifier , id_type = cls .RESOURCE_ID )
76-
77- @classmethod
78- def user_assigned_object_id (cls , identifier ):
79- """Construct a ManagedIdentity instance from a user-assigned object id.
74+ class UserAssignedManagedIdentity (ManagedIdentity ):
75+ def __init__ (self , client_id = None , resource_id = None , object_id = None ):
76+ """Construct a user-assigned managed identity.
8077
81- The outcome will be equivalent to::
78+ Depends on the id you provided, the outcome is equivalent to one of below ::
8279
80+ {"ManagedIdentityIdType": "ClientId", "Id": "foo"}
81+ {"ManagedIdentityIdType": "ResourceId", "Id": "foo"}
8382 {"ManagedIdentityIdType": "ObjectId", "Id": "foo"}
8483 """
85- return ManagedIdentity (identifier = identifier , id_type = cls .OBJECT_ID )
86-
87- def __init__ (self , identifier = None , id_type = None ):
88- # Undocumented. Use other class methods instead.
89- super (ManagedIdentity , self ).__init__ ({
90- self .ID_TYPE : id_type ,
91- self .ID : identifier ,
92- })
84+ if client_id and not resource_id and not object_id :
85+ super (UserAssignedManagedIdentity , self ).__init__ (
86+ id_type = self .CLIENT_ID , identifier = client_id )
87+ elif not client_id and resource_id and not object_id :
88+ super (UserAssignedManagedIdentity , self ).__init__ (
89+ id_type = self .RESOURCE_ID , identifier = resource_id )
90+ elif not client_id and not resource_id and object_id :
91+ super (UserAssignedManagedIdentity , self ).__init__ (
92+ id_type = self .OBJECT_ID , identifier = object_id )
93+ else :
94+ raise ValueError (
95+ "You shall specify one of the three parameters: "
96+ "client_id, resource_id, object_id" )
9397
9498
9599def _scope_to_resource (scope ): # This is an experimental reasonable-effort approach
@@ -268,7 +272,7 @@ def __init__(self, http_client, managed_identity, token_cache=None):
268272 import msal, requests
269273 client = msal.ManagedIdentityClient(
270274 requests.Session(),
271- msal.UserAssignedManagedIdentity.from_client_id( "foo"),
275+ msal.UserAssignedManagedIdentity(client_id= "foo"),
272276 )
273277
274278 Recipe: Write once, run everywhere.
0 commit comments