Skip to content

Commit 6f10b27

Browse files
authored
Merge pull request #30 from drone/FFM-1147_cluster_id
(FFM-1147) Adds cluster Identifier support
2 parents a779e3a + be2d427 commit 6f10b27

File tree

3 files changed

+39
-22
lines changed

3 files changed

+39
-22
lines changed

cache/lru.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,6 @@ func (lru *LRUCache) Updated() time.Time {
9393
}
9494

9595
// SetLogger set logger
96-
func (lru LRUCache) SetLogger(logger logger.Logger) {
96+
func (lru *LRUCache) SetLogger(logger logger.Logger) {
9797
lru.logger = logger
9898
}

client/client.go

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,21 @@ import (
3737
// that any pending analytics events have been delivered.
3838
//
3939
type CfClient struct {
40-
mux sync.RWMutex
41-
api rest.ClientWithResponsesInterface
42-
metricsapi metricsclient.ClientWithResponsesInterface
43-
sdkKey string
44-
auth rest.AuthenticationRequest
45-
config *config
46-
environmentID string
47-
token string
48-
persistence cache.Persistence
49-
cancelFunc context.CancelFunc
50-
streamConnected bool
51-
authenticated chan struct{}
52-
initialized chan bool
53-
analyticsService *analyticsservice.AnalyticsService
40+
mux sync.RWMutex
41+
api rest.ClientWithResponsesInterface
42+
metricsapi metricsclient.ClientWithResponsesInterface
43+
sdkKey string
44+
auth rest.AuthenticationRequest
45+
config *config
46+
environmentID string
47+
token string
48+
persistence cache.Persistence
49+
cancelFunc context.CancelFunc
50+
streamConnected bool
51+
authenticated chan struct{}
52+
initialized chan bool
53+
analyticsService *analyticsservice.AnalyticsService
54+
clusterIdentifier string
5455
}
5556

5657
// NewCfClient creates a new client instance that connects to CF with the default configuration.
@@ -71,11 +72,12 @@ func NewCfClient(sdkKey string, options ...ConfigOption) (*CfClient, error) {
7172
analyticsService := analyticsservice.NewAnalyticsService(time.Minute, config.Logger)
7273

7374
client := &CfClient{
74-
sdkKey: sdkKey,
75-
config: config,
76-
authenticated: make(chan struct{}),
77-
initialized: make(chan bool),
78-
analyticsService: analyticsService,
75+
sdkKey: sdkKey,
76+
config: config,
77+
authenticated: make(chan struct{}),
78+
initialized: make(chan bool),
79+
analyticsService: analyticsService,
80+
clusterIdentifier: "1",
7981
}
8082
ctx, client.cancelFunc = context.WithCancel(context.Background())
8183

@@ -158,7 +160,7 @@ func (c *CfClient) streamConnect() {
158160
c.mux.RLock()
159161
defer c.mux.RUnlock()
160162
c.config.Logger.Info("Registering SSE consumer")
161-
sseClient := sse.NewClient(fmt.Sprintf("%s/stream", c.config.url))
163+
sseClient := sse.NewClient(fmt.Sprintf("%s/stream?cluster=%s", c.config.url, c.clusterIdentifier))
162164
conn := stream.NewSSEClient(c.sdkKey, c.token, sseClient, c.config.Cache, c.api)
163165
err := conn.Connect(c.environmentID)
164166
if err != nil {
@@ -240,6 +242,12 @@ func (c *CfClient) authenticate(ctx context.Context, target evaluation.Target) {
240242
return
241243
}
242244

245+
c.clusterIdentifier, ok = claims["clusterIdentifier"].(string)
246+
if !ok {
247+
c.config.Logger.Error(errors.New("cluster identifier not present"))
248+
c.clusterIdentifier = "1"
249+
}
250+
243251
// network layer setup
244252
bearerTokenProvider, bearerTokenProviderErr := securityprovider.NewSecurityProviderBearerToken(c.token)
245253
if bearerTokenProviderErr != nil {
@@ -248,6 +256,7 @@ func (c *CfClient) authenticate(ctx context.Context, target evaluation.Target) {
248256
}
249257
restClient, err := rest.NewClientWithResponses(c.config.url,
250258
rest.WithRequestEditorFn(bearerTokenProvider.Intercept),
259+
rest.WithRequestEditorFn(c.InterceptAddCluster),
251260
rest.WithHTTPClient(c.config.httpClient),
252261
)
253262
if err != nil {
@@ -539,6 +548,14 @@ func (c *CfClient) Environment() string {
539548
return c.environmentID
540549
}
541550

551+
// InterceptAddCluster adds cluster ID to calls
552+
func (c *CfClient) InterceptAddCluster(ctx context.Context, req *http.Request) error {
553+
q := req.URL.Query()
554+
q.Add("cluster", c.clusterIdentifier)
555+
req.URL.RawQuery = q.Encode()
556+
return nil
557+
}
558+
542559
// contains determines if the string variation is in the slice of variations.
543560
// returns true if found, otherwise false.
544561
func contains(variations []string, variation string) bool {

storage/file.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,6 @@ func (ds *FileStore) PersistedAt() time.Time {
110110
}
111111

112112
// SetLogger set logger
113-
func (ds FileStore) SetLogger(logger logger.Logger) {
113+
func (ds *FileStore) SetLogger(logger logger.Logger) {
114114
ds.logger = logger
115115
}

0 commit comments

Comments
 (0)