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 Dec 1, 2024
1 parent ab411df commit c537e02
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 18 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
40 changes: 29 additions & 11 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,40 +263,46 @@ 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,
}
}

// 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
teamLevelAPIKey := os.Getenv(EnvCoralogixTeamLevelAPIKey)
userLevelAPIKey := os.Getenv(EnvCoralogixUserLevelAPIKey)
orgLevelAPIKey := os.Getenv(EnvCoralogixOrgLevelAPIKey)
if teamLevelAPIKey == "" && userLevelAPIKey == "" && orgLevelAPIKey == "" {
return AuthContext{}, fmt.Errorf("at least one of %s, %s, or %s must be set", EnvCoralogixTeamLevelAPIKey, EnvCoralogixUserLevelAPIKey, EnvCoralogixOrgLevelAPIKey)
}
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 +322,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 c537e02

Please sign in to comment.