/
client.go
62 lines (50 loc) · 2.02 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package graph
// Copyright (c) Microsoft Corporation.
// Licensed under the Apache License 2.0.
import (
"errors"
abstractions "github.com/microsoft/kiota-abstractions-go"
"github.com/microsoft/kiota-abstractions-go/store"
az "github.com/microsoftgraph/msgraph-sdk-go-core/authentication"
"github.com/Azure/ARO-RP/pkg/util/azureclient/azuresdk/azcore"
"github.com/Azure/ARO-RP/pkg/util/graph/graphsdk"
)
type GraphServiceClient struct {
graphsdk.GraphBaseServiceClient
}
// Create a GraphServiceClient for use.
//
// NOTE: If you want to update the underlying SDK, see docs/updating-graphsdk.md for instructions.
func NewGraphServiceClient(adapter abstractions.RequestAdapter) *GraphServiceClient {
client := graphsdk.NewGraphBaseServiceClient(adapter, store.BackingStoreFactoryInstance)
return &GraphServiceClient{
*client,
}
}
// NewGraphServiceClientWithCredentials instantiates a new GraphServiceClient with provided credentials and scopes
func NewGraphServiceClientWithCredentials(credential azcore.TokenCredential, scopes []string) (*GraphServiceClient, error) {
if credential == nil {
return nil, errors.New("credential cannot be nil")
}
if len(scopes) == 0 {
scopes = []string{"https://graph.microsoft.com/.default"}
}
validhosts := []string{"graph.microsoft.com", "graph.microsoft.us", "dod-graph.microsoft.us", "graph.microsoft.de", "canary.graph.microsoft.com"}
auth, err := az.NewAzureIdentityAuthenticationProviderWithScopesAndValidHosts(credential, scopes, validhosts)
if err != nil {
return nil, err
}
adapter, err := NewGraphRequestAdapter(auth)
if err != nil {
return nil, err
}
client := NewGraphServiceClient(adapter)
return client, nil
}
// GetAdapter returns the client current adapter, Method should only be called when the user is certain an adapter has been provided
func (m *GraphServiceClient) GetAdapter() abstractions.RequestAdapter {
if m.BaseRequestBuilder.RequestAdapter == nil {
panic(errors.New("request adapter has not been initialized"))
}
return m.BaseRequestBuilder.RequestAdapter
}