forked from hashicorp/terraform-provider-azurerm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
57 lines (46 loc) · 1.93 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
package client
import (
"context"
"fmt"
"github.com/Azure/go-autorest/autorest"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/common"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/services/appconfiguration/sdk/1.0/appconfiguration"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/services/appconfiguration/sdk/2020-06-01/configurationstores"
)
type Client struct {
ConfigurationStoresClient *configurationstores.ConfigurationStoresClient
tokenFunc func(endpoint string) (autorest.Authorizer, error)
configureClientFunc func(c *autorest.Client, authorizer autorest.Authorizer)
}
func (c Client) DataPlaneClient(ctx context.Context, configurationStoreId string) (*appconfiguration.BaseClient, error) {
appConfigId, err := configurationstores.ParseConfigurationStoreID(configurationStoreId)
if err != nil {
return nil, err
}
// TODO: caching all of this
appConfig, err := c.ConfigurationStoresClient.Get(ctx, *appConfigId)
if err != nil {
// TODO: if not found etc
return nil, err
}
if appConfig.Model == nil || appConfig.Model.Properties == nil || appConfig.Model.Properties.Endpoint == nil {
return nil, fmt.Errorf("endpoint was nil")
}
endpoint := *appConfig.Model.Properties.Endpoint
appConfigAuth, err := c.tokenFunc(endpoint)
if err != nil {
return nil, fmt.Errorf("obtaining auth token for %q: %+v", endpoint, err)
}
client := appconfiguration.NewWithoutDefaults("", endpoint)
c.configureClientFunc(&client.Client, appConfigAuth)
return &client, nil
}
func NewClient(o *common.ClientOptions) *Client {
configurationStores := configurationstores.NewConfigurationStoresClientWithBaseURI(o.ResourceManagerEndpoint)
o.ConfigureClient(&configurationStores.Client, o.ResourceManagerAuthorizer)
return &Client{
ConfigurationStoresClient: &configurationStores,
tokenFunc: o.TokenFunc,
configureClientFunc: o.ConfigureClient,
}
}