Skip to content

Instantly share code, notes, and snippets.

@TheoBrigitte
Created August 17, 2018 14:36
Show Gist options
  • Save TheoBrigitte/9d1fadca91c69c337f6ff467599db848 to your computer and use it in GitHub Desktop.
Save TheoBrigitte/9d1fadca91c69c337f6ff467599db848 to your computer and use it in GitHub Desktop.
Program used to Azure API $filter query parameters
package main
import (
"context"
"fmt"
"log"
"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2018-02-01/resources"
"github.com/Azure/go-autorest/autorest"
"github.com/Azure/go-autorest/autorest/adal"
"github.com/Azure/go-autorest/autorest/azure"
)
const (
azureClientID = "<your azure client id>"
azureClientSecret = "<your azure secret key>"
azureSubscriptionID = "<your azure subscription id>"
azureTenantID = "<your azure tenant id>"
)
func NewGroupsClient() (*resources.GroupsClient, error) {
env := azure.PublicCloud
oauthConfig, err := adal.NewOAuthConfig(env.ActiveDirectoryEndpoint, azureTenantID)
if err != nil {
return nil, err
}
token, err := adal.NewServicePrincipalToken(*oauthConfig, azureClientID, azureClientSecret, env.ServiceManagementEndpoint)
if err != nil {
return nil, err
}
client := resources.NewGroupsClient(azureSubscriptionID)
client.Authorizer = autorest.NewBearerAuthorizer(token)
return &client, nil
}
func main() {
log.Println("start")
groupsClient, err := NewGroupsClient()
if err != nil {
panic(err)
}
ctx := context.Background()
list, err := groupsClient.List(ctx, "name eq 'foo'", nil)
if err != nil {
panic(err)
}
fmt.Printf("name\n")
for ; list.NotDone(); list.Next() {
for _, group := range list.Values() {
fmt.Printf("%s\n", *group.Name)
}
}
log.Println("end")
}
@TheoBrigitte
Copy link
Author

Download this file and replace Azure credentials at the top.

Then run

dep init
go run ./azure-sdk-filter.go

It should panic with a similar error

resources.GroupsClient#List: Failure responding to request: StatusCode=400 -- Original Error: autorest/azure: Service returned an error. Status=400 Code="InvalidFilterInQueryString" Message="Invalid $filter 'name eq 'foo'' specified in the query string."

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment