Skip to content

Commit c537e02

Browse files
committed
Add Org level API key
1 parent ab411df commit c537e02

File tree

4 files changed

+55
-18
lines changed

4 files changed

+55
-18
lines changed

README.md

+1
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

+18
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

+29-11
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,8 @@ func (c *ClientSet) Groups() *GroupsClient {
146146
}
147147

148148
// NewClientSet Creates a new ClientSet.
149-
func NewClientSet(targetURL, teamsLevelAPIKey string, userLevelAPIKey string) *ClientSet {
150-
authContext := NewAuthContext(teamsLevelAPIKey, userLevelAPIKey)
149+
func NewClientSet(targetURL, teamsLevelAPIKey, userLevelAPIKey, orgLevelAPIKey string) *ClientSet {
150+
authContext := NewAuthContext(teamsLevelAPIKey, userLevelAPIKey, orgLevelAPIKey)
151151
apikeyCPC := NewCallPropertiesCreator(targetURL, authContext)
152152

153153
return &ClientSet{
@@ -185,6 +185,9 @@ const EnvCoralogixTeamLevelAPIKey = "CORALOGIX_TEAM_API_KEY"
185185
// EnvCoralogixUserLevelAPIKey is the name of the environment variable that contains the Coralogix User API key.
186186
const EnvCoralogixUserLevelAPIKey = "CORALOGIX_USER_API_KEY"
187187

188+
// EnvCoralogixOrgLevelAPIKey is the name of the environment variable that contains the Coralogix Organization API key.
189+
const EnvCoralogixOrgLevelAPIKey = "CORALOGIX_ORG_API_KEY"
190+
188191
// CoralogixRegionFromEnv reads the Coralogix region from environment variables.
189192
func CoralogixRegionFromEnv() (string, error) {
190193
regionIdentifier := strings.ToLower(os.Getenv(EnvCoralogxRegion))
@@ -260,40 +263,46 @@ const (
260263
type AuthContext struct {
261264
teamLevelAPIKey string
262265
userLevelAPIKey string
266+
orgLevelAPIKey string
263267
}
264268

265269
// NewAuthContext creates a new AuthContext.
266-
func NewAuthContext(teamLevelAPIKey, userLevelAPIKey string) AuthContext {
270+
func NewAuthContext(teamLevelAPIKey, userLevelAPIKey, orgLevelAPIKey string) AuthContext {
267271
if teamLevelAPIKey == "" {
268272
log.Println("Warning: teamLevelAPIKey was not provided. Some functionality will not be available.")
269273
}
270274
if userLevelAPIKey == "" {
271275
log.Println("Warning: userLevelAPIKey was not provided. Some functionality will not be available.")
272276
}
277+
if orgLevelAPIKey == "" {
278+
log.Println("Warning: orgLevelAPIKey was not provided. Some functionality will not be available.")
279+
}
273280
return AuthContext{
274281
teamLevelAPIKey: teamLevelAPIKey,
275282
userLevelAPIKey: userLevelAPIKey,
283+
orgLevelAPIKey: orgLevelAPIKey,
276284
}
277285
}
278286

279287
// AuthContextFromEnv reads the Coralogix API keys from environment variables.
280288
func AuthContextFromEnv() (AuthContext, error) {
281-
teamLevelAPIKey, err := CoralogixTeamsLevelAPIKeyFromEnv()
282-
if err != nil {
283-
return AuthContext{}, err
284-
}
285-
userLevelAPIKey, err := CoralogixUserLevelAPIKeyFromEnv()
286-
if err != nil {
287-
return AuthContext{}, err
289+
teamLevelAPIKey := os.Getenv(EnvCoralogixTeamLevelAPIKey)
290+
userLevelAPIKey := os.Getenv(EnvCoralogixUserLevelAPIKey)
291+
orgLevelAPIKey := os.Getenv(EnvCoralogixOrgLevelAPIKey)
292+
if teamLevelAPIKey == "" && userLevelAPIKey == "" && orgLevelAPIKey == "" {
293+
return AuthContext{}, fmt.Errorf("at least one of %s, %s, or %s must be set", EnvCoralogixTeamLevelAPIKey, EnvCoralogixUserLevelAPIKey, EnvCoralogixOrgLevelAPIKey)
288294
}
289295
if teamLevelAPIKey == "" {
290296
log.Println("Warning: teamLevelAPIKey is empty. Some functionality will not be available.")
291297
}
292298
if userLevelAPIKey == "" {
293299
log.Println("Warning: userLevelAPIKey is empty. Some functionality will not be available.")
294300
}
301+
if orgLevelAPIKey == "" {
302+
log.Println("Warning: orgLevelAPIKey is empty. Some functionality will not be available.")
303+
}
295304

296-
return AuthContext{teamLevelAPIKey: teamLevelAPIKey, userLevelAPIKey: userLevelAPIKey}, nil
305+
return AuthContext{teamLevelAPIKey: teamLevelAPIKey, userLevelAPIKey: userLevelAPIKey, orgLevelAPIKey: orgLevelAPIKey}, nil
297306
}
298307

299308
// CoralogixTeamsLevelAPIKeyFromEnv reads the Coralogix Team API key from environment variables.
@@ -313,3 +322,12 @@ func CoralogixUserLevelAPIKeyFromEnv() (string, error) {
313322
}
314323
return apiKey, nil
315324
}
325+
326+
// CoralogixOrgLevelAPIKeyFromEnv reads the Coralogix Organization API key from environment variables.
327+
func CoralogixOrgLevelAPIKeyFromEnv() (string, error) {
328+
apiKey := os.Getenv(EnvCoralogixOrgLevelAPIKey)
329+
if apiKey == "" {
330+
return "", fmt.Errorf("the %s environment variable is not set", EnvCoralogixOrgLevelAPIKey)
331+
}
332+
return apiKey, nil
333+
}

go/teams-client.go

+7-7
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)