This repository has been archived by the owner on Dec 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
129 lines (118 loc) · 3.17 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package client
import (
"context"
"errors"
datadogv1 "github.com/DataDog/datadog-api-client-go/api/v1/datadog"
datadogv2 "github.com/DataDog/datadog-api-client-go/api/v2/datadog"
"github.com/cloudquery/cq-provider-sdk/cqproto"
"github.com/cloudquery/cq-provider-sdk/provider/diag"
"github.com/cloudquery/cq-provider-sdk/provider/schema"
"github.com/hashicorp/go-hclog"
)
type Client struct {
logger hclog.Logger
Accounts []DDAccount
AccountConfigs []Account
// this is set by the table client multiplexer
MultiPlexedAccount DDAccount
}
func (c *Client) Logger() hclog.Logger {
return c.logger
}
type DDAccount struct {
V1Context context.Context
V2Context context.Context
V1Config datadogv1.Configuration
V2Config datadogv2.Configuration
Name string
}
func (c Client) withAccount(account Account) *Client {
return &Client{
logger: c.logger.With("id", account.Name),
Accounts: c.Accounts,
MultiPlexedAccount: DDAccount{
Name: account.Name,
V1Context: context.WithValue(
context.Background(),
datadogv1.ContextAPIKeys,
map[string]datadogv1.APIKey{
"apiKeyAuth": {
Key: account.APIKey,
},
"appKeyAuth": {
Key: account.AppKey,
},
},
),
V2Context: context.WithValue(
context.Background(),
datadogv2.ContextAPIKeys,
map[string]datadogv2.APIKey{
"apiKeyAuth": {
Key: account.APIKey,
},
"appKeyAuth": {
Key: account.AppKey,
},
},
),
V1Config: *datadogv1.NewConfiguration(),
V2Config: *datadogv2.NewConfiguration(),
}}
}
func NewConfig(f cqproto.ConfigFormat) *Config {
return &Config{
requestedFormat: f,
}
}
func Configure(logger hclog.Logger, config interface{}) (schema.ClientMeta, diag.Diagnostics) {
logger.Info("in datadog configure")
diag.WithDetails("In Datadog configure")
ddConfig := config.(*Config)
if len(ddConfig.Accounts) == 0 {
logger.Error("error no datadog accounts configured")
return nil, diag.FromError(errors.New("error no datadog accounts configured"), diag.USER)
}
ddAccounts := make([]DDAccount, 0)
for _, account := range ddConfig.Accounts {
logger.Debug("user definied account", "account", account.Name)
ddAccounts = append(ddAccounts, DDAccount{
Name: account.Name,
V1Context: context.WithValue(
context.Background(),
datadogv1.ContextAPIKeys,
map[string]datadogv1.APIKey{
"apiKeyAuth": {
Key: account.APIKey,
},
"appKeyAuth": {
Key: account.AppKey,
},
},
),
V2Context: context.WithValue(
context.Background(),
datadogv2.ContextAPIKeys,
map[string]datadogv2.APIKey{
"apiKeyAuth": {
Key: account.APIKey,
},
"appKeyAuth": {
Key: account.AppKey,
},
},
),
V1Config: *datadogv1.NewConfiguration(),
V2Config: *datadogv2.NewConfiguration(),
})
}
// TODO: Figure out how to initialize all the datadog clients and pass them in here
// For now, going to do that in the resources. Still not sure how multiplexers work
client := Client{
logger: logger,
Accounts: ddAccounts,
AccountConfigs: ddConfig.Accounts,
}
// Return the initialized client and it will be passed to your resources
return &client, nil
}