Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for env name from auth #410

Merged
merged 2 commits into from
Feb 24, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions route/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -650,18 +650,18 @@ func getFirstValueFromMetadata(key string, md metadata.MD) string {
type environmentCache struct {
mutex sync.RWMutex
items map[string]*cacheItem
ttl time.Duration
ttl time.Duration
getFn func(string) (string, error)
}

func (r *Router) SetEnvironmentCache(ttl time.Duration, getFn func(string)(string, error)) {
func (r *Router) SetEnvironmentCache(ttl time.Duration, getFn func(string) (string, error)) {
r.environmentCache = newEnvironmentCache(ttl, getFn)
}

func newEnvironmentCache(ttl time.Duration, getFn func(string)(string, error)) *environmentCache {
func newEnvironmentCache(ttl time.Duration, getFn func(string) (string, error)) *environmentCache {
return &environmentCache{
items: make(map[string]*cacheItem),
ttl: ttl,
ttl: ttl,
getFn: getFn,
}
}
Expand All @@ -681,7 +681,7 @@ func (c *environmentCache) get(key string) (string, error) {
}

// get write lock early so we don't execute getFn in parallel so the
// the result will be cached before the next lock is aquired to prevent
// the result will be cached before the next lock is aquired to prevent
// subsequent calls to getFn for the same key
c.mutex.Lock()
defer c.mutex.Unlock()
Expand Down Expand Up @@ -711,14 +711,19 @@ func (c *environmentCache) addItem(key string, value string, ttl time.Duration)
}
}

type SlugInfo struct {
type TeamInfo struct {
Slug string `json:"slug"`
}

type EnvironmentInfo struct {
Slug string `json:"slug"`
Name string `json:"name"`
}

type AuthInfo struct {
APIKeyAccess map[string]bool `json:"api_key_access"`
Team SlugInfo `json:"team"`
Environment SlugInfo `json:"environment"`
Team TeamInfo `json:"team"`
Environment EnvironmentInfo `json:"environment"`
}

func (r *Router) getEnvironmentName(apiKey string) (string, error) {
Expand Down Expand Up @@ -769,6 +774,6 @@ func (r *Router) lookupEnvironment(apiKey string) (string, error) {
if err := json.NewDecoder(resp.Body).Decode(&authinfo); err != nil {
return "", fmt.Errorf("failed to JSON decode of AuthInfo response from Honeycomb API")
}
r.Logger.Debug().WithString("environment", authinfo.Environment.Slug).Logf("Got environment")
return authinfo.Environment.Slug, nil
r.Logger.Debug().WithString("environment", authinfo.Environment.Name).Logf("Got environment")
return authinfo.Environment.Name, nil
}