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

how does one consume armresourcegraph QueryResponse #21004

Closed
unmarshall opened this issue Jun 12, 2023 · 9 comments
Closed

how does one consume armresourcegraph QueryResponse #21004

unmarshall opened this issue Jun 12, 2023 · 9 comments
Assignees
Labels
ARM customer-reported Issues that are reported by GitHub users external to the Azure organization. Mgmt This issue is related to a management-plane library. needs-team-attention Workflow: This issue needs attention from Azure service team or SDK team question The issue doesn't require a change to the product in order to be resolved. Most issues start as that Resource Graph Service Attention Workflow: This issue is responsible by Azure service team.

Comments

@unmarshall
Copy link

unmarshall commented Jun 12, 2023

Bug Report

  • What happened?
    As recommended we are now migrating to use resourcegraph API. The API seems to work fine when i just print out the ClientResourcesResponse.Data but if i now want to iterate over rows/JsonObjectArray then i am currently not able to find a way or an example to do just that. The data type of Data is any.

I have checked the documentation links and each and every example is incomplete.
Example (the list is not comprehensive):

All the examples fall short of how to extract values from Data. You cannot range over it because its not an iterable. You cannot cast it to a slice as that is not allowed and is also incorrect. I even tried to change the options via:

Options: &armresourcegraph.QueryRequestOptions{
	ResultFormat: to.Ptr(armresourcegraph.ResultFormatObjectArray),
},

But since type of Data is any it becomes difficult to consume it.

  • What did you expect or want to happen?
    At least give one complete example on how to iterate either over table rows (if ResultFormat is set to ResultFormatTable) or consume ResultFormatObjectArray.

  • How can we reproduce it?
    Just use the resource graph query to return a result which has more than 1 rows.

  • Anything we should know about your environment.

Testing on MacOs Version 13.4
Golang Version 1.20.5
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resourcegraph/armresourcegraph v0.7.1
@github-actions github-actions bot added ARM customer-reported Issues that are reported by GitHub users external to the Azure organization. Mgmt This issue is related to a management-plane library. needs-team-triage Workflow: This issue needs the team to triage. question The issue doesn't require a change to the product in order to be resolved. Most issues start as that labels Jun 12, 2023
@unmarshall
Copy link
Author

Ok finally figured out the way to do this:
Using ObjectArray result format

Options: &armresourcegraph.QueryRequestOptions{
	ResultFormat: to.Ptr(armresourcegraph.ResultFormatObjectArray),
},

Code to parse:

if m, ok := resp.Data.([]interface{}); ok {
   for _, r := range m {
	items := r.(map[string]interface{})
	for k, v := range items {
		fmt.Printf("k: %s, v: %+v\n", k, v)
	}
  }
}

If you see the above usage of the response you will immediately see the following:

  • Too many casts, we had to use a IDE debugger to figure out the exact type of each element which tells me that the type management in the API response is wanting.
  • Facets is empty. I would have expected that Facets would contain the type of each column so that it helps with type casting with a proper type for each column value. Otherwise you end up dealing with interfaces

In general this is a design flaw where it expects each consumer to interpret any type for response.Data.

@unmarshall
Copy link
Author

My general recommendation:

  • Please update your documentation to include a complete example (possibly an idiomatic way to consume Data whose type is any)
  • Relook at the design to make consumption of the response data typed or provide sufficient metadata to help cast properly.

@jhendrixMSFT jhendrixMSFT removed the needs-team-triage Workflow: This issue needs the team to triage. label Jun 14, 2023
@github-actions github-actions bot added the needs-team-attention Workflow: This issue needs attention from Azure service team or SDK team label Jun 14, 2023
@raych1
Copy link
Member

raych1 commented Jun 30, 2023

Thanks @unmarshall for reporting this issue.

I want to mention that SDK code is generated automatically, and the examples are all generated from the REST api examples. I would ask if service team could consider adding more REST API examples.

