microsoftgraph / msgraph-sdk-go

Microsoft Graph SDK for Go
https://docs.microsoft.com/en-us/graph/sdks/sdks-overview
MIT License
245 stars 38 forks source link

Trying to add member to group #197

Closed amarbut24 closed 2 years ago

amarbut24 commented 2 years ago

Hello,

Using this example to attempt to add a member to an azure ad group.

I have this bit of code

graphClient := msgraphsdk.NewGraphServiceClient(requestAdapter)

requestBody := msgraphsdk.New()
requestBody.SetAdditionalData(map[string]interface{}{
    "@odata.id": "https://graph.microsoft.com/v1.0/directoryObjects/<userid>",
}
groupId := "<objectid for group>"
add, err := graphClient .GroupsById(&groupId).Members().Ref().Post(requestBody)

I get below error

Exception has occurred: panic
"interface conversion: interface is nil, not ref.Refable"
Stack:
    3  0x0000000000c73d37 in github.com/microsoftgraph/msgraph-sdk-go/groups/item/members/ref.(*RefRequestBuilder).PostWithRequestConfigurationAndResponseHandler
        at /home/anthony/go/pkg/mod/github.com/microsoftgraph/msgraph-sdk-go@v0.25.0/groups/item/members/ref/ref_request_builder.go:142
    4  0x0000000000c73977 in github.com/microsoftgraph/msgraph-sdk-go/groups/item/members/ref.(*RefRequestBuilder).Post
        at /home/anthony/go/pkg/mod/github.com/microsoftgraph/msgraph-sdk-go@v0.25.0/groups/item/members/ref/ref_request_builder.go:126

It seems to be this line specifically that is panicking https://github.com/microsoftgraph/msgraph-sdk-go/blob/main/groups/item/members/ref/ref_request_builder.go#L144

When debugging things I notice that the value for res Here is github.com/microsoft/kiota-abstractions-go/serialization.Parsable nil which I believe explains the error I'm getting

Its possible I'm doing something horrible wrong, namely the add line add, err := graphClient .GroupsById(&groupId).Members().Ref().Post(requestBody) Am I using the .Ref() portion properly?

Oddly enough the user is being added to the group successfully. So the add is working minus the fatal panic 😄

baywet commented 2 years ago

Hi @amarbut24 , Thanks for reaching out and for using the Go SDK. Please have a look at this answer and let us know if you have any questions.

amarbut24 commented 2 years ago

Hey @baywet

Thanks for the super quick response and all the work on this SDK. Apologies for not catching the other post/answer.

I've given it a try and am running into a different error

Code

graphClient.GroupsById(group.ObjectID).Members().Post(reference)

Error (this is presenting itself in VSCode)


graphClient.GroupsById(group.ObjectID).Members().Post undefined (type *"github.com/microsoftgraph/msgraph-sdk-go/groups/item/members".MembersRequestBuilder has no field or method Post)

I made sure I ran go get github.com/microsoftgraph/msgraph-sdk-go@v0.26.0 and updated my .mod file

require (
    github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.1.0
    github.com/microsoft/kiota-authentication-azure-go v0.3.1
    github.com/microsoftgraph/msgraph-sdk-go v0.26.0
    github.com/microsoftgraph/msgraph-sdk-go-core v0.26.1
)
amarbut24 commented 2 years ago

Hey @baywet

Please disregard my previous comment. I mistakenly read your comment in the previous thread and wasn't including the .Ref() method in my call

Everything is working now

amarbut24 commented 2 years ago

I take that back 😞

If I use r, err := graphClient.GroupsById(group.ObjectID).Members().Ref().Post(reference) I get the interface error

If I try and use r, err := graphClient.GroupsById(group.ObjectID).Members().Post(reference) I'm told that graphClient.GroupsById(group.ObjectID).Members().Post undefined (type *"github.com/microsoftgraph/msgraph-sdk-go/groups/item/members".MembersRequestBuilder has no field or method Post)

Sorry if I'm doing something dumb

abjoseph commented 2 years ago

@baywet despite #155 been closed and marked as resolved this is still very much an issue and not working as of release 0.26.0

To recap, I have the following code (similar to @amarbut24) and I continue to get the error below:

    requestBody := ref.NewRef()
    oDataLink := fmt.Sprintf("https://graph.microsoft.com/v1.0/directoryObjects/%s", userId)
    requestBody.GetAdditionalData()["@odata.id"] = oDataLink

    _, err := graphClient.GroupsById(group.Id).Members().Ref().Post(requestBody)

where ref.NewRef() is imported from "github.com/microsoftgraph/msgraph-sdk-go/groups/item/members/ref"

Error from invoking the above code snippet:

panic: interface conversion: interface is nil, not ref.Refable

goroutine 1 [running]:
github.com/microsoftgraph/msgraph-sdk-go/groups/item/members/ref.(*RefRequestBuilder).PostWithRequestConfigurationAndResponseHandler(0xc00055a5e8, {0x37e3b48?, 0xc000006058?}, 0xa?, 0x2?)
        C:/Users/bjoseph/go/pkg/mod/github.com/microsoftgraph/msgraph-sdk-go@v0.26.0/groups/item/members/ref/ref_request_builder.go:144 +0x11e
github.com/microsoftgraph/msgraph-sdk-go/groups/item/members/ref.(*RefRequestBuilder).Post(...)
        C:/Users/bjoseph/go/pkg/mod/github.com/microsoftgraph/msgraph-sdk-go@v0.26.0/groups/item/members/ref/ref_request_builder.go:128
abjoseph commented 2 years ago

Update:

I also tried the workaround of using the RequestBuilder directly and that also failed with a go panic error, code snippet and error below:

    url := fmt.Sprintf("https://graph.microsoft.com/v1.0/groups/%s/members/$ref", group.Id)
    requestBuilder := groups.NewGroupsRequestBuilder(url, graphClient.requestAdapter)
    objectToAdd := models.NewGroup()
    objectToAdd.GetAdditionalData()["@odata.id"] = oDataLink
    _, err := requestBuilder.Post(objectToAdd)

Error from invoking the above code snippet:

panic: interface conversion: interface is nil, not models.Groupable

goroutine 1 [running]:
github.com/microsoftgraph/msgraph-sdk-go/groups.(*GroupsRequestBuilder).PostWithRequestConfigurationAndResponseHandler(0xc00081e5e8, {0x398aed0?, 0xc000261400?}, 0x9?, 0x1?)
        C:/Users/bjoseph/go/pkg/mod/github.com/microsoftgraph/msgraph-sdk-go@v0.26.0/groups/groups_request_builder.go:169 +0x11e
github.com/microsoftgraph/msgraph-sdk-go/groups.(*GroupsRequestBuilder).Post(...)
        C:/Users/bjoseph/go/pkg/mod/github.com/microsoftgraph/msgraph-sdk-go@v0.26.0/groups/groups_request_builder.go:153
abjoseph commented 2 years ago

#https://github.com/microsoftgraph/msgraph-sdk-go/issues/155#issuecomment-1156264835

abjoseph commented 2 years ago

@baywet Is it possible to get an ETA on this issue and #155, which I believe are both affected by the same underlying bug. This is an Active blocker preventing us from making further progress.

baywet commented 2 years ago

Hey everyone, Thanks for your patience on this matter, I was out all of last week being sick.

I've provided a reply to #155 here for the details. I'll circle back with the dev who owns the conversion library to get things pushed forward. No ETA to share at the moment.

amarbut24 commented 2 years ago

Hey @baywet

thanks for the follow up and I hope you are feeling better. I've gone ahead and subscribed to https://github.com/microsoft/OpenAPI.NET.OData/issues/228 in the meantime

abjoseph commented 2 years ago

Thanks for the update @baywet, and hope you're feeling better. I will also keep an eye on the progress of https://github.com/microsoft/OpenAPI.NET.OData/issues/228

abjoseph commented 2 years ago

@baywet This here is good news right?

baywet commented 2 years ago

yep, it's progress in the right direction. Now the conversion library still needs to be released, then hidi, the tool we're using for the conversion needs to grab this new release, and get released itself. And finally we'll be able to generate a new version of the Go SDK, which should include those fixes. Hopefully by next Tuesday, otherwise by the next one.

amarbut24 commented 2 years ago

🎉

baywet commented 2 years ago

Hi everyone! Thanks for your patience on this one.

211 fixed that issue and was just released a couple of minutes ago as v0.29.0

As you can see the post operation on the ref node under members now accepts a proper model. https://github.com/microsoftgraph/msgraph-sdk-go/blob/32e95517554481b1846846df8198fdab79909f76/groups/item/members/ref/ref_request_builder.go#L126

Let us know if you need anything else.

amarbut24 commented 2 years ago

awesome! Thanks for your help @baywet Going to do some testing this weekend and let you know if I notice anything

abjoseph commented 2 years ago

@baywet , I hope I'm just missing the obvious but I can't seem to find the method to create a struct of type ReferenceCreateable to pass to the Post method...can you help me out here.

baywet commented 2 years ago

models.NewReferenceCreate() ?

baywet commented 2 years ago

https://github.com/microsoftgraph/msgraph-sdk-go/blob/32e95517554481b1846846df8198fdab79909f76/models/reference_create.go#L15

abjoseph commented 2 years ago

@baywet Thanks! 🙏 It's been almost a month and apparently I already forgot the convention, I was searching the repo for ReferenceCreateable and didn't find anything. Just tried for ReferenceCreate and no results either, I suspect GitHub hasn't indexed the new files/types as yet.

baywet commented 2 years ago

file search is usually much faster in my experience. (t on the repo main page, then reference_create)

abjoseph commented 2 years ago

Yup, that works much better though I would need to know the file name but at least it's something.

baywet commented 2 years ago

for this repo, the file names match the type name with the difference that types are UpperCamelCased and the file names snake_cased for future reference :)