azure-devops-python-samples icon indicating copy to clipboard operation
azure-devops-python-samples copied to clipboard

Functionality to add a user to azure devops from AAD

Open AnjanaAK opened this issue 5 years ago • 11 comments

Is it possible to use azure.devops.v5_0.graph module? Can't find any sample that supports the functionality to create a new user from AAD to azure devops.

Do we have something like this in python? : https://github.com/microsoft/azure-devops-dotnet-samples/blob/master/ClientLibrary/Samples/Graph/UsersSample.cs

I am able to list the users. But cannot create a new user as 'GraphUserPrincipalNameContext' class is not available in the API.

AnjanaAK avatar Jun 18 '19 04:06 AnjanaAK

https://github.com/microsoft/azure-devops-python-api/blob/dev/azure-devops/azure/devops/v5_0/graph/graph_client.py ?

vtbassmatt avatar Jun 18 '19 13:06 vtbassmatt

https://github.com/microsoft/azure-devops-python-api/blob/dev/azure-devops/azure/devops/v5_0/graph/graph_client.py ?

I already referred to it. We can use the create_user function from that client. But the function asks for a <azure.devops.v5_0.graph.models.GraphUserCreationContext> object as input parameter.

But the microsoft documentation ( https://docs.microsoft.com/en-us/rest/api/azure/devops/graph/users/create?view=azure-devops-rest-5.0 ) says that "The body of the request must be a derived type of GraphUserCreationContext such as GraphUserMailAddressCreationContext, GraphUserOriginIdCreationContext , or GraphUserPrincipalNameCreationContext."

So i decided to use GraphUserPrincipalNameCreationContext. But such a class/interface is not available in python. I am confused that how should I use the create_user method. That's why I asked for a sample.

AnjanaAK avatar Jun 19 '19 04:06 AnjanaAK

Ah, sorry, thanks for the correction. @tedchamb @ramrajesh it looks like nothing in the Python API derives from GraphUserCreationContext. I don't know if this is a client gen problem or a graph API problem. Can one of you help?

vtbassmatt avatar Jun 19 '19 11:06 vtbassmatt

This is a duplicate to this issue: https://github.com/microsoft/azure-devops-python-api/issues/176

The generator does not know about the sub types, so does not generate them. We need to fix that. In the meantime you should be able to create your own sub types for these.

tedchamb avatar Jun 19 '19 13:06 tedchamb

This is a duplicate to this issue: microsoft/azure-devops-python-api#176

The generator does not know about the sub types, so does not generate them. We need to fix that. In the meantime you should be able to create your own sub types for these.

Do you mean that I should define a GraphUserPrincipalNameCreationContext class as a subclass of GraphUserCreationContext in my script itself as of now?

AnjanaAK avatar Jun 20 '19 09:06 AnjanaAK

Yes.

vtbassmatt avatar Jun 20 '19 10:06 vtbassmatt

@AnjanaAK I added this to my project:

class GraphUserPrincipalNameCreationContext(GraphUserCreationContext):

    _attribute_map = {
        'storage_key': {'key': 'storageKey', 'type': 'str'},
        'principal_name': {'key': 'principalName', 'type': 'str'}
    }

    def __init__(self, storage_key=None, principal_name=None):
        super(GraphUserPrincipalNameCreationContext, self).__init__()
        self.principal_name = principal_name

KaiWalter avatar Aug 01 '19 18:08 KaiWalter

@tedchamb do you have any indication when the generator is fixed or point me to where I could help to add this sub-classes to the generator

KaiWalter avatar Aug 01 '19 18:08 KaiWalter

@tedchamb Any updates on this, we are also impacted by this bug.

mo-saeed avatar Aug 11 '20 12:08 mo-saeed

@KaiWalter @vtbassmatt @tedchamb Could you please help me how to configure this missing class properly, I added it to my script and then used it like that:

 client = self.__get_connection().clients_v6_0.get_graph_client()
 creation_context = GraphUserPrincipalNameCreationContext(
            principal_name="GROUP_NAME"
        )
        try:
            group = client.create_group(
                creation_context=creation_context,
                scope_descriptor="DEV"
            )

but still getting the same error:

VS860014: Must have exactly one of originId, principalName or displayName set

mo-saeed avatar Aug 11 '20 12:08 mo-saeed

@mo-saeed can you please check my script https://github.com/KaiWalter/azure-devops-principalName-migration/blob/b5cdae45f47bcdcfb9c70e3c4e0d7fd2d59eb89e/modules/rebuild.py#L150 - I do it with an own definition of GraphUserOriginIdCreationContext https://github.com/KaiWalter/azure-devops-principalName-migration/blob/b5cdae45f47bcdcfb9c70e3c4e0d7fd2d59eb89e/modules/rebuild.py#L196

maybe this helps

KaiWalter avatar Aug 11 '20 14:08 KaiWalter