Actually, the example you referenced here includes the response example and how does the data look like.

// If the HTTP response code is 200 as defined in example definition, your response structure would look as follows. Please pay attention that all the values in the output are fake values for just demo purposes.
	// res.QueryResponse = armresourcegraph.QueryResponse{
	// 	Count: to.Ptr[int64](3),
	// 	Data: []any{
	// 		map[string]any{
	// 			"name": "myNetworkInterface",
	// 			"type": "microsoft.network/networkinterfaces",
	// 			"id": "/subscriptions/cfbbd179-59d2-4052-aa06-9270a38aa9d6/resourceGroups/RG1/providers/Microsoft.Network/networkInterfaces/myNetworkInterface",
	// 			"location": "centralus",
	// 			"tags":map[string]any{
	// 				"tag1": "Value1",
	// 			},
	// 		},
       //...

@raych1 raych1 added the Service Attention Workflow: This issue is responsible by Azure service team. label Jun 30, 2023
@unmarshall
Copy link
Author

I would ask if service team could consider adding more REST API examples.

It would be nice if you could give idiomatic ways to consume the response using Go as this repository is specifically for Go based SDK.
Also in comment i have pointed out some additional problems (lack of ease for consumers).

@raych1
Copy link
Member

raych1 commented Jul 4, 2023

It would be nice if you could give idiomatic ways to consume the response using Go as this repository is specifically for Go based SDK. Also in comment i have pointed out some additional problems (lack of ease for consumers).

@unmarshall , sdk code is generated automatically based on the swagger. Generated code has no idea of unmarshalling the any type. The design change request needs the service team's response.

@raych1 raych1 added the needs-author-feedback Workflow: More information is needed from author to address the issue. label Jul 4, 2023
@github-actions
Copy link

github-actions bot commented Jul 4, 2023

Hi @unmarshall. Thank you for opening this issue and giving us the opportunity to assist. To help our team better understand your issue and the details of your scenario please provide a response to the question asked above or the information requested above. This will help us more accurately address your issue.

@github-actions github-actions bot removed the needs-team-attention Workflow: This issue needs attention from Azure service team or SDK team label Jul 4, 2023
@github-actions
Copy link

Hi @unmarshall, we're sending this friendly reminder because we haven't heard back from you in 7 days. We need more information about this issue to help address it. Please be sure to give us your input. If we don't hear back from you within 14 days of this comment the issue will be automatically closed. Thank you!

@github-actions github-actions bot added the no-recent-activity There has been no recent activity on this issue. label Jul 11, 2023
@unmarshall
Copy link
Author

The design change request needs the service team's response.

I thought this was more a question/comment for the design team and only an information for me. The generated code in this case has created consumption difficult. API responses should be typed or sufficient metadata information should be provided allowing the consumers to do proper casts (If a Cast is required then its a sign of design smell anyways).

For now I have figured out a way to consume the response (even though it does not look pretty). I will leave it to your judgement on how you see your own response type. If you do not see any issue then feel free to close the ticket.

@github-actions github-actions bot added needs-team-attention Workflow: This issue needs attention from Azure service team or SDK team and removed needs-author-feedback Workflow: More information is needed from author to address the issue. no-recent-activity There has been no recent activity on this issue. labels Jul 11, 2023
@raych1
Copy link
Member

raych1 commented Jul 25, 2023

I close this issue as you figured out the right solution. Thanks again for reporting this.

@raych1 raych1 closed this as completed Jul 25, 2023
@github-actions github-actions bot locked and limited conversation to collaborators Oct 23, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
ARM customer-reported Issues that are reported by GitHub users external to the Azure organization. Mgmt This issue is related to a management-plane library. needs-team-attention Workflow: This issue needs attention from Azure service team or SDK team question The issue doesn't require a change to the product in order to be resolved. Most issues start as that Resource Graph Service Attention Workflow: This issue is responsible by Azure service team.
Projects
None yet
Development

No branches or pull requests

4 participants