Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to assign licence to user #573

Open
rg-jcastrillon opened this issue Sep 8, 2023 · 0 comments
Open

Unable to assign licence to user #573

rg-jcastrillon opened this issue Sep 8, 2023 · 0 comments
Assignees
Labels
Needs Attention 👋 question Further information is requested

Comments

@rg-jcastrillon
Copy link

Hello!

Following the example here I am trying to assing a licence to an user in Azure AD. I created the following dummy script to illustrate my problem:

package main

import (
	"context"
	"log"

	"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
	"github.com/google/uuid"
	msgraphsdk "github.com/microsoftgraph/msgraph-sdk-go"
	graphmodels "github.com/microsoftgraph/msgraph-sdk-go/models"
	graphusers "github.com/microsoftgraph/msgraph-sdk-go/users"
)

func main() {
	// For testing fill the values here
	tenantID := "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
	clientID := "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
	clientSecret := "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
	userID := "02a83423-br0c-4376-93a8-ed405936e0e6" // I made this up for creating the issue
	licenceSKUID := "e8b967fb-3c0c-4ba7-225e-f857999a9d30_f30db741-07e9-47e9-999c-80727f46fr8f" // This as well

	// Authenticate with Azure AD and create a Graph client
	cred, err := azidentity.NewClientSecretCredential(tenantID, clientID, clientSecret, nil)
	if err != nil {
		log.Fatal(err)
	}
	graphClient, err := msgraphsdk.NewGraphServiceClientWithCredentials(
		cred,
		[]string{"https://graph.microsoft.com/.default"},
	)
	if err != nil {
		log.Fatal(err)
	}

	// Look for the user to make sure the UUID is correct
	u, err := graphClient.Users().ByUserIdString(userID).Get(context.Background(), nil)
	if err != nil {
		log.Fatal(err)
	}
	username := *u.GetDisplayName()
	log.Printf("User %s found", username)

	// Look for the license to make sure the UUID is correct
	l, err := graphClient.SubscribedSkus().BySubscribedSkuIdString(licenceSKUID).Get(context.Background(), nil)
	if err != nil {
		log.Fatal(err)
	}
	skuID := l.GetSkuId()
	licenceName := *l.GetSkuPartNumber()
	log.Printf("License %s found", licenceName)
	log.Printf("Assigning license %s to user %s", licenceName, username)

	// Assign the license to the user according to the documentation
	requestBody := graphusers.NewItemAssignLicensePostRequestBody()

	assignedLicense := graphmodels.NewAssignedLicense()
	disabledPlans := []uuid.UUID{}
	assignedLicense.SetDisabledPlans(disabledPlans)
	assignedLicense.SetSkuId(skuID)
	addLicenses := []graphmodels.AssignedLicenseable{
		assignedLicense,
	}
	requestBody.SetAddLicenses(addLicenses)
	removeLicenses := []uuid.UUID{}
	requestBody.SetRemoveLicenses(removeLicenses)
	assignLicense, err := graphClient.Users().ByUserIdString(userID).AssignLicense().Post(context.Background(), requestBody, nil)
	if err != nil {
		log.Fatal(err)
	}
	log.Printf("%v", assignLicense.GetAssignedLicenses())
}

So to make sure I only pass existing IDs to the licence assignment code, I use the same graph client to get the objects before. After running it, the logs are:

2023/09/08 16:25:02 User Test User found
2023/09/08 16:25:02 License FLOW_FREE found
2023/09/08 16:25:02 Assigning license FLOW_FREE to user Test User
2023/09/08 16:25:03 error status code received from the API
exit status 1

I made sure the client being used has the appropriate permissions:

image

Given the very vague error message I can not really debug any further on the issue. I tried to use a debugger to look at the error but the values are strange:

err.(*odataerrors.ODataError).ApiError.Message = ""
err.(*odataerrors.ODataError).ApiError.ResponseStatusCode = 0
err.(*odataerrors.ODataError).ApiError.ResponseHeaders.header.headers = map[string]map[string]struct {} []

As a side note (maybe it its of importance). I am using the "long" version of the licence ID in the code (<TENANT_ID>_<SKU_ID>) because if I don't then this line

l, err := graphClient.SubscribedSkus().BySubscribedSkuIdString(licenceSKUID).Get(context.Background(), nil)

will be the one failing with a similar error (also empty message and status code 0)

I would appreciate some help, because a response code of 0 seems to be me like an indication of something bigger being wrong. It could also be that the documentation is not correct.

Versions

I am using the following library versions:

github.com/google/uuid v1.3.1
github.com/microsoftgraph/msgraph-sdk-go v1.17.0
@baywet baywet added question Further information is requested Needs Attention 👋 labels Sep 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Attention 👋 question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants