Skip to content

Commit

Permalink
implement the variables context check
Browse files Browse the repository at this point in the history
  • Loading branch information
shadi-altarsha committed May 11, 2022
1 parent 342f4bd commit 970bd97
Show file tree
Hide file tree
Showing 6 changed files with 434 additions and 10 deletions.
45 changes: 45 additions & 0 deletions __generator__/predefined.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2093,3 +2093,48 @@ LF:
reference: "https://developer.fastly.com/reference/vcl/types/string/"
on: [RECV, HASH, HIT, MISS, PASS, FETCH, ERROR, DELIVER, LOG]
get: STRING

ratecounter.%any%.bucket.10s:
reference: "https://developer.fastly.com/reference/vcl/variables/rate-limiting/ratecounter-bucket-10s/"
on: [RECV, HASH, HIT, MISS, PASS, FETCH, ERROR, DELIVER, LOG]
get: INTEGER

ratecounter.%any%.bucket.20s:
reference: "https://developer.fastly.com/reference/vcl/variables/rate-limiting/ratecounter-bucket-20s/"
on: [RECV, HASH, HIT, MISS, PASS, FETCH, ERROR, DELIVER, LOG]
get: INTEGER

ratecounter.%any%.bucket.30s:
reference: "https://developer.fastly.com/reference/vcl/variables/rate-limiting/ratecounter-bucket-30s/"
on: [RECV, HASH, HIT, MISS, PASS, FETCH, ERROR, DELIVER, LOG]
get: INTEGER

ratecounter.%any%.bucket.40s:
reference: "https://developer.fastly.com/reference/vcl/variables/rate-limiting/ratecounter-bucket-40s/"
on: [RECV, HASH, HIT, MISS, PASS, FETCH, ERROR, DELIVER, LOG]
get: INTEGER

ratecounter.%any%.bucket.50s:
reference: "https://developer.fastly.com/reference/vcl/variables/rate-limiting/ratecounter-bucket-50s/"
on: [RECV, HASH, HIT, MISS, PASS, FETCH, ERROR, DELIVER, LOG]
get: INTEGER

ratecounter.%any%.bucket.60s:
reference: "https://developer.fastly.com/reference/vcl/variables/rate-limiting/ratecounter-bucket-60s/"
on: [RECV, HASH, HIT, MISS, PASS, FETCH, ERROR, DELIVER, LOG]
get: INTEGER

ratecounter.%any%.rate.1s:
reference: "https://developer.fastly.com/reference/vcl/variables/rate-limiting/ratecounter-rate-1s/"
on: [RECV, HASH, HIT, MISS, PASS, FETCH, ERROR, DELIVER, LOG]
get: FLOAT

ratecounter.%any%.rate.10s:
reference: "https://developer.fastly.com/reference/vcl/variables/rate-limiting/ratecounter-rate-10s/"
on: [RECV, HASH, HIT, MISS, PASS, FETCH, ERROR, DELIVER, LOG]
get: FLOAT

ratecounter.%any%.rate.60s:
reference: "https://developer.fastly.com/reference/vcl/variables/rate-limiting/ratecounter-rate-60s/"
on: [RECV, HASH, HIT, MISS, PASS, FETCH, ERROR, DELIVER, LOG]
get: FLOAT
31 changes: 31 additions & 0 deletions context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,31 @@ func (c *Context) GetRegexGroupVariable(name string) (types.Type, error) {
return types.StringType, nil
}

// Get ratecounter variable
func (c *Context) GetRatecounterVariable(name string) (types.Type, error) {
// Ratecounter variables have the shape: ratecounter.{Variable Name}.[bucket/rate].Time
nameComponents := strings.Split(name, ".")
if len(nameComponents) != 4 {
return types.NullType, fmt.Errorf(`undefined variable "%s"`, name)
}

ratecounterVariableName := nameComponents[1]
// Check first if this ratecounter is defined in the first place.
if _, ok := c.Ratecounters[ratecounterVariableName]; !ok {
return types.NullType, fmt.Errorf(`undefined variable "%s"`, name)
}

// nameComponents[0] should be "ratecounter"
// nameComponents[1] should be the variable name
// nameComponents[2] should be either "bucket" or "rate"
// nameComponents[3] should be the time (10s, 60s, etc)
if v, ok := c.Variables[nameComponents[0]].Items["%any%"].Items[nameComponents[2]].Items[nameComponents[3]]; ok {
return v.Value.Get, nil
}

return types.NullType, fmt.Errorf(`undefined variable "%s"`, name)
}

func (c *Context) AddAcl(name string, acl *types.Acl) error {
// check existence
if _, duplicated := c.Acls[name]; duplicated {
Expand Down Expand Up @@ -301,6 +326,12 @@ func (c *Context) Get(name string) (types.Type, error) {
return c.GetRegexGroupVariable(name)
}

// If program want to access to ratecounter variables like "ratecounter.{Name}.bucket.10s",
// proxy to dedicated getter.
if first == "ratecounter" {
return c.GetRatecounterVariable(name)
}

obj, ok := c.Variables[first]
if !ok {
return types.NullType, fmt.Errorf(`undefined variable "%s"`, name)
Expand Down
106 changes: 106 additions & 0 deletions context/predefined.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 970bd97

Please sign in to comment.