Skip to content

Commit

Permalink
feat: add support for env name from auth (#410)
Browse files Browse the repository at this point in the history
* add support for env name from auth
* use only env name in auth call and cache

closes [Support non-slugified environment names](#407) #407
* Environment name will be returned in auth call in addition to slug.
* Environment slug is no longer supported in the rules file. If it becomes an issue and there is high demand for it, we can consider adding it as an option later.
* Using the environment name instead of the slug is more intuitive for end users, as they set the environment name intentionally, not the slug. This can now be used in the rules config instead of worrying about the structure of the slugified version of the environment.
  • Loading branch information
JamieDanielson authored Feb 24, 2022
1 parent a6ac6a1 commit 6932877
Showing 1 changed file with 15 additions and 10 deletions.
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
}

0 comments on commit 6932877

Please sign in to comment.