Skip to content

Commit

Permalink
Add Org level API key
Browse files Browse the repository at this point in the history
  • Loading branch information
assafad1 committed Nov 28, 2024
1 parent ab411df commit 37197ea
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 11 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ The SDK can be configured using environment variables:

- `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.
- `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.
- `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.
- `CORALGOIX_REGION`: The region/cluster to connect to as a shorthand (EU2, AP1, etc. read more [here](https://coralogix.com/docs/coralogix-domain/)).

Furthermore, if you want to run the examples locally, you're going to to have set the following environment variables:
Expand Down
18 changes: 18 additions & 0 deletions go/callPropertiesCreator.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type CallPropertiesCreator struct {
coraglogixRegion string
teamsLevelAPIKey string
userLevelAPIKey string
orgLevelAPIKey string
correlationID string
sdkVersion string
//allowRetry bool
Expand Down Expand Up @@ -75,6 +76,21 @@ func (c CallPropertiesCreator) GetUserLevelCallProperties(ctx context.Context) (
return &CallProperties{Ctx: ctx, Connection: conn, CallOptions: callOptions}, nil
}

// 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.
func (c CallPropertiesCreator) GetOrgLevelCallProperties(ctx context.Context) (*CallProperties, error) {
ctx = createContext(ctx, c.orgLevelAPIKey, c.correlationID, c.sdkVersion)

endpoint := CoralogixGrpcEndpointFromRegion(c.coraglogixRegion)
conn, err := createSecureConnection(endpoint)
if err != nil {
return nil, err
}

callOptions := createCallOptions()

return &CallProperties{Ctx: ctx, Connection: conn, CallOptions: callOptions}, nil
}

func createCallOptions() []grpc.CallOption {
var callOptions []grpc.CallOption
callOptions = append(callOptions, grpc_retry.WithMax(5))
Expand All @@ -99,6 +115,7 @@ func NewCallPropertiesCreator(region string, authContext AuthContext) *CallPrope
coraglogixRegion: region,
teamsLevelAPIKey: authContext.teamLevelAPIKey,
userLevelAPIKey: authContext.userLevelAPIKey,
orgLevelAPIKey: authContext.orgLevelAPIKey,
correlationID: uuid.New().String(),
sdkVersion: vanillaSdkVersion,
}
Expand All @@ -109,6 +126,7 @@ func NewCallPropertiesCreatorTerraformOperator(region string, authContext AuthCo
return &CallPropertiesCreator{
coraglogixRegion: region,
teamsLevelAPIKey: authContext.teamLevelAPIKey,
orgLevelAPIKey: authContext.orgLevelAPIKey,
userLevelAPIKey: authContext.userLevelAPIKey,
correlationID: uuid.New().String(),
sdkVersion: terraformOperatorVersion,
Expand Down
32 changes: 28 additions & 4 deletions go/cxsdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ func (c *ClientSet) Groups() *GroupsClient {
}

// NewClientSet Creates a new ClientSet.
func NewClientSet(targetURL, teamsLevelAPIKey string, userLevelAPIKey string) *ClientSet {
authContext := NewAuthContext(teamsLevelAPIKey, userLevelAPIKey)
func NewClientSet(targetURL, teamsLevelAPIKey, userLevelAPIKey, orgLevelAPIKey string) *ClientSet {
authContext := NewAuthContext(teamsLevelAPIKey, userLevelAPIKey, orgLevelAPIKey)
apikeyCPC := NewCallPropertiesCreator(targetURL, authContext)

return &ClientSet{
Expand Down Expand Up @@ -185,6 +185,9 @@ 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"

// EnvCoralogixOrgLevelAPIKey is the name of the environment variable that contains the Coralogix Organization API key.
const EnvCoralogixOrgLevelAPIKey = "CORALOGIX_ORG_API_KEY"

// CoralogixRegionFromEnv reads the Coralogix region from environment variables.
func CoralogixRegionFromEnv() (string, error) {
regionIdentifier := strings.ToLower(os.Getenv(EnvCoralogxRegion))
Expand Down Expand Up @@ -260,19 +263,24 @@ const (
type AuthContext struct {
teamLevelAPIKey string
userLevelAPIKey string
orgLevelAPIKey string
}

// NewAuthContext creates a new AuthContext.
func NewAuthContext(teamLevelAPIKey, userLevelAPIKey string) AuthContext {
func NewAuthContext(teamLevelAPIKey, userLevelAPIKey, orgLevelAPIKey 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.")
}
if orgLevelAPIKey == "" {
log.Println("Warning: orgLevelAPIKey was not provided. Some functionality will not be available.")
}
return AuthContext{
teamLevelAPIKey: teamLevelAPIKey,
userLevelAPIKey: userLevelAPIKey,
orgLevelAPIKey: orgLevelAPIKey,
}
}

Expand All @@ -286,14 +294,21 @@ func AuthContextFromEnv() (AuthContext, error) {
if err != nil {
return AuthContext{}, err
}
orgLevelAPIKey, err := CoralogixOrgLevelAPIKeyFromEnv()
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.")
}
if orgLevelAPIKey == "" {
log.Println("Warning: orgLevelAPIKey is empty. Some functionality will not be available.")
}

return AuthContext{teamLevelAPIKey: teamLevelAPIKey, userLevelAPIKey: userLevelAPIKey}, nil
return AuthContext{teamLevelAPIKey: teamLevelAPIKey, userLevelAPIKey: userLevelAPIKey, orgLevelAPIKey: orgLevelAPIKey}, nil
}

// CoralogixTeamsLevelAPIKeyFromEnv reads the Coralogix Team API key from environment variables.
Expand All @@ -313,3 +328,12 @@ func CoralogixUserLevelAPIKeyFromEnv() (string, error) {
}
return apiKey, nil
}

// CoralogixOrgLevelAPIKeyFromEnv reads the Coralogix Organization API key from environment variables.
func CoralogixOrgLevelAPIKeyFromEnv() (string, error) {
apiKey := os.Getenv(EnvCoralogixOrgLevelAPIKey)
if apiKey == "" {
return "", fmt.Errorf("the %s environment variable is not set", EnvCoralogixOrgLevelAPIKey)
}
return apiKey, nil
}
14 changes: 7 additions & 7 deletions go/teams-client.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ type TeamsClient struct {

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

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

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

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

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

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

// MoveQuota moves the quota from one team to another.
func (c TeamsClient) MoveQuota(ctx context.Context, req *MoveQuotaRequest) (*teams.MoveQuotaResponse, error) {
callProperties, err := c.callPropertiesCreator.GetTeamsLevelCallProperties(ctx)
callProperties, err := c.callPropertiesCreator.GetOrgLevelCallProperties(ctx)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 37197ea

Please sign in to comment.