-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcxsdk.go
376 lines (327 loc) · 11.2 KB
/
cxsdk.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
// Copyright 2024 Coralogix Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cxsdk
import (
"errors"
"fmt"
"log"
"os"
"strings"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
// GRPC URLs for the Coralogix regions.
const (
GrpcUS1 = "ng-api-grpc.coralogix.us:443"
GrpcUS2 = "ng-api-grpc.cx498.coralogix.com:443"
GrpcEU1 = "ng-api-grpc.coralogix.com:443"
GrpcEU2 = "ng-api-grpc.eu2.coralogix.com:443"
GrpcAP1 = "ng-api-grpc.app.coralogix.in:443"
GrpcAP2 = "ng-api-grpc.coralogixsg.com:443"
GrpcAP3 = "ng-api-grpc.ap3.coralogix.com:443"
)
// RESt URLs for the Coralogix regions.
const (
RestUS1 = "https://ng-api-http.coralogix.us"
RestUS2 = "https://ng-api-http.cx498.coralogix.com"
RestEU1 = "https://ng-api-http.coralogix.com"
RestEU2 = "https://ng-api-http.eu2.coralogix.com"
RestAP1 = "https://ng-api-http.app.coralogix.in"
RestAP2 = "https://ng-api-http.coralogixsg.com"
RestAP3 = "https://ng-api-http.ap3.coralogix.com"
)
// SdkAPIError is an error that occurs in the Coralogix SDK.
type SdkAPIError struct {
apiError error
endpoint string
featureGroupID string
}
// NewSdkAPIError creates a new SdkAPIError.
func NewSdkAPIError(
apiError error,
endpoint string,
featureGroupID string,
) *SdkAPIError {
return &SdkAPIError{
apiError: apiError,
endpoint: endpoint,
featureGroupID: featureGroupID,
}
}
func (e *SdkAPIError) Error() string {
return fmt.Sprintf("SDK API error from %s for feature group %s: %s", e.endpoint, e.featureGroupID, e.apiError.Error())
}
// Code returns the error code of the SdkAPIError.apiError.
func Code(err error) codes.Code {
var sdkErr *SdkAPIError
if errors.As(err, &sdkErr) {
return status.Code(sdkErr.apiError)
}
return codes.Unknown
}
// ClientSet is a set of clients for the Coralogix SDK.
type ClientSet struct {
ruleGroups *RuleGroupsClient
recordingRuleGroups *RecordingRuleGroupSetsClient
alerts *AlertsClient
enrichments *EnrichmentsClient
dataSet *DataSetClient
dashboards *DashboardsClient
actions *ActionsClient
tcoPolicies *TCOPoliciesClient
webhooks *WebhooksClient
events2Metrics *Events2MetricsClient
archiveRetentions *ArchiveRetentionsClient
archiveMetrics *ArchiveMetricsClient
archiveLogs *ArchiveLogsClient
alertScheduler *AlertSchedulerClient
teams *TeamsClient
slos *SLOsClient
scopes *ScopesClient
apiKeys *ApikeysClient
users *UsersClient
groups *GroupsClient
saml *SamlClient
dataUsage *DataUsageClient
roles *RolesClient
}
// RuleGroups gets a RuleGroupsClient from the ClientSet.
func (c *ClientSet) RuleGroups() *RuleGroupsClient {
return c.ruleGroups
}
// RecordingRuleGroups gets a RecordingRuleGroupSetsClient from the ClientSet.
func (c *ClientSet) RecordingRuleGroups() *RecordingRuleGroupSetsClient {
return c.recordingRuleGroups
}
// Alerts gets an AlertsClient from the ClientSet.
func (c *ClientSet) Alerts() *AlertsClient {
return c.alerts
}
// Enrichments gets an EnrichmentsClient from the ClientSet.
func (c *ClientSet) Enrichments() *EnrichmentsClient {
return c.enrichments
}
// DataSet gets a DataSetClient from the ClientSet.
func (c *ClientSet) DataSet() *DataSetClient {
return c.dataSet
}
// func (c *ClientSet) Dashboards() *DashboardsClient {
// return c.dashboards
// }
// Actions gets an ActionsClient from the ClientSet.
func (c *ClientSet) Actions() *ActionsClient {
return c.actions
}
// TCOPolicies gets a TCOPoliciesClient from the ClientSet.
func (c *ClientSet) TCOPolicies() *TCOPoliciesClient {
return c.tcoPolicies
}
// Webhooks gets a WebhooksClient from the ClientSet.
func (c *ClientSet) Webhooks() *WebhooksClient {
return c.webhooks
}
// Events2Metrics gets an Events2MetricsClient from the ClientSet.
func (c *ClientSet) Events2Metrics() *Events2MetricsClient {
return c.events2Metrics
}
// ArchiveRetentions gets an ArchiveRetentionsClient from the ClientSet.
func (c *ClientSet) ArchiveRetentions() *ArchiveRetentionsClient {
return c.archiveRetentions
}
// ArchiveMetrics gets an ArchiveMetricsClient from the ClientSet.
func (c *ClientSet) ArchiveMetrics() *ArchiveMetricsClient {
return c.archiveMetrics
}
// ArchiveLogs gets an ArchiveLogsClient from the ClientSet.
func (c *ClientSet) ArchiveLogs() *ArchiveLogsClient {
return c.archiveLogs
}
// AlertSchedulers Gets an AlertSchedulerClient from the ClientSet.
func (c *ClientSet) AlertSchedulers() *AlertSchedulerClient {
return c.alertScheduler
}
// Teams gets a TeamsClient from the ClientSet.
func (c *ClientSet) Teams() *TeamsClient {
return c.teams
}
// APIKeys gets an SLOsClient from the ClientSet.
func (c *ClientSet) APIKeys() *ApikeysClient {
return c.apiKeys
}
// SLOs gets an SLOsClient from the ClientSet.
func (c *ClientSet) SLOs() *SLOsClient {
return c.slos
}
// Scopes gets a ScopesClient from the ClientSet.
func (c *ClientSet) Scopes() *ScopesClient {
return c.scopes
}
// Users gets a UsersClient from the ClientSet.
func (c *ClientSet) Users() *UsersClient {
return c.users
}
// Groups gets a GroupsClient from the ClientSet.
func (c *ClientSet) Groups() *GroupsClient {
return c.groups
}
// Saml gets a SamlClient from the ClientSet.
func (c *ClientSet) Saml() *SamlClient {
return c.saml
}
// DataUsage gets a DataUsageClient from the ClientSet.
func (c *ClientSet) DataUsage() *DataUsageClient {
return c.dataUsage
}
// Roles gets a RolesClient from the ClientSet.
func (c *ClientSet) Roles() *RolesClient {
return c.roles
}
// NewClientSet Creates a new ClientSet.
func NewClientSet(targetURL, teamsLevelAPIKey string, userLevelAPIKey string) *ClientSet {
authContext := NewAuthContext(teamsLevelAPIKey, userLevelAPIKey)
apikeyCPC := NewCallPropertiesCreator(targetURL, authContext)
return &ClientSet{
ruleGroups: NewRuleGroupsClient(apikeyCPC),
recordingRuleGroups: NewRecordingRuleGroupSetsClient(apikeyCPC),
alerts: NewAlertsClient(apikeyCPC),
events2Metrics: NewEvents2MetricsClient(apikeyCPC),
enrichments: NewEnrichmentClient(apikeyCPC),
dataSet: NewDataSetClient(apikeyCPC),
dashboards: NewDashboardsClient(apikeyCPC),
actions: NewActionsClient(apikeyCPC),
tcoPolicies: NewTCOPoliciesClient(apikeyCPC),
webhooks: NewWebhooksClient(apikeyCPC),
archiveRetentions: NewArchiveRetentionsClient(apikeyCPC),
archiveMetrics: NewArchiveMetricsClient(apikeyCPC),
archiveLogs: NewArchiveLogsClient(apikeyCPC),
alertScheduler: NewAlertSchedulerClient(apikeyCPC),
teams: NewTeamsClient(apikeyCPC),
slos: NewSLOsClient(apikeyCPC),
scopes: NewScopesClient(apikeyCPC),
roles: NewRolesClient(apikeyCPC),
// dahboardsFolders: NewDashboardsFoldersClient(apikeyCPC),
apiKeys: NewAPIKeysClient(apikeyCPC),
groups: NewGroupsClient(apikeyCPC),
saml: NewSamlClient(apikeyCPC),
users: NewUsersClient(apikeyCPC),
}
}
// EnvCoralogxRegion is the name of the environment variable that contains the Coralogix region.
const EnvCoralogxRegion = "CORALOGIX_REGION"
// EnvCoralogixTeamLevelAPIKey is the name of the environment variable that contains the Coralogix Team API key.
const EnvCoralogixTeamLevelAPIKey = "CORALOGIX_TEAM_API_KEY"
// EnvCoralogixUserLevelAPIKey is the name of the environment variable that contains the Coralogix User API key.
const EnvCoralogixUserLevelAPIKey = "CORALOGIX_USER_API_KEY"
// CoralogixRegionFromEnv reads the Coralogix region from environment variables.
func CoralogixRegionFromEnv() (string, error) {
regionIdentifier := strings.ToLower(os.Getenv(EnvCoralogxRegion))
if regionIdentifier == "" {
return "", fmt.Errorf("the %s environment variable is not set", EnvCoralogxRegion)
}
return regionIdentifier, nil
}
// CoralogixGrpcEndpointFromRegion reads the Coralogix region from environment variables.
func CoralogixGrpcEndpointFromRegion(regionIdentifier string) string {
switch regionIdentifier {
case "us1":
return GrpcUS1
case "us2":
return GrpcUS2
case "eu1":
return GrpcEU1
case "eu2":
return GrpcEU2
case "ap1":
return GrpcAP1
case "ap2":
return GrpcAP2
case "ap3":
return GrpcAP3
default:
return regionIdentifier
}
}
// CoralogixRestEndpointFromRegion reads the Coralogix REST endpoint from environment variables.
func CoralogixRestEndpointFromRegion(regionIdentifier string) string {
switch regionIdentifier {
case "us1":
return RestUS1
case "us2":
return RestUS2
case "eu1":
return RestEU1
case "eu2":
return RestEU2
case "ap1":
return RestAP1
case "ap2":
return RestAP2
case "ap3":
return RestAP3
default:
return regionIdentifier
}
}
// AuthContext is a struct that holds the API keys for the Coralogix SDK.
type AuthContext struct {
teamLevelAPIKey string
userLevelAPIKey string
}
// NewAuthContext creates a new AuthContext.
func NewAuthContext(teamLevelAPIKey, userLevelAPIKey string) AuthContext {
if teamLevelAPIKey == "" {
log.Println("Warning: teamLevelAPIKey was not provided. Some functionality will not be available.")
}
if userLevelAPIKey == "" {
log.Println("Warning: userLevelAPIKey was not provided. Some functionality will not be available.")
}
return AuthContext{
teamLevelAPIKey: teamLevelAPIKey,
userLevelAPIKey: userLevelAPIKey,
}
}
// AuthContextFromEnv reads the Coralogix API keys from environment variables.
func AuthContextFromEnv() (AuthContext, error) {
teamLevelAPIKey, err := CoralogixTeamsLevelAPIKeyFromEnv()
if err != nil {
return AuthContext{}, err
}
userLevelAPIKey, err := CoralogixUserLevelAPIKeyFromEnv()
if err != nil {
return AuthContext{}, err
}
if teamLevelAPIKey == "" {
log.Println("Warning: teamLevelAPIKey is empty. Some functionality will not be available.")
}
if userLevelAPIKey == "" {
log.Println("Warning: userLevelAPIKey is empty. Some functionality will not be available.")
}
return AuthContext{teamLevelAPIKey: teamLevelAPIKey, userLevelAPIKey: userLevelAPIKey}, nil
}
// CoralogixTeamsLevelAPIKeyFromEnv reads the Coralogix Team API key from environment variables.
func CoralogixTeamsLevelAPIKeyFromEnv() (string, error) {
apiKey := os.Getenv(EnvCoralogixTeamLevelAPIKey)
if apiKey == "" {
return "", fmt.Errorf("the %s environment variable is not set", EnvCoralogixTeamLevelAPIKey)
}
return apiKey, nil
}
// CoralogixUserLevelAPIKeyFromEnv reads the Coralogix User API key from environment variables.
func CoralogixUserLevelAPIKeyFromEnv() (string, error) {
apiKey := os.Getenv(EnvCoralogixUserLevelAPIKey)
if apiKey == "" {
return "", fmt.Errorf("the %s environment variable is not set", EnvCoralogixUserLevelAPIKey)
}
return apiKey, nil
}