Skip to content

Commit 35ed741

Browse files
committed
Add Org level API key
1 parent d93b1d5 commit 35ed741

File tree

4 files changed

+55
-18
lines changed

4 files changed

+55
-18
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ The SDK can be configured using environment variables:
2323

2424
- `CORALOGIX_TEAM_API_KEY`: The API key that is used for all team-level interactions. Note that it has to have appropriate permissions. Read the [docs](https://coralogix.com/docs/api-keys/) for more information.
2525
- `CORALOGIX_USER_API_KEY`: The API key that is used for all user-level interactions. Note that it has to have appropriate permissions. Read the [docs](https://coralogix.com/docs/api-keys/) for more information.
26+
- `CORALOGIX_ORG_API_KEY`: The API key that is used for all organization-level interactions. Note that it has to have appropriate permissions. Read the [docs](https://coralogix.com/docs/api-keys/) for more information.
2627
- `CORALGOIX_REGION`: The region/cluster to connect to as a shorthand (EU2, AP1, etc. read more [here](https://coralogix.com/docs/coralogix-domain/)).
2728

2829
Furthermore, if you want to run the examples locally, you're going to to have set the following environment variables:

go/callPropertiesCreator.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ type CallPropertiesCreator struct {
3333
coraglogixRegion string
3434
teamsLevelAPIKey string
3535
userLevelAPIKey string
36+
orgLevelAPIKey string
3637
correlationID string
3738
sdkVersion string
3839
//allowRetry bool
@@ -75,6 +76,21 @@ func (c CallPropertiesCreator) GetUserLevelCallProperties(ctx context.Context) (
7576
return &CallProperties{Ctx: ctx, Connection: conn, CallOptions: callOptions}, nil
7677
}
7778

79+
// GetOrgLevelCallProperties returns a new CallProperties object built from an organization-level API key. It essentially prepares the context, connection, and call options for a gRPC call.
80+
func (c CallPropertiesCreator) GetOrgLevelCallProperties(ctx context.Context) (*CallProperties, error) {
81+
ctx = createContext(ctx, c.orgLevelAPIKey, c.correlationID, c.sdkVersion)
82+
83+
endpoint := CoralogixGrpcEndpointFromRegion(c.coraglogixRegion)
84+
conn, err := createSecureConnection(endpoint)
85+
if err != nil {
86+
return nil, err
87+
}
88+
89+
callOptions := createCallOptions()
90+
91+
return &CallProperties{Ctx: ctx, Connection: conn, CallOptions: callOptions}, nil
92+
}
93+
7894
func createCallOptions() []grpc.CallOption {
7995
var callOptions []grpc.CallOption
8096
callOptions = append(callOptions, grpc_retry.WithMax(5))
@@ -99,6 +115,7 @@ func NewCallPropertiesCreator(region string, authContext AuthContext) *CallPrope
99115
coraglogixRegion: region,
100116
teamsLevelAPIKey: authContext.teamLevelAPIKey,
101117
userLevelAPIKey: authContext.userLevelAPIKey,
118+
orgLevelAPIKey: authContext.orgLevelAPIKey,
102119
correlationID: uuid.New().String(),
103120
sdkVersion: vanillaSdkVersion,
104121
}
@@ -109,6 +126,7 @@ func NewCallPropertiesCreatorTerraformOperator(region string, authContext AuthCo
109126
return &CallPropertiesCreator{
110127
coraglogixRegion: region,
111128
teamsLevelAPIKey: authContext.teamLevelAPIKey,
129+
orgLevelAPIKey: authContext.orgLevelAPIKey,
112130
userLevelAPIKey: authContext.userLevelAPIKey,
113131
correlationID: uuid.New().String(),
114132
sdkVersion: terraformOperatorVersion,

go/cxsdk.go

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,8 @@ func (c *ClientSet) DataUsage() *DataUsageClient {
157157
}
158158

159159
// NewClientSet Creates a new ClientSet.
160-
func NewClientSet(targetURL, teamsLevelAPIKey string, userLevelAPIKey string) *ClientSet {
161-
authContext := NewAuthContext(teamsLevelAPIKey, userLevelAPIKey)
160+
func NewClientSet(targetURL, teamsLevelAPIKey, userLevelAPIKey, orgLevelAPIKey string) *ClientSet {
161+
authContext := NewAuthContext(teamsLevelAPIKey, userLevelAPIKey, orgLevelAPIKey)
162162
apikeyCPC := NewCallPropertiesCreator(targetURL, authContext)
163163

164164
return &ClientSet{
@@ -196,6 +196,9 @@ const EnvCoralogixTeamLevelAPIKey = "CORALOGIX_TEAM_API_KEY"
196196
// EnvCoralogixUserLevelAPIKey is the name of the environment variable that contains the Coralogix User API key.
197197
const EnvCoralogixUserLevelAPIKey = "CORALOGIX_USER_API_KEY"
198198

199+
// EnvCoralogixOrgLevelAPIKey is the name of the environment variable that contains the Coralogix Organization API key.
200+
const EnvCoralogixOrgLevelAPIKey = "CORALOGIX_ORG_API_KEY"
201+
199202
// CoralogixRegionFromEnv reads the Coralogix region from environment variables.
200203
func CoralogixRegionFromEnv() (string, error) {
201204
regionIdentifier := strings.ToLower(os.Getenv(EnvCoralogxRegion))
@@ -271,40 +274,46 @@ const (
271274
type AuthContext struct {
272275
teamLevelAPIKey string
273276
userLevelAPIKey string
277+
orgLevelAPIKey string
274278
}
275279

276280
// NewAuthContext creates a new AuthContext.
277-
func NewAuthContext(teamLevelAPIKey, userLevelAPIKey string) AuthContext {
281+
func NewAuthContext(teamLevelAPIKey, userLevelAPIKey, orgLevelAPIKey string) AuthContext {
278282
if teamLevelAPIKey == "" {
279283
log.Println("Warning: teamLevelAPIKey was not provided. Some functionality will not be available.")
280284
}
281285
if userLevelAPIKey == "" {
282286
log.Println("Warning: userLevelAPIKey was not provided. Some functionality will not be available.")
283287
}
288+
if orgLevelAPIKey == "" {
289+
log.Println("Warning: orgLevelAPIKey was not provided. Some functionality will not be available.")
290+
}
284291
return AuthContext{
285292
teamLevelAPIKey: teamLevelAPIKey,
286293
userLevelAPIKey: userLevelAPIKey,
294+
orgLevelAPIKey: orgLevelAPIKey,
287295
}
288296
}
289297

290298
// AuthContextFromEnv reads the Coralogix API keys from environment variables.
291299
func AuthContextFromEnv() (AuthContext, error) {
292-
teamLevelAPIKey, err := CoralogixTeamsLevelAPIKeyFromEnv()
293-
if err != nil {
294-
return AuthContext{}, err
295-
}
296-
userLevelAPIKey, err := CoralogixUserLevelAPIKeyFromEnv()
297-
if err != nil {
298-
return AuthContext{}, err
300+
teamLevelAPIKey := os.Getenv(EnvCoralogixTeamLevelAPIKey)
301+
userLevelAPIKey := os.Getenv(EnvCoralogixUserLevelAPIKey)
302+
orgLevelAPIKey := os.Getenv(EnvCoralogixOrgLevelAPIKey)
303+
if teamLevelAPIKey == "" && userLevelAPIKey == "" && orgLevelAPIKey == "" {
304+
return AuthContext{}, fmt.Errorf("at least one of %s, %s, or %s must be set", EnvCoralogixTeamLevelAPIKey, EnvCoralogixUserLevelAPIKey, EnvCoralogixOrgLevelAPIKey)
299305
}
300306
if teamLevelAPIKey == "" {
301307
log.Println("Warning: teamLevelAPIKey is empty. Some functionality will not be available.")
302308
}
303309
if userLevelAPIKey == "" {
304310
log.Println("Warning: userLevelAPIKey is empty. Some functionality will not be available.")
305311
}
312+
if orgLevelAPIKey == "" {
313+
log.Println("Warning: orgLevelAPIKey is empty. Some functionality will not be available.")
314+
}
306315

307-
return AuthContext{teamLevelAPIKey: teamLevelAPIKey, userLevelAPIKey: userLevelAPIKey}, nil
316+
return AuthContext{teamLevelAPIKey: teamLevelAPIKey, userLevelAPIKey: userLevelAPIKey, orgLevelAPIKey: orgLevelAPIKey}, nil
308317
}
309318

310319
// CoralogixTeamsLevelAPIKeyFromEnv reads the Coralogix Team API key from environment variables.
@@ -324,3 +333,12 @@ func CoralogixUserLevelAPIKeyFromEnv() (string, error) {
324333
}
325334
return apiKey, nil
326335
}
336+
337+
// CoralogixOrgLevelAPIKeyFromEnv reads the Coralogix Organization API key from environment variables.
338+
func CoralogixOrgLevelAPIKeyFromEnv() (string, error) {
339+
apiKey := os.Getenv(EnvCoralogixOrgLevelAPIKey)
340+
if apiKey == "" {
341+
return "", fmt.Errorf("the %s environment variable is not set", EnvCoralogixOrgLevelAPIKey)
342+
}
343+
return apiKey, nil
344+
}

go/teams-client.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ type TeamsClient struct {
6363

6464
// Create creates a new team.
6565
func (c TeamsClient) Create(ctx context.Context, req *CreateTeamInOrgRequest) (*teams.CreateTeamInOrgResponse, error) {
66-
callProperties, err := c.callPropertiesCreator.GetTeamsLevelCallProperties(ctx)
66+
callProperties, err := c.callPropertiesCreator.GetOrgLevelCallProperties(ctx)
6767
if err != nil {
6868
return nil, err
6969
}
@@ -77,7 +77,7 @@ func (c TeamsClient) Create(ctx context.Context, req *CreateTeamInOrgRequest) (*
7777

7878
// Update updates a team.
7979
func (c TeamsClient) Update(ctx context.Context, req *UpdateTeamRequest) (*teams.UpdateTeamResponse, error) {
80-
callProperties, err := c.callPropertiesCreator.GetTeamsLevelCallProperties(ctx)
80+
callProperties, err := c.callPropertiesCreator.GetOrgLevelCallProperties(ctx)
8181
if err != nil {
8282
return nil, err
8383
}
@@ -91,7 +91,7 @@ func (c TeamsClient) Update(ctx context.Context, req *UpdateTeamRequest) (*teams
9191

9292
// Get gets a team.
9393
func (c TeamsClient) Get(ctx context.Context, req *GetTeamRequest) (*teams.GetTeamResponse, error) {
94-
callProperties, err := c.callPropertiesCreator.GetTeamsLevelCallProperties(ctx)
94+
callProperties, err := c.callPropertiesCreator.GetOrgLevelCallProperties(ctx)
9595
if err != nil {
9696
return nil, err
9797
}
@@ -105,7 +105,7 @@ func (c TeamsClient) Get(ctx context.Context, req *GetTeamRequest) (*teams.GetTe
105105

106106
// Delete deletes a team.
107107
func (c TeamsClient) Delete(ctx context.Context, req *DeleteTeamRequest) (*teams.DeleteTeamResponse, error) {
108-
callProperties, err := c.callPropertiesCreator.GetTeamsLevelCallProperties(ctx)
108+
callProperties, err := c.callPropertiesCreator.GetOrgLevelCallProperties(ctx)
109109
if err != nil {
110110
return nil, err
111111
}
@@ -119,7 +119,7 @@ func (c TeamsClient) Delete(ctx context.Context, req *DeleteTeamRequest) (*teams
119119

120120
// SetDailyQuota sets the daily quota for a team.
121121
func (c TeamsClient) SetDailyQuota(ctx context.Context, req *SetDailyQuotaRequest) (*teams.SetDailyQuotaResponse, error) {
122-
callProperties, err := c.callPropertiesCreator.GetTeamsLevelCallProperties(ctx)
122+
callProperties, err := c.callPropertiesCreator.GetOrgLevelCallProperties(ctx)
123123
if err != nil {
124124
return nil, err
125125
}
@@ -133,7 +133,7 @@ func (c TeamsClient) SetDailyQuota(ctx context.Context, req *SetDailyQuotaReques
133133

134134
// GetQuota gets the quota for a team.
135135
func (c TeamsClient) GetQuota(ctx context.Context, req *GetTeamQuotaRequest) (*teams.GetTeamQuotaResponse, error) {
136-
callProperties, err := c.callPropertiesCreator.GetTeamsLevelCallProperties(ctx)
136+
callProperties, err := c.callPropertiesCreator.GetOrgLevelCallProperties(ctx)
137137
if err != nil {
138138
return nil, err
139139
}
@@ -147,7 +147,7 @@ func (c TeamsClient) GetQuota(ctx context.Context, req *GetTeamQuotaRequest) (*t
147147

148148
// MoveQuota moves the quota from one team to another.
149149
func (c TeamsClient) MoveQuota(ctx context.Context, req *MoveQuotaRequest) (*teams.MoveQuotaResponse, error) {
150-
callProperties, err := c.callPropertiesCreator.GetTeamsLevelCallProperties(ctx)
150+
callProperties, err := c.callPropertiesCreator.GetOrgLevelCallProperties(ctx)
151151
if err != nil {
152152
return nil, err
153153
}

0 commit comments

Comments
 (0